Skip to content

Commit 2a2324a

Browse files
authored
[HLSL] Do not remove HLSLVkBindingAttr if the target is not SPIR-V (#161752)
The attribute needs to be preserved for rewriter scenarios. Two places were updated to use the `ResourceBindingAttrs` helper struct to make sure the `HLSLVkBindingAttr` is ignored when the target is DirectX.
1 parent 25933f6 commit 2a2324a

File tree

4 files changed

+30
-40
lines changed

4 files changed

+30
-40
lines changed

clang/include/clang/AST/HLSLResource.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ struct ResourceBindingAttrs {
6969
assert(hasImplicitOrderID());
7070
return RegBinding->getImplicitBindingOrderID();
7171
}
72+
73+
void setImplicitOrderID(unsigned Value) const {
74+
assert(hasBinding() && !isExplicit() && !hasImplicitOrderID());
75+
RegBinding->setImplicitBindingOrderID(Value);
76+
}
7277
};
7378

7479
} // namespace hlsl

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -598,18 +598,17 @@ void SemaHLSL::ActOnFinishBuffer(Decl *Dcl, SourceLocation RBrace) {
598598

599599
validatePackoffset(SemaRef, BufDecl);
600600

601-
// create buffer layout struct
602601
createHostLayoutStructForBuffer(SemaRef, BufDecl);
603602

604-
HLSLVkBindingAttr *VkBinding = Dcl->getAttr<HLSLVkBindingAttr>();
605-
HLSLResourceBindingAttr *RBA = Dcl->getAttr<HLSLResourceBindingAttr>();
606-
if (!VkBinding && (!RBA || !RBA->hasRegisterSlot())) {
603+
// Handle implicit binding if needed.
604+
ResourceBindingAttrs ResourceAttrs(Dcl);
605+
if (!ResourceAttrs.isExplicit()) {
607606
SemaRef.Diag(Dcl->getLocation(), diag::warn_hlsl_implicit_binding);
608607
// Use HLSLResourceBindingAttr to transfer implicit binding order_ID
609608
// to codegen. If it does not exist, create an implicit attribute.
610609
uint32_t OrderID = getNextImplicitBindingOrderID();
611-
if (RBA)
612-
RBA->setImplicitBindingOrderID(OrderID);
610+
if (ResourceAttrs.hasBinding())
611+
ResourceAttrs.setImplicitOrderID(OrderID);
613612
else
614613
addImplicitBindingAttrToDecl(SemaRef, BufDecl,
615614
BufDecl->isCBuffer() ? RegisterType::CBuffer
@@ -1590,10 +1589,6 @@ void SemaHLSL::handleVkConstantIdAttr(Decl *D, const ParsedAttr &AL) {
15901589
}
15911590

15921591
void SemaHLSL::handleVkBindingAttr(Decl *D, const ParsedAttr &AL) {
1593-
// The vk::binding attribute only applies to SPIR-V.
1594-
if (!getASTContext().getTargetInfo().getTriple().isSPIRV())
1595-
return;
1596-
15971592
uint32_t Binding = 0;
15981593
if (!SemaRef.checkUInt32Argument(AL, AL.getArgAsExpr(0), Binding))
15991594
return;
@@ -3780,17 +3775,15 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
37803775
// If the resource array does not have an explicit binding attribute,
37813776
// create an implicit one. It will be used to transfer implicit binding
37823777
// order_ID to codegen.
3783-
if (!VD->hasAttr<HLSLVkBindingAttr>()) {
3784-
HLSLResourceBindingAttr *RBA = VD->getAttr<HLSLResourceBindingAttr>();
3785-
if (!RBA || !RBA->hasRegisterSlot()) {
3786-
uint32_t OrderID = getNextImplicitBindingOrderID();
3787-
if (RBA)
3788-
RBA->setImplicitBindingOrderID(OrderID);
3789-
else
3790-
addImplicitBindingAttrToDecl(
3791-
SemaRef, VD, getRegisterType(getResourceArrayHandleType(VD)),
3792-
OrderID);
3793-
}
3778+
ResourceBindingAttrs Binding(VD);
3779+
if (!Binding.isExplicit()) {
3780+
uint32_t OrderID = getNextImplicitBindingOrderID();
3781+
if (Binding.hasBinding())
3782+
Binding.setImplicitOrderID(OrderID);
3783+
else
3784+
addImplicitBindingAttrToDecl(
3785+
SemaRef, VD, getRegisterType(getResourceArrayHandleType(VD)),
3786+
OrderID);
37943787
}
37953788
}
37963789
}

clang/test/AST/HLSL/resource_binding_attr.hlsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ cbuffer CB3 {
9292
StructuredBuffer<float> SB[10];
9393

9494
// CHECK: VarDecl {{.*}} SB2 'StructuredBuffer<float>[10]'
95+
// CHECK: HLSLVkBindingAttr {{.*}} 2 0
9596
// DXIL: HLSLResourceBindingAttr {{.*}} Implicit
96-
// DXIL-NOT: HLSLVkBindingAttr
97-
// SPV: HLSLVkBindingAttr {{.*}} 2 0
9897
// SPV-NOT: HLSLResourceBindingAttr {{.*}} Implicit
9998
[[vk::binding(2)]]
10099
StructuredBuffer<float> SB2[10];

clang/test/AST/HLSL/vk_binding_attr.hlsl

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 102
1111
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0
1212
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0
13-
// SPV: HLSLVkBindingAttr {{.*}} 23 102
14-
// DXIL-NOT: HLSLVkBindingAttr
13+
// CHECK: HLSLVkBindingAttr {{.*}} 23 102
1514
[[vk::binding(23, 102)]] StructuredBuffer<float> Buf;
1615

1716
// CHECK: VarDecl {{.*}} Buf2 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>'
@@ -23,8 +22,7 @@
2322
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 1
2423
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 23
2524
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 102
26-
// SPV: HLSLVkBindingAttr {{.*}} 14 1
27-
// DXIL-NOT: HLSLVkBindingAttr
25+
// CHECK: HLSLVkBindingAttr {{.*}} 14 1
2826
// CHECK: HLSLResourceBindingAttr {{.*}} "t23" "space102"
2927
[[vk::binding(14, 1)]] StructuredBuffer<float> Buf2 : register(t23, space102);
3028

@@ -37,15 +35,13 @@
3735
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0
3836
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 23
3937
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 102
40-
// SPV: HLSLVkBindingAttr {{.*}} 14 0
41-
// DXIL-NOT: HLSLVkBindingAttr
38+
// CHECK: HLSLVkBindingAttr {{.*}} 14 0
4239
// CHECK: HLSLResourceBindingAttr {{.*}} "t23" "space102"
4340
[[vk::binding(14)]] StructuredBuffer<float> Buf3 : register(t23, space102);
4441

4542
// CHECK: HLSLBufferDecl {{.*}} cbuffer CB
4643
// CHECK-NEXT: HLSLResourceClassAttr {{.*}} Implicit CBuffer
47-
// SPV-NEXT: HLSLVkBindingAttr {{.*}} 1 2
48-
// DXIL-NOT: HLSLVkBindingAttr
44+
// CHECK: HLSLVkBindingAttr {{.*}} 1 2
4945
[[vk::binding(1, 2)]] cbuffer CB {
5046
float a;
5147
}
@@ -54,15 +50,14 @@
5450
// CHECK-NEXT: CallExpr {{.*}} 'Buffer<int>':'hlsl::Buffer<int>'
5551
// CHECK-NEXT: ImplicitCastExpr {{.*}} 'hlsl::Buffer<int> (*)(unsigned int, unsigned int, int, unsigned int, const char *)' <FunctionToPointerDecay>
5652
// SPV-NEXT: DeclRefExpr {{.*}} 'hlsl::Buffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
57-
// SPV-NEXT-SAME: CXXMethod {{.*}} '__createFromBinding' 'Buffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
53+
// SPV-NEXT-SAME: CXXMethod {{.*}} '__createFromBinding' 'hlsl::Buffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
5854
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 24
5955
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 103
60-
// DXIL-NEXT: DeclRefExpr {{.*}} 'hlsl::Buffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
61-
// DXIL-NEXT-SAME: CXXMethod {{.*}} '__createFromImplicitBinding' 'Buffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
56+
// DXIL-NEXT: DeclRefExpr {{.*}} 'hlsl::Buffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
57+
// DXIL-NEXT-SAME: CXXMethod {{.*}} '__createFromImplicitBinding' 'hlsl::Buffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
6258
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 2
6359
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0
64-
// SPV: HLSLVkBindingAttr {{.*}} 24 103
65-
// DXIL-NOT: HLSLVkBindingAttr
60+
// CHECK: HLSLVkBindingAttr {{.*}} 24 103
6661
[[vk::binding(24, 103)]] Buffer<int> Buf4;
6762

6863
// CHECK: VarDecl {{.*}} Buf5 'RWBuffer<int2>':'hlsl::RWBuffer<vector<int, 2>>'
@@ -76,8 +71,7 @@
7671
// DXIL-NEXT-SAME: CXXMethod {{.*}} '__createFromImplicitBinding' 'Buffer<int2> (unsigned int, unsigned int, int, unsigned int, const char *)'
7772
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 3
7873
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0
79-
// SPV: HLSLVkBindingAttr {{.*}} 25 104
80-
// DXIL-NOT: HLSLVkBindingAttr
74+
// CHECK: HLSLVkBindingAttr {{.*}} 25 104
8175
[[vk::binding(25, 104)]] RWBuffer<int2> Buf5;
8276

8377
// CHECK: VarDecl {{.*}} Buf6 'RWStructuredBuffer<int>':'hlsl::RWStructuredBuffer<int>'
@@ -91,6 +85,5 @@
9185
// DXIL-NEXT-SAME: CXXMethod {{.*}} '__createFromBinding' 'hlsl::RWStructuredBuffer<int> (unsigned int, unsigned int, int, unsigned int, const char *)'
9286
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 4
9387
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0
94-
// SPV: HLSLVkBindingAttr {{.*}} 26 105
95-
// DXIL-NOT: HLSLVkBindingAttr
88+
// CHECK: HLSLVkBindingAttr {{.*}} 26 105
9689
[[vk::binding(26, 105)]] RWStructuredBuffer<int> Buf6;

0 commit comments

Comments
 (0)