Skip to content

Commit 1d8fad9

Browse files
authored
[HLSL] Allow resource type attributes only on __hlsl_resource_t (#110143)
Resource type attributes should only ever be used on the intangible type `__hlsl_resource_t`.
1 parent c117222 commit 1d8fad9

File tree

8 files changed

+26
-4
lines changed

8 files changed

+26
-4
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12388,6 +12388,7 @@ def err_hlsl_packoffset_alignment_mismatch : Error<"packoffset at 'y' not match
1238812388
def err_hlsl_pointers_unsupported : Error<
1238912389
"%select{pointers|references}0 are unsupported in HLSL">;
1239012390
def err_hlsl_missing_resource_class : Error<"HLSL resource needs to have [[hlsl::resource_class()]] attribute">;
12391+
def err_hlsl_attribute_needs_intangible_type: Error<"attribute %0 can be used only on HLSL intangible type %1">;
1239112392

1239212393
def err_hlsl_operator_unsupported : Error<
1239312394
"the '%select{&|*|->}0' operator is unsupported in HLSL">;

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SemaHLSL : public SemaBase {
7070
void handleShaderAttr(Decl *D, const ParsedAttr &AL);
7171
void handleResourceBindingAttr(Decl *D, const ParsedAttr &AL);
7272
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
73-
bool handleResourceTypeAttr(const ParsedAttr &AL);
73+
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
7474

7575
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
7676
QualType ProcessResourceTypeAttributes(QualType Wrapped);

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,13 +693,19 @@ bool clang::CreateHLSLAttributedResourceType(
693693
// HLSL resource. The attributes are collected in HLSLResourcesTypeAttrs and at
694694
// the end of the declaration they are applied to the declaration type by
695695
// wrapping it in HLSLAttributedResourceType.
696-
bool SemaHLSL::handleResourceTypeAttr(const ParsedAttr &AL) {
697-
Attr *A = nullptr;
696+
bool SemaHLSL::handleResourceTypeAttr(QualType T, const ParsedAttr &AL) {
697+
// only allow resource type attributes on intangible types
698+
if (!T->isHLSLResourceType()) {
699+
Diag(AL.getLoc(), diag::err_hlsl_attribute_needs_intangible_type)
700+
<< AL << getASTContext().HLSLResourceTy;
701+
return false;
702+
}
698703

699704
// validate number of arguments
700705
if (!AL.checkExactlyNumArgs(SemaRef, AL.getMinArgs()))
701706
return false;
702707

708+
Attr *A = nullptr;
703709
switch (AL.getKind()) {
704710
case ParsedAttr::AT_HLSLResourceClass: {
705711
if (!AL.isArgIdent(0)) {

clang/lib/Sema/SemaType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8860,7 +8860,7 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type,
88608860
// decl-specifier-seq; do not collect attributes on declarations or those
88618861
// that get to slide after declaration name.
88628862
if (TAL == TAL_DeclSpec &&
8863-
state.getSema().HLSL().handleResourceTypeAttr(attr))
8863+
state.getSema().HLSL().handleResourceTypeAttr(type, attr))
88648864
attr.setUsedAsTypeAttr();
88658865
break;
88668866
}

clang/test/ParserHLSL/hlsl_contained_type_attr_error.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ __hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::contained_type(float)]]
2222

2323
// expected-warning@+1{{attribute 'contained_type' is already applied with different arguments}}
2424
__hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::contained_type(float)]] [[hlsl::contained_type(int)]] h8;
25+
26+
// expected-error@+2{{attribute 'resource_class' can be used only on HLSL intangible type '__hlsl_resource_t'}}
27+
// expected-error@+1{{attribute 'contained_type' can be used only on HLSL intangible type '__hlsl_resource_t'}}
28+
float [[hlsl::resource_class(UAV)]] [[hlsl::contained_type(float)]] res5;

clang/test/ParserHLSL/hlsl_is_rov_attr_error.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ __hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::is_rov(gibberish)]] res3
1414

1515
// expected-warning@+1{{attribute 'is_rov' is already applied}}
1616
__hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::is_rov]] [[hlsl::is_rov]] res4;
17+
18+
// expected-error@+2{{attribute 'resource_class' can be used only on HLSL intangible type '__hlsl_resource_t'}}
19+
// expected-error@+1{{attribute 'is_rov' can be used only on HLSL intangible type '__hlsl_resource_t'}}
20+
float [[hlsl::resource_class(UAV)]] [[hlsl::is_rov]] res5;

clang/test/ParserHLSL/hlsl_raw_buffer_attr_error.hlsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ __hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::raw_buffer(gibberish)]]
1111

1212
// expected-warning@+1{{attribute 'raw_buffer' is already applied}}
1313
__hlsl_resource_t [[hlsl::resource_class(UAV)]] [[hlsl::raw_buffer]] [[hlsl::raw_buffer]] res4;
14+
15+
// expected-error@+2{{attribute 'resource_class' can be used only on HLSL intangible type '__hlsl_resource_t'}}
16+
// expected-error@+1{{attribute 'raw_buffer' can be used only on HLSL intangible type '__hlsl_resource_t'}}
17+
float [[hlsl::resource_class(UAV)]] [[hlsl::raw_buffer]] res5;

clang/test/ParserHLSL/hlsl_resource_class_attr_error.hlsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ __hlsl_resource_t [[hlsl::resource_class(SRV)]] [[hlsl::resource_class(SRV)]] e4
1717

1818
// expected-error@+1{{'resource_class' attribute takes one argument}}
1919
__hlsl_resource_t [[hlsl::resource_class(SRV, "aa")]] e5;
20+
21+
// expected-error@+1{{attribute 'resource_class' can be used only on HLSL intangible type '__hlsl_resource_t'}}
22+
float [[hlsl::resource_class(UAV)]] e6;

0 commit comments

Comments
 (0)