Skip to content

Commit ac36a79

Browse files
authored
[SPIR-V] Fix cast elision with initializer lists (microsoft#6992)
When an a chain of casts were performed in an initializer list, only the source and destination types were considered. This means float -> uint was the same as float -> int -> uint. This however is not correct: intermediate casts can change the result. Removing the shortcut we took solves this issue. Fixes microsoft#6975 Signed-off-by: Nathan Gauër <[email protected]>
1 parent 5704c47 commit ac36a79

File tree

6 files changed

+96
-43
lines changed

6 files changed

+96
-43
lines changed

tools/clang/lib/SPIRV/InitListHandler.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,6 @@ void InitListHandler::flatten(const InitListExpr *expr) {
7777
const Expr *init = expr->getInit(i);
7878
if (const auto *subInitList = dyn_cast<InitListExpr>(init)) {
7979
flatten(subInitList);
80-
} else if (const auto *subInitList = dyn_cast<InitListExpr>(
81-
// Ignore constructor casts which are no-ops
82-
// For cases like: <type>(<initializer-list>)
83-
init->IgnoreParenNoopCasts(theEmitter.getASTContext()))) {
84-
flatten(subInitList);
8580
} else {
8681
auto *initializer = theEmitter.loadIfGLValue(init);
8782
if (!initializer) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %dxc -T cs_6_6 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
RWStructuredBuffer<float> buffer;
4+
RWStructuredBuffer<float4> buffer4;
5+
6+
[numthreads(1, 1, 1)]
7+
void main() {
8+
{
9+
// CHECK: [[f:%[0-9]+]] = OpLoad %float {{.*}}
10+
// CHECK: [[s:%[0-9]+]] = OpConvertFToS %int [[f]]
11+
// CHECK: [[u:%[0-9]+]] = OpBitcast %uint [[s]]
12+
// CHECK: OpStore {{.*}} [[u]]
13+
uint p = uint(int(buffer[0]));
14+
}
15+
16+
{
17+
// CHECK: [[f:%[0-9]+]] = OpLoad %v4float {{.*}}
18+
// CHECK: [[s:%[0-9]+]] = OpConvertFToS %v4int [[f]]
19+
// CHECK: [[u:%[0-9]+]] = OpBitcast %v4uint [[s]]
20+
// CHECK: OpStore {{.*}} [[u]]
21+
uint4 p4 = uint4(int4(buffer4[0]));
22+
}
23+
24+
{
25+
// CHECK: [[f:%[0-9]+]] = OpLoad %v4float {{.*}}
26+
// CHECK: [[s:%[0-9]+]] = OpConvertFToS %v4int [[f]]
27+
// CHECK: [[u:%[0-9]+]] = OpBitcast %v4uint [[s]]
28+
// CHECK: OpStore {{.*}} [[u]]
29+
uint4 p4 = int4(buffer4[0]);
30+
}
31+
32+
{
33+
// CHECK: [[f:%[0-9]+]] = OpLoad %v4float {{.*}}
34+
// CHECK: [[u:%[0-9]+]] = OpConvertFToU %v4uint [[f]]
35+
// CHECK: OpStore {{.*}} [[u]]
36+
uint4 p4 = uint4(uint4(buffer4[0]));
37+
}
38+
}

tools/clang/test/CodeGenSPIRV/shader.debug.line.composite.hlsl

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,79 @@ void main() {
4545

4646
int4 a = {
4747
float2(1, 0),
48-
// CHECK: DebugLine [[src]] %uint_50 %uint_50 %uint_7 %uint_19
49-
// CHECK: OpFunctionCall %int4_bool_float3_0 %test_struct
48+
// CHECK: DebugLine [[src]] %uint_51 %uint_51 %uint_7 %uint_19
49+
// CHECK-NEXT: OpFunctionCall %int4_bool_float3_0 %test_struct
50+
// CHECK: OpVectorShuffle %v2float
5051
test_struct().c.zx
51-
// CHECK: OpCompositeExtract %float {{%[0-9]+}} 0
52+
// CHECK: DebugLine [[src]] %uint_46 %uint_46 %uint_12 %uint_12
53+
// CHECK-NEXT: OpCompositeExtract %float {{%[0-9]+}} 0
5254
// CHECK-NEXT: OpCompositeExtract %float {{%[0-9]+}} 1
53-
// CHECK-NEXT: DebugLine [[src]] %uint_46 %uint_46 %uint_12 %uint_12
5455
// CHECK-NEXT: OpConvertFToS %int
5556
// CHECK-NEXT: OpConvertFToS %int
57+
// CHECK: DebugLine [[src]] %uint_51 %uint_51 %uint_7 %uint_23
58+
// CHECK-NEXT: OpCompositeExtract %float {{%[0-9]+}} 0
59+
// CHECK-NEXT: OpCompositeExtract %float {{%[0-9]+}} 1
60+
// CHECK: DebugLine [[src]] %uint_46 %uint_46 %uint_12 %uint_12
61+
// CHECK-NEXT: OpConvertFToS %int
62+
// CHECK-NEXT: OpConvertFToS %int
63+
// CHECK: DebugLine [[src]] %uint_46 %uint_65 %uint_12 %uint_3
5664
// CHECK: OpCompositeConstruct %v4int
5765
};
5866

5967
// CHECK: OpFDiv %float {{%[0-9]+}} %float_2
60-
// CHECK-NEXT: DebugLine [[src]] %uint_64 %uint_64 %uint_16 %uint_57
68+
// CHECK-NEXT: DebugLine [[src]] %uint_72 %uint_72 %uint_16 %uint_57
6169
// CHECK-NEXT: [[first:%[0-9]+]] = OpCompositeConstruct %v2float {{%[0-9]+}} {{%[0-9]+}}
6270
// CHECK-NEXT: [[second:%[0-9]+]] = OpCompositeConstruct %v2float {{%[0-9]+}} {{%[0-9]+}}
6371
// CHECK-NEXT: {{%[0-9]+}} = OpCompositeConstruct %mat2v2float [[first]] [[second]]
6472
float2x2 b = float2x2(a.x, b._m00, 2 + a.y, b._m11 / 2);
6573

66-
// CHECK: DebugLine [[src]] %uint_69 %uint_69 %uint_12 %uint_14
74+
// CHECK: DebugLine [[src]] %uint_77 %uint_77 %uint_12 %uint_14
6775
// CHECK-NEXT: [[y:%[0-9]+]] = OpAccessChain %_ptr_Uniform_int4_bool_float3 %CONSTANTS %int_0
6876
// CHECK-NEXT: {{%[0-9]+}} = OpAccessChain %_ptr_Uniform_v4int [[y]] %int_0
6977
int4 c = y.a;
7078

71-
// CHECK: DebugLine [[src]] %uint_76 %uint_76 %uint_3 %uint_3
79+
// CHECK: DebugLine [[src]] %uint_84 %uint_84 %uint_3 %uint_3
7280
// CHECK-NEXT: [[z:%[0-9]+]] = OpLoad %type_2d_image %z
7381
// CHECK-NEXT: [[z_0:%[0-9]+]] = OpImageRead %v4int [[z]] {{%[0-9]+}} None
7482
// CHECK-NEXT: [[z_1:%[0-9]+]] = OpVectorShuffle %v3int [[z_0]] [[z_0]] 0 1 2
7583
// CHECK: {{%[0-9]+}} = OpCompositeInsert %v3int %int_16 [[z_1]] 0
7684
z[uint2(2, 3)].x = 16;
7785

78-
// CHECK: DebugLine [[src]] %uint_82 %uint_82 %uint_3 %uint_4
86+
// CHECK: DebugLine [[src]] %uint_90 %uint_90 %uint_3 %uint_4
7987
// CHECK-NEXT: OpLoad %mat2v2float %b
80-
// CHECK: DebugLine [[src]] %uint_82 %uint_82 %uint_3 %uint_4
88+
// CHECK: DebugLine [[src]] %uint_90 %uint_90 %uint_3 %uint_4
8189
// CHECK-NEXT: OpFSub %v2float
8290
b--;
8391

8492
int2x2 d;
85-
// CHECK: DebugLine [[src]] %uint_91 %uint_91 %uint_8 %uint_8
93+
// CHECK: DebugLine [[src]] %uint_99 %uint_99 %uint_8 %uint_8
8694
// CHECK-NEXT: OpLoad %mat2v2float %b
87-
// CHECK-NEXT: DebugLine [[src]] %uint_91 %uint_91 %uint_3 %uint_12
95+
// CHECK-NEXT: DebugLine [[src]] %uint_99 %uint_99 %uint_3 %uint_12
8896
// CHECK-NEXT: OpCompositeExtract %v2float
8997
// CHECK: OpCompositeConstruct %_arr_v2int_uint_2
9098
// CHECK-NEXT: OpStore %d
9199
modf(b, d);
92100

93-
// CHECK-TODO: DebugLine [[src]] %uint_95 %uint_95 %uint_3 %uint_11
101+
// CHECK-TODO: DebugLine [[src]] %uint_103 %uint_103 %uint_3 %uint_11
94102
// CHECK-NEXT-TODO: OpFunctionCall %void %S_inc %foo
95103
// TODO(greg-lunarg): foo.inc();
96104

97-
// CHECK-TODO: DebugLine [[src]] %uint_99 %uint_99 %uint_3 %uint_14
105+
// CHECK-TODO: DebugLine [[src]] %uint_107 %uint_107 %uint_3 %uint_14
98106
// CHECK-NEXT-TODO: OpFunctionCall %void %S_inc %temp_var_S
99107
// TODO(greg-lunarg): getS().inc();
100108

101-
// CHECK: DebugLine [[src]] %uint_105 %uint_105 %uint_19 %uint_19
109+
// CHECK: DebugLine [[src]] %uint_113 %uint_113 %uint_19 %uint_19
102110
// CHECK-NEXT: OpLoad %init %bar
103-
// CHECK: DebugLine [[src]] %uint_105 %uint_105 %uint_12 %uint_12
111+
// CHECK: DebugLine [[src]] %uint_113 %uint_113 %uint_12 %uint_12
104112
// CHECK-NEXT: OpConvertFToS %int
105113
int4 e = {1, 2, bar};
106114

107-
// CHECK: DebugLine [[src]] %uint_111 %uint_111 %uint_7 %uint_25
115+
// CHECK: DebugLine [[src]] %uint_119 %uint_119 %uint_7 %uint_25
108116
// CHECK-NEXT: OpCompositeConstruct %v2float %float_1 %float_2
109-
// CHECK-NEXT: DebugLine [[src]] %uint_111 %uint_111 %uint_22 %uint_22
117+
// CHECK-NEXT: DebugLine [[src]] %uint_119 %uint_119 %uint_22 %uint_22
110118
// CHECK-NEXT: OpCompositeExtract %int
111119
b = float2x2(1, 2, bar);
112-
// CHECK: DebugLine [[src]] %uint_111 %uint_111 %uint_3 %uint_25
120+
// CHECK: DebugLine [[src]] %uint_119 %uint_119 %uint_3 %uint_25
113121
// CHECK-NEXT: OpStore %b
114122

115123
// TODO(jaebaek): Update InitListHandler to properly emit debug info.

tools/clang/test/CodeGenSPIRV/spirv.debug.opline.composite.hlsl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,63 +53,67 @@ void main() {
5353
// CHECK-NEXT: OpCompositeExtract %float {{%[0-9]+}} 1
5454
// CHECK-NEXT: OpConvertFToS %int
5555
// CHECK-NEXT: OpConvertFToS %int
56+
// CHECK-NEXT: OpCompositeExtract %float {{%[0-9]+}} 0
57+
// CHECK-NEXT: OpCompositeExtract %float {{%[0-9]+}} 1
58+
// CHECK-NEXT: OpConvertFToS %int
59+
// CHECK-NEXT: OpConvertFToS %int
5660
// CHECK-NEXT: OpCompositeConstruct %v4int
5761
};
5862

5963
// CHECK: OpFDiv %float {{%[0-9]+}} %float_2
60-
// CHECK-NEXT: OpLine [[file]] 64 24
64+
// CHECK-NEXT: OpLine [[file]] 68 24
6165
// CHECK-NEXT: [[first:%[0-9]+]] = OpCompositeConstruct %v2float {{%[0-9]+}} {{%[0-9]+}}
6266
// CHECK-NEXT: [[second:%[0-9]+]] = OpCompositeConstruct %v2float {{%[0-9]+}} {{%[0-9]+}}
6367
// CHECK-NEXT: {{%[0-9]+}} = OpCompositeConstruct %mat2v2float [[first]] [[second]]
6468
float2x2 b = float2x2(a.x, b._m00, 2 + a.y, b._m11 / 2);
6569

66-
// CHECK: OpLine [[file]] 69 12
70+
// CHECK: OpLine [[file]] 73 12
6771
// CHECK-NEXT: [[y:%[0-9]+]] = OpAccessChain %_ptr_Uniform_int4_bool_float3 %CONSTANTS %int_0
6872
// CHECK-NEXT: {{%[0-9]+}} = OpAccessChain %_ptr_Uniform_v4int [[y]] %int_0
6973
int4 c = y.a;
7074

71-
// CHECK: OpLine [[file]] 76 3
75+
// CHECK: OpLine [[file]] 80 3
7276
// CHECK-NEXT: [[z:%[0-9]+]] = OpLoad %type_2d_image %z
7377
// CHECK-NEXT: [[z_0:%[0-9]+]] = OpImageRead %v4int [[z]] {{%[0-9]+}} None
7478
// CHECK-NEXT: [[z_1:%[0-9]+]] = OpVectorShuffle %v3int [[z_0]] [[z_0]] 0 1 2
7579
// CHECK-NEXT: {{%[0-9]+}} = OpCompositeInsert %v3int %int_16 [[z_1]] 0
7680
z[uint2(2, 3)].x = 16;
7781

78-
// CHECK: OpLine [[file]] 82 3
82+
// CHECK: OpLine [[file]] 86 3
7983
// CHECK-NEXT: OpLoad %mat2v2float %b
80-
// CHECK: OpLine [[file]] 82 4
84+
// CHECK: OpLine [[file]] 86 4
8185
// CHECK-NEXT: OpFSub %v2float
8286
b--;
8387

8488
int2x2 d;
85-
// CHECK: OpLine [[file]] 91 8
89+
// CHECK: OpLine [[file]] 95 8
8690
// CHECK-NEXT: OpLoad %mat2v2float %b
87-
// CHECK-NEXT: OpLine [[file]] 91 3
91+
// CHECK-NEXT: OpLine [[file]] 95 3
8892
// CHECK-NEXT: OpCompositeExtract %v2float
89-
// CHECK: OpLine [[file]] 91 11
93+
// CHECK: OpLine [[file]] 95 11
9094
// CHECK: OpStore %d
9195
modf(b, d);
9296

93-
// CHECK: OpLine [[file]] 95 7
97+
// CHECK: OpLine [[file]] 99 7
9498
// CHECK-NEXT: OpFunctionCall %void %S_inc %foo
9599
foo.inc();
96100

97-
// CHECK: OpLine [[file]] 99 10
101+
// CHECK: OpLine [[file]] 103 10
98102
// CHECK-NEXT: OpFunctionCall %void %S_inc %temp_var_S
99103
getS().inc();
100104

101-
// CHECK: OpLine [[file]] 105 19
105+
// CHECK: OpLine [[file]] 109 19
102106
// CHECK-NEXT: OpLoad %init %bar
103-
// CHECK: OpLine [[file]] 105 12
107+
// CHECK: OpLine [[file]] 109 12
104108
// CHECK-NEXT: OpConvertFToS %int
105109
int4 e = {1, 2, bar};
106110

107-
// CHECK: OpLine [[file]] 111 15
111+
// CHECK: OpLine [[file]] 115 15
108112
// CHECK-NEXT: OpCompositeConstruct %v2float %float_1 %float_2
109-
// CHECK-NEXT: OpLine [[file]] 111 22
113+
// CHECK-NEXT: OpLine [[file]] 115 22
110114
// CHECK-NEXT: OpCompositeExtract %int
111115
b = float2x2(1, 2, bar);
112-
// CHECK: OpLine [[file]] 111 3
116+
// CHECK: OpLine [[file]] 115 3
113117
// CHECK-NEXT: OpStore %b
114118

115119
// TODO(jaebaek): Update InitListHandler to properly emit debug info.

tools/clang/test/CodeGenSPIRV/type.template.function.empty-struct-argument.hlsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %dxc -T ps_6_0 -E main -HV 2021 -fcgl %s -spirv | FileCheck %s
22

3+
// CHECK: [[const:%[0-9]+]] = OpConstantComposite %v4float %float_1 %float_2 %float_3 %float_4
34
struct A {};
45

56
template <typename T0, typename T1 = A>
@@ -9,7 +10,8 @@ struct B {
910
};
1011

1112
float4 main() : SV_Target {
12-
// CHECK: {{%[0-9]+}} = OpCompositeConstruct %v4float %float_1 %float_2 %float_3 %float_4
13+
// CHECK: [[A:%[0-9]+]] = OpCompositeConstruct %A
14+
// CHECK: {{%[0-9]+}} = OpCompositeConstruct %B [[const]] [[A]]
1315
B<float4> b = { float4(1, 2, 3, 4) };
1416
return b.m0;
1517
}

tools/clang/test/CodeGenSPIRV/var.init.matrix.mxn.hlsl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// TODO: optimize to generate constant composite for suitable initializers
44
// TODO: decompose matrix in initializer
55

6-
// CHECK: [[v3fc1:%[0-9]+]] = OpConstantComposite %v3float %float_1 %float_1 %float_1
7-
// CHECK-NEXT: [[v3fc0:%[0-9]+]] = OpConstantComposite %v3float %float_0 %float_0 %float_0
6+
// CHECK-DAG: [[v3fc0:%[0-9]+]] = OpConstantComposite %v3float %float_0 %float_0 %float_0
7+
// CHECK-DAG: [[f2_1_2:%[0-9]+]] = OpConstantComposite %v2float %float_1 %float_2
8+
// CHECK-DAG: [[i2_1_2:%[0-9]+]] = OpConstantComposite %v2int %int_1 %int_2
9+
// CHECK-DAG: [[v3fc1:%[0-9]+]] = OpConstantComposite %v3float %float_1 %float_1 %float_1
810

911
void main() {
1012
// CHECK-LABEL: %bb_entry = OpLabel
@@ -58,7 +60,9 @@ void main() {
5860
// CHECK-NEXT: [[ce05:%[0-9]+]] = OpCompositeExtract %float [[vec2a]] 0
5961
// CHECK-NEXT: [[ce06:%[0-9]+]] = OpCompositeExtract %float [[vec2a]] 1
6062
// CHECK-NEXT: [[cc15:%[0-9]+]] = OpCompositeConstruct %v4float [[ce02]] [[ce03]] [[ce04]] [[ce05]]
61-
// CHECK-NEXT: [[cc16:%[0-9]+]] = OpCompositeConstruct %v4float [[ce06]] %float_1 %float_2 %float_3
63+
// CHECK-NEXT: [[f_1:%[0-9]+]] = OpCompositeExtract %float [[f2_1_2]] 0
64+
// CHECK-NEXT: [[f_2:%[0-9]+]] = OpCompositeExtract %float [[f2_1_2]] 1
65+
// CHECK-NEXT: [[cc16:%[0-9]+]] = OpCompositeConstruct %v4float [[ce06]] [[f_1]] [[f_2]] %float_3
6266
// CHECK-NEXT: [[cc17:%[0-9]+]] = OpCompositeConstruct %mat4v4float [[cc14]] [[cc15]] [[cc16]] [[vec4]]
6367
// CHECK-NEXT: OpStore %mat5 [[cc17]]
6468
float4x4 mat5 = {scalar, vec1, vec2, // [0]
@@ -193,7 +197,9 @@ void main() {
193197
// CHECK-NEXT: [[ce05_0:%[0-9]+]] = OpCompositeExtract %int [[vec2a_0]] 0
194198
// CHECK-NEXT: [[ce06_0:%[0-9]+]] = OpCompositeExtract %int [[vec2a_0]] 1
195199
// CHECK-NEXT: [[cc15_0:%[0-9]+]] = OpCompositeConstruct %v4int [[ce02_0]] [[ce03_0]] [[ce04_0]] [[ce05_0]]
196-
// CHECK-NEXT: [[cc16_0:%[0-9]+]] = OpCompositeConstruct %v4int [[ce06_0]] %int_1 %int_2 %int_3
200+
// CHECK-NEXT: [[i_1:%[0-9]+]] = OpCompositeExtract %int [[i2_1_2]] 0
201+
// CHECK-NEXT: [[i_2:%[0-9]+]] = OpCompositeExtract %int [[i2_1_2]] 1
202+
// CHECK-NEXT: [[cc16_0:%[0-9]+]] = OpCompositeConstruct %v4int [[ce06_0]] [[i_1]] [[i_2]] %int_3
197203
// CHECK-NEXT: [[cc17_0:%[0-9]+]] = OpCompositeConstruct %_arr_v4int_uint_4 [[cc14_0]] [[cc15_0]] [[cc16_0]] [[vec4_0]]
198204
// CHECK-NEXT: OpStore %imat5 [[cc17_0]]
199205
int4x4 imat5 = {intScalar, intVec1, intVec2, // [0]

0 commit comments

Comments
 (0)