Skip to content

Commit b0158db

Browse files
committed
[TBAA] Don't emit pointer tbaa for unnamed structs or unions. (llvm#116596)
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. PR: llvm#116596 (cherry picked from commit 41a0c66)
1 parent ad950e6 commit b0158db

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,22 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
248248
// Be conservative if the type isn't a RecordType. We are specifically
249249
// required to do this for member pointers until we implement the
250250
// similar-types rule.
251-
if (!Ty->isRecordType())
251+
const auto *RT = Ty->getAs<RecordType>();
252+
if (!RT)
253+
return AnyPtr;
254+
255+
// For unnamed structs or unions C's compatible types rule applies. Two
256+
// compatible types in different compilation units can have different
257+
// mangled names, meaning the metadata emitted below would incorrectly
258+
// mark them as no-alias. Use AnyPtr for such types in both C and C++, as
259+
// C and C++ types may be visible when doing LTO.
260+
//
261+
// Note that using AnyPtr is overly conservative. We could summarize the
262+
// members of the type, as per the C compatibility rule in the future.
263+
// This also covers anonymous structs and unions, which have a different
264+
// compatibility rule, but it doesn't matter because you can never have a
265+
// pointer to an anonymous struct or union.
266+
if (!RT->getDecl()->getDeclName())
252267
return AnyPtr;
253268

254269
// For non-builtin types use the mangled name of the canonical type.

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 %ptr)
@@ -239,5 +237,4 @@ void unamed_struct_typedef(TypedefS *ptr) {
239237
// DEFAULT: [[S2_TY]] = !{!"S2", [[ANY_POINTER]], i64 0}
240238
// COMMON: [[INT_TAG]] = !{[[INT_TY:!.+]], [[INT_TY]], i64 0}
241239
// COMMON: [[INT_TY]] = !{!"int", [[CHAR]], i64 0}
242-
// ENABLED: [[P1TYPEDEF]] = !{[[P1TYPEDEF_TY:!.+]], [[P1TYPEDEF_TY]], i64 0}
243-
// ENABLED: [[P1TYPEDEF_TY]] = !{!"p1 _ZTS8TypedefS", [[ANY_POINTER]], i64 0}
240+
// ENABLED: [[P1TYPEDEF]] = !{[[ANY_POINTER]], [[ANY_POINTER]], i64 0}

0 commit comments

Comments
 (0)