diff --git a/src/program_builder.h b/src/program_builder.h index fd3e685893..d78c0779f3 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -1643,11 +1643,11 @@ class ProgramBuilder { const ast::PhonyExpression* Phony() { return create(); } /// @param expr the expression to ignore - /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is - /// passed the single `expr` argument + /// @returns a `ast::AssignmentStatement` that assigns 'expr' to the phony + /// (underscore) variable. template - const ast::CallStatement* Ignore(EXPR&& expr) { - return create(Call("ignore", Expr(expr))); + const ast::AssignmentStatement* Ignore(EXPR&& expr) { + return create(Phony(), Expr(expr)); } /// @param lhs the left hand argument to the addition operation diff --git a/src/writer/msl/generator_impl_loop_test.cc b/src/writer/msl/generator_impl_loop_test.cc index d96ac67af1..7cbf492446 100644 --- a/src/writer/msl/generator_impl_loop_test.cc +++ b/src/writer/msl/generator_impl_loop_test.cc @@ -190,28 +190,33 @@ TEST_F(MslGeneratorImplTest, Emit_ForLoopWithSimpleInit) { } TEST_F(MslGeneratorImplTest, Emit_ForLoopWithMultiStmtInit) { + // fn f(i : i32) {} + // // var a : atomic; - // for({ignore(1); ignore(2);}; ; ) { + // for({f(1); f(2);}; ; ) { // return; // } + Func("f", {Param("i", ty.i32())}, ty.void_(), {}); + auto f = [&](auto&& expr) { return CallStmt(Call("f", expr)); }; + Func("a_statement", {}, ty.void_(), {}); Global("a", ty.atomic(), ast::StorageClass::kWorkgroup); - auto* multi_stmt = Block(Ignore(1), Ignore(2)); - auto* f = + auto* multi_stmt = Block(f(1), f(2)); + auto* loop = For(multi_stmt, nullptr, nullptr, Block(CallStmt(Call("a_statement")))); - WrapInFunction(f); + WrapInFunction(loop); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitStatement(f)) << gen.error(); + ASSERT_TRUE(gen.EmitStatement(loop)) << gen.error(); EXPECT_EQ(gen.result(), R"( { { - (void) 1; - (void) 2; + f(1); + f(2); } for(; ; ) { a_statement(); @@ -267,29 +272,34 @@ TEST_F(MslGeneratorImplTest, Emit_ForLoopWithSimpleCont) { } TEST_F(MslGeneratorImplTest, Emit_ForLoopWithMultiStmtCont) { + // fn f(i : i32) {} + // // var a : atomic; - // for(; ; { ignore(1); ignore(2); }) { + // for(; ; { f(1); f(2); }) { // return; // } + Func("f", {Param("i", ty.i32())}, ty.void_(), {}); + auto f = [&](auto&& expr) { return CallStmt(Call("f", expr)); }; + Func("a_statement", {}, ty.void_(), {}); Global("a", ty.atomic(), ast::StorageClass::kWorkgroup); - auto* multi_stmt = Block(Ignore(1), Ignore(2)); - auto* f = + auto* multi_stmt = Block(f(1), f(2)); + auto* loop = For(nullptr, nullptr, multi_stmt, Block(CallStmt(Call("a_statement")))); - WrapInFunction(f); + WrapInFunction(loop); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitStatement(f)) << gen.error(); + ASSERT_TRUE(gen.EmitStatement(loop)) << gen.error(); EXPECT_EQ(gen.result(), R"( while (true) { a_statement(); { - (void) 1; - (void) 2; + f(1); + f(2); } } )"); @@ -320,36 +330,41 @@ TEST_F(MslGeneratorImplTest, Emit_ForLoopWithSimpleInitCondCont) { } TEST_F(MslGeneratorImplTest, Emit_ForLoopWithMultiStmtInitCondCont) { + // fn f(i : i32) {} + // // var a : atomic; - // for({ ignore(1); ignore(2); }; true; { ignore(3); ignore(4); }) { + // for({ f(1); f(2); }; true; { f(3); f(4); }) { // return; // } + Func("f", {Param("i", ty.i32())}, ty.void_(), {}); + auto f = [&](auto&& expr) { return CallStmt(Call("f", expr)); }; + Func("a_statement", {}, ty.void_(), {}); Global("a", ty.atomic(), ast::StorageClass::kWorkgroup); - auto* multi_stmt_a = Block(Ignore(1), Ignore(2)); - auto* multi_stmt_b = Block(Ignore(3), Ignore(4)); - auto* f = For(multi_stmt_a, Expr(true), multi_stmt_b, - Block(CallStmt(Call("a_statement")))); - WrapInFunction(f); + auto* multi_stmt_a = Block(f(1), f(2)); + auto* multi_stmt_b = Block(f(3), f(4)); + auto* loop = For(multi_stmt_a, Expr(true), multi_stmt_b, + Block(CallStmt(Call("a_statement")))); + WrapInFunction(loop); GeneratorImpl& gen = Build(); gen.increment_indent(); - ASSERT_TRUE(gen.EmitStatement(f)) << gen.error(); + ASSERT_TRUE(gen.EmitStatement(loop)) << gen.error(); EXPECT_EQ(gen.result(), R"( { { - (void) 1; - (void) 2; + f(1); + f(2); } while (true) { if (!(true)) { break; } a_statement(); { - (void) 3; - (void) 4; + f(3); + f(4); } } } diff --git a/src/writer/wgsl/generator_impl_loop_test.cc b/src/writer/wgsl/generator_impl_loop_test.cc index be5651f86b..d0b2e75bb2 100644 --- a/src/writer/wgsl/generator_impl_loop_test.cc +++ b/src/writer/wgsl/generator_impl_loop_test.cc @@ -79,8 +79,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithMultiStmtInit) { ASSERT_TRUE(gen.EmitStatement(f)) << gen.error(); EXPECT_EQ(gen.result(), R"( for({ - ignore(1); - ignore(2); + _ = 1; + _ = 2; }; ; ) { return; } @@ -143,8 +143,8 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithMultiStmtCont) { ASSERT_TRUE(gen.EmitStatement(f)) << gen.error(); EXPECT_EQ(gen.result(), R"( for(; ; { - ignore(1); - ignore(2); + _ = 1; + _ = 2; }) { return; } @@ -188,11 +188,11 @@ TEST_F(WgslGeneratorImplTest, Emit_ForLoopWithMultiStmtInitCondCont) { ASSERT_TRUE(gen.EmitStatement(f)) << gen.error(); EXPECT_EQ(gen.result(), R"( for({ - ignore(1); - ignore(2); + _ = 1; + _ = 2; }; true; { - ignore(3); - ignore(4); + _ = 3; + _ = 4; }) { return; }