Skip to content

Commit 34a2d65

Browse files
dj2Dawn LUCI CQ
authored andcommitted
[hlsl][ir] Skip promoting lets to lets
In the `PromoteInitializers` transform, if a let is used as a value, we may determine that the value needs to be hoisted to a new let. But, this ends up just creating a let of a let. Skip creating the duplicate let in this case. Bug: 369450791 Change-Id: I45178ec0029b13019314ab189df8073781a9dd9b Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/211817 Reviewed-by: James Price <[email protected]> Commit-Queue: dan sinclair <[email protected]> Reviewed-by: Antonio Maiorano <[email protected]>
1 parent e1fd66b commit 34a2d65

File tree

4,669 files changed

+10477
-19895
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,669 files changed

+10477
-19895
lines changed

src/tint/lang/hlsl/writer/access_test.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,8 +1903,7 @@ void v_1(uint offset, float3 obj[5]) {
19031903
}
19041904
19051905
void foo() {
1906-
float3 v_4[5] = (float3[5])0;
1907-
float3 ary[5] = v_4;
1906+
float3 ary[5] = (float3[5])0;
19081907
v_1(0u, ary);
19091908
}
19101909
@@ -2061,8 +2060,7 @@ void v_4(uint offset, SB obj) {
20612060
}
20622061
20632062
void foo() {
2064-
SB v_6 = (SB)0;
2065-
SB s = v_6;
2063+
SB s = (SB)0;
20662064
v_4(0u, s);
20672065
}
20682066
@@ -2157,8 +2155,7 @@ void v_9(uint offset, SB obj) {
21572155
}
21582156
21592157
void foo() {
2160-
SB v_11 = (SB)0;
2161-
SB s = v_11;
2158+
SB s = (SB)0;
21622159
v_9(0u, s);
21632160
}
21642161

src/tint/lang/hlsl/writer/function_test.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,13 @@ void frag_main_inner(Interface inputs) {
309309
310310
vert_main_outputs vert_main() {
311311
Interface v_1 = vert_main_inner();
312-
Interface v_2 = v_1;
313-
Interface v_3 = v_1;
314-
Interface v_4 = v_1;
315-
vert_main_outputs v_5 = {v_3.col1, v_4.col2, v_2.pos};
316-
return v_5;
312+
vert_main_outputs v_2 = {v_1.col1, v_1.col2, v_1.pos};
313+
return v_2;
317314
}
318315
319316
void frag_main(frag_main_inputs inputs) {
320-
Interface v_6 = {float4(inputs.Interface_pos.xyz, (1.0f / inputs.Interface_pos[3u])), inputs.Interface_col1, inputs.Interface_col2};
321-
frag_main_inner(v_6);
317+
Interface v_3 = {float4(inputs.Interface_pos.xyz, (1.0f / inputs.Interface_pos[3u])), inputs.Interface_col1, inputs.Interface_col2};
318+
frag_main_inner(v_3);
322319
}
323320
324321
)");

