Skip to content

Commit 31bbeb2

Browse files
authored
[SPIR-V] Fix layout rule propagation for function variable and parameter aliasing for ConstantBuffer and TextureBuffer. (#8326)
Fixes #8244 There are 2 issues being fixed: - Layout rule for resources should be propagated when aliased through function variable. (above issue) - Fix the incorrect layout rule propagation for `ConstantBuffer` vs `TextureBuffer` when creating function parameter.
1 parent 8832f3b commit 31bbeb2

File tree

4 files changed

+95
-19
lines changed

4 files changed

+95
-19
lines changed

tools/clang/lib/SPIRV/DeclResultIdMapper.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,10 @@ DeclResultIdMapper::createFnParam(const ParmVarDecl *param,
10681068
(void)getTypeAndCreateCounterForPotentialAliasVar(param, &isAlias);
10691069
fnParamInstr->setContainsAliasComponent(isAlias);
10701070

1071-
if (isConstantTextureBuffer(type))
1071+
if (isConstantBuffer(type))
10721072
fnParamInstr->setLayoutRule(spirvOptions.cBufferLayoutRule);
1073+
if (isTextureBuffer(type))
1074+
fnParamInstr->setLayoutRule(spirvOptions.tBufferLayoutRule);
10731075

10741076
assert(astDecls[param].instr == nullptr);
10751077
registerVariableForDecl(param, fnParamInstr);
@@ -1118,6 +1120,11 @@ DeclResultIdMapper::createFnVar(const VarDecl *var,
11181120
spvBuilder.addFnVar(type, loc, name, isPrecise, isNointerp,
11191121
init.hasValue() ? init.getValue() : nullptr);
11201122

1123+
if (isConstantBuffer(type))
1124+
varInstr->setLayoutRule(spirvOptions.cBufferLayoutRule);
1125+
if (isTextureBuffer(type))
1126+
varInstr->setLayoutRule(spirvOptions.tBufferLayoutRule);
1127+
11211128
bool isAlias = false;
11221129
(void)getTypeAndCreateCounterForPotentialAliasVar(var, &isAlias);
11231130
varInstr->setContainsAliasComponent(isAlias);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %dxc -T vs_6_0 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK-DAG: OpMemberDecorate %Inner 0 Offset 0
4+
5+
// CHECK-DAG: %Inner = OpTypeStruct %float
6+
// CHECK-DAG: %type_ConstantBuffer_CBS = OpTypeStruct %Inner
7+
// CHECK-DAG: %Inner_0 = OpTypeStruct %float
8+
// CHECK-DAG: %CBS = OpTypeStruct %Inner_0
9+
struct Inner
10+
{
11+
float field;
12+
};
13+
14+
struct CBS
15+
{
16+
Inner entry;
17+
};
18+
19+
ConstantBuffer<CBS> input;
20+
21+
float foo()
22+
{
23+
ConstantBuffer<CBS> local = input;
24+
CBS alias = local;
25+
// CHECK: %local = OpVariable %_ptr_Function_type_ConstantBuffer_CBS Function
26+
// CHECK: [[loadedInput:%[0-9]+]] = OpLoad %type_ConstantBuffer_CBS %input
27+
// CHECK: OpStore %local [[loadedInput]]
28+
// CHECK: [[loadedLocal:%[0-9]+]] = OpLoad %type_ConstantBuffer_CBS %local
29+
// CHECK: [[e1:%[0-9]+]] = OpCompositeExtract %Inner [[loadedLocal]]
30+
// CHECK: [[e2:%[0-9]+]] = OpCompositeExtract %float [[e1]]
31+
// CHECK: [[c2:%[0-9]+]] = OpCompositeConstruct %Inner_0 [[e2]]
32+
// CHECK: [[c1:%[0-9]+]] = OpCompositeConstruct %CBS [[c2]]
33+
// CHECK: OpStore %alias [[c1]]
34+
return alias.entry.field;
35+
}
36+
37+
float main() : A
38+
{
39+
return foo();
40+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %dxc -T vs_6_0 -E main -fvk-use-gl-layout -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK-DAG: OpDecorate %_arr_float_uint_2 ArrayStride 4
4+
// CHECK-DAG: %type_TextureBuffer_CBS = OpTypeStruct %_arr_float_uint_2
5+
// CHECK-DAG: %CBS = OpTypeStruct %_arr_float_uint_2_0
6+
7+
struct CBS
8+
{
9+
float entry[2];
10+
};
11+
12+
TextureBuffer<CBS> input;
13+
14+
float foo()
15+
{
16+
TextureBuffer<CBS> local = input;
17+
CBS alias = local;
18+
// CHECK: %local = OpVariable %_ptr_Function_type_TextureBuffer_CBS Function
19+
// CHECK: [[loadedInput:%[0-9]+]] = OpLoad %type_TextureBuffer_CBS %input
20+
// CHECK: OpStore %local [[loadedInput]]
21+
// CHECK: [[loadedLocal:%[0-9]+]] = OpLoad %type_TextureBuffer_CBS %local
22+
// CHECK: [[e1:%[0-9]+]] = OpCompositeExtract %_arr_float_uint_2 [[loadedLocal]]
23+
// CHECK: [[e2:%[0-9]+]] = OpCompositeExtract %float [[e1]] 0
24+
// CHECK: [[e3:%[0-9]+]] = OpCompositeExtract %float [[e1]] 1
25+
// CHECK: [[c1:%[0-9]+]] = OpCompositeConstruct %_arr_float_uint_2_0 [[e2]] [[e3]]
26+
// CHECK: [[c2:%[0-9]+]] = OpCompositeConstruct %CBS [[c1]]
27+
// CHECK: OpStore %alias [[c2]]
28+
return alias.entry[1];
29+
}
30+
31+
float main() : A
32+
{
33+
return foo();
34+
}

tools/clang/test/CodeGenSPIRV/type.texture-buffer.pass.hlsl

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
1-
// RUN: %dxc -T vs_6_0 -E main -fcgl %s -spirv | FileCheck %s
1+
// RUN: %dxc -T vs_6_0 -E main -fvk-use-gl-layout -fcgl %s -spirv | FileCheck %s
22

3-
// CHECK-DAG: OpMemberDecorate %Inner 0 Offset 0
4-
5-
// CHECK-DAG: %Inner = OpTypeStruct %float
6-
// CHECK-DAG: %type_TextureBuffer_CBS = OpTypeStruct %Inner
7-
// CHECK-DAG: %Inner_0 = OpTypeStruct %float
8-
// CHECK-DAG: %CBS = OpTypeStruct %Inner_0
9-
struct Inner
10-
{
11-
float field;
12-
};
3+
// CHECK-DAG: OpDecorate %_arr_float_uint_2 ArrayStride 4
4+
// CHECK-DAG: %type_TextureBuffer_CBS = OpTypeStruct %_arr_float_uint_2
5+
// CHECK-DAG: %CBS = OpTypeStruct %_arr_float_uint_2_0
136

147
struct CBS
158
{
16-
Inner entry;
9+
float entry[2];
1710
};
1811

1912
float foo(TextureBuffer<CBS> param)
2013
{
2114
CBS alias = param;
15+
// CHECK: %param = OpFunctionParameter %_ptr_Function_type_TextureBuffer_CBS
2216
// CHECK: [[copy:%[0-9]+]] = OpLoad %type_TextureBuffer_CBS %param
23-
// CHECK: [[e1:%[0-9]+]] = OpCompositeExtract %Inner [[copy]]
24-
// CHECK: [[e2:%[0-9]+]] = OpCompositeExtract %float [[e1]]
25-
// CHECK: [[c2:%[0-9]+]] = OpCompositeConstruct %Inner_0 [[e2]]
26-
// CHECK: [[c1:%[0-9]+]] = OpCompositeConstruct %CBS [[c2]]
27-
// CHECK: OpStore %alias [[c1]]
28-
return alias.entry.field;
17+
// CHECK: [[e1:%[0-9]+]] = OpCompositeExtract %_arr_float_uint_2 [[copy]]
18+
// CHECK: [[e2:%[0-9]+]] = OpCompositeExtract %float [[e1]] 0
19+
// CHECK: [[e3:%[0-9]+]] = OpCompositeExtract %float [[e1]] 1
20+
// CHECK: [[c1:%[0-9]+]] = OpCompositeConstruct %_arr_float_uint_2_0 [[e2]] [[e3]]
21+
// CHECK: [[c2:%[0-9]+]] = OpCompositeConstruct %CBS [[c1]]
22+
// CHECK: OpStore %alias [[c2]]
23+
return alias.entry[1];
2924
}
3025

3126
TextureBuffer<CBS> input;

0 commit comments

Comments
 (0)