Skip to content

Commit 1026043

Browse files
committed
[TBAA] Don't emit pointer tbaa for unnamed structs or unions.
For unnamed structs or unions, C's compatible types rule applies. Two compatible types in different compilation units can have different mangled names, meaning the metadata emitted below would incorrectly mark them as no-alias. Use AnyPtr for such types in both C and C++, as C and C++ types may be visible when doing LTO.
1 parent d5032b9 commit 1026043

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
230230
->getString();
231231
TyName = Name;
232232
} else {
233+
// For unnamed structs or unions C's compatible types rule applies. Two
234+
// compatible types in different compilation units can have different
235+
// mangled names, meaning the metadata emitted below would incorrectly
236+
// mark them as no-alias. Use AnyPtr for such types in both C and C++, as
237+
// C and C++ types may be visible when doing LTO.
238+
const auto *RT = Ty->getAs<RecordType>();
239+
if (RT && !RT->getDecl()->getDeclName())
240+
return AnyPtr;
241+
233242
// For non-builtin types use the mangled name of the canonical type.
234243
llvm::raw_svector_ostream TyOut(TyName);
235244
MangleCtx->mangleCanonicalTypeName(QualType(Ty, 0), TyOut);

clang/test/CodeGen/tbaa-pointers.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ typedef struct {
190190
int i1;
191191
} TypedefS;
192192

193-
// FIXME: The !tbaa tag for unnamed structs doesn't account for compatible
194-
// types in C.
195193
void unamed_struct_typedef(TypedefS *ptr) {
196194
// COMMON-LABEL: define void @unamed_struct_typedef(
197195
// COMMON-SAME: ptr noundef [[PTRA:%.+]])
@@ -238,5 +236,4 @@ void unamed_struct_typedef(TypedefS *ptr) {
238236
// DEFAULT: [[S2_TY]] = !{!"S2", [[ANY_POINTER]], i64 0}
239237
// COMMON: [[INT_TAG]] = !{[[INT_TY:!.+]], [[INT_TY]], i64 0}
240238
// COMMON: [[INT_TY]] = !{!"int", [[CHAR]], i64 0}
241-
// ENABLED: [[P1TYPEDEF]] = !{[[P1TYPEDEF_TY:!.+]], [[P1TYPEDEF_TY]], i64 0}
242-
// ENABLED: [[P1TYPEDEF_TY]] = !{!"p1 _ZTS8TypedefS", [[ANY_POINTER]], i64 0}
239+
// ENABLED: [[P1TYPEDEF]] = !{[[ANY_POINTER]], [[ANY_POINTER]], i64 0}

0 commit comments

Comments
 (0)