[clang] [Win/X86] Make _m_prefetch[w] builtins to avoid winnt.h conflicts (PR #115099)

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 30 15:49:20 PST 2025


https://github.com/rnk updated https://github.com/llvm/llvm-project/pull/115099

>From e5f485ad8000c296229794346fdd627b90e504d2 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Tue, 5 Nov 2024 16:05:53 -0800
Subject: [PATCH 1/6] [Win/X86] Make _m_prefetch[w] builtins to avoid winnt.h
 conflicts

---
 clang/include/clang/Basic/BuiltinsX86.td |  7 ++++++-
 clang/lib/CodeGen/CGBuiltin.cpp          | 10 ++++++++++
 clang/lib/Headers/prfchwintrin.h         | 23 ++++++++++-------------
 3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/clang/include/clang/Basic/BuiltinsX86.td b/clang/include/clang/Basic/BuiltinsX86.td
index 572ac7235be02f..00bee2051caa85 100644
--- a/clang/include/clang/Basic/BuiltinsX86.td
+++ b/clang/include/clang/Basic/BuiltinsX86.td
@@ -146,10 +146,15 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in
 // current formulation is based on what was easiest to recognize from the
 // pre-TableGen version.
 
-let Features = "mmx", Attributes = [NoThrow, Const] in {
+let Features = "mmx", Header = "immintrin.h", Attributes = [NoThrow, Const] in {
   def _mm_prefetch : X86NoPrefixBuiltin<"void(char const *, int)">;
 }
 
+let Features = "mmx", Header = "intrin.h", Attributes = [NoThrow, Const] in {
+  def _m_prefetch : X86NoPrefixBuiltin<"void(void *)">;
+  def _m_prefetchw : X86NoPrefixBuiltin<"void(const void *)">;
+}
+
 let Features = "sse", Attributes = [NoThrow] in {
   def ldmxcsr : X86Builtin<"void(unsigned int)">;
 }
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 7ec9d59bfed5cf..0224238d976193 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -15254,6 +15254,16 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
     Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
     return Builder.CreateCall(F, {Address, RW, Locality, Data});
   }
+  case X86::BI_m_prefetch:
+  case X86::BI_m_prefetchw: {
+    Value *Address = Ops[0];
+    // The 'w' suffix implies write.
+    Value *RW = ConstantInt::get(Int32Ty, BuiltinID == X86::BI_m_prefetchw ? 1 : 0);
+    Value *Locality = ConstantInt::get(Int32Ty, 0x3);
+    Value *Data = ConstantInt::get(Int32Ty, 1);
+    Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());
+    return Builder.CreateCall(F, {Address, RW, Locality, Data});
+  }
   case X86::BI_mm_clflush: {
     return Builder.CreateCall(CGM.getIntrinsic(Intrinsic::x86_sse2_clflush),
                               Ops[0]);
diff --git a/clang/lib/Headers/prfchwintrin.h b/clang/lib/Headers/prfchwintrin.h
index eaea5f3cf8febf..8ec55d7073716f 100644
--- a/clang/lib/Headers/prfchwintrin.h
+++ b/clang/lib/Headers/prfchwintrin.h
@@ -14,6 +14,10 @@
 #ifndef __PRFCHWINTRIN_H
 #define __PRFCHWINTRIN_H
 
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
 /// Loads a memory sequence containing the specified memory address into
 ///    all data cache levels.
 ///
@@ -26,11 +30,7 @@
 ///
 /// \param __P
 ///    A pointer specifying the memory address to be prefetched.
-static __inline__ void __attribute__((__always_inline__, __nodebug__))
-_m_prefetch(void *__P)
-{
-  __builtin_prefetch (__P, 0, 3 /* _MM_HINT_T0 */);
-}
+void _m_prefetch(void *__P);
 
 /// Loads a memory sequence containing the specified memory address into
 ///    the L1 data cache and sets the cache-coherency state to modified.
@@ -48,13 +48,10 @@ _m_prefetch(void *__P)
 ///
 /// \param __P
 ///    A pointer specifying the memory address to be prefetched.
-static __inline__ void __attribute__((__always_inline__, __nodebug__))
-_m_prefetchw(volatile const void *__P)
-{
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wcast-qual"
-  __builtin_prefetch ((const void*)__P, 1, 3 /* _MM_HINT_T0 */);
-#pragma clang diagnostic pop
-}
+void _m_prefetchw(volatile const void *__P);
+
+#if defined(__cplusplus)
+} // extern "C"
+#endif
 
 #endif /* __PRFCHWINTRIN_H */

>From 80e6138ccc1e970d12c86be937562a2ac96e8685 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Wed, 6 Nov 2024 00:45:51 +0000
Subject: [PATCH 2/6] format

