[clang] [Clang][TBAA] Describe complex types as two fields in !tbaa.struct (PR #234539)

Mattéo Rizza Murgier via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 01:56:48 PST 2026


https://github.com/matteo-rm updated https://github.com/llvm/llvm-project/pull/234539

>From bd2ba533988dbf4c4931e79a67e4c6d47d6a327d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Matt=C3=A9o=20Rizza=20Murgier?=
 <matteo.rizza-murgier at sipearl.com>
Date: Wed, 21 Oct 2026 17:01:39 +0200
Subject: [PATCH 1/3] [Clang][TBAA] Describe complex types as two fields in
 !tbaa.struct

---
 clang/lib/CodeGen/CodeGenTBAA.cpp  | 12 ++++++++++
 clang/test/CodeGen/tbaa-struct.cpp | 36 ++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index d33188db9da158..8573193534573d 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -493,6 +493,18 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
     return true;
   }
 
+  if (const auto *CTy = QTy->getAsCanonical<ComplexType>()) {
+    QualType ElemQTy = CTy->getElementType();
+    uint64_t Size = Context.getTypeSizeInChars(ElemQTy).getQuantity();
+    llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(ElemQTy);
+    llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
+    Fields.push_back(
+        llvm::MDBuilder::TBAAStructField(BaseOffset, Size, TBAATag));
+    Fields.push_back(
+        llvm::MDBuilder::TBAAStructField(BaseOffset + Size, Size, TBAATag));
+    return true;
+  }
+
   /* Otherwise, treat whatever it is as a field. */
   uint64_t Offset = BaseOffset;
   uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
diff --git a/clang/test/CodeGen/tbaa-struct.cpp b/clang/test/CodeGen/tbaa-struct.cpp
index 2776ea2e4e8619..102650c54215ef 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -184,6 +184,38 @@ void copy12(UnionMember2 *a1, UnionMember2 *a2) {
   *a1 = *a2;
 }
 
