Author: Florian Hahn
Date: 2026-04-03T20:11:04Z
New Revision: 6476619f301a563f73f1d0ce552b8fb3e054ee2d

URL: 
https://github.com/llvm/llvm-project/commit/6476619f301a563f73f1d0ce552b8fb3e054ee2d
DIFF: 
https://github.com/llvm/llvm-project/commit/6476619f301a563f73f1d0ce552b8fb3e054ee2d.diff

LOG: [Matrix] Use matrix element type for TBAA nodes. (#190029)

Matrix loads and stores are accesses of their element types. Emit TBAA
nodes using their element type to allow more precise TBAA alias
analysis.

PR: https://github.com/llvm/llvm-project/pull/190029

Added: 
    

Modified: 
    clang/lib/CodeGen/CodeGenTBAA.cpp
    clang/test/CodeGen/tbaa-matrix.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CodeGenTBAA.cpp 
b/clang/lib/CodeGen/CodeGenTBAA.cpp
index cd08f3ec397a0..1854df7c7c0f1 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -329,6 +329,13 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type 
*Ty) {
   if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
     return getTypeInfo(cast<ArrayType>(Ty)->getElementType());
 
+  // Accesses to matrix types are accesses to objects of their element types.
+  if (const auto *MTy = dyn_cast<MatrixType>(Ty)) {
+    assert(isa<ConstantMatrixType>(Ty) &&
+           "only ConstantMatrixType should reach CodeGen");
+    return getTypeInfo(MTy->getElementType());
+  }
+
   // Enum types are distinct types. In C++ they have "underlying types",
   // however they aren't related for TBAA.
   if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {

diff  --git a/clang/test/CodeGen/tbaa-matrix.c 
b/clang/test/CodeGen/tbaa-matrix.c
index aacf19ceef327..739dec2950ecf 100644
--- a/clang/test/CodeGen/tbaa-matrix.c
+++ b/clang/test/CodeGen/tbaa-matrix.c
@@ -16,9 +16,9 @@ typedef int ix3x3_t __attribute__((matrix_type(3, 3)));
 // CHECK-NEXT:    store ptr [[A]], ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5:![0-9]+]]
 // CHECK-NEXT:    store ptr [[B]], ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    [[TMP1:%.*]] = load <25 x double>, ptr [[TMP0]], align 8, 
!tbaa [[CHAR_TBAA7:![0-9]+]]
+// CHECK-NEXT:    [[TMP1:%.*]] = load <25 x double>, ptr [[TMP0]], align 8, 
!tbaa [[DOUBLE_TBAA7:![0-9]+]]
 // CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    store <25 x double> [[TMP1]], ptr [[TMP2]], align 8, !tbaa 
[[CHAR_TBAA7]]
+// CHECK-NEXT:    store <25 x double> [[TMP1]], ptr [[TMP2]], align 8, !tbaa 
[[DOUBLE_TBAA7]]
 // CHECK-NEXT:    ret void
 //
 void load_double_matrix(dx5x5_t *a, dx5x5_t *b) {
@@ -33,9 +33,9 @@ void load_double_matrix(dx5x5_t *a, dx5x5_t *b) {
 // CHECK-NEXT:    store ptr [[A]], ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
 // CHECK-NEXT:    store ptr [[B]], ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    [[TMP1:%.*]] = load <6 x float>, ptr [[TMP0]], align 4, 
!tbaa [[CHAR_TBAA7]]
+// CHECK-NEXT:    [[TMP1:%.*]] = load <6 x float>, ptr [[TMP0]], align 4, 
!tbaa [[FLOAT_TBAA9:![0-9]+]]
 // CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    store <6 x float> [[TMP1]], ptr [[TMP2]], align 4, !tbaa 
[[CHAR_TBAA7]]
+// CHECK-NEXT:    store <6 x float> [[TMP1]], ptr [[TMP2]], align 4, !tbaa 
[[FLOAT_TBAA9]]
 // CHECK-NEXT:    ret void
 //
 void load_float_matrix(fx2x3_t *a, fx2x3_t *b) {
@@ -50,9 +50,9 @@ void load_float_matrix(fx2x3_t *a, fx2x3_t *b) {
 // CHECK-NEXT:    store ptr [[A]], ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
 // CHECK-NEXT:    store ptr [[B]], ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    [[TMP1:%.*]] = load <9 x i32>, ptr [[TMP0]], align 4, !tbaa 
[[CHAR_TBAA7]]
+// CHECK-NEXT:    [[TMP1:%.*]] = load <9 x i32>, ptr [[TMP0]], align 4, !tbaa 
[[INT_TBAA1:![0-9]+]]
 // CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    store <9 x i32> [[TMP1]], ptr [[TMP2]], align 4, !tbaa 
[[CHAR_TBAA7]]
+// CHECK-NEXT:    store <9 x i32> [[TMP1]], ptr [[TMP2]], align 4, !tbaa 
[[INT_TBAA1]]
 // CHECK-NEXT:    ret void
 //
 void load_int_matrix(ix3x3_t *a, ix3x3_t *b) {
@@ -69,15 +69,15 @@ void load_int_matrix(ix3x3_t *a, ix3x3_t *b) {
 // CHECK-NEXT:    [[LOCAL:%.*]] = alloca [25 x double], align 8
 // CHECK-NEXT:    [[D:%.*]] = alloca double, align 8
 // CHECK-NEXT:    store ptr [[M]], ptr [[M_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    store ptr [[S]], ptr [[S_ADDR]], align 8, !tbaa 
[[DOUBLEPTR_TBAA8:![0-9]+]]
+// CHECK-NEXT:    store ptr [[S]], ptr [[S_ADDR]], align 8, !tbaa 
[[DOUBLEPTR_TBAA11:![0-9]+]]
 // CHECK-NEXT:    call void @llvm.lifetime.start.p0(ptr [[LOCAL]]) 
#[[ATTR2:[0-9]+]]
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[M_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    [[TMP1:%.*]] = load <25 x double>, ptr [[TMP0]], align 8, 
!tbaa [[CHAR_TBAA7]]
-// CHECK-NEXT:    store <25 x double> [[TMP1]], ptr [[LOCAL]], align 8, !tbaa 
[[CHAR_TBAA7]]
+// CHECK-NEXT:    [[TMP1:%.*]] = load <25 x double>, ptr [[TMP0]], align 8, 
!tbaa [[DOUBLE_TBAA7]]
+// CHECK-NEXT:    store <25 x double> [[TMP1]], ptr [[LOCAL]], align 8, !tbaa 
[[DOUBLE_TBAA7]]
 // CHECK-NEXT:    call void @llvm.lifetime.start.p0(ptr [[D]]) #[[ATTR2]]
-// CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[S_ADDR]], align 8, !tbaa 
[[DOUBLEPTR_TBAA8]]
-// CHECK-NEXT:    [[TMP3:%.*]] = load double, ptr [[TMP2]], align 8, !tbaa 
[[DOUBLE_TBAA10:![0-9]+]]
-// CHECK-NEXT:    store double [[TMP3]], ptr [[D]], align 8, !tbaa 
[[DOUBLE_TBAA10]]
+// CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[S_ADDR]], align 8, !tbaa 
[[DOUBLEPTR_TBAA11]]
+// CHECK-NEXT:    [[TMP3:%.*]] = load double, ptr [[TMP2]], align 8, !tbaa 
[[DOUBLE_TBAA7]]
+// CHECK-NEXT:    store double [[TMP3]], ptr [[D]], align 8, !tbaa 
[[DOUBLE_TBAA7]]
 // CHECK-NEXT:    call void @llvm.lifetime.end.p0(ptr [[D]]) #[[ATTR2]]
 // CHECK-NEXT:    call void @llvm.lifetime.end.p0(ptr [[LOCAL]]) #[[ATTR2]]
 // CHECK-NEXT:    ret void
@@ -99,9 +99,9 @@ typedef aliasing_int __attribute__((matrix_type(2, 2))) aim_t;
 // CHECK-NEXT:    store ptr [[A]], ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
 // CHECK-NEXT:    store ptr [[B]], ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
 // CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    [[TMP1:%.*]] = load <4 x i32>, ptr [[TMP0]], align 4, !tbaa 
[[CHAR_TBAA7]]
+// CHECK-NEXT:    [[TMP1:%.*]] = load <4 x i32>, ptr [[TMP0]], align 4, !tbaa 
[[INT_TBAA1]]
 // CHECK-NEXT:    [[TMP2:%.*]] = load ptr, ptr [[B_ADDR]], align 8, !tbaa 
[[ANYPTR_TBAA5]]
-// CHECK-NEXT:    store <4 x i32> [[TMP1]], ptr [[TMP2]], align 4, !tbaa 
[[CHAR_TBAA7]]
+// CHECK-NEXT:    store <4 x i32> [[TMP1]], ptr [[TMP2]], align 4, !tbaa 
[[INT_TBAA1]]
 // CHECK-NEXT:    ret void
 //
 void load_may_alias_matrix(aim_t *a, aim_t *b) {
@@ -109,13 +109,16 @@ void load_may_alias_matrix(aim_t *a, aim_t *b) {
 }
 
 //.
-// CHECK: [[META3:![0-9]+]] = !{!"omnipotent char", [[META4:![0-9]+]], i64 0}
+// CHECK: [[INT_TBAA1]] = !{[[META2:![0-9]+]], [[META2]], i64 0}
+// CHECK: [[META2]] = !{!"int", [[META3:![0-9]+]], i64 0}
+// CHECK: [[META3]] = !{!"omnipotent char", [[META4:![0-9]+]], i64 0}
 // CHECK: [[META4]] = !{!"Simple C/C++ TBAA"}
 // CHECK: [[ANYPTR_TBAA5]] = !{[[META6:![0-9]+]], [[META6]], i64 0}
 // CHECK: [[META6]] = !{!"any pointer", [[META3]], i64 0}
-// CHECK: [[CHAR_TBAA7]] = !{[[META3]], [[META3]], i64 0}
-// CHECK: [[DOUBLEPTR_TBAA8]] = !{[[META9:![0-9]+]], [[META9]], i64 0}
-// CHECK: [[META9]] = !{!"p1 double", [[META6]], i64 0}
-// CHECK: [[DOUBLE_TBAA10]] = !{[[META11:![0-9]+]], [[META11]], i64 0}
-// CHECK: [[META11]] = !{!"double", [[META3]], i64 0}
+// CHECK: [[DOUBLE_TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
+// CHECK: [[META8]] = !{!"double", [[META3]], i64 0}
+// CHECK: [[FLOAT_TBAA9]] = !{[[META10:![0-9]+]], [[META10]], i64 0}
+// CHECK: [[META10]] = !{!"float", [[META3]], i64 0}
+// CHECK: [[DOUBLEPTR_TBAA11]] = !{[[META12:![0-9]+]], [[META12]], i64 0}
+// CHECK: [[META12]] = !{!"p1 double", [[META6]], i64 0}
 //.


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to