src/tint/lang/hlsl/writer/raise/promote_initializers.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ struct State {
9696
Vector<core::ir::Construct*, 4> const_worklist;
9797
for (auto& item : worklist) {
9898
if (auto* res = As<core::ir::InstructionResult>(item.val)) {
99-
PutInLet(item.inst, item.index, res);
99+
// If the value isn't already a `let`, put it into a `let`.
100+
if (!res->Instruction()->Is<core::ir::Let>()) {
101+
PutInLet(item.inst, item.index, res);
102+
}
100103
} else if (auto* val = As<core::ir::Constant>(item.val)) {
101104
auto* let = PutInLet(item.inst, item.index, val);
102105
auto ret = HoistModuleScopeLetToConstruct(is_root_block, item.inst, let, val);

src/tint/lang/hlsl/writer/raise/promote_initializers_test.cc

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,5 +753,115 @@ TEST_F(HlslWriterPromoteInitializersTest, DuplicateConstant) {
753753
EXPECT_EQ(expect, str());
754754
}
755755

756+
// TODO(dsinclair): Fixup duplicate let creation
757+
TEST_F(HlslWriterPromoteInitializersTest, DISABLED_DuplicateAccess) {
758+
capabilities = core::ir::Capabilities{core::ir::Capability::kAllowModuleScopeLets};
759+
760+
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
761+
b.Append(func->Block(), [&] {
762+
auto* ary = b.Construct(ty.array(ty.f32(), 8));
763+
b.Access(ty.f32(), ary, 0_u);
764+
b.Access(ty.f32(), ary, 1_u);
765+
b.Access(ty.f32(), ary, 2_u);
766+
b.Return(func);
767+
});
768+
769+
auto* src = R"(
770+
%foo = @fragment func():void {
771+
$B1: {
772+
%2:array<f32, 8> = construct
773+
%3:f32 = access %2, 0u
774+
%4:f32 = access %2, 1u
775+
%5:f32 = access %2, 2u
776+
ret
777+
}
778+
}
779+
)";
780+
EXPECT_EQ(src, str());
781+
782+
auto* expect = R"(
783+
%foo = @fragment func():void {
784+
$B1: {
785+
%2:array<f32, 8> = construct
786+
%3:array<f32, 8> = let %2
787+
%4:f32 = access %3, 0u
788+
%5:f32 = access %3, 1u
789+
%6:f32 = access %3, 2u
790+
ret
791+
}
792+
}
793+
)";
794+
795+
Run(PromoteInitializers);
796+
EXPECT_EQ(expect, str());
797+
}
798+
799+
TEST_F(HlslWriterPromoteInitializersTest, LetOfLet) {
800+
capabilities = core::ir::Capabilities{core::ir::Capability::kAllowModuleScopeLets};
801+
802+
auto* str_ty = ty.Struct(mod.symbols.New("S"), {
803+
{mod.symbols.New("a"), ty.vec4<i32>()},
804+
});
805+
806+
auto* inner = b.Function("inner", str_ty);
807+
b.Append(inner->Block(),
808+
[&] { b.Return(inner, b.Construct(str_ty, b.Splat(ty.vec4<i32>(), 1_i))); });
809+
810+
auto* func = b.Function("foo", ty.void_(), core::ir::Function::PipelineStage::kFragment);
811+
b.Append(func->Block(), [&] {
812+
auto* in = b.Call(inner);
813+
auto* l = b.Let("a", in);
814+
b.Access(ty.vec4<i32>(), l, 0_u);
815+
b.Return(func);
816+
});
817+
818+
auto* src = R"(
819+
S = struct @align(16) {
820+
a:vec4<i32> @offset(0)
821+
}
822+
823+
%inner = func():S {
824+
$B1: {
825+
%2:S = construct vec4<i32>(1i)
826+
ret %2
827+
}
828+
}
829+
%foo = @fragment func():void {
830+
$B2: {
831+
%4:S = call %inner
832+
%a:S = let %4
833+
%6:vec4<i32> = access %a, 0u
834+
ret
835+
}
836+
}
837+
)";
838+
EXPECT_EQ(src, str());
839+
840+
auto* expect = R"(
841+
S = struct @align(16) {
842+
a:vec4<i32> @offset(0)
843+
}
844+
845+
%inner = func():S {
846+
$B1: {
847+
%2:S = construct vec4<i32>(1i)
848+
%3:S = let %2
849+
ret %3
850+
}
851+
}
852+
%foo = @fragment func():void {
853+
$B2: {
854+
%5:S = call %inner
855+
%a:S = let %5
856+
%7:vec4<i32> = access %a, 0u
857+
ret
858+
}
859+
}
860+
)";
861+
862+
Run(PromoteInitializers);
863+
EXPECT_EQ(expect, str());
864+
}
865+
756866
} // namespace
757867
} // namespace tint::hlsl::writer::raise

