[clang] 2c75934 - [Clang][TBAA] Describe complex types as two fields in !tbaa.struct (#234539)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 02:57:30 PST 2026
Author: Mattéo Rizza Murgier
Date: 2026-11-04T11:57:24+01:00
New Revision: 2c75934f822ccec80e8f9817acdc379b58857607
URL: https://github.com/llvm/llvm-project/commit/2c75934f822ccec80e8f9817acdc379b58857607
DIFF: https://github.com/llvm/llvm-project/commit/2c75934f822ccec80e8f9817acdc379b58857607.diff
LOG: [Clang][TBAA] Describe complex types as two fields in !tbaa.struct (#234539)
Add a special case for complex numbers in CollectFields so they're
represented as two fields of their real type instead of a single char
field. This follows the standard complex layout as per C99 6.2.5p13.
Enables vectorization of e.g. https://godbolt.org/z/3PevG697r
Added:
Modified:
clang/lib/CodeGen/CodeGenTBAA.cpp
clang/test/CodeGen/tbaa-struct.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index d33188db9da15..8573193534573 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 2776ea2e4e861..a8f53ef9b806e 100644
--- a/clang/test/CodeGen/tbaa-struct.cpp
+++ b/clang/test/CodeGen/tbaa-struct.cpp
@@ -184,6 +184,51 @@ 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 [[TBAA_STRUCT47:![0-9]+]]
+ *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 [[TBAA50:![0-9]+]], !tbaa.struct
+ *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}
@@ -208,6 +253,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, [[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]]}
//.
// CHECK-NEW: [[META4:![0-9]+]] = !{[[META5:![0-9]+]], i64 1, !"omnipotent char"}
// CHECK-NEW: [[META5]] = !{!"Simple C++ TBAA"}
@@ -234,4 +283,11 @@ void copy12(UnionMember2 *a1, UnionMember2 *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: [[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