Skip to content

Commit 948cc91

Browse files
authored
Reapply "[Win/X86] Make _m_prefetch[w] builtins to avoid winnt.h conflicts (#115099)" (#138360)
This reverts commit 83ff9d4. Don't change the builtin signature of _mm_prefetch this time.
1 parent 77af8bf commit 948cc91

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

clang/include/clang/Basic/BuiltinHeaders.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ HEADER(INTRIN_H, "intrin.h")
2323
HEADER(MALLOC_H, "malloc.h")
2424
HEADER(MATH_H, "math.h")
2525
HEADER(MEMORY, "memory")
26+
HEADER(MMINTRIN_H, "mmintrin.h")
2627
HEADER(OBJC_MESSAGE_H, "objc/message.h")
2728
HEADER(OBJC_OBJC_AUTO_H, "objc/objc-auto.h")
2829
HEADER(OBJC_OBJC_EXCEPTION_H, "objc/objc-exception.h")

clang/include/clang/Basic/BuiltinsX86.td

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in
138138
}
139139
}
140140

141+
let Features = "mmx", Header = "mmintrin.h", Attributes = [NoThrow, Const] in {
142+
def _m_prefetch : X86LibBuiltin<"void(void *)">;
143+
}
144+
145+
// PRFCHW
146+
let Features = "prfchw", Header = "intrin.h", Attributes = [NoThrow, Const] in {
147+
def _m_prefetchw : X86LibBuiltin<"void(void volatile const *)">;
148+
}
141149

142150
// Mechanically ported builtins from the original `.def` file.
143151
//
@@ -146,8 +154,8 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in
146154
// current formulation is based on what was easiest to recognize from the
147155
// pre-TableGen version.
148156

149-
let Features = "mmx", Attributes = [NoThrow, Const] in {
150-
def _mm_prefetch : X86NoPrefixBuiltin<"void(char const *, int)">;
157+
let Features = "sse", Header = "xmmintrin.h", Attributes = [NoThrow, Const] in {
158+
def _mm_prefetch : X86LibBuiltin<"void(char const *, int)">;
151159
}
152160

153161
let Features = "sse", Attributes = [NoThrow] in {

clang/lib/CodeGen/TargetBuiltins/X86.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,17 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
804804
Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
805805
return Builder.CreateCall(F, {Address, RW, Locality, Data});
806806
}
807+
case X86::BI_m_prefetch:
808+
case X86::BI_m_prefetchw: {
809+
Value *Address = Ops[0];
810+
// The 'w' suffix implies write.
811+
Value *RW =
812+
ConstantInt::get(Int32Ty, BuiltinID == X86::BI_m_prefetchw ? 1 : 0);
813+
Value *Locality = ConstantInt::get(Int32Ty, 0x3);
814+
Value *Data = ConstantInt::get(Int32Ty, 1);
815+
Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
816+
return Builder.CreateCall(F, {Address, RW, Locality, Data});
817+
}
807818
case X86::BI_mm_clflush: {
808819
return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse2_clflush),
809820
Ops[0]);

clang/lib/Headers/prfchwintrin.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
#ifndef __PRFCHWINTRIN_H
1515
#define __PRFCHWINTRIN_H
1616

17+
#if defined(__cplusplus)
18+
extern "C" {
19+
#endif
20+
1721
/// Loads a memory sequence containing the specified memory address into
1822
/// all data cache levels.
1923
///
@@ -26,11 +30,7 @@
2630
///
2731
/// \param __P
2832
/// A pointer specifying the memory address to be prefetched.
29-
static __inline__ void __attribute__((__always_inline__, __nodebug__))
30-
_m_prefetch(void *__P)
31-
{
32-
__builtin_prefetch (__P, 0, 3 /* _MM_HINT_T0 */);
33-
}
33+
void _m_prefetch(void *__P);
3434

3535
/// Loads a memory sequence containing the specified memory address into
3636
/// the L1 data cache and sets the cache-coherency state to modified.
@@ -48,13 +48,10 @@ _m_prefetch(void *__P)
4848
///
4949
/// \param __P
5050
/// A pointer specifying the memory address to be prefetched.
51-
static __inline__ void __attribute__((__always_inline__, __nodebug__))
52-
_m_prefetchw(volatile const void *__P)
53-
{
54-
#pragma clang diagnostic push
55-
#pragma clang diagnostic ignored "-Wcast-qual"
56-
__builtin_prefetch ((const void*)__P, 1, 3 /* _MM_HINT_T0 */);
57-
#pragma clang diagnostic pop
58-
}
51+
void _m_prefetchw(volatile const void *__P);
52+
53+
#if defined(__cplusplus)
54+
} // extern "C"
55+
#endif
5956

6057
#endif /* __PRFCHWINTRIN_H */

clang/lib/Headers/xmmintrin.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,8 +2198,9 @@ _mm_storer_ps(float *__p, __m128 __a)
21982198
#define _MM_HINT_NTA 0
21992199

22002200
#ifndef _MSC_VER
2201-
/* FIXME: We have to #define this because "sel" must be a constant integer, and
2202-
Sema doesn't do any form of constant propagation yet. */
2201+
// If _MSC_VER is defined, we use the builtin variant of _mm_prefetch.
2202+
// Otherwise, we provide this macro, which includes a cast, allowing the user
2203+
// to pass a pointer of any time. The _mm_prefetch accepts char to match MSVC.
22032204

22042205
/// Loads one cache line of data from the specified address to a location
22052206
/// closer to the processor.

0 commit comments

Comments
 (0)