test/tint/array/assign_to_function_var.wgsl.expected.ir.dxc.hlsl

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,45 @@ void foo(int4 src_param[4]) {
7676
tint_symbol = src_param;
7777
int4 v_11[4] = ret_arr();
7878
tint_symbol = v_11;
79-
int4 v_12[4] = (int4[4])0;
80-
int4 src_let[4] = v_12;
79+
int4 src_let[4] = (int4[4])0;
8180
tint_symbol = src_let;
82-
int4 v_13[4] = src_function;
81+
int4 v_12[4] = src_function;
82+
tint_symbol = v_12;
83+
int4 v_13[4] = src_private;
8384
tint_symbol = v_13;
84-
int4 v_14[4] = src_private;
85+
int4 v_14[4] = src_workgroup;
8586
tint_symbol = v_14;
86-
int4 v_15[4] = src_workgroup;
87-
tint_symbol = v_15;
88-
S v_16 = ret_struct_arr();
89-
int4 v_17[4] = v_16.arr;
87+
S v_15 = ret_struct_arr();
88+
int4 v_16[4] = v_15.arr;
89+
tint_symbol = v_16;
90+
int4 v_17[4] = v_6(0u);
9091
tint_symbol = v_17;
91-
int4 v_18[4] = v_6(0u);
92+
int4 v_18[4] = v_2(0u);
9293
tint_symbol = v_18;
93-
int4 v_19[4] = v_2(0u);
94-
tint_symbol = v_19;
9594
int dst_nested[4][3][2] = (int[4][3][2])0;
9695
int src_nested[4][3][2] = (int[4][3][2])0;
97-
int v_20[4][3][2] = src_nested;
98-
dst_nested = v_20;
96+
int v_19[4][3][2] = src_nested;
97+
dst_nested = v_19;
9998
}
10099

