Skip to content

Commit ee92122

Browse files
authored
[Win/X86] Make _m_prefetch[w] builtins to avoid winnt.h conflicts (#115099)
This is similar in spirit to previous changes to make _mm_mfence builtins to avoid conflicts with winnt.h and other MSVC ecosystem headers that pre-declare compiler intrinsics as extern "C" symbols. Also update the feature flag for _mm_prefetch to sse, which is more accurate than mmx. This should fix issue #87515.
1 parent 7c5100d commit ee92122

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
lines changed

clang/include/clang/Basic/BuiltinsX86.td

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<128>] in {
130130
}
131131
}
132132

133+
let Features = "sse", Header = "xmmintrin.h", Attributes = [NoThrow, Const] in {
134+
def _mm_prefetch : X86LibBuiltin<"void(void const *, int)">;
135+
}
136+
133137
// AVX
134138
let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in {
135139
foreach Op = ["addsub", "hadd", "hsub", "max", "min"] in {
@@ -138,6 +142,12 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in
138142
}
139143
}
140144

145+
// PRFCHW
146+
let Features = "prfchw", Header = "x86intrin.h", Attributes = [NoThrow, Const] in {
147+
def _m_prefetch : X86LibBuiltin<"void(void *)">;
148+
def _m_prefetchw : X86LibBuiltin<"void(void volatile const *)">;
149+
}
150+
141151

142152
// Mechanically ported builtins from the original `.def` file.
143153
//
@@ -146,10 +156,6 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in
146156
// current formulation is based on what was easiest to recognize from the
147157
// pre-TableGen version.
148158

149-
let Features = "mmx", Attributes = [NoThrow, Const] in {
150-
def _mm_prefetch : X86NoPrefixBuiltin<"void(char const *, int)">;
151-
}
152-
153159
let Features = "sse", Attributes = [NoThrow] in {
154160
def ldmxcsr : X86Builtin<"void(unsigned int)">;
155161
}

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15374,6 +15374,17 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
1537415374
Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
1537515375
return Builder.CreateCall(F, {Address, RW, Locality, Data});
1537615376
}
15377+
case X86::BI_m_prefetch:
15378+
case X86::BI_m_prefetchw: {
15379+
Value *Address = Ops[0];
15380+
// The 'w' suffix implies write.
15381+
Value *RW =
15382+
ConstantInt::get(Int32Ty, BuiltinID == X86::BI_m_prefetchw ? 1 : 0);
15383+
Value *Locality = ConstantInt::get(Int32Ty, 0x3);
15384+
Value *Data = ConstantInt::get(Int32Ty, 1);
15385+
Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
15386+
return Builder.CreateCall(F, {Address, RW, Locality, Data});
15387+
}
1537715388
case X86::BI_mm_clflush: {
1537815389
return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse2_clflush),
1537915390
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,10 +2197,7 @@ _mm_storer_ps(float *__p, __m128 __a)
21972197
#define _MM_HINT_T2 1
21982198
#define _MM_HINT_NTA 0
21992199

2200-
#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. */
2203-
2200+
#if 0
22042201
/// Loads one cache line of data from the specified address to a location
22052202
/// closer to the processor.
22062203
///
@@ -2225,6 +2222,10 @@ _mm_storer_ps(float *__p, __m128 __a)
22252222
/// be generated. \n
22262223
/// _MM_HINT_T2: Move data using the T2 hint. The PREFETCHT2 instruction will
22272224
/// be generated.
2225+
///
2226+
/// _mm_prefetch is implemented as a "library builtin" directly in Clang,
2227+
/// similar to how it is done in MSVC. Clang will warn if the user doesn't
2228+
/// include xmmintrin.h or immintrin.h.
22282229
#define _mm_prefetch(a, sel) (__builtin_prefetch((const void *)(a), \
22292230
((sel) >> 2) & 1, (sel) & 0x3))
22302231
#endif

0 commit comments

Comments
 (0)