Skip to content

Commit 2b90cd3

Browse files
[SPIR-V] Create ResourceHeap when directly returned (#8006)
Currently, HLSL of the form "return ResourceDescriptorHeap[0];" will break compilation, if this is the first reference to ResourceDescriptorHeap encountered by the SpirvEmitter. Please see the enclosed testcase, at [https://godbolt.org/z/3aWcq5roa](https://godbolt.org/z/3aWcq5roa)
1 parent 4c613aa commit 2b90cd3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,6 +2926,17 @@ void SpirvEmitter::doReturnStmt(const ReturnStmt *stmt) {
29262926
bool returnsVoid = curFunction->getReturnType().getTypePtr()->isVoidType();
29272927
if (!returnsVoid) {
29282928
assert(retVal);
2929+
const Expr *srcExpr = retVal->IgnoreParenCasts();
2930+
if (isDescriptorHeap(srcExpr)) {
2931+
const Expr *base = nullptr;
2932+
getDescriptorHeapOperands(srcExpr, &base, /* index= */ nullptr);
2933+
const Expr *parentExpr = cast<CastExpr>(parentMap->getParent(srcExpr));
2934+
QualType resourceType = parentExpr->getType();
2935+
const auto *declRefExpr = dyn_cast<DeclRefExpr>(base->IgnoreCasts());
2936+
auto *decl = cast<VarDecl>(declRefExpr->getDecl());
2937+
declIdMapper.createResourceHeap(decl, resourceType);
2938+
}
2939+
29292940
// Update counter variable associated with function returns
29302941
tryToAssignCounterVar(curFunction, retVal);
29312942

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %dxc -T cs_6_6 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
RWStructuredBuffer<uint> GetBindlessResource_UIntBuffer() {
4+
return ResourceDescriptorHeap[0];
5+
}
6+
7+
[numthreads(1, 1, 1)]
8+
void main() {
9+
// CHECK-DAG: OpDecorate %ResourceDescriptorHeap DescriptorSet 0
10+
// CHECK-DAG: OpDecorate %ResourceDescriptorHeap Binding 0
11+
// CHECK-DAG: OpDecorate %counter_var_ResourceDescriptorHeap DescriptorSet 0
12+
// CHECK-DAG: OpDecorate %counter_var_ResourceDescriptorHeap Binding 1
13+
RWStructuredBuffer<uint> a = GetBindlessResource_UIntBuffer();
14+
15+
a.IncrementCounter();
16+
}

0 commit comments

Comments
 (0)