101100
void main_inner(uint tint_local_index) {
102101
{
103-
uint v_21 = 0u;
104-
v_21 = tint_local_index;
102+
uint v_20 = 0u;
103+
v_20 = tint_local_index;
105104
while(true) {
106-
uint v_22 = v_21;
107-
if ((v_22 >= 4u)) {
105+
uint v_21 = v_20;
106+
if ((v_21 >= 4u)) {
108107
break;
109108
}
110-
src_workgroup[v_22] = (int(0)).xxxx;
109+
src_workgroup[v_21] = (int(0)).xxxx;
111110
{
112-
v_21 = (v_22 + 1u);
111+
v_20 = (v_21 + 1u);
113112
}
114113
continue;
115114
}
116115
}
117116
GroupMemoryBarrierWithGroupSync();
118-
int4 v_23[4] = (int4[4])0;
119-
int4 val[4] = v_23;
117+
int4 val[4] = (int4[4])0;
120118
foo(val);
121119
}
122120

test/tint/array/assign_to_function_var.wgsl.expected.ir.fxc.hlsl

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,45 @@ void foo(int4 src_param[4]) {
7676
tint_symbol = src_param;
7777
int4 v_11[4] = ret_arr();
7878
tint_symbol = v_11;
79-
int4 v_12[4] = (int4[4])0;
80-
int4 src_let[4] = v_12;
79+
int4 src_let[4] = (int4[4])0;
8180
tint_symbol = src_let;
82-
int4 v_13[4] = src_function;
81+
int4 v_12[4] = src_function;
82+
tint_symbol = v_12;
83+
int4 v_13[4] = src_private;
8384
tint_symbol = v_13;
84-
int4 v_14[4] = src_private;
85+
int4 v_14[4] = src_workgroup;
8586
tint_symbol = v_14;
86-
int4 v_15[4] = src_workgroup;
87-
tint_symbol = v_15;
88-
S v_16 = ret_struct_arr();
89-
int4 v_17[4] = v_16.arr;
87+
S v_15 = ret_struct_arr();
88+
int4 v_16[4] = v_15.arr;
89+
tint_symbol = v_16;
90+
int4 v_17[4] = v_6(0u);
9091
tint_symbol = v_17;
91-
int4 v_18[4] = v_6(0u);
92+
int4 v_18[4] = v_2(0u);
9293
tint_symbol = v_18;
93-
int4 v_19[4] = v_2(0u);
94-
tint_symbol = v_19;
9594
int dst_nested[4][3][2] = (int[4][3][2])0;
9695
int src_nested[4][3][2] = (int[4][3][2])0;
97-
int v_20[4][3][2] = src_nested;
98-
dst_nested = v_20;
96+
int v_19[4][3][2] = src_nested;
97+
dst_nested = v_19;
9998
}
10099

101100
void main_inner(uint tint_local_index) {
102101
{
103-
uint v_21 = 0u;
104-
v_21 = tint_local_index;
102+
uint v_20 = 0u;
103+
v_20 = tint_local_index;
105104
while(true) {
106-
uint v_22 = v_21;
107-
if ((v_22 >= 4u)) {
105+
uint v_21 = v_20;
106+
if ((v_21 >= 4u)) {
108107
break;
109108
}
110-
src_workgroup[v_22] = (int(0)).xxxx;
109+
src_workgroup[v_21] = (int(0)).xxxx;
111110
{
112-
v_21 = (v_22 + 1u);
111+
v_20 = (v_21 + 1u);
113112
}
114113
continue;
115114
}
116115
}
117116
GroupMemoryBarrierWithGroupSync();
118-
int4 v_23[4] = (int4[4])0;
119-
int4 val[4] = v_23;
117+
int4 val[4] = (int4[4])0;
120118
foo(val);
121119
}
122120

test/tint/array/assign_to_private_var.wgsl.expected.ir.dxc.hlsl

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,46 +77,44 @@ void foo(int4 src_param[4]) {
7777
tint_symbol = src_param;
7878
int4 v_11[4] = ret_arr();
7979
tint_symbol = v_11;
80-
int4 v_12[4] = (int4[4])0;
81-
int4 src_let[4] = v_12;
80+
int4 src_let[4] = (int4[4])0;
8281
tint_symbol = src_let;
83-
int4 v_13[4] = src_function;
82+
int4 v_12[4] = src_function;
83+
tint_symbol = v_12;
84+
int4 v_13[4] = src_private;
8485
tint_symbol = v_13;
85-
int4 v_14[4] = src_private;
86+
int4 v_14[4] = src_workgroup;
8687
tint_symbol = v_14;
87-
int4 v_15[4] = src_workgroup;
88-
tint_symbol = v_15;
89-
S v_16 = ret_struct_arr();
90-
int4 v_17[4] = v_16.arr;
88+
S v_15 = ret_struct_arr();
89+
int4 v_16[4] = v_15.arr;
90+
tint_symbol = v_16;
91+
int4 v_17[4] = v_6(0u);
9192
tint_symbol = v_17;
92-
int4 v_18[4] = v_6(0u);
93+
int4 v_18[4] = v_2(0u);
9394
tint_symbol = v_18;
94-
int4 v_19[4] = v_2(0u);
95-
tint_symbol = v_19;
9695
int src_nested[4][3][2] = (int[4][3][2])0;
97-
int v_20[4][3][2] = src_nested;
98-
dst_nested = v_20;
96+
int v_19[4][3][2] = src_nested;
97+
dst_nested = v_19;
9998
}
10099

101100
void main_inner(uint tint_local_index) {
102101
{
103-
uint v_21 = 0u;
104-
v_21 = tint_local_index;
102+
uint v_20 = 0u;
103+
v_20 = tint_local_index;
105104
while(true) {
106-
uint v_22 = v_21;
107-
if ((v_22 >= 4u)) {
105+
uint v_21 = v_20;
106+
if ((v_21 >= 4u)) {
108107
break;
109108
}
110-
src_workgroup[v_22] = (int(0)).xxxx;
109+
src_workgroup[v_21] = (int(0)).xxxx;
111110
{
112-
v_21 = (v_22 + 1u);
111+
v_20 = (v_21 + 1u);
113112
}
114113
continue;
115114
}
116115
}
117116
GroupMemoryBarrierWithGroupSync();
118-
int4 v_23[4] = (int4[4])0;
119-
int4 a[4] = v_23;
117+
int4 a[4] = (int4[4])0;
120118
foo(a);
121119
}
122120

0 commit comments

Comments
 (0)