Skip to content

Commit 66bb772

Browse files
committed
Identify matrices and vectors by attributes
I guess it was about time. Should simplify some things later as well as at present and it was too easy to not do. Specifically, I was going to need to add another string check to the template instantiation code to identify longvectors. This is cleaner. Incidentally convert another feedback texture string check to use attribs. Incidentally resort the recently-added attribs to not break up the node shader attribs.
1 parent 466bb14 commit 66bb772

File tree

5 files changed

+74
-65
lines changed

5 files changed

+74
-65
lines changed

tools/clang/include/clang/Basic/Attr.td

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,39 @@ def HLSLCXXOverload : InheritableAttr {
939939
let Documentation = [Undocumented];
940940
}
941941

942+
def HLSLVector : InheritableAttr {
943+
let Spellings = []; // No spellings!
944+
let Subjects = SubjectList<[CXXRecord]>;
945+
let Documentation = [Undocumented];
946+
}
947+
948+
def HLSLMatrix : InheritableAttr {
949+
let Spellings = []; // No spellings!
950+
let Subjects = SubjectList<[CXXRecord]>;
951+
let Documentation = [Undocumented];
952+
}
953+
954+
def HLSLTessPatch : InheritableAttr {
955+
let Spellings = []; // No spellings!
956+
let Args = [BoolArgument<"IsInput">];
957+
let Subjects = SubjectList<[CXXRecord]>;
958+
let Documentation = [Undocumented];
959+
}
960+
961+
def HLSLStreamOutput : InheritableAttr {
962+
let Spellings = []; // No spellings!
963+
let Args = [UnsignedArgument<"Vertices">];
964+
let Subjects = SubjectList<[CXXRecord]>;
965+
let Documentation = [Undocumented];
966+
}
967+
968+
def HLSLResource : InheritableAttr {
969+
let Spellings = []; // No spellings!
970+
let Args = [UnsignedArgument<"ResKind">, UnsignedArgument<"ResClass">];
971+
let Subjects = SubjectList<[CXXRecord]>;
972+
let Documentation = [Undocumented];
973+
}
974+
942975
def HLSLNodeLaunch : InheritableAttr {
943976
let Spellings = [CXX11<"", "nodelaunch", 2017>];
944977
let Args = [StringArgument<"LaunchType">]; // one of broadcasting, coalescing, thread
@@ -992,28 +1025,6 @@ def HLSLNodeTrackRWInputSharing : InheritableAttr {
9921025
let Documentation = [Undocumented];
9931026
}
9941027

995-
996-
def HLSLTessPatch : InheritableAttr {
997-
let Spellings = []; // No spellings!
998-
let Args = [BoolArgument<"IsInput">];
999-
let Subjects = SubjectList<[CXXRecord]>;
1000-
let Documentation = [Undocumented];
1001-
}
1002-
1003-
def HLSLStreamOutput : InheritableAttr {
1004-
let Spellings = []; // No spellings!
1005-
let Args = [UnsignedArgument<"Vertices">];
1006-
let Subjects = SubjectList<[CXXRecord]>;
1007-
let Documentation = [Undocumented];
1008-
}
1009-
1010-
def HLSLResource : InheritableAttr {
1011-
let Spellings = []; // No spellings!
1012-
let Args = [UnsignedArgument<"ResKind">, UnsignedArgument<"ResClass">];
1013-
let Subjects = SubjectList<[CXXRecord]>;
1014-
let Documentation = [Undocumented];
1015-
}
1016-
10171028
def HLSLNodeObject : InheritableAttr {
10181029
let Spellings = []; // No spellings!
10191030
let Subjects = SubjectList<[CXXRecord]>;

tools/clang/lib/AST/ASTContextHLSL.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ void hlsl::AddHLSLMatrixTemplate(ASTContext &context,
329329

330330
typeDeclBuilder.addField("h", vectorArrayType);
331331

332+
typeDeclBuilder.getRecordDecl()->addAttr(
333+
HLSLMatrixAttr::CreateImplicit(context));
334+
332335
// Add an operator[]. The operator ranges from zero to rowcount-1, and returns
333336
// a vector of colcount elements.
334337
const unsigned int templateDepth = 0;
@@ -385,6 +388,9 @@ void hlsl::AddHLSLVectorTemplate(ASTContext &context,
385388
// Add an 'h' field to hold the handle.
386389
typeDeclBuilder.addField("h", vectorType);
387390

391+
typeDeclBuilder.getRecordDecl()->addAttr(
392+
HLSLVectorAttr::CreateImplicit(context));
393+
388394
// Add an operator[]. The operator ranges from zero to colcount-1, and returns
389395
// a scalar.
390396

tools/clang/lib/AST/HlslTypes.cpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -53,44 +53,44 @@ ConvertHLSLVecMatTypeToExtVectorType(const clang::ASTContext &context,
5353
return nullptr;
5454
}
5555

56+
template <typename AttrType> static AttrType *getAttr(clang::QualType type) {
57+
type = type.getCanonicalType();
58+
if (const RecordType *RT = type->getAs<RecordType>()) {
59+
if (const auto *Spec =
60+
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
61+
if (const auto *Template =
62+
dyn_cast<ClassTemplateDecl>(Spec->getSpecializedTemplate()))
63+
return Template->getTemplatedDecl()->getAttr<AttrType>();
64+
if (const auto *Decl = dyn_cast<CXXRecordDecl>(RT->getDecl()))
65+
return Decl->getAttr<AttrType>();
66+
}
67+
return nullptr;
68+
}
69+
5670
bool IsHLSLVecMatType(clang::QualType type) {
57-
const Type *Ty = type.getCanonicalType().getTypePtr();
58-
if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
59-
if (const ClassTemplateSpecializationDecl *templateDecl =
60-
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl())) {
61-
if (templateDecl->getName() == "vector") {
62-
return true;
63-
} else if (templateDecl->getName() == "matrix") {
64-
return true;
65-
}
66-
}
71+
type = type.getCanonicalType();
72+
if (const RecordType *RT = type->getAs<RecordType>()) {
73+
if (const auto *Spec =
74+
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
75+
if (const auto *Template =
76+
dyn_cast<ClassTemplateDecl>(Spec->getSpecializedTemplate()))
77+
return Template->getTemplatedDecl()->getAttr<HLSLMatrixAttr>() ||
78+
Template->getTemplatedDecl()->getAttr<HLSLVectorAttr>();
79+
if (const auto *Decl = dyn_cast<CXXRecordDecl>(RT->getDecl()))
80+
return Decl->getAttr<HLSLMatrixAttr>() || Decl->getAttr<HLSLVectorAttr>();
6781
}
6882
return false;
6983
}
7084

7185
bool IsHLSLMatType(clang::QualType type) {
72-
const clang::Type *Ty = type.getCanonicalType().getTypePtr();
73-
if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
74-
if (const ClassTemplateSpecializationDecl *templateDecl =
75-
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl())) {
76-
if (templateDecl->getName() == "matrix") {
77-
return true;
78-
}
79-
}
80-
}
86+
if (getAttr<HLSLMatrixAttr>(type))
87+
return true;
8188
return false;
8289
}
8390

8491
bool IsHLSLVecType(clang::QualType type) {
85-
const clang::Type *Ty = type.getCanonicalType().getTypePtr();
86-
if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
87-
if (const ClassTemplateSpecializationDecl *templateDecl =
88-
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl())) {
89-
if (templateDecl->getName() == "vector") {
90-
return true;
91-
}
92-
}
93-
}
92+
if (getAttr<HLSLVectorAttr>(type))
93+
return true;
9494
return false;
9595
}
9696

@@ -475,20 +475,6 @@ clang::QualType GetHLSLMatElementType(clang::QualType type) {
475475
return elemTy;
476476
}
477477

478-
template <typename AttrType> static AttrType *getAttr(clang::QualType type) {
479-
type = type.getCanonicalType();
480-
if (const RecordType *RT = type->getAs<RecordType>()) {
481-
if (const auto *Spec =
482-
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
483-
if (const auto *Template =
484-
dyn_cast<ClassTemplateDecl>(Spec->getSpecializedTemplate()))
485-
return Template->getTemplatedDecl()->getAttr<AttrType>();
486-
if (const auto *Decl = dyn_cast<CXXRecordDecl>(RT->getDecl()))
487-
return Decl->getAttr<AttrType>();
488-
}
489-
return nullptr;
490-
}
491-
492478
// TODO: Add type cache to ASTContext.
493479
bool IsHLSLInputPatchType(QualType type) {
494480
type = type.getCanonicalType();
@@ -812,7 +798,11 @@ QualType GetHLSLResourceResultType(QualType type) {
812798
if (const ClassTemplateSpecializationDecl *templateDecl =
813799
dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
814800

815-
if (RD->getName().startswith("FeedbackTexture")) {
801+
const HLSLResourceAttr *Attr = getAttr<HLSLResourceAttr>(type);
802+
if (Attr && (Attr->getResKind() ==
803+
(unsigned)DXIL::ResourceKind::FeedbackTexture2D ||
804+
Attr->getResKind() ==
805+
(unsigned)DXIL::ResourceKind::FeedbackTexture2DArray)) {
816806
// Feedback textures are write-only and the data is opaque,
817807
// so there is no result type per se.
818808
return {};

tools/clang/test/HLSLFileCheck/hlsl/types/matrix/matrix-ast.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// ext_vector array.
1616
// CHECK-NEXT: CXXRecordDecl {{0x[0-9a-fA-F]+}} <<invalid sloc>> <invalid sloc> implicit class matrix definition
1717
// CHECK-NEXT: FinalAttr {{0x[0-9a-fA-F]+}} <<invalid sloc>> Implicit final
18+
// CHECK-NEXT: HLSLMatrixAttr {{0x[0-9a-fA-F]+}} <<invalid sloc>> Implicit
1819
// CHECK-NEXT: FieldDecl {{0x[0-9a-fA-F]+}} <<invalid sloc>> <invalid sloc> implicit h 'element [row_count] __attribute__((ext_vector_type(col_count)))'
1920

2021

tools/clang/test/HLSLFileCheck/hlsl/types/vector/vector-ast.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// Verify the class, final attribute and ext_vector field decl.
1313
// CHECK-NEXT: CXXRecordDecl {{0x[0-9a-fA-F]+}} <<invalid sloc>> <invalid sloc> implicit class vector definition
1414
// CHECK-NEXT: FinalAttr {{0x[0-9a-fA-F]+}} <<invalid sloc>> Implicit final
15+
// CHECK-NEXT: HLSLVectorAttr {{0x[0-9a-fA-F]+}} <<invalid sloc>> Implicit
1516
// CHECK-NEXT: FieldDecl {{0x[0-9a-fA-F]+}} <<invalid sloc>> <invalid sloc> implicit h 'element __attribute__((ext_vector_type(element_count)))'
1617

1718
// Verify operator overloads for const vector subscript operators.

0 commit comments

Comments
 (0)