[clang] [Clang][TBAA] Describe complex types as two fields in !tbaa.struct (PR #234539)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 22 01:54:56 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mattéo Rizza Murgier (matteo-rm)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/234539.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CodeGenTBAA.cpp (+12)
- (modified) clang/test/CodeGen/tbaa-struct.cpp (+36)
``````````diff
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..102650c54215e 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"}
``````````
</details>
https://github.com/llvm/llvm-project/pull/234539
More information about the cfe-commits
mailing list