Skip to content

Commit 72c373b

Browse files
[C++17] Support __GCC_[CON|DE]STRUCTIVE_SIZE (#89446)
These macros are used by STL implementations to support implementation of std::hardware_destructive_interference_size and std::hardware_constructive_interference_size Fixes #60174 --------- Co-authored-by: Louis Dionne <[email protected]>
1 parent 0620a63 commit 72c373b

File tree

24 files changed

+181
-38
lines changed

24 files changed

+181
-38
lines changed

clang-tools-extra/test/pp-trace/pp-trace-pragma-general.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ void foo() {
2121

2222
// CHECK: ---
2323
// CHECK-NEXT: - Callback: PragmaDirective
24+
// CHECK-NEXT: Loc: "<built-in>:{{.+}}:1"
25+
// CHECK-NEXT: Introducer: PIK_HashPragma
26+
// CHECK-NEXT: - Callback: PragmaDirective
27+
// CHECK-NEXT: Loc: "<built-in>:{{.+}}:1"
28+
// CHECK-NEXT: Introducer: PIK_HashPragma
29+
// CHECK-NEXT: - Callback: PragmaDirective
2430
// CHECK-NEXT: Loc: "{{.*}}{{[/\\]}}pp-trace-pragma-general.cpp:3:1"
2531
// CHECK-NEXT: Introducer: PIK_HashPragma
2632
// CHECK-NEXT: - Callback: PragmaDiagnosticPush

clang-tools-extra/test/pp-trace/pp-trace-pragma-ms.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
// CHECK: ---
2020
// CHECK-NEXT: - Callback: PragmaDirective
21+
// CHECK-NEXT: Loc: "<built-in>:{{.+}}:1"
22+
// CHECK-NEXT: Introducer: PIK_HashPragma
23+
// CHECK-NEXT: - Callback: PragmaDirective
24+
// CHECK-NEXT: Loc: "<built-in>:{{.+}}:1"
25+
// CHECK-NEXT: Introducer: PIK_HashPragma
26+
// CHECK-NEXT: - Callback: PragmaDirective
2127
// CHECK-NEXT: Loc: "{{.*}}{{[/\\]}}pp-trace-pragma-ms.cpp:3:1"
2228
// CHECK-NEXT: Introducer: PIK_HashPragma
2329
// CHECK-NEXT: - Callback: PragmaComment
@@ -67,7 +73,7 @@
6773
// CHECK-NEXT: Introducer: PIK_HashPragma
6874
// CHECK-NEXT: - Callback: PragmaMessage
6975
// CHECK-NEXT: Loc: "{{.*}}{{[/\\]}}pp-trace-pragma-ms.cpp:13:9"
70-
// CHECK-NEXT: Namespace:
76+
// CHECK-NEXT: Namespace:
7177
// CHECK-NEXT: Kind: PMK_Message
7278
// CHECK-NEXT: Str: message argument
7379
// CHECK-NEXT: - Callback: PragmaDirective

clang-tools-extra/test/pp-trace/pp-trace-pragma-opencl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
// CHECK: ---
88
// CHECK-NEXT: - Callback: PragmaDirective
9+
// CHECK-NEXT: Loc: "<built-in>:{{.+}}:1"
10+
// CHECK-NEXT: Introducer: PIK_HashPragma
11+
// CHECK-NEXT: - Callback: PragmaDirective
12+
// CHECK-NEXT: Loc: "<built-in>:{{.+}}:1"
13+
// CHECK-NEXT: Introducer: PIK_HashPragma
14+
// CHECK-NEXT: - Callback: PragmaDirective
915
// CHECK-NEXT: Loc: "{{.*}}{{[/\\]}}pp-trace-pragma-opencl.cpp:3:1"
1016
// CHECK-NEXT: Introducer: PIK_HashPragma
1117
// CHECK-NEXT: - Callback: PragmaOpenCLExtension

clang/docs/LanguageExtensions.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5572,3 +5572,25 @@ but the expression has no runtime effects.
55725572
Type- and value-dependent expressions are not supported yet.
55735573
55745574
This facility is designed to aid with testing name lookup machinery.
5575+
5576+
Predefined Macros
5577+
=================
5578+
5579+
`__GCC_DESTRUCTIVE_SIZE` and `__GCC_CONSTRUCTIVE_SIZE`
5580+
------------------------------------------------------
5581+
Specify the mimum offset between two objects to avoid false sharing and the
5582+
maximum size of contiguous memory to promote true sharing, respectively. These
5583+
macros are predefined in all C and C++ language modes, but can be redefined on
5584+
the command line with ``-D`` to specify different values as needed or can be
5585+
undefined on the command line with ``-U`` to disable support for the feature.
5586+
5587+
**Note: the values the macros expand to are not guaranteed to be stable. They
5588+
are are affected by architectures and CPU tuning flags, can change between
5589+
releases of Clang and will not match the values defined by other compilers such
5590+
as GCC.**
5591+
5592+
Compiling different TUs depending on these flags (including use of
5593+
``std::hardware_constructive_interference`` or
5594+
``std::hardware_destructive_interference``) with different compilers, macro
5595+
definitions, or architecture flags will lead to ODR violations and should be
5596+
avoided.

clang/docs/ReleaseNotes.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ C++14 Feature Support
9595
- Sized deallocation is enabled by default in C++14 onwards. The user may specify
9696
``-fno-sized-deallocation`` to disable it if there are some regressions.
9797

98+
C++17 Feature Support
99+
^^^^^^^^^^^^^^^^^^^^^
100+
- Clang now exposes ``__GCC_DESTRUCTIVE_SIZE`` and ``__GCC_CONSTRUCTIVE_SIZE``
101+
predefined macros to support standard library implementations of
102+
``std::hardware_destructive_interference_size`` and
103+
``std::hardware_constructive_interference_size``, respectively. These macros
104+
are predefined in all C and C++ language modes. The values the macros
105+
expand to are not stable between releases of Clang and do not need to match
106+
the values produced by GCC, so these macros should not be used from header
107+
files because they may not be stable across multiple TUs (the values may vary
108+
based on compiler version as well as CPU tuning). #GH60174
109+
98110
C++20 Feature Support
99111
^^^^^^^^^^^^^^^^^^^^^
100112

clang/include/clang/Basic/TargetInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <cassert>
4141
#include <optional>
4242
#include <string>
43+
#include <utility>
4344
#include <vector>
4445

4546
namespace llvm {
@@ -1792,6 +1793,15 @@ class TargetInfo : public TransferrableTargetInfo,
17921793
/// Whether to support HIP image/texture API's.
17931794
virtual bool hasHIPImageSupport() const { return true; }
17941795

1796+
/// The first value in the pair is the minimum offset between two objects to
1797+
/// avoid false sharing (destructive interference). The second value in the
1798+
/// pair is maximum size of contiguous memory to promote true sharing
1799+
/// (constructive interference). Neither of these values are considered part
1800+
/// of the ABI and can be changed by targets at any time.
1801+
virtual std::pair<unsigned, unsigned> hardwareInterferenceSizes() const {
1802+
return std::make_pair(64, 64);
1803+
}
1804+
17951805
protected:
17961806
/// Copy type and layout related info.
17971807
void copyAuxTarget(const TargetInfo *Aux);

clang/lib/Basic/Targets/ARM.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ class LLVM_LIBRARY_VISIBILITY ARMTargetInfo : public TargetInfo {
225225
bool hasBitIntType() const override { return true; }
226226

227227
const char *getBFloat16Mangling() const override { return "u6__bf16"; };
228+
229+
std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
230+
return std::make_pair(getTriple().isArch64Bit() ? 256 : 64, 64);
231+
}
228232
};
229233

230234
class LLVM_LIBRARY_VISIBILITY ARMleTargetInfo : public ARMTargetInfo {

clang/lib/Basic/Targets/AVR.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ class LLVM_LIBRARY_VISIBILITY AVRTargetInfo : public TargetInfo {
175175
std::optional<std::string> handleAsmEscapedChar(char EscChar) const override;
176176
StringRef getABI() const override { return ABI; }
177177

178+
std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
179+
return std::make_pair(32, 32);
180+
}
181+
178182
protected:
179183
std::string CPU;
180184
StringRef ABI;

clang/lib/Basic/Targets/BPF.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
113113
StringRef CPUName(Name);
114114
return isValidCPUName(CPUName);
115115
}
116+
117+
std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
118+
return std::make_pair(32, 32);
119+
}
116120
};
117121
} // namespace targets
118122
} // namespace clang

clang/lib/Basic/Targets/M68k.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class LLVM_LIBRARY_VISIBILITY M68kTargetInfo : public TargetInfo {
5656
BuiltinVaListKind getBuiltinVaListKind() const override;
5757
bool setCPU(const std::string &Name) override;
5858
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override;
59+
60+
std::pair<unsigned, unsigned> hardwareInterferenceSizes() const override {
61+
return std::make_pair(32, 32);
62+
}
5963
};
6064

6165
} // namespace targets

0 commit comments

Comments
 (0)