Skip to content

Commit 1222bbb

Browse files
committed
[Clang] Enable -fpointer-tbaa by default.
Support for more precise TBAA metadata has been added a while ago (behind the -fpointer-tbaa flag). The more precise TBAA metadata allows treating accesses of different pointer types as no-alias. This helps to remove more redundant loads and stores in a number of workloads. Some highlights on the impact across llvm-test-suite's MultiSource, SPEC2006 & SPEC2017 include: * +2% more NoAlias results for memory access * +4% more loops vectorized * +3% more stores removed by DSE. This closes a relatively big gap to GCC, which has been supporting disambiguating based on pointer types for a long time. (https://clang.godbolt.org/z/K7Wbhrz4q) Pointer-TBAA support for pointers to builtin types has been added in #76612. Support for user-defined types has been added in #110569. There are 2 pending PRs with bug fixes for special cases uncovered during some of my testing: * #116991 * #116596
1 parent 157d847 commit 1222bbb

File tree

11 files changed

+459
-439
lines changed

11 files changed

+459
-439
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ C++ Language Changes
211211
- The builtin type alias ``__builtin_common_type`` has been added to improve the
212212
performance of ``std::common_type``.
213213

214+
- Clang now emits distinct type-based alias analysis tags for incompatible
215+
pointers by default, enabling more powerful alias analysis when accessing
216+
pointer types. The new default behavior can be disabled using ``-fno-pointer-tbaa``.
217+
214218
C++2c Feature Support
215219
^^^^^^^^^^^^^^^^^^^^^
216220

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ ENUM_CODEGENOPT(StructReturnConvention, StructReturnConventionKind, 2, SRCK_Defa
242242

243243
CODEGENOPT(RelaxAll , 1, 0) ///< Relax all machine code instructions.
244244
CODEGENOPT(RelaxedAliasing , 1, 0) ///< Set when -fno-strict-aliasing is enabled.
245-
CODEGENOPT(PointerTBAA, 1, 0) ///< Whether or not to use distinct TBAA tags for pointers.
245+
CODEGENOPT(PointerTBAA , 1, 1) ///< Whether or not to use distinct TBAA tags for pointers.
246246
CODEGENOPT(StructPathTBAA , 1, 0) ///< Whether or not to use struct-path TBAA.
247247
CODEGENOPT(NewStructPathTBAA , 1, 0) ///< Whether or not to use enhanced struct-path TBAA.
248248
CODEGENOPT(SaveTempLabels , 1, 0) ///< Save temporary labels.

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7361,9 +7361,12 @@ def fuse_register_sized_bitfield_access: Flag<["-"], "fuse-register-sized-bitfie
73617361
def relaxed_aliasing : Flag<["-"], "relaxed-aliasing">,
73627362
HelpText<"Turn off Type Based Alias Analysis">,
73637363
MarshallingInfoFlag<CodeGenOpts<"RelaxedAliasing">>;
7364-
def pointer_tbaa: Flag<["-"], "pointer-tbaa">,
7365-
HelpText<"Turn on Type Based Alias Analysis for pointer accesses">,
7366-
MarshallingInfoFlag<CodeGenOpts<"PointerTBAA">>;
7364+
defm pointer_tbaa: BoolOption<"", "pointer-tbaa", CodeGenOpts<"PointerTBAA">,
7365+
DefaultTrue,
7366+
PosFlag<SetTrue, [], [ClangOption], "Enable">,
7367+
NegFlag<SetFalse, [], [ClangOption], "Disable">,
7368+
BothFlags<[], [ClangOption], " that single precision floating-point divide and sqrt used in ">>
7369+
;
73677370
def no_struct_path_tbaa : Flag<["-"], "no-struct-path-tbaa">,
73687371
HelpText<"Turn off struct-path aware Type Based Alias Analysis">,
73697372
MarshallingInfoNegativeFlag<CodeGenOpts<"StructPathTBAA">>;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5935,9 +5935,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
59355935
if (!Args.hasFlag(options::OPT_fstrict_aliasing, StrictAliasingAliasOption,
59365936
options::OPT_fno_strict_aliasing, !IsWindowsMSVC))
59375937
CmdArgs.push_back("-relaxed-aliasing");
5938-
if (Args.hasFlag(options::OPT_fpointer_tbaa, options::OPT_fno_pointer_tbaa,
5938+
if (Args.hasFlag(options::OPT_fno_pointer_tbaa, options::OPT_fpointer_tbaa,
59395939
false))
5940-
CmdArgs.push_back("-pointer-tbaa");
5940+
CmdArgs.push_back("-no-pointer-tbaa");
59415941
if (!Args.hasFlag(options::OPT_fstruct_path_tbaa,
59425942
options::OPT_fno_struct_path_tbaa, true))
59435943
CmdArgs.push_back("-no-struct-path-tbaa");

clang/test/CodeGen/attr-counted-by.c

Lines changed: 28 additions & 28 deletions
Large diffs are not rendered by default.

clang/test/CodeGen/tbaa-pointers.c

Lines changed: 102 additions & 102 deletions
Large diffs are not rendered by default.

clang/test/CodeGen/tbaa-reference.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s -check-prefixes=CHECK,OLD-PATH
2-
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes -pointer-tbaa %s -emit-llvm -o - | FileCheck %s -check-prefixes=CHECK,OLD-PATH-POINTER
3-
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s -emit-llvm -new-struct-path-tbaa -o - | FileCheck %s -check-prefixes=CHECK,NEW-PATH
4-
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s -pointer-tbaa -emit-llvm -new-struct-path-tbaa -o - | FileCheck %s -check-prefixes=CHECK,NEW-PATH-POINTER
1+
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes -no-pointer-tbaa %s -emit-llvm -o - | FileCheck %s -check-prefixes=CHECK,OLD-PATH
2+
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s -emit-llvm -o - | FileCheck %s -check-prefixes=CHECK,OLD-PATH-POINTER
3+
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s -no-pointer-tbaa -emit-llvm -new-struct-path-tbaa -o - | FileCheck %s -check-prefixes=CHECK,NEW-PATH
4+
// RUN: %clang_cc1 -triple x86_64-linux -O1 -disable-llvm-passes %s -emit-llvm -new-struct-path-tbaa -o - | FileCheck %s -check-prefixes=CHECK,NEW-PATH-POINTER
55
//
66
// Check that we generate correct TBAA information for reference accesses.
77

clang/test/CodeGenCXX/template-instantiation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
// CHECK2-NOT: _ZTVN5test31SIiEE
1919
// CHECK2-NOT: _ZTSN5test31SIiEE
20+
// CHECK2: !{!"p1 _ZTSN5test31SIiEE",
2021

2122
// CHECK-LABEL: define linkonce_odr void @_ZN5test21CIiEC1Ev(ptr {{[^,]*}} %this) unnamed_addr
2223
// CHECK-LABEL: define linkonce_odr void @_ZN5test21CIiE6foobarIdEEvT_(

clang/test/CodeGenOpenCL/amdgpu-enqueue-kernel.cl

Lines changed: 80 additions & 73 deletions
Large diffs are not rendered by default.

clang/test/OpenMP/nvptx_target_parallel_reduction_codegen_tbaa_PR46146.cpp

Lines changed: 222 additions & 222 deletions
Large diffs are not rendered by default.

clang/unittests/CodeGen/TBAAMetadataTest.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ auto OmnipotentCharC = MMTuple(
3939
MConstInt(0, 64)
4040
);
4141

42+
auto AnyPtr = MMTuple(
43+
MMString("any pointer"),
44+
OmnipotentCharC,
45+
MConstInt(0, 64)
46+
);
4247

4348
auto OmnipotentCharCXX = MMTuple(
4449
MMString("omnipotent char"),
@@ -116,8 +121,8 @@ TEST(TBAAMetadataTest, BasicTypes) {
116121
MValType(PointerType::getUnqual(Compiler.Context)),
117122
MMTuple(
118123
MMTuple(
119-
MMString("any pointer"),
120-
OmnipotentCharC,
124+
MMString("p1 void"),
125+
AnyPtr,
121126
MConstInt(0)),
122127
MSameAs(0),
123128
MConstInt(0))));
@@ -128,8 +133,8 @@ TEST(TBAAMetadataTest, BasicTypes) {
128133
MValType(PointerType::getUnqual(Compiler.Context)),
129134
MMTuple(
130135
MMTuple(
131-
MMString("any pointer"),
132-
OmnipotentCharC,
136+
MMString("p1 int"),
137+
AnyPtr,
133138
MConstInt(0)),
134139
MSameAs(0),
135140
MConstInt(0))));

0 commit comments

Comments
 (0)