+// Make sure that complex members are described as two fields of their element
+// type.
+struct ComplexMembers {
+  int i;
+  _Complex float f;
+  _Complex double d;
+};
+
+void copy13(ComplexMembers *a1, ComplexMembers *a2) {
+// CHECK-LABEL: _Z6copy13P14ComplexMembersS0_
+// CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %a1, ptr noundef nonnull align 8 dereferenceable(32) %a2, i64 32, i1 false),
+// CHECK-OLD-SAME: !tbaa.struct [[TBAA_STRUCT21:![0-9]+]]
+// CHECK-NEW-SAME: !tbaa [[TBAA45:![0-9]+]], !tbaa.struct
+  *a1 = *a2;
+}
+
+// Make sure that may_alias on a complex member is respected.
+typedef _Complex double __attribute__((may_alias)) AliasingComplex;
+
+struct MayAliasComplexMember {
+  int i;
+  AliasingComplex d;
+};
+
+void copy14(MayAliasComplexMember *a1, MayAliasComplexMember *a2) {
+// CHECK-LABEL: _Z6copy14P21MayAliasComplexMemberS0_
+// CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %a1, ptr noundef nonnull align 8 dereferenceable(24) %a2, i64 24, i1 false),
+// CHECK-OLD-SAME: !tbaa.struct [[TBAA_STRUCT24:![0-9]+]]
+// CHECK-NEW-SAME: !tbaa [[TBAA49:![0-9]+]], !tbaa.struct
+  *a1 = *a2;
+}
+
 // (offset, size) = (0,1) char; (4,2) short; (8,4) int; (12,1) char; (16,4) int; (20,4) int
 // (offset, size) = (0,8) char; (0,2) char; (4,8) char
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
@@ -208,6 +240,10 @@ void copy12(UnionMember2 *a1, UnionMember2 *a2) {
 // CHECK-OLD: [[TBAA_STRUCT18]] = !{i64 0, i64 4, [[META9]], i64 8, i64 8, [[META15]]}
 // CHECK-OLD: [[TBAA_STRUCT19]] = !{i64 0, i64 8, [[META9]], i64 8, i64 4, [[META2]]}
 // CHECK-OLD: [[TBAA_STRUCT20]] = !{i64 0, i64 4, [[META2]], i64 8, i64 8, [[META9]]}
+// CHECK-OLD: [[TBAA_STRUCT21]] = !{i64 0, i64 4, [[META2]], i64 4, i64 4, [[META22:![0-9]+]], i64 8, i64 4, [[META22]], i64 16, i64 8, [[META15]], i64 24, i64 8, [[META15]]}
+// CHECK-OLD: [[META22]] = !{[[META23:![0-9]+]], [[META23]], i64 0}
+// CHECK-OLD: [[META23]] = !{!"float", [[META4]], i64 0}
+// CHECK-OLD: [[TBAA_STRUCT24]] = !{i64 0, i64 4, [[META2]], i64 8, i64 8, [[META9]], i64 16, i64 8, [[META9]]}
 //.
 // CHECK-NEW: [[META4:![0-9]+]] = !{[[META5:![0-9]+]], i64 1, !"omnipotent char"}
 // CHECK-NEW: [[META5]] = !{!"Simple C++ TBAA"}

>From 33d413d85808eaf1cb0373c887dad5023b6f4c95 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Matt=C3=A9o=20Rizza=20Murgier?=
 <matteo.rizza-murgier at sipearl.com>
Date: Thu, 22 Oct 2026 13:25:45 +0200
Subject: [PATCH 2/3] Add missing check lines

---
 clang/test/CodeGen/tbaa-struct.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/clang/test/CodeGen/tbaa-struct.cpp b/clang/test/CodeGen/tbaa-struct.cpp
index 102650c54215ef..00c09ebc359bbc 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -212,7 +212,7 @@ void copy14(MayAliasComplexMember *a1, MayAliasComplexMember *a2) {
 // CHECK-LABEL: _Z6copy14P21MayAliasComplexMemberS0_
 // CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(24) %a1, ptr noundef nonnull align 8 dereferenceable(24) %a2, i64 24, i1 false),
 // CHECK-OLD-SAME: !tbaa.struct [[TBAA_STRUCT24:![0-9]+]]
-// CHECK-NEW-SAME: !tbaa [[TBAA49:![0-9]+]], !tbaa.struct
+// CHECK-NEW-SAME: !tbaa [[TBAA50:![0-9]+]], !tbaa.struct
   *a1 = *a2;
 }
 
@@ -270,4 +270,8 @@ void copy14(MayAliasComplexMember *a1, MayAliasComplexMember *a2) {
 // CHECK-NEW: [[META38]] = !{[[META4]], i64 16, !"_ZTS12UnionMember1", [[META4]], i64 0, i64 8, [[META3]], i64 8, i64 4}
 // CHECK-NEW: [[TBAA41]] = !{[[META42:![0-9]+]], [[META42]], i64 0, i64 16}
 // CHECK-NEW: [[META42]] = !{[[META4]], i64 16, !"_ZTS12UnionMember2", [[META3]], i64 0, i64 4, [[META4]], i64 8, i64 8}
+// CHECK-NEW: [[TBAA45]] = !{[[META46:![0-9]+]], [[META46]], i64 0, i64 32}
+// CHECK-NEW: [[META46]] = !{[[META4]], i64 32, !"_ZTS14ComplexMembers", [[META3]], i64 0, i64 4, [[META4]], i64 4, i64 8, [[META4]], i64 16, i64 16}
+// CHECK-NEW: [[TBAA50]] = !{[[META51:![0-9]+]], [[META51]], i64 0, i64 24}
+// CHECK-NEW: [[META51]] = !{[[META4]], i64 24, !"_ZTS21MayAliasComplexMember", [[META3]], i64 0, i64 4, [[META4]], i64 8, i64 16}
 //.

>From 199a749b06df7926867f873d2a70e83f91d15741 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Matt=C3=A9o=20Rizza=20Murgier?=
 <matteo.rizza-murgier at sipearl.com>
Date: Wed, 4 Nov 2026 10:50:29 +0100
Subject: [PATCH 3/3] Add __real__/__imag__ store test

---
 clang/test/CodeGen/tbaa-struct.cpp | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/clang/test/CodeGen/tbaa-struct.cpp b/clang/test/CodeGen/tbaa-struct.cpp
index 00c09ebc359bbc..a8f53ef9b806e4 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -196,7 +196,7 @@ void copy13(ComplexMembers *a1, ComplexMembers *a2) {
 // CHECK-LABEL: _Z6copy13P14ComplexMembersS0_
 // CHECK:   tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %a1, ptr noundef nonnull align 8 dereferenceable(32) %a2, i64 32, i1 false),
 // CHECK-OLD-SAME: !tbaa.struct [[TBAA_STRUCT21:![0-9]+]]
-// CHECK-NEW-SAME: !tbaa [[TBAA45:![0-9]+]], !tbaa.struct
+// CHECK-NEW-SAME: !tbaa [[TBAA45:![0-9]+]], !tbaa.struct [[TBAA_STRUCT47:![0-9]+]]
   *a1 = *a2;
 }
 
@@ -216,6 +216,19 @@ void copy14(MayAliasComplexMember *a1, MayAliasComplexMember *a2) {
   *a1 = *a2;
 }
 
+// Make sure that __real__ and __imag__ accesses are tagged correctly.
+void store_parts(ComplexMembers *a, float re, float im) {
+// CHECK-LABEL: _Z11store_partsP14ComplexMembersff
+// CHECK:   store float %re, ptr %{{.*}}, align 4,
+// CHECK-OLD-SAME: !tbaa [[TBAA23:![0-9]+]]
+// CHECK-NEW-SAME: !tbaa [[TBAA48:![0-9]+]]
+// CHECK:   store float %im, ptr %{{.*}}, align 4,
+// CHECK-OLD-SAME: !tbaa [[TBAA23]]
+// CHECK-NEW-SAME: !tbaa [[TBAA48]]
+  __real__ a->f = re;
+  __imag__ a->f = im;
+}
+
 // (offset, size) = (0,1) char; (4,2) short; (8,4) int; (12,1) char; (16,4) int; (20,4) int
 // (offset, size) = (0,8) char; (0,2) char; (4,8) char
 // CHECK-OLD  [[DOUBLE]] = !{!"double", [[CHAR]], i64 0}
@@ -240,8 +253,8 @@ void copy14(MayAliasComplexMember *a1, MayAliasComplexMember *a2) {
 // CHECK-OLD: [[TBAA_STRUCT18]] = !{i64 0, i64 4, [[META9]], i64 8, i64 8, [[META15]]}
 // CHECK-OLD: [[TBAA_STRUCT19]] = !{i64 0, i64 8, [[META9]], i64 8, i64 4, [[META2]]}
 // CHECK-OLD: [[TBAA_STRUCT20]] = !{i64 0, i64 4, [[META2]], i64 8, i64 8, [[META9]]}
-// CHECK-OLD: [[TBAA_STRUCT21]] = !{i64 0, i64 4, [[META2]], i64 4, i64 4, [[META22:![0-9]+]], i64 8, i64 4, [[META22]], i64 16, i64 8, [[META15]], i64 24, i64 8, [[META15]]}
-// CHECK-OLD: [[META22]] = !{[[META23:![0-9]+]], [[META23]], i64 0}
+// CHECK-OLD: [[TBAA_STRUCT21]] = !{i64 0, i64 4, [[META2]], i64 4, i64 4, [[TBAA23]], i64 8, i64 4, [[TBAA23]], i64 16, i64 8, [[META15]], i64 24, i64 8, [[META15]]}
+// CHECK-OLD: [[TBAA23]] = !{[[META23:![0-9]+]], [[META23]], i64 0}
 // CHECK-OLD: [[META23]] = !{!"float", [[META4]], i64 0}
 // CHECK-OLD: [[TBAA_STRUCT24]] = !{i64 0, i64 4, [[META2]], i64 8, i64 8, [[META9]], i64 16, i64 8, [[META9]]}
 //.
@@ -272,6 +285,9 @@ void copy14(MayAliasComplexMember *a1, MayAliasComplexMember *a2) {
 // CHECK-NEW: [[META42]] = !{[[META4]], i64 16, !"_ZTS12UnionMember2", [[META3]], i64 0, i64 4, [[META4]], i64 8, i64 8}
 // CHECK-NEW: [[TBAA45]] = !{[[META46:![0-9]+]], [[META46]], i64 0, i64 32}
 // CHECK-NEW: [[META46]] = !{[[META4]], i64 32, !"_ZTS14ComplexMembers", [[META3]], i64 0, i64 4, [[META4]], i64 4, i64 8, [[META4]], i64 16, i64 16}
+// CHECK-NEW: [[TBAA_STRUCT47]] = !{i64 0, i64 4, [[META11:![0-9]+]], i64 4, i64 4, [[TBAA48]], i64 8, i64 4, [[TBAA48]], i64 16, i64 8, [[META30:![0-9]+]], i64 24, i64 8, [[META30]]}
+// CHECK-NEW: [[TBAA48]] = !{[[META49:![0-9]+]], [[META49]], i64 0, i64 4}
+// CHECK-NEW: [[META49]] = !{[[META4]], i64 4, !"float"}
 // CHECK-NEW: [[TBAA50]] = !{[[META51:![0-9]+]], [[META51]], i64 0, i64 24}
 // CHECK-NEW: [[META51]] = !{[[META4]], i64 24, !"_ZTS21MayAliasComplexMember", [[META3]], i64 0, i64 4, [[META4]], i64 8, i64 16}
 //.



More information about the cfe-commits mailing list