Quellcode durchsuchen

Switch HLSL matrix major attribute to TypeAttr (#5087)

This change removes redundant information saved in AST.
Now major information only saved in AttributedType.
Xiang Li vor 2 Jahren
Ursprung
Commit
ca6d80f511

+ 2 - 2
tools/clang/include/clang/Basic/Attr.td

@@ -791,13 +791,13 @@ def HLSLSemantic : InheritableAttr {
   let Documentation = [Undocumented];
 }
 
-def HLSLColumnMajor : InheritableAttr {
+def HLSLColumnMajor : TypeAttr {
   let Spellings = [CXX11<"", "column_major", 2015>];
   let Subjects = SubjectList<[Function, Var, ParmVar, Field, TypedefName]>;
   let Documentation = [Undocumented];
 }
 
-def HLSLRowMajor : InheritableAttr {
+def HLSLRowMajor : TypeAttr {
   let Spellings = [CXX11<"", "row_major", 2015>];
   let Subjects = SubjectList<[Function, Var, ParmVar, Field, TypedefName]>;
   let Documentation = [Undocumented];

+ 4 - 6
tools/clang/lib/SPIRV/AstTypeProbe.cpp

@@ -1232,21 +1232,19 @@ bool isFloatOrVecMatOfFloatType(QualType type) {
 bool isOrContainsNonFpColMajorMatrix(const ASTContext &astContext,
                                      const SpirvCodeGenOptions &spirvOptions,
                                      QualType type, const Decl *decl) {
-  const auto isColMajorDecl = [&spirvOptions](const Decl *decl) {
-    return decl->hasAttr<clang::HLSLColumnMajorAttr>() ||
-           (!decl->hasAttr<clang::HLSLRowMajorAttr>() &&
-            !spirvOptions.defaultRowMajor);
+  const auto isColMajorDecl = [&spirvOptions](QualType matTy) {
+    return !hlsl::IsHLSLMatRowMajor(matTy, spirvOptions.defaultRowMajor);
   };
 
   QualType elemType = {};
   if (isMxNMatrix(type, &elemType) && !elemType->isFloatingType()) {
-    return isColMajorDecl(decl);
+    return isColMajorDecl(type);
   }
 
   if (const auto *arrayType = astContext.getAsConstantArrayType(type)) {
     if (isMxNMatrix(arrayType->getElementType(), &elemType) &&
         !elemType->isFloatingType())
-      return isColMajorDecl(decl);
+      return isColMajorDecl(arrayType->getElementType());
     if (const auto *structType =
             arrayType->getElementType()->getAs<RecordType>()) {
       return isOrContainsNonFpColMajorMatrix(astContext, spirvOptions,

+ 0 - 13
tools/clang/lib/Sema/SemaHLSL.cpp

@@ -12230,15 +12230,6 @@ void hlsl::HandleDeclAttributeForHLSL(Sema &S, Decl *D, const AttributeList &A,
       A.getAttributeSpellingListIndex());
     break;
 
-  case AttributeList::AT_HLSLColumnMajor:
-    declAttr = ::new (S.Context) HLSLColumnMajorAttr(A.getRange(), S.Context,
-      A.getAttributeSpellingListIndex());
-    break;
-  case AttributeList::AT_HLSLRowMajor:
-    declAttr = ::new (S.Context) HLSLRowMajorAttr(A.getRange(), S.Context,
-      A.getAttributeSpellingListIndex());
-    break;
-
   case AttributeList::AT_HLSLUnorm:
     declAttr = ::new (S.Context) HLSLUnormAttr(A.getRange(), S.Context,
       A.getAttributeSpellingListIndex());
@@ -13904,8 +13895,6 @@ void hlsl::CustomPrintHLSLAttr(const clang::Attr *A, llvm::raw_ostream &Out, con
     break;
   
   // These four cases are printed in TypePrinter::printAttributedBefore
-  case clang::attr::HLSLColumnMajor:  
-  case clang::attr::HLSLRowMajor:
   case clang::attr::HLSLSnorm:
   case clang::attr::HLSLUnorm:
     break;
@@ -13963,7 +13952,6 @@ bool hlsl::IsHLSLAttr(clang::attr::Kind AttrKind) {
   case clang::attr::HLSLCall:
   case clang::attr::HLSLCentroid:
   case clang::attr::HLSLClipPlanes:
-  case clang::attr::HLSLColumnMajor:
   case clang::attr::HLSLDomain:
   case clang::attr::HLSLEarlyDepthStencil:
   case clang::attr::HLSLFastOpt:
@@ -13988,7 +13976,6 @@ bool hlsl::IsHLSLAttr(clang::attr::Kind AttrKind) {
   case clang::attr::HLSLPatchConstantFunc:
   case clang::attr::HLSLMaxVertexCount:
   case clang::attr::HLSLPrecise:
-  case clang::attr::HLSLRowMajor:
   case clang::attr::HLSLSample:
   case clang::attr::HLSLSemantic:
   case clang::attr::HLSLShader:

+ 35 - 0
tools/clang/test/HLSLFileCheck/hlsl/types/matrix/matrix_attr.hlsl

@@ -0,0 +1,35 @@
+// RUN: %dxc -E main -T ps_6_0 %s  -ast-dump | FileCheck %s
+
+// Check matrix attribute.
+
+struct Mat
+{
+// CHECK:-FieldDecl {{.*}}, col:22> col:22 referenced m 'row_major float2x2':'matrix<float, 2, 2>'
+// CHECK-NOT: |-HLSLRowMajorAttr 0x{{.*}} <col:3>
+  row_major float2x2 m;
+};
+
+Mat m;
+// CHECK:|-VarDecl {{.*}}, col:10> col:10 used t 'const float3x2':'const matrix<float, 3, 2>'
+float3x2 t;
+
+// CHECK:|-VarDecl {{.*}}, col:10> col:10 used tt 'const float3x3':'const matrix<float, 3, 3>'
+float3x3 tt;
+
+// CHECK:|-FunctionDecl 0x{{.*}}:20 used foo 'row_major float3x3 ()'
+// CHECK-NEXT:CompoundStmt 0x{{.*}} <col:26,
+// CHECK-NEXT:| `-ReturnStmt 0x{{.*}}, col:10>
+// CHECK-NEXT:|   `-ImplicitCastExpr 0x{{.*}} <col:10> 'float3x3':'matrix<float, 3, 3>' <LValueToRValue>
+// CHECK-NEXT:|     `-DeclRefExpr 0x{{.*}} <col:10> 'const float3x3':'const matrix<float, 3, 3>' lvalue Var 0x{{.*}} 'tt' 'const float3x3':'const matrix<float, 3, 3>'
+// CHECK-NOT:|-HLSLRowMajorAttr
+row_major float3x3 foo() {
+  return tt;
+}
+
+// CHECK:-FunctionDecl 0x{{.*}} main 'float4 (column_major float4x4)'
+// CHECK-NEXT:|-ParmVarDecl 0x{{.*}} <col:13, col:35> col:35 used m2 'column_major float4x4':'matrix<float, 4, 4>'
+// CHECK-NOT:|-HLSLColumnMajorAttr
+float4 main(column_major float4x4 m2 : M) :SV_Target {
+    Mat lm = m;
+    return lm.m + t[0].xxxx + m2[1] + foo()[1].xxxx;
+}