Skip to content

Commit 1f53295

Browse files
authored
[SPIR-V] Fix counter in direct return stmt (#8413)
There is a logic to create counter variables, and find them from a source expression. For return statements, we were first looking for the referenced counter before dealing with the RHS of the expression: the call. The fact that it worked so far was just luck: - if the function decl was handled/seen before the return statement, the counter variable association was handled. But in case of a direct return, this was not the case. Fixes #8215
1 parent d73829d commit 1f53295

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,13 +2967,13 @@ void SpirvEmitter::doReturnStmt(const ReturnStmt *stmt) {
29672967
declIdMapper.createResourceHeap(decl, resourceType);
29682968
}
29692969

2970-
// Update counter variable associated with function returns
2971-
tryToAssignCounterVar(curFunction, retVal);
2972-
29732970
auto *retInfo = loadIfGLValue(retVal);
29742971
if (!retInfo)
29752972
return;
29762973

2974+
// Update counter variable associated with function returns
2975+
tryToAssignCounterVar(curFunction, retVal);
2976+
29772977
auto retType = retVal->getType();
29782978
if (retInfo->getLayoutRule() != SpirvLayoutRule::Void &&
29792979
retType->isStructureType()) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %dxc -T cs_6_6 -E main -O3 %s -spirv | FileCheck %s
2+
3+
RWStructuredBuffer<uint> GetBindlessResource_UIntBuffer_inner() {
4+
return ResourceDescriptorHeap[1];
5+
}
6+
7+
RWStructuredBuffer<uint> GetBindlessResource_UIntBuffer_outter() {
8+
return GetBindlessResource_UIntBuffer_inner();
9+
}
10+
11+
RWStructuredBuffer<uint> GetBindlessResource_UIntBuffer() {
12+
return ResourceDescriptorHeap[0];
13+
}
14+
15+
[numthreads(1, 1, 1)]
16+
void main() {
17+
RWStructuredBuffer<uint> a = GetBindlessResource_UIntBuffer();
18+
RWStructuredBuffer<uint> b = GetBindlessResource_UIntBuffer_outter();
19+
// CHECK-DAG: [[counter_a:%[0-9]+]] = OpAccessChain %_ptr_Uniform_int %counter_var_ResourceDescriptorHeap %uint_0 %uint_0
20+
// CHECK-DAG: [[counter_b:%[0-9]+]] = OpAccessChain %_ptr_Uniform_int %counter_var_ResourceDescriptorHeap %uint_1 %uint_0
21+
22+
a.IncrementCounter();
23+
b.IncrementCounter();
24+
25+
// CHECK-DAG: {{%[0-9]+}} = OpAtomicIAdd %int [[counter_a]] %uint_1 %uint_0 %int_1
26+
// CHECK-DAG: {{%[0-9]+}} = OpAtomicIAdd %int [[counter_b]] %uint_1 %uint_0 %int_1
27+
}

tools/clang/test/CodeGenSPIRV/spirv.legal.sbuffer.usage.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ float4 useAsStaticRWSBuffer() {
8787
// CHECK: %paramRWSBuffer = OpFunctionParameter %_ptr_Function__ptr_Uniform_type_RWStructuredBuffer_S
8888
RWStructuredBuffer<S> returnRWSBuffer(RWStructuredBuffer<S> paramRWSBuffer) {
8989
// CHECK: [[ptr_1:%[0-9]+]] = OpLoad %_ptr_Uniform_type_RWStructuredBuffer_S %paramRWSBuffer
90-
// CHECK-NEXT: OpReturnValue [[ptr_1]]
90+
// CHECK: OpReturnValue [[ptr_1]]
9191
return paramRWSBuffer;
9292
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %dxc -T cs_6_6 -E main -O3 %s -spirv | FileCheck %s
2+
3+
RWStructuredBuffer<uint> resource;
4+
5+
RWStructuredBuffer<uint> get_resource_inner() {
6+
return resource;
7+
}
8+
9+
RWStructuredBuffer<uint> get_resource_outter() {
10+
return get_resource_inner();
11+
}
12+
13+
RWStructuredBuffer<uint> get_resource() {
14+
return resource;
15+
}
16+
17+
[numthreads(1, 1, 1)]
18+
void main() {
19+
RWStructuredBuffer<uint> a = get_resource();
20+
RWStructuredBuffer<uint> b = get_resource_outter();
21+
// CHECK-DAG: [[counter:%[0-9]+]] = OpAccessChain %_ptr_Uniform_int %counter_var_resource %uint_0
22+
23+
a.IncrementCounter();
24+
b.IncrementCounter();
25+
26+
// CHECK-DAG: {{%[0-9]+}} = OpAtomicIAdd %int [[counter]] %uint_1 %uint_0 %int_1
27+
// CHECK-DAG: {{%[0-9]+}} = OpAtomicIAdd %int [[counter]] %uint_1 %uint_0 %int_1
28+
}

0 commit comments

Comments
 (0)