Skip to content

Commit 41a0c66

Browse files
authored
[TBAA] Don't emit pointer tbaa for unnamed structs or unions. (#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: #116596
1 parent bcf654c commit 41a0c66

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

252267
// 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 [[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)