[clang] 4daebae - [Clang][HLSL] Reject matrix layout modifiers on array typedefs (#227364)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 29 18:19:17 PDT 2026


Author: Farzon Lotfi
Date: 2026-09-29T21:19:10-04:00
New Revision: 4daebaeb9b4e36a23cef9f222fcc7c4faa27b64c

URL: https://github.com/llvm/llvm-project/commit/4daebaeb9b4e36a23cef9f222fcc7c4faa27b64c
DIFF: https://github.com/llvm/llvm-project/commit/4daebaeb9b4e36a23cef9f222fcc7c4faa27b64c.diff

LOG: [Clang][HLSL] Reject matrix layout modifiers on array typedefs (#227364)

Fixes #226251

Require row_major and column_major modifiers to apply directly to a
matrix type rather than accepting an array typedef whose element type is
a matrix.

This preserves valid declarations of arrays of annotated matrix elements
while rejecting attempts to apply a layout modifier to the array type
itself.

Added: 
    

Modified: 
    clang/lib/Sema/SemaHLSL.cpp
    clang/test/SemaHLSL/matrix_layout_attr.hlsl

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 794371ce9a008..05e8293ffbd02 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -2678,10 +2678,8 @@ void SemaHLSL::handleParamModifierAttr(Decl *D, const ParsedAttr &AL) {
     D->addAttr(NewAttr);
 }
 
-static bool isMatrixOrArrayOfMatrix(const ASTContext &Ctx, QualType QT) {
+static bool isMatrixType(QualType QT) {
   const Type *Ty = QT->getUnqualifiedDesugaredType();
-  while (isa<ArrayType>(Ty))
-    Ty = Ty->getArrayElementTypeNoTypeQual();
   return Ty->isDependentType() || Ty->isConstantMatrixType();
 }
 
@@ -2711,9 +2709,8 @@ Attr *SemaHLSL::buildMatrixLayoutTypeAttr(QualType T, const ParsedAttr &AL) {
                          ? attr::HLSLRowMajor
                          : attr::HLSLColumnMajor;
 
-  // For non-dependent types, the operand must be a matrix (or array of
-  // matrices).
-  if (!T->isDependentType() && !isMatrixOrArrayOfMatrix(Ctx, T)) {
+  // For non-dependent types, the operand must be a matrix.
+  if (!T->isDependentType() && !isMatrixType(T)) {
     Diag(AL.getLoc(), diag::err_hlsl_matrix_layout_non_matrix)
         << AL.getAttrName();
     AL.setInvalid();
@@ -2755,7 +2752,7 @@ bool SemaHLSL::diagnoseMatrixLayoutInstantiation(attr::Kind K, QualType T,
     return false;
   if (T.isNull() || T->isDependentType())
     return false;
-  if (isMatrixOrArrayOfMatrix(getASTContext(), T))
+  if (isMatrixType(T))
     return false;
   IdentifierInfo *II = &getASTContext().Idents.get(
       K == attr::HLSLRowMajor ? "row_major" : "column_major");

diff  --git a/clang/test/SemaHLSL/matrix_layout_attr.hlsl b/clang/test/SemaHLSL/matrix_layout_attr.hlsl
index 16ef8fb2e63a7..c17c4364ec512 100644
--- a/clang/test/SemaHLSL/matrix_layout_attr.hlsl
+++ b/clang/test/SemaHLSL/matrix_layout_attr.hlsl
@@ -15,6 +15,14 @@ column_major float4x4 cm_mat_arr[3];
 row_major float3x3 rm_mat_arr_2d[2][3];
 column_major float4x4 cm_mat_arr_2d[3][2];
 
+typedef float2x3 MatrixArray[2];
+
+// Invalid: the modifier applies to the array typedef, not its matrix elements.
+// expected-error at +1 {{'row_major' attribute can only be applied to a matrix type}}
+row_major MatrixArray invalid_row_array;
+// expected-error at +1 {{'column_major' attribute can only be applied to a matrix type}}
+column_major MatrixArray invalid_column_array;
+
 // Valid: on struct fields with matrix type.
 struct S {
   row_major float2x2 mat1;


        


More information about the cfe-commits mailing list