Skip to content

[TBAA] Don't emit pointer tbaa for unnamed structs or unions. #116596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion clang/lib/CodeGen/CodeGenTBAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,22 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
// Be conservative if the type isn't a RecordType. We are specifically
// required to do this for member pointers until we implement the
// similar-types rule.
if (!Ty->isRecordType())
const auto *RT = Ty->getAs<RecordType>();
if (!RT)
return AnyPtr;

// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like this to note that

  • using AnyPtr is over-conservative here, and we could make this summarize the members as per the C compatibility rule in the future and
  • this also covers anonymous structs and unions, which have a different compatibility rule, but it doesn't matter because you can never have a pointer to an anonymous struct or union.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added the clarifications as suggested!

//
// Note that using AnyPtr is overly conservative. We could summarize the
// members of the type, as per the C compatibility rule in the future.
// This also covers anonymous structs and unions, which have a different
// compatibility rule, but it doesn't matter because you can never have a
// pointer to an anonymous struct or union.
if (!RT->getDecl()->getDeclName())
return AnyPtr;

// For non-builtin types use the mangled name of the canonical type.
Expand Down
5 changes: 1 addition & 4 deletions clang/test/CodeGen/tbaa-pointers.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ typedef struct {
int i1;
} TypedefS;

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