Skip to content

Commit eae1d65

Browse files
authored
[HLSL] Split out the ROV attribute from the resource attribute, make it a new spellable attribute. (#102414)
Much like #98193, this PR takes some more data out of the resource attribute, specifically the ROV data. This PR introduces a new attribute called HLSLROVAttr, which contains data on whether or not the decl the attribute applies to is an ROV. Tests were added to ensure the attribute is found on the AST. This attribute may take any boolean condition as an argument. If the condition is true, then the object the attribute applies to "is" an ROV. Fixes ##102392
1 parent 4d08bb1 commit eae1d65

File tree

9 files changed

+57
-15
lines changed

9 files changed

+57
-15
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,12 +4599,18 @@ def HLSLResource : InheritableAttr {
45994599
"CBuffer", "Sampler", "TBuffer", "RTAccelerationStructure",
46004600
"FeedbackTexture2D", "FeedbackTexture2DArray"
46014601
],
4602-
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1, /*isCovered=*/0>,
4603-
DefaultBoolArgument<"isROV", /*default=*/0>
4602+
/*opt=*/0, /*fake=*/0, /*isExternalType=*/1, /*isCovered=*/0>
46044603
];
46054604
let Documentation = [InternalOnly];
46064605
}
46074606

4607+
def HLSLROV : InheritableAttr {
4608+
let Spellings = [CXX11<"hlsl", "is_rov">];
4609+
let Subjects = SubjectList<[Struct]>;
4610+
let LangOpts = [HLSL];
4611+
let Documentation = [InternalOnly];
4612+
}
4613+
46084614
def HLSLResourceClass : InheritableAttr {
46094615
let Spellings = [CXX11<"hlsl", "resource_class">];
46104616
let Subjects = SubjectList<[Struct]>;

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class SemaHLSL : public SemaBase {
5656
void handleSV_DispatchThreadIDAttr(Decl *D, const ParsedAttr &AL);
5757
void handlePackOffsetAttr(Decl *D, const ParsedAttr &AL);
5858
void handleShaderAttr(Decl *D, const ParsedAttr &AL);
59+
void handleROVAttr(Decl *D, const ParsedAttr &AL);
5960
void handleResourceClassAttr(Decl *D, const ParsedAttr &AL);
6061
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
6162
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
301301

302302
llvm::hlsl::ResourceClass RC = HLSLResClassAttr->getResourceClass();
303303
llvm::hlsl::ResourceKind RK = HLSLResAttr->getResourceKind();
304-
bool IsROV = HLSLResAttr->getIsROV();
304+
bool IsROV = FD->hasAttr<HLSLROVAttr>();
305305
llvm::hlsl::ElementType ET = calculateElementType(CGM.getContext(), Ty);
306306

307307
BufferResBinding Binding(D->getAttr<HLSLResourceBindingAttr>());

clang/lib/Sema/HLSLExternalSemaSource.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ struct BuiltinTypeDeclBuilder {
9696
nullptr, false, InClassInitStyle::ICIS_NoInit);
9797
Field->setAccess(Access);
9898
Field->setImplicit(true);
99-
for (Attr *A : Attrs)
100-
Field->addAttr(A);
99+
for (Attr *A : Attrs) {
100+
if (A)
101+
Field->addAttr(A);
102+
}
103+
101104
Record->addDecl(Field);
102105
Fields[Name] = Field;
103106
return *this;
@@ -116,12 +119,15 @@ struct BuiltinTypeDeclBuilder {
116119
QualType(TTD->getTypeForDecl(), 0));
117120
}
118121
// add handle member
119-
llvm::SmallVector<Attr *, 2> Attrs;
120122
Attr *ResourceClassAttr =
121123
HLSLResourceClassAttr::CreateImplicit(Record->getASTContext(), RC);
122124
Attr *ResourceAttr =
123-
HLSLResourceAttr::CreateImplicit(Record->getASTContext(), RK, IsROV);
124-
addMemberVariable("h", Ty, {ResourceClassAttr, ResourceAttr}, Access);
125+
HLSLResourceAttr::CreateImplicit(Record->getASTContext(), RK);
126+
Attr *ROVAttr =
127+
IsROV ? HLSLROVAttr::CreateImplicit(Record->getASTContext()) : nullptr;
128+
addMemberVariable("h", Ty, {ResourceClassAttr, ResourceAttr, ROVAttr},
129+
Access);
130+
125131
return *this;
126132
}
127133

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6901,6 +6901,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
69016901
case ParsedAttr::AT_HLSLResourceBinding:
69026902
S.HLSL().handleResourceBindingAttr(D, AL);
69036903
break;
6904+
case ParsedAttr::AT_HLSLROV:
6905+
handleSimpleAttribute<HLSLROVAttr>(S, D, AL);
6906+
break;
69046907
case ParsedAttr::AT_HLSLResourceClass:
69056908
S.HLSL().handleResourceClassAttr(D, AL);
69066909
break;

