[libcxx-commits] [clang] [libcxx] [C++17] Support __GCC_[CON|DE]STRUCTIVE_SIZE (PR #89446)
Aaron Ballman via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Apr 24 11:24:14 PDT 2024
https://github.com/AaronBallman updated https://github.com/llvm/llvm-project/pull/89446
>From 9d394964d3d04c9d7ffd753751f2aaa3a96c9480 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Fri, 19 Apr 2024 15:56:54 -0400
Subject: [PATCH 1/8] [C++17] Support __GCC_[CON|DE]STRUCTIVE_SIZE
These macros are used by STL implementations to support implementation
of std::hardware_destructive_interference_size and
std::hardware_constructive_interference_size
Fixes #60174
---
clang/docs/ReleaseNotes.rst | 13 +++++
clang/include/clang/Basic/TargetInfo.h | 5 ++
clang/lib/Frontend/InitPreprocessor.cpp | 7 +++
clang/test/Preprocessor/init-aarch64.c | 12 +++--
clang/test/Preprocessor/init.c | 49 +++++++++++--------
.../test/Preprocessor/predefined-win-macros.c | 6 ++-
6 files changed, 65 insertions(+), 27 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index aea8fda35bb29c..7414856fd6a03e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -89,6 +89,19 @@ sections with improvements to Clang's support for those languages.
C++ Language Changes
--------------------
+C++17 Feature Support
+^^^^^^^^^^^^^^^^^^^^^
+- Clang now exposes ``__GCC_DESTRUCTIVE_SIZE`` and ``__GCC_CONSTRUCTIVE_SIZE``
+ predefined macros to support standard library implementations of
+ ``std::hardware_destructive_interference_size`` and
+ ``std::hardware_constructive_interference_size``, respectively. These macros
+ are predefined in all C and C++ language modes. These macros can be
+ overridden on the command line with ``-D``, if desired. The values the macros
+ expand to are not stable between releases of Clang and do not need to match
+ the values produced by GCC, so these macros should not be used from header
+ files because they may not be stable across multiple TUs (the values may vary
+ based on compiler version as well as CPU tuning). #GH60174
+
C++20 Feature Support
^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index e1ef7454f01669..a7b112a41f1b99 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1792,6 +1792,11 @@ class TargetInfo : public TransferrableTargetInfo,
/// Whether to support HIP image/texture API's.
virtual bool hasHIPImageSupport() const { return true; }
+ /// The minimum offset between two objects to avoid false sharing.
+ virtual unsigned hardwareDestructiveInterferenceSize() const { return 64; }
+ // The maximum size of contiguous memory to promote true sharing.
+ virtual unsigned hardwareConstructiveInterferenceSize() const { return 64; }
+
protected:
/// Copy type and layout related info.
void copyAuxTarget(const TargetInfo *Aux);
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 4f44c3b7b89d4d..b88cd86e09fa1e 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1309,6 +1309,13 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
}
+ // GCC defines these macros in both C and C++ modes despite them being needed
+ // mostly for STL implementations in C++.
+ Builder.defineMacro("__GCC_DESTRUCTIVE_SIZE",
+ Twine(TI.hardwareDestructiveInterferenceSize()));
+ Builder.defineMacro("__GCC_CONSTRUCTIVE_SIZE",
+ Twine(TI.hardwareConstructiveInterferenceSize()));
+
auto addLockFreeMacros = [&](const llvm::Twine &Prefix) {
// Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
#define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
diff --git a/clang/test/Preprocessor/init-aarch64.c b/clang/test/Preprocessor/init-aarch64.c
index cf96870b27acb3..f0845985c9efc3 100644
--- a/clang/test/Preprocessor/init-aarch64.c
+++ b/clang/test/Preprocessor/init-aarch64.c
@@ -119,6 +119,8 @@
// AARCH64-NEXT: #define __FP_FAST_FMA 1
// AARCH64-NEXT: #define __FP_FAST_FMAF 1
// AARCH64-NEXT: #define __GCC_ASM_FLAG_OUTPUTS__ 1
+// AARCH64-NEXT: #define __GCC_CONSTRUCTIVE_SIZE {{.+}}
+// AARCH64-NEXT: #define __GCC_DESTRUCTIVE_SIZE {{.+}}
// AARCH64-NEXT: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1
// AARCH64-NEXT: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16 1
// AARCH64-NEXT: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1
@@ -220,11 +222,11 @@
// AARCH64-NEXT: #define __LONG_MAX__ 9223372036854775807L
// AARCH64-NEXT: #define __LONG_WIDTH__ 64
// AARCH64-NEXT: #define __LP64__ 1
-// AARCH64-NEXT: #define __MEMORY_SCOPE_DEVICE 1
-// AARCH64-NEXT: #define __MEMORY_SCOPE_SINGLE 4
-// AARCH64-NEXT: #define __MEMORY_SCOPE_SYSTEM 0
-// AARCH64-NEXT: #define __MEMORY_SCOPE_WRKGRP 2
-// AARCH64-NEXT: #define __MEMORY_SCOPE_WVFRNT 3
+// AARCH64-NEXT: #define __MEMORY_SCOPE_DEVICE 1
+// AARCH64-NEXT: #define __MEMORY_SCOPE_SINGLE 4
+// AARCH64-NEXT: #define __MEMORY_SCOPE_SYSTEM 0
+// AARCH64-NEXT: #define __MEMORY_SCOPE_WRKGRP 2
+// AARCH64-NEXT: #define __MEMORY_SCOPE_WVFRNT 3
// AARCH64-NEXT: #define __NO_INLINE__ 1
// AARCH64-NEXT: #define __NO_MATH_ERRNO__ 1
// AARCH64-NEXT: #define __OBJC_BOOL_IS_BOOL 0
diff --git a/clang/test/Preprocessor/init.c b/clang/test/Preprocessor/init.c
index c4a55efca6f712..2641fee940231f 100644
--- a/clang/test/Preprocessor/init.c
+++ b/clang/test/Preprocessor/init.c
@@ -1,3 +1,10 @@
+// RUN: %clang_cc1 -E -dM < /dev/null | FileCheck -match-full-lines -check-prefix INTERFERENCE %s
+//
+// We purposefully do not test the values produced, only that the macros are
+// predefined to some value.
+// INTERFERENCE:#define __GCC_CONSTRUCTIVE_SIZE {{.+}}
+// INTERFERENCE:#define __GCC_DESTRUCTIVE_SIZE {{.+}}
+
// RUN: %clang_cc1 -E -dM -x assembler-with-cpp < /dev/null | FileCheck -match-full-lines -check-prefix ASM %s
//
// ASM:#define __ASSEMBLER__ 1
@@ -1697,6 +1704,8 @@
// WEBASSEMBLY-NEXT:#define __GCC_ATOMIC_SHORT_LOCK_FREE 2
// WEBASSEMBLY-NEXT:#define __GCC_ATOMIC_TEST_AND_SET_TRUEVAL 1
// WEBASSEMBLY-NEXT:#define __GCC_ATOMIC_WCHAR_T_LOCK_FREE 2
+// WEBASSEMBLY-NEXT:#define __GCC_CONSTRUCTIVE_SIZE {{.+}}
+// WEBASSEMBLY-NEXT:#define __GCC_DESTRUCTIVE_SIZE {{.+}}
// WEBASSEMBLY-NEXT:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1
// WEBASSEMBLY-NEXT:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1
// WEBASSEMBLY-NEXT:#define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
@@ -1806,11 +1815,11 @@
// WEBASSEMBLY64-NEXT:#define __LONG_MAX__ 9223372036854775807L
// WEBASSEMBLY64-NEXT:#define __LONG_WIDTH__ 64
// WEBASSEMBLY64-NEXT:#define __LP64__ 1
-// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_DEVICE 1
-// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_SINGLE 4
-// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_SYSTEM 0
-// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_WRKGRP 2
-// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_WVFRNT 3
+// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_DEVICE 1
+// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_SINGLE 4
+// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_SYSTEM 0
+// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_WRKGRP 2
+// WEBASSEMBLY-NEXT:#define __MEMORY_SCOPE_WVFRNT 3
// WEBASSEMBLY-NEXT:#define __NO_INLINE__ 1
// WEBASSEMBLY-NEXT:#define __NO_MATH_ERRNO__ 1
// WEBASSEMBLY-NEXT:#define __OBJC_BOOL_IS_BOOL 0
@@ -2126,11 +2135,11 @@
// AVR:#define __LDBL_MIN__ 1.17549435e-38L
// AVR:#define __LONG_LONG_MAX__ 9223372036854775807LL
// AVR:#define __LONG_MAX__ 2147483647L
-// AVR:#define __MEMORY_SCOPE_DEVICE 1
-// AVR:#define __MEMORY_SCOPE_SINGLE 4
-// AVR:#define __MEMORY_SCOPE_SYSTEM 0
-// AVR:#define __MEMORY_SCOPE_WRKGRP 2
-// AVR:#define __MEMORY_SCOPE_WVFRNT 3
+// AVR:#define __MEMORY_SCOPE_DEVICE 1
+// AVR:#define __MEMORY_SCOPE_SINGLE 4
+// AVR:#define __MEMORY_SCOPE_SYSTEM 0
+// AVR:#define __MEMORY_SCOPE_WRKGRP 2
+// AVR:#define __MEMORY_SCOPE_WVFRNT 3
// AVR:#define __NO_INLINE__ 1
// AVR:#define __ORDER_BIG_ENDIAN__ 4321
// AVR:#define __ORDER_LITTLE_ENDIAN__ 1234
@@ -2422,11 +2431,11 @@
// RISCV32: #define __LITTLE_ENDIAN__ 1
// RISCV32: #define __LONG_LONG_MAX__ 9223372036854775807LL
// RISCV32: #define __LONG_MAX__ 2147483647L
-// RISCV32: #define __MEMORY_SCOPE_DEVICE 1
-// RISCV32: #define __MEMORY_SCOPE_SINGLE 4
-// RISCV32: #define __MEMORY_SCOPE_SYSTEM 0
-// RISCV32: #define __MEMORY_SCOPE_WRKGRP 2
-// RISCV32: #define __MEMORY_SCOPE_WVFRNT 3
+// RISCV32: #define __MEMORY_SCOPE_DEVICE 1
+// RISCV32: #define __MEMORY_SCOPE_SINGLE 4
+// RISCV32: #define __MEMORY_SCOPE_SYSTEM 0
+// RISCV32: #define __MEMORY_SCOPE_WRKGRP 2
+// RISCV32: #define __MEMORY_SCOPE_WVFRNT 3
// RISCV32: #define __NO_INLINE__ 1
// RISCV32: #define __POINTER_WIDTH__ 32
// RISCV32: #define __PRAGMA_REDEFINE_EXTNAME 1
@@ -2634,11 +2643,11 @@
// RISCV64: #define __LONG_LONG_MAX__ 9223372036854775807LL
// RISCV64: #define __LONG_MAX__ 9223372036854775807L
// RISCV64: #define __LP64__ 1
-// RISCV64: #define __MEMORY_SCOPE_DEVICE 1
-// RISCV64: #define __MEMORY_SCOPE_SINGLE 4
-// RISCV64: #define __MEMORY_SCOPE_SYSTEM 0
-// RISCV64: #define __MEMORY_SCOPE_WRKGRP 2
-// RISCV64: #define __MEMORY_SCOPE_WVFRNT 3
+// RISCV64: #define __MEMORY_SCOPE_DEVICE 1
+// RISCV64: #define __MEMORY_SCOPE_SINGLE 4
+// RISCV64: #define __MEMORY_SCOPE_SYSTEM 0
+// RISCV64: #define __MEMORY_SCOPE_WRKGRP 2
+// RISCV64: #define __MEMORY_SCOPE_WVFRNT 3
// RISCV64: #define __NO_INLINE__ 1
// RISCV64: #define __POINTER_WIDTH__ 64
// RISCV64: #define __PRAGMA_REDEFINE_EXTNAME 1
diff --git a/clang/test/Preprocessor/predefined-win-macros.c b/clang/test/Preprocessor/predefined-win-macros.c
index b830dc39d477dd..14e2f584bd0938 100644
--- a/clang/test/Preprocessor/predefined-win-macros.c
+++ b/clang/test/Preprocessor/predefined-win-macros.c
@@ -3,7 +3,7 @@
// RUN: %clang_cc1 %s -x c++ -E -dM -triple x86_64-pc-win32 -fms-extensions -fms-compatibility \
// RUN: -fms-compatibility-version=19.00 -std=c++14 -o - | FileCheck -match-full-lines %s --check-prefix=CHECK-MS64
// RUN: %clang_cc1 %s -x c++ -E -dM -triple x86_64-pc-win32 -fms-extensions -fms-compatibility \
-// RUN: -fms-compatibility-version=19.00 -std=c++14 -o - | grep GCC | count 5
+// RUN: -fms-compatibility-version=19.00 -std=c++14 -o - | grep GCC | count 7
// CHECK-MS64: #define _INTEGRAL_MAX_BITS 64
// CHECK-MS64: #define _ISO_VOLATILE 1
// CHECK-MS64: #define _MSC_EXTENSIONS 1
@@ -26,7 +26,7 @@
// RUN: %clang_cc1 %s -x c++ -E -dM -triple i686-pc-win32 -fms-extensions -fms-compatibility \
// RUN: -fms-compatibility-version=19.00 -std=c++17 -o - | FileCheck -match-full-lines %s --check-prefix=CHECK-MS
// RUN: %clang_cc1 %s -x c++ -E -dM -triple i686-pc-win32 -fms-extensions -fms-compatibility \
-// RUN: -fms-compatibility-version=19.00 -std=c++17 -o - | grep GCC | count 5
+// RUN: -fms-compatibility-version=19.00 -std=c++17 -o - | grep GCC | count 7
// CHECK-MS: #define _INTEGRAL_MAX_BITS 64
// CHECK-MS: #define _ISO_VOLATILE 1
// CHECK-MS: #define _MSC_EXTENSIONS 1
@@ -39,6 +39,8 @@
// CHECK-MS-NOT: GNU
// CHECK-MS-NOT: GXX
// CHECK-MS: #define __GCC_ASM_FLAG_OUTPUTS__ 1
+// CHECK-MS: #define __GCC_CONSTRUCTIVE_SIZE {{.+}}
+// CHECK-MS: #define __GCC_DESTRUCTIVE_SIZE {{.+}}
// CHECK-MS: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 1
// CHECK-MS: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 1
// CHECK-MS: #define __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 1
>From 658a329bd820af181c00a54d0df74d36e86e0387 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 22 Apr 2024 12:04:16 -0400
Subject: [PATCH 2/8] Handle SystemZ
---
clang/lib/Basic/Targets/SystemZ.h | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/clang/lib/Basic/Targets/SystemZ.h b/clang/lib/Basic/Targets/SystemZ.h
index 8e302acd51b8ad..189d8a5ac87de4 100644
--- a/clang/lib/Basic/Targets/SystemZ.h
+++ b/clang/lib/Basic/Targets/SystemZ.h
@@ -220,6 +220,13 @@ class LLVM_LIBRARY_VISIBILITY SystemZTargetInfo : public TargetInfo {
int getEHDataRegisterNumber(unsigned RegNo) const override {
return RegNo < 4 ? 6 + RegNo : -1;
}
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 256;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 256;
+ }
};
} // namespace targets
} // namespace clang
>From 6b6da8f3f763eda12633da12e8f08669de2cc469 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 22 Apr 2024 14:44:07 -0400
Subject: [PATCH 3/8] Remove mention of using -D from the release notes
---
clang/docs/ReleaseNotes.rst | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7414856fd6a03e..337649371b6ead 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -95,8 +95,7 @@ C++17 Feature Support
predefined macros to support standard library implementations of
``std::hardware_destructive_interference_size`` and
``std::hardware_constructive_interference_size``, respectively. These macros
- are predefined in all C and C++ language modes. These macros can be
- overridden on the command line with ``-D``, if desired. The values the macros
+ are predefined in all C and C++ language modes. The values the macros
expand to are not stable between releases of Clang and do not need to match
the values produced by GCC, so these macros should not be used from header
files because they may not be stable across multiple TUs (the values may vary
>From 3f9118255c823992bcb539bfb2c88d98e52550e3 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 22 Apr 2024 15:07:49 -0400
Subject: [PATCH 4/8] Add non-default values for the following targets:
ARM64
AVR
BPF
M68K
mips32
mips64
Power
Power64
RISC-V
SPARC
---
clang/lib/Basic/Targets/ARM.h | 4 ++++
clang/lib/Basic/Targets/AVR.h | 7 +++++++
clang/lib/Basic/Targets/BPF.h | 7 +++++++
clang/lib/Basic/Targets/M68k.h | 7 +++++++
clang/lib/Basic/Targets/Mips.h | 7 +++++++
clang/lib/Basic/Targets/PPC.h | 14 ++++++++++++++
clang/lib/Basic/Targets/RISCV.h | 7 +++++++
clang/lib/Basic/Targets/Sparc.h | 7 +++++++
8 files changed, 60 insertions(+)
diff --git a/clang/lib/Basic/Targets/ARM.h b/clang/lib/Basic/Targets/ARM.h
index e69adbe754739f..7174a6a5e6dc86 100644
--- a/clang/lib/Basic/Targets/ARM.h
+++ b/clang/lib/Basic/Targets/ARM.h
@@ -225,6 +225,10 @@ class LLVM_LIBRARY_VISIBILITY ARMTargetInfo : public TargetInfo {
bool hasBitIntType() const override { return true; }
const char *getBFloat16Mangling() const override { return "u6__bf16"; };
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return getTriple().isArch64Bit() ? 256 : 64;
+ }
};
class LLVM_LIBRARY_VISIBILITY ARMleTargetInfo : public ARMTargetInfo {
diff --git a/clang/lib/Basic/Targets/AVR.h b/clang/lib/Basic/Targets/AVR.h
index 9376c46cd98ca1..58c7f0ae8a923a 100644
--- a/clang/lib/Basic/Targets/AVR.h
+++ b/clang/lib/Basic/Targets/AVR.h
@@ -175,6 +175,13 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
std::optional<std::string> handleAsmEscapedChar(char EscChar) const override;
StringRef getABI() const override { return ABI; }
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 32;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 32;
+ }
+
protected:
std::string CPU;
StringRef ABI;
diff --git a/clang/lib/Basic/Targets/BPF.h b/clang/lib/Basic/Targets/BPF.h
index 489f29fc4fead3..1340c47cc86df5 100644
--- a/clang/lib/Basic/Targets/BPF.h
+++ b/clang/lib/Basic/Targets/BPF.h
@@ -113,6 +113,13 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
StringRef CPUName(Name);
return isValidCPUName(CPUName);
}
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 32;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 32;
+ }
};
} // namespace targets
} // namespace clang
diff --git a/clang/lib/Basic/Targets/M68k.h b/clang/lib/Basic/Targets/M68k.h
index 7ffa901127e504..9807fdc9fc2e72 100644
--- a/clang/lib/Basic/Targets/M68k.h
+++ b/clang/lib/Basic/Targets/M68k.h
@@ -56,6 +56,13 @@ class LLVM_LIBRARY_VISIBILITY M68kTargetInfo : public TargetInfo {
BuiltinVaListKind getBuiltinVaListKind() const override;
bool setCPU(const std::string &Name) override;
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override;
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 32;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 32;
+ }
};
} // namespace targets
diff --git a/clang/lib/Basic/Targets/Mips.h b/clang/lib/Basic/Targets/Mips.h
index 0d6e4b4d080890..a9af626c959526 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -431,6 +431,13 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
bool validateTarget(DiagnosticsEngine &Diags) const override;
bool hasBitIntType() const override { return true; }
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 32;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 32;
+ }
};
} // namespace targets
} // namespace clang
diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h
index fa2f442e25846d..840d0f1d680faf 100644
--- a/clang/lib/Basic/Targets/PPC.h
+++ b/clang/lib/Basic/Targets/PPC.h
@@ -422,6 +422,13 @@ class LLVM_LIBRARY_VISIBILITY PPC32TargetInfo : public PPCTargetInfo {
// This is the ELF definition
return TargetInfo::PowerABIBuiltinVaList;
}
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 32;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 32;
+ }
};
// Note: ABI differences may eventually require us to have a separate
@@ -502,6 +509,13 @@ class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
return CCCR_Warning;
}
}
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 128;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 128;
+ }
};
class LLVM_LIBRARY_VISIBILITY AIXPPC32TargetInfo :
diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h
index 78580b5b1c1063..2db8a535251217 100644
--- a/clang/lib/Basic/Targets/RISCV.h
+++ b/clang/lib/Basic/Targets/RISCV.h
@@ -122,6 +122,13 @@ class RISCVTargetInfo : public TargetInfo {
void fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const override;
bool supportsTargetAttributeTune() const override { return true; }
ParsedTargetAttr parseTargetAttr(StringRef Str) const override;
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 32;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 32;
+ }
};
class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
public:
diff --git a/clang/lib/Basic/Targets/Sparc.h b/clang/lib/Basic/Targets/Sparc.h
index 214fef88e1dcd4..1d15010ac9b1e8 100644
--- a/clang/lib/Basic/Targets/Sparc.h
+++ b/clang/lib/Basic/Targets/Sparc.h
@@ -140,6 +140,13 @@ class LLVM_LIBRARY_VISIBILITY SparcTargetInfo : public TargetInfo {
CPU = getCPUKind(Name);
return CPU != CK_GENERIC;
}
+
+ virtual unsigned hardwareDestructiveInterferenceSize() const override {
+ return 32;
+ }
+ virtual unsigned hardwareConstructiveInterferenceSize() const override {
+ return 32;
+ }
};
// SPARC v8 is the 32-bit mode selected by Triple::sparc.
>From 6b6cec1fa3ed5a23749ba03c4093c3aa318ce4b7 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 22 Apr 2024 15:32:56 -0400
Subject: [PATCH 5/8] Add explicit documentation for these predefined macros
---
clang/docs/LanguageExtensions.rst | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 3bead159c8f946..74327e23e9eb32 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -5571,3 +5571,17 @@ but the expression has no runtime effects.
Type- and value-dependent expressions are not supported yet.
This facility is designed to aid with testing name lookup machinery.
+
+Predefined Macros
+=================
+
+`__GCC_DESTRUCTIVE_SIZE` and `__GCC_CONSTRUCTIVE_SIZE`
+------------------------------------------------------
+Specify the mimum offset between two objects to avoid false sharing and the
+maximum size of contiguous memory to promote true sharing, respectively. These
+macros are predefined in all C and C++ language modes.
+
+**Note: the values the macros expand to are not stable between releases of Clang
+and do not need to match the values produced by GCC, so these macros should not
+be used from header files because they may not be stable across multiple TUs
+(the values may vary based on compiler version as well as CPU tuning).**
>From 0d085f1a090d4998f45e26f9232f99ab5c9fac15 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 23 Apr 2024 10:50:23 -0400
Subject: [PATCH 6/8] Try to fix XFAILs in libc++
---
.../support.dynamic/hardware_inference_size.compile.pass.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/std/language.support/support.dynamic/hardware_inference_size.compile.pass.cpp b/libcxx/test/std/language.support/support.dynamic/hardware_inference_size.compile.pass.cpp
index ae277d53e46fd0..2eab368e3db76f 100644
--- a/libcxx/test/std/language.support/support.dynamic/hardware_inference_size.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/hardware_inference_size.compile.pass.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
-// XFAIL: (clang || apple-clang) && stdlib=libc++
+// XFAIL: (clang-17 || clang-18 || apple-clang-15) && stdlib=libc++
#include <new>
>From b16c85c924b1909c832867c7517dd79f951b8bee Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Wed, 24 Apr 2024 08:44:14 -0400
Subject: [PATCH 7/8] Change the interface to return a pair instead of two
functions
Also, updated the comments on the base function to make it clear that
the values returned are not part of the ABI.
---
clang/include/clang/Basic/TargetInfo.h | 13 +++++++++----
clang/lib/Basic/Targets/ARM.h | 4 ++--
clang/lib/Basic/Targets/AVR.h | 7 ++-----
clang/lib/Basic/Targets/BPF.h | 7 ++-----
clang/lib/Basic/Targets/M68k.h | 7 ++-----
clang/lib/Basic/Targets/Mips.h | 7 ++-----
clang/lib/Basic/Targets/PPC.h | 14 ++++----------
clang/lib/Basic/Targets/RISCV.h | 7 ++-----
clang/lib/Basic/Targets/Sparc.h | 7 ++-----
clang/lib/Basic/Targets/SystemZ.h | 7 ++-----
clang/lib/Frontend/InitPreprocessor.cpp | 7 +++----
11 files changed, 32 insertions(+), 55 deletions(-)
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index a7b112a41f1b99..3ced2e7397a754 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -40,6 +40,7 @@
#include <cassert>
#include <optional>
#include <string>
+#include <utility>
#include <vector>
namespace llvm {
@@ -1792,10 +1793,14 @@ class TargetInfo : public TransferrableTargetInfo,
/// Whether to support HIP image/texture API's.
virtual bool hasHIPImageSupport() const { return true; }
- /// The minimum offset between two objects to avoid false sharing.
- virtual unsigned hardwareDestructiveInterferenceSize() const { return 64; }
- // The maximum size of contiguous memory to promote true sharing.
- virtual unsigned hardwareConstructiveInterferenceSize() const { return 64; }
+ /// The first value in the pair is the minimum offset between two objects to
+ /// avoid false sharing (destructive interference). The second value in the
+ /// pair is maximum size of contiguous memory to promote true sharing
+ /// (constructive interference). Neither of these values are considered part
+ /// of the ABI and can be changed by targets at any time.
+ virtual std::pair<unsigned, unsigned> hardwareInterferenceSizes() const {
+ return std::make_pair(64, 64);
+ }
protected:
/// Copy type and layout related info.
diff --git a/clang/lib/Basic/Targets/ARM.h b/clang/lib/Basic/Targets/ARM.h
index 7174a6a5e6dc86..df9855a52e61c0 100644
--- a/clang/lib/Basic/Targets/ARM.h
+++ b/clang/lib/Basic/Targets/ARM.h
@@ -226,8 +226,8 @@ class LLVM_LIBRARY_VISIBILITY ARMTargetInfo : public TargetInfo {
const char *getBFloat16Mangling() const override { return "u6__bf16"; };
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return getTriple().isArch64Bit() ? 256 : 64;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(getTriple().isArch64Bit() ? 256 : 64, 64);
}
};
diff --git a/clang/lib/Basic/Targets/AVR.h b/clang/lib/Basic/Targets/AVR.h
index 58c7f0ae8a923a..feeb04f37eeba7 100644
--- a/clang/lib/Basic/Targets/AVR.h
+++ b/clang/lib/Basic/Targets/AVR.h
@@ -175,11 +175,8 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
std::optional<std::string> handleAsmEscapedChar(char EscChar) const override;
StringRef getABI() const override { return ABI; }
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 32;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 32;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(32, 32);
}
protected:
diff --git a/clang/lib/Basic/Targets/BPF.h b/clang/lib/Basic/Targets/BPF.h
index 1340c47cc86df5..d19b37dd4df7a7 100644
--- a/clang/lib/Basic/Targets/BPF.h
+++ b/clang/lib/Basic/Targets/BPF.h
@@ -114,11 +114,8 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
return isValidCPUName(CPUName);
}
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 32;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 32;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(32, 32);
}
};
} // namespace targets
diff --git a/clang/lib/Basic/Targets/M68k.h b/clang/lib/Basic/Targets/M68k.h
index 9807fdc9fc2e72..b732add77e0340 100644
--- a/clang/lib/Basic/Targets/M68k.h
+++ b/clang/lib/Basic/Targets/M68k.h
@@ -57,11 +57,8 @@ class LLVM_LIBRARY_VISIBILITY M68kTargetInfo : public TargetInfo {
bool setCPU(const std::string &Name) override;
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override;
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 32;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 32;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(32, 32);
}
};
diff --git a/clang/lib/Basic/Targets/Mips.h b/clang/lib/Basic/Targets/Mips.h
index a9af626c959526..730deb674aa579 100644
--- a/clang/lib/Basic/Targets/Mips.h
+++ b/clang/lib/Basic/Targets/Mips.h
@@ -432,11 +432,8 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
bool validateTarget(DiagnosticsEngine &Diags) const override;
bool hasBitIntType() const override { return true; }
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 32;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 32;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(32, 32);
}
};
} // namespace targets
diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h
index 840d0f1d680faf..3661927e159df1 100644
--- a/clang/lib/Basic/Targets/PPC.h
+++ b/clang/lib/Basic/Targets/PPC.h
@@ -423,11 +423,8 @@ class LLVM_LIBRARY_VISIBILITY PPC32TargetInfo : public PPCTargetInfo {
return TargetInfo::PowerABIBuiltinVaList;
}
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 32;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 32;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(32, 32);
}
};
@@ -510,11 +507,8 @@ class LLVM_LIBRARY_VISIBILITY PPC64TargetInfo : public PPCTargetInfo {
}
}
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 128;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 128;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(128, 128);
}
};
diff --git a/clang/lib/Basic/Targets/RISCV.h b/clang/lib/Basic/Targets/RISCV.h
index 2db8a535251217..7d482d4fd37383 100644
--- a/clang/lib/Basic/Targets/RISCV.h
+++ b/clang/lib/Basic/Targets/RISCV.h
@@ -123,11 +123,8 @@ class RISCVTargetInfo : public TargetInfo {
bool supportsTargetAttributeTune() const override { return true; }
ParsedTargetAttr parseTargetAttr(StringRef Str) const override;
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 32;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 32;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(32, 32);
}
};
class LLVM_LIBRARY_VISIBILITY RISCV32TargetInfo : public RISCVTargetInfo {
diff --git a/clang/lib/Basic/Targets/Sparc.h b/clang/lib/Basic/Targets/Sparc.h
index 1d15010ac9b1e8..3357bee33e1ac7 100644
--- a/clang/lib/Basic/Targets/Sparc.h
+++ b/clang/lib/Basic/Targets/Sparc.h
@@ -141,11 +141,8 @@ class LLVM_LIBRARY_VISIBILITY SparcTargetInfo : public TargetInfo {
return CPU != CK_GENERIC;
}
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 32;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 32;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(32, 32);
}
};
diff --git a/clang/lib/Basic/Targets/SystemZ.h b/clang/lib/Basic/Targets/SystemZ.h
index 189d8a5ac87de4..73d3aa01a043f4 100644
--- a/clang/lib/Basic/Targets/SystemZ.h
+++ b/clang/lib/Basic/Targets/SystemZ.h
@@ -221,11 +221,8 @@ class LLVM_LIBRARY_VISIBILITY SystemZTargetInfo : public TargetInfo {
return RegNo < 4 ? 6 + RegNo : -1;
}
- virtual unsigned hardwareDestructiveInterferenceSize() const override {
- return 256;
- }
- virtual unsigned hardwareConstructiveInterferenceSize() const override {
- return 256;
+ std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
+ return std::make_pair(256, 256);
}
};
} // namespace targets
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index b88cd86e09fa1e..9fbcfc87a2459e 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1311,10 +1311,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
// GCC defines these macros in both C and C++ modes despite them being needed
// mostly for STL implementations in C++.
- Builder.defineMacro("__GCC_DESTRUCTIVE_SIZE",
- Twine(TI.hardwareDestructiveInterferenceSize()));
- Builder.defineMacro("__GCC_CONSTRUCTIVE_SIZE",
- Twine(TI.hardwareConstructiveInterferenceSize()));
+ auto [Destructive, Constructive] = TI.hardwareInterferenceSizes();
+ Builder.defineMacro("__GCC_DESTRUCTIVE_SIZE", Twine(Destructive));
+ Builder.defineMacro("__GCC_CONSTRUCTIVE_SIZE", Twine(Constructive));
auto addLockFreeMacros = [&](const llvm::Twine &Prefix) {
// Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
>From 29874405c55fa196547c40e6370b2bada7455f96 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Wed, 24 Apr 2024 14:23:46 -0400
Subject: [PATCH 8/8] Allow redefining or undefining the macros from the
command line
---
clang/docs/LanguageExtensions.rst | 4 +++-
clang/lib/Frontend/InitPreprocessor.cpp | 4 ++++
.../test/Preprocessor/hardware_interference.cpp | 17 +++++++++++++++++
3 files changed, 24 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Preprocessor/hardware_interference.cpp
diff --git a/clang/docs/LanguageExtensions.rst b/clang/docs/LanguageExtensions.rst
index 74327e23e9eb32..ea0011dbea40a5 100644
--- a/clang/docs/LanguageExtensions.rst
+++ b/clang/docs/LanguageExtensions.rst
@@ -5579,7 +5579,9 @@ Predefined Macros
------------------------------------------------------
Specify the mimum offset between two objects to avoid false sharing and the
maximum size of contiguous memory to promote true sharing, respectively. These
-macros are predefined in all C and C++ language modes.
+macros are predefined in all C and C++ language modes, but can be redefined on
+the command line with ``-D`` to specify different values as needed or can be
+undefined on the command line with ``-U`` to disable support for the feature.
**Note: the values the macros expand to are not stable between releases of Clang
and do not need to match the values produced by GCC, so these macros should not
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 9fbcfc87a2459e..db53ef5bf32b90 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1314,6 +1314,10 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
auto [Destructive, Constructive] = TI.hardwareInterferenceSizes();
Builder.defineMacro("__GCC_DESTRUCTIVE_SIZE", Twine(Destructive));
Builder.defineMacro("__GCC_CONSTRUCTIVE_SIZE", Twine(Constructive));
+ // We need to use push_macro to allow users to redefine these macros from the
+ // command line with -D and not issue a -Wmacro-redefined warning.
+ Builder.append("#pragma push_macro(\"__GCC_DESTRUCTIVE_SIZE\")");
+ Builder.append("#pragma push_macro(\"__GCC_CONSTRUCTIVE_SIZE\")");
auto addLockFreeMacros = [&](const llvm::Twine &Prefix) {
// Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
diff --git a/clang/test/Preprocessor/hardware_interference.cpp b/clang/test/Preprocessor/hardware_interference.cpp
new file mode 100644
index 00000000000000..f3727aadd32a2f
--- /dev/null
+++ b/clang/test/Preprocessor/hardware_interference.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -E -dM -D__GCC_CONSTRUCTIVE_SIZE=1000 -D__GCC_DESTRUCTIVE_SIZE=1001 %s -verify -Weverything | FileCheck %s
+// RUN: %clang_cc1 -D__GCC_CONSTRUCTIVE_SIZE=1000 -D__GCC_DESTRUCTIVE_SIZE=1001 %s -verify -Weverything
+// RUN: %clang_cc1 -E -dM -U__GCC_CONSTRUCTIVE_SIZE -U__GCC_DESTRUCTIVE_SIZE %s -verify -Weverything | FileCheck --check-prefix DISABLED %s
+// expected-no-diagnostics
+
+// Validate that we can set a new value on the command line without issuing any
+// diagnostics and that we can disabled the macro on the command line without
+// issuing any diagnostics.
+
+// CHECK: #define __GCC_CONSTRUCTIVE_SIZE 1000
+// CHECK: #define __GCC_DESTRUCTIVE_SIZE 1001
+// DISABLED-NOT: __GCC_CONSTRUCTIVE_SIZE
+// DISABLED-NOT: __GCC_DESTRUCTIVE_SIZE
+
+int main() {
+ return 0;
+}
More information about the libcxx-commits
mailing list