---
 clang/lib/CodeGen/CGBuiltin.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 0224238d976193..ce3b9f1d99c947 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -15258,7 +15258,8 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
   case X86::BI_m_prefetchw: {
     Value *Address = Ops[0];
     // The 'w' suffix implies write.
-    Value *RW = ConstantInt::get(Int32Ty, BuiltinID == X86::BI_m_prefetchw ? 1 : 0);
+    Value *RW =
+        ConstantInt::get(Int32Ty, BuiltinID == X86::BI_m_prefetchw ? 1 : 0);
     Value *Locality = ConstantInt::get(Int32Ty, 0x3);
     Value *Data = ConstantInt::get(Int32Ty, 1);
     Function *F = CGM.getIntrinsic(Intrinsic::prefetch, Address->getType());

>From 2c0770538955e63886423a01a8d1320e6ff1a765 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Thu, 30 Jan 2025 21:52:46 +0000
Subject: [PATCH 3/6] Use X86LibBuiltin so we can indicate which header these
 builtins are supposed to be from

---
 clang/include/clang/Basic/BuiltinsX86.td | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/BuiltinsX86.td b/clang/include/clang/Basic/BuiltinsX86.td
index 00bee2051caa85..0ebfdd920ee6d0 100644
--- a/clang/include/clang/Basic/BuiltinsX86.td
+++ b/clang/include/clang/Basic/BuiltinsX86.td
@@ -147,12 +147,12 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in
 // pre-TableGen version.
 
 let Features = "mmx", Header = "immintrin.h", Attributes = [NoThrow, Const] in {
-  def _mm_prefetch : X86NoPrefixBuiltin<"void(char const *, int)">;
+  def _mm_prefetch : X86LibBuiltin<"void(char const *, int)">;
 }
 
 let Features = "mmx", Header = "intrin.h", Attributes = [NoThrow, Const] in {
-  def _m_prefetch : X86NoPrefixBuiltin<"void(void *)">;
-  def _m_prefetchw : X86NoPrefixBuiltin<"void(const void *)">;
+  def _m_prefetch : X86LibBuiltin<"void(void *)">;
+  def _m_prefetchw : X86LibBuiltin<"void(void volatile const *)">;
 }
 
 let Features = "sse", Attributes = [NoThrow] in {

>From 452f72b1736d82ca28fe0d7ae4d4f37a75311bcd Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Thu, 30 Jan 2025 22:32:31 +0000
Subject: [PATCH 4/6] Remove _mm_prefetch macro definition and associated
 _MSC_VER ifndefs

---
 clang/lib/Headers/xmmintrin.h | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Headers/xmmintrin.h b/clang/lib/Headers/xmmintrin.h
index 20e66d190113a3..c2c337cccce8ca 100644
--- a/clang/lib/Headers/xmmintrin.h
+++ b/clang/lib/Headers/xmmintrin.h
@@ -2197,10 +2197,6 @@ _mm_storer_ps(float *__p, __m128 __a)
 #define _MM_HINT_T2  1
 #define _MM_HINT_NTA 0
 
-#ifndef _MSC_VER
-/* FIXME: We have to #define this because "sel" must be a constant integer, and
-   Sema doesn't do any form of constant propagation yet. */
-
 /// Loads one cache line of data from the specified address to a location
 ///    closer to the processor.
 ///
@@ -2225,9 +2221,10 @@ _mm_storer_ps(float *__p, __m128 __a)
 ///    be generated. \n
 ///    _MM_HINT_T2: Move data using the T2 hint. The PREFETCHT2 instruction will
 ///    be generated.
-#define _mm_prefetch(a, sel) (__builtin_prefetch((const void *)(a), \
-                                                 ((sel) >> 2) & 1, (sel) & 0x3))
-#endif
+///
+/// _mm_prefetch is implemented as a "library builtin" directly in Clang,
+/// similar to how it is done in MSVC. Clang will warn if the user doesn't
+/// include xmmintrin.h or immintrin.h.
 
 /// Stores a 64-bit integer in the specified aligned memory location. To
 ///    minimize caching, the data is flagged as non-temporal (unlikely to be

>From 371d448338982fb9335d32a9ff38993c78edcdd8 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Thu, 30 Jan 2025 23:04:05 +0000
Subject: [PATCH 5/6] Adjust mm_prefetch prototype to const void * to fix test

---
 clang/include/clang/Basic/BuiltinsX86.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/BuiltinsX86.td b/clang/include/clang/Basic/BuiltinsX86.td
index 0ebfdd920ee6d0..c33332fa4aabfa 100644
--- a/clang/include/clang/Basic/BuiltinsX86.td
+++ b/clang/include/clang/Basic/BuiltinsX86.td
@@ -147,7 +147,7 @@ let Attributes = [Const, NoThrow, RequiredVectorWidth<256>], Features = "avx" in
 // pre-TableGen version.
 
 let Features = "mmx", Header = "immintrin.h", Attributes = [NoThrow, Const] in {
-  def _mm_prefetch : X86LibBuiltin<"void(char const *, int)">;
+  def _mm_prefetch : X86LibBuiltin<"void(void const *, int)">;
 }
 
 let Features = "mmx", Header = "intrin.h", Attributes = [NoThrow, Const] in {

>From 57f92b75a857117e96385f64c30dcfab49f9e029 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Thu, 30 Jan 2025 23:49:00 +0000
Subject: [PATCH 6/6] Put back the macro in #if 0 for Doxygen

---
 clang/lib/Headers/xmmintrin.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/lib/Headers/xmmintrin.h b/clang/lib/Headers/xmmintrin.h
index c2c337cccce8ca..1fb070bca827e9 100644
--- a/clang/lib/Headers/xmmintrin.h
+++ b/clang/lib/Headers/xmmintrin.h
@@ -2197,6 +2197,7 @@ _mm_storer_ps(float *__p, __m128 __a)
 #define _MM_HINT_T2  1
 #define _MM_HINT_NTA 0
 
+#if 0
 /// Loads one cache line of data from the specified address to a location
 ///    closer to the processor.
 ///
@@ -2225,6 +2226,9 @@ _mm_storer_ps(float *__p, __m128 __a)
 /// _mm_prefetch is implemented as a "library builtin" directly in Clang,
 /// similar to how it is done in MSVC. Clang will warn if the user doesn't
 /// include xmmintrin.h or immintrin.h.
+#define _mm_prefetch(a, sel) (__builtin_prefetch((const void *)(a), \
+                                                 ((sel) >> 2) & 1, (sel) & 0x3))
+#endif
 
 /// Stores a 64-bit integer in the specified aligned memory location. To
 ///    minimize caching, the data is flagged as non-temporal (unlikely to be



More information about the cfe-commits mailing list