[clang] [TBAA] Don't emit pointer tbaa for unnamed structs or unions. (PR #116596)
Florian Hahn via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 20 11:12:14 PST 2024
https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/116596
>From 1026043edbc1cb3e31c626ad7a1e621c595917f2 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 17 Nov 2024 20:07:57 +0000
Subject: [PATCH 1/2] [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.
---
clang/lib/CodeGen/CodeGenTBAA.cpp | 9 +++++++++
clang/test/CodeGen/tbaa-pointers.c | 5 +----
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index c31579e8323174..95d9f893a2c7d1 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -230,6 +230,15 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
->getString();
TyName = Name;
} else {
+ // 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.
+ const auto *RT = Ty->getAs<RecordType>();
+ if (RT && !RT->getDecl()->getDeclName())
+ return AnyPtr;
+
// For non-builtin types use the mangled name of the canonical type.
llvm::raw_svector_ostream TyOut(TyName);
MangleCtx->mangleCanonicalTypeName(QualType(Ty, 0), TyOut);
diff --git a/clang/test/CodeGen/tbaa-pointers.c b/clang/test/CodeGen/tbaa-pointers.c
index 9417a0e2f09e8c..068459f4dce118 100644
--- a/clang/test/CodeGen/tbaa-pointers.c
+++ b/clang/test/CodeGen/tbaa-pointers.c
@@ -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:%.+]])
@@ -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}
>From ddd22cf71ce5fff3262056099673e9d02f4339a7 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Wed, 20 Nov 2024 19:11:37 +0000
Subject: [PATCH 2/2] !fixup add clarifying note as suggested, thanks!
---
clang/lib/CodeGen/CodeGenTBAA.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 95d9f893a2c7d1..5544dd8b189c26 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -235,6 +235,12 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
// 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.
+ //
+ // 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.
const auto *RT = Ty->getAs<RecordType>();
if (RT && !RT->getDecl()->getDeclName())
return AnyPtr;
More information about the cfe-commits
mailing list