diff --git a/src/tint/transform/remove_phonies.cc b/src/tint/transform/remove_phonies.cc index b6d877da04..e5aecd2940 100644 --- a/src/tint/transform/remove_phonies.cc +++ b/src/tint/transform/remove_phonies.cc @@ -88,10 +88,22 @@ Transform::ApplyResult RemovePhonies::Apply(const Program* src, const DataMap&, } if (side_effects.size() == 1) { - if (auto* call = side_effects[0]->As()) { + if (auto* call_expr = side_effects[0]->As()) { // Phony assignment with single call side effect. - // Replace phony assignment with call. - ctx.Replace(stmt, [&, call] { return b.CallStmt(ctx.Clone(call)); }); + auto* call = sem.Get(call_expr)->Unwrap()->As(); + if (call->Target()->MustUse()) { + // Replace phony assignment assignment to uniquely named let. + ctx.Replace(stmt, [&, call_expr] { // + auto name = b.Symbols().New("tint_phony"); + auto* rhs = ctx.Clone(call_expr); + return b.Decl(b.Let(name, rhs)); + }); + } else { + // Replace phony assignment with call statement. + ctx.Replace(stmt, [&, call_expr] { // + return b.CallStmt(ctx.Clone(call_expr)); + }); + } return; } } diff --git a/src/tint/transform/remove_phonies_test.cc b/src/tint/transform/remove_phonies_test.cc index dc91b5a951..60252a1902 100644 --- a/src/tint/transform/remove_phonies_test.cc +++ b/src/tint/transform/remove_phonies_test.cc @@ -131,6 +131,60 @@ fn f() { EXPECT_EQ(expect, str(got)); } +TEST_F(RemovePhoniesTest, SingleSideEffectsMustUse) { + auto* src = R"( +@must_use +fn neg(a : i32) -> i32 { + return -(a); +} + +@must_use +fn add(a : i32, b : i32) -> i32 { + return (a + b); +} + +fn f() { + let tint_phony = 42; + _ = neg(1); + _ = 100 + add(2, 3) + 200; + _ = add(neg(4), neg(5)) + 6; + _ = u32(neg(6)); + _ = f32(add(7, 8)); + _ = vec2(f32(neg(9))); + _ = vec3(1, neg(10), 3); + _ = mat2x2(1.0, f32(add(11, 12)), 3.0, 4.0); +} +)"; + + auto* expect = R"( +@must_use +fn neg(a : i32) -> i32 { + return -(a); +} + +@must_use +fn add(a : i32, b : i32) -> i32 { + return (a + b); +} + +fn f() { + let tint_phony = 42; + let tint_phony_1 = neg(1); + let tint_phony_2 = add(2, 3); + let tint_phony_3 = add(neg(4), neg(5)); + let tint_phony_4 = neg(6); + let tint_phony_5 = add(7, 8); + let tint_phony_6 = neg(9); + let tint_phony_7 = neg(10); + let tint_phony_8 = add(11, 12); +} +)"; + + auto got = Run(src); + + EXPECT_EQ(expect, str(got)); +} + TEST_F(RemovePhoniesTest, SingleSideEffects_OutOfOrder) { auto* src = R"( fn f() { @@ -185,6 +239,7 @@ fn neg(a : i32) -> i32 { return -(a); } +@must_use fn add(a : i32, b : i32) -> i32 { return (a + b); } @@ -206,6 +261,7 @@ fn neg(a : i32) -> i32 { return -(a); } +@must_use fn add(a : i32, b : i32) -> i32 { return (a + b); }