[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 13:55:07 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/3] [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/3] 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/3] 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 {
More information about the cfe-commits
mailing list