clang/test/Misc/pragma-attribute-supported-attributes-list.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
// CHECK-NEXT: FunctionReturnThunks (SubjectMatchRule_function)
8282
// CHECK-NEXT: GNUInline (SubjectMatchRule_function)
8383
// CHECK-NEXT: HIPManaged (SubjectMatchRule_variable)
84+
// CHECK-NEXT: HLSLROV (SubjectMatchRule_record_not_is_union)
8485
// CHECK-NEXT: HLSLResourceClass (SubjectMatchRule_record_not_is_union)
8586
// CHECK-NEXT: Hot (SubjectMatchRule_function)
8687
// CHECK-NEXT: HybridPatchable (SubjectMatchRule_function)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -ast-dump -o - %s | FileCheck %s
2+
3+
4+
// CHECK: -HLSLROVAttr 0x{{[0-9a-f]+}} <col:10, col:16>
5+
struct [[hlsl::is_rov]] Eg1 {
6+
int i;
7+
};
8+
9+
Eg1 e1;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -ast-dump -o - %s -verify
2+
3+
// expected-error@+1{{'is_rov' attribute takes no arguments}}
4+
struct [[hlsl::is_rov(3)]] Eg1 {
5+
int i;
6+
};
7+
8+
Eg1 e1;
9+
10+
// expected-error@+1{{use of undeclared identifier 'gibberish'}}
11+
struct [[hlsl::is_rov(gibberish)]] Eg2 {
12+
int i;
13+
};
14+
15+
Eg2 e2;
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -x hlsl -ast-dump -o - %s | FileCheck %s
22

3-
// CHECK: -ClassTemplateDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit RWBuffer
4-
// CHECK: -CXXRecordDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit class RWBuffer definition
5-
// CHECK: -FieldDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit h 'element_type *'
6-
// CHECK: -HLSLResourceClassAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit UAV
7-
// CHECK: -HLSLResourceAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit TypedBuffer
8-
RasterizerOrderedBuffer<vector<float, 4> > BufferArray3[4] : register(u4, space1);
9-
103
// CHECK: -ClassTemplateSpecializationDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> class RWBuffer definition implicit_instantiation
114
// CHECK: -FieldDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit referenced h 'float *'
125
// CHECK: -HLSLResourceClassAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit UAV
136
// CHECK: -HLSLResourceAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit TypedBuffer
147
RWBuffer<float> Buffer1;
8+
9+
// CHECK: -ClassTemplateDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit RasterizerOrderedBuffer
10+
// CHECK: -CXXRecordDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit class RasterizerOrderedBuffer definition
11+
// CHECK: -FieldDecl 0x{{[0-9a-f]+}} <<invalid sloc>> <invalid sloc> implicit h 'element_type *'
12+
// CHECK: -HLSLResourceClassAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit UAV
13+
// CHECK: -HLSLResourceAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit TypedBuffer
14+
// CHECK: -HLSLROVAttr 0x{{[0-9a-f]+}} <<invalid sloc>> Implicit
15+
RasterizerOrderedBuffer<vector<float, 4> > BufferArray3[4] : register(u4, space1);

0 commit comments

Comments
 (0)