| // Copyright 2023 The Dawn & Tint Authors |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // 1. Redistributions of source code must retain the above copyright notice, this |
| // list of conditions and the following disclaimer. |
| // |
| // 2. Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // |
| // 3. Neither the name of the copyright holder nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| #include "src/tint/lang/spirv/writer/common/helper_test.h" |
| |
| using namespace tint::core::number_suffixes; // NOLINT |
| |
| namespace tint::spirv::writer { |
| namespace { |
| |
| TEST_F(SpirvWriterTest, Loop_BreakIf) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| b.Append(loop->Body(), [&] { // |
| b.Continue(loop); |
| |
| b.Append(loop->Continuing(), [&] { // |
| b.BreakIf(loop, true); |
| }); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %6 |
| %6 = OpLabel |
| OpBranchConditional %true %8 %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| // Test that we still emit the continuing block with a back-edge, even when it is unreachable. |
| TEST_F(SpirvWriterTest, Loop_UnconditionalBreakInBody) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| b.Append(loop->Body(), [&] { // |
| b.ExitLoop(loop); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %8 |
| %6 = OpLabel |
| OpBranch %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_ConditionalBreakInBody) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| b.Append(loop->Body(), [&] { |
| auto* cond_break = b.If(true); |
| b.Append(cond_break->True(), [&] { // |
| b.ExitLoop(loop); |
| }); |
| b.Append(cond_break->False(), [&] { // |
| b.ExitIf(cond_break); |
| }); |
| b.Continue(loop); |
| |
| b.Append(loop->Continuing(), [&] { // |
| b.NextIteration(loop); |
| }); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpSelectionMerge %9 None |
| OpBranchConditional %true %10 %9 |
| %10 = OpLabel |
| OpBranch %8 |
| %9 = OpLabel |
| OpBranch %6 |
| %6 = OpLabel |
| OpBranch %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_ConditionalContinueInBody) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| b.Append(loop->Body(), [&] { |
| auto* cond_break = b.If(true); |
| b.Append(cond_break->True(), [&] { // |
| b.Continue(loop); |
| }); |
| b.Append(cond_break->False(), [&] { // |
| b.ExitIf(cond_break); |
| }); |
| b.ExitLoop(loop); |
| |
| b.Append(loop->Continuing(), [&] { // |
| b.NextIteration(loop); |
| }); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpSelectionMerge %9 None |
| OpBranchConditional %true %10 %9 |
| %10 = OpLabel |
| OpBranch %6 |
| %9 = OpLabel |
| OpBranch %8 |
| %6 = OpLabel |
| OpBranch %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| // Test that we still emit the continuing block with a back-edge, and the merge block, even when |
| // they are unreachable. |
| TEST_F(SpirvWriterTest, Loop_UnconditionalReturnInBody) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| b.Append(loop->Body(), [&] { // |
| b.Return(func); |
| }); |
| b.Unreachable(); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %8 |
| %6 = OpLabel |
| OpBranch %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_UseResultFromBodyInContinuing) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| b.Append(loop->Body(), [&] { |
| auto* result = b.Equal(ty.bool_(), 1_i, 2_i); |
| b.Continue(loop, result); |
| |
| b.Append(loop->Continuing(), [&] { // |
| b.BreakIf(loop, result); |
| }); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| %9 = OpIEqual %bool %int_1 %int_2 |
| OpBranch %6 |
| %6 = OpLabel |
| OpBranchConditional %9 %8 %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_NestedLoopInBody) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* outer_loop = b.Loop(); |
| b.Append(outer_loop->Body(), [&] { |
| auto* inner_loop = b.Loop(); |
| b.Append(inner_loop->Body(), [&] { |
| b.ExitLoop(inner_loop); |
| |
| b.Append(inner_loop->Continuing(), [&] { // |
| b.NextIteration(inner_loop); |
| }); |
| }); |
| b.Continue(outer_loop); |
| |
| b.Append(outer_loop->Continuing(), |
| [&] { // |
| b.BreakIf(outer_loop, true); |
| }); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %11 |
| %11 = OpLabel |
| OpLoopMerge %12 %10 None |
| OpBranch %9 |
| %9 = OpLabel |
| OpBranch %12 |
| %10 = OpLabel |
| OpBranch %11 |
| %12 = OpLabel |
| OpBranch %6 |
| %6 = OpLabel |
| OpBranchConditional %true %8 %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_NestedLoopInContinuing) { |
| auto* func = b.Function("foo", ty.void_()); |
| b.Append(func->Block(), [&] { |
| auto* outer_loop = b.Loop(); |
| b.Append(outer_loop->Body(), [&] { |
| b.Continue(outer_loop); |
| |
| b.Append(outer_loop->Continuing(), [&] { |
| auto* inner_loop = b.Loop(); |
| b.Append(inner_loop->Body(), [&] { |
| b.Continue(inner_loop); |
| |
| b.Append(inner_loop->Continuing(), [&] { // |
| b.BreakIf(inner_loop, true); |
| }); |
| }); |
| b.BreakIf(outer_loop, true); |
| }); |
| }); |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %6 |
| %6 = OpLabel |
| OpBranch %11 |
| %11 = OpLabel |
| OpLoopMerge %12 %10 None |
| OpBranch %9 |
| %9 = OpLabel |
| OpBranch %10 |
| %10 = OpLabel |
| OpBranchConditional %true %12 %11 |
| %12 = OpLabel |
| OpBranchConditional %true %8 %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_Phi_SingleValue) { |
| auto* func = b.Function("foo", ty.void_()); |
| |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| |
| b.Append(loop->Initializer(), [&] { // |
| b.NextIteration(loop, 1_i); |
| }); |
| |
| auto* loop_param = b.BlockParam(ty.i32()); |
| loop->Body()->SetParams({loop_param}); |
| |
| b.Append(loop->Body(), [&] { |
| auto* inc = b.Add(ty.i32(), loop_param, 1_i); |
| b.Continue(loop, inc); |
| }); |
| |
| auto* cont_param = b.BlockParam(ty.i32()); |
| loop->Continuing()->SetParams({cont_param}); |
| b.Append(loop->Continuing(), [&] { |
| auto* cmp = b.GreaterThan(ty.bool_(), cont_param, 5_i); |
| b.BreakIf(loop, cmp, cont_param); |
| }); |
| |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %5 = OpLabel |
| OpBranch %8 |
| %8 = OpLabel |
| %11 = OpPhi %int %int_1 %5 %13 %7 |
| OpLoopMerge %9 %7 None |
| OpBranch %6 |
| %6 = OpLabel |
| %14 = OpIAdd %int %11 %int_1 |
| OpBranch %7 |
| %7 = OpLabel |
| %13 = OpPhi %int %14 %6 |
| %15 = OpSGreaterThan %bool %13 %int_5 |
| OpBranchConditional %15 %9 %8 |
| %9 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_Phi_MultipleValue) { |
| auto* func = b.Function("foo", ty.void_()); |
| |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| |
| b.Append(loop->Initializer(), [&] { // |
| b.NextIteration(loop, 1_i, false); |
| }); |
| |
| auto* loop_param_a = b.BlockParam(ty.i32()); |
| auto* loop_param_b = b.BlockParam(ty.bool_()); |
| loop->Body()->SetParams({loop_param_a, loop_param_b}); |
| |
| b.Append(loop->Body(), [&] { |
| auto* inc = b.Add(ty.i32(), loop_param_a, 1_i); |
| b.Continue(loop, inc, loop_param_b); |
| }); |
| |
| auto* cont_param_a = b.BlockParam(ty.i32()); |
| auto* cont_param_b = b.BlockParam(ty.bool_()); |
| loop->Continuing()->SetParams({cont_param_a, cont_param_b}); |
| b.Append(loop->Continuing(), [&] { |
| auto* cmp = b.GreaterThan(ty.bool_(), cont_param_a, 5_i); |
| auto* not_b = b.Not(ty.bool_(), cont_param_b); |
| b.BreakIf(loop, cmp, cont_param_a, not_b); |
| }); |
| |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %5 = OpLabel |
| OpBranch %8 |
| %8 = OpLabel |
| %11 = OpPhi %int %int_1 %5 %13 %7 |
| %15 = OpPhi %bool %false %5 %17 %7 |
| OpLoopMerge %9 %7 None |
| OpBranch %6 |
| %6 = OpLabel |
| %18 = OpIAdd %int %11 %int_1 |
| OpBranch %7 |
| %7 = OpLabel |
| %13 = OpPhi %int %18 %6 |
| %19 = OpPhi %bool %15 %6 |
| %20 = OpSGreaterThan %bool %13 %int_5 |
| %17 = OpLogicalEqual %bool %19 %false |
| OpBranchConditional %20 %9 %8 |
| %9 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_Phi_NestedIf) { |
| auto* func = b.Function("foo", ty.void_()); |
| |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| b.Append(loop->Initializer(), [&] { // |
| b.NextIteration(loop, 1_i); |
| }); |
| |
| auto* loop_param = b.BlockParam(ty.i32()); |
| loop->Body()->SetParams({loop_param}); |
| b.Append(loop->Body(), [&] { |
| auto* inner = b.If(true); |
| inner->SetResults(b.InstructionResult(ty.i32())); |
| b.Append(inner->True(), [&] { // |
| b.ExitIf(inner, 10_i); |
| }); |
| b.Append(inner->False(), [&] { // |
| b.ExitIf(inner, 20_i); |
| }); |
| b.Continue(loop, inner->Result(0)); |
| }); |
| |
| auto* cont_param = b.BlockParam(ty.i32()); |
| loop->Continuing()->SetParams({cont_param}); |
| b.Append(loop->Continuing(), [&] { |
| auto* cmp = b.GreaterThan(ty.bool_(), cont_param, 5_i); |
| b.BreakIf(loop, cmp, cont_param); |
| }); |
| |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %8 |
| %8 = OpLabel |
| %11 = OpPhi %int %int_1 %5 %13 %7 |
| OpLoopMerge %9 %7 None |
| OpBranch %6 |
| %6 = OpLabel |
| OpSelectionMerge %14 None |
| OpBranchConditional %true %15 %16 |
| %15 = OpLabel |
| OpBranch %14 |
| %16 = OpLabel |
| OpBranch %14 |
| %14 = OpLabel |
| %19 = OpPhi %int %int_10 %15 %int_20 %16 |
| OpBranch %7 |
| %7 = OpLabel |
| %13 = OpPhi %int %19 %14 |
| %22 = OpSGreaterThan %bool %13 %int_5 |
| OpBranchConditional %22 %9 %8 |
| %9 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_Phi_NestedLoop) { |
| auto* func = b.Function("foo", ty.void_()); |
| |
| b.Append(func->Block(), [&] { |
| auto* outer = b.Loop(); |
| b.Append(outer->Initializer(), [&] { // |
| b.NextIteration(outer, 1_i); |
| }); |
| |
| auto* outer_param = b.BlockParam(ty.i32()); |
| outer->Body()->SetParams({outer_param}); |
| b.Append(outer->Body(), [&] { |
| auto* inner = b.Loop(); |
| b.Append(inner->Initializer(), [&] { // |
| b.NextIteration(inner); |
| }); |
| b.Append(inner->Body(), [&] { // |
| b.Continue(inner); |
| }); |
| b.Append(inner->Continuing(), [&] { // |
| b.BreakIf(inner, true); |
| }); |
| |
| b.Continue(outer, outer_param); |
| }); |
| |
| auto* cont_param = b.BlockParam(ty.i32()); |
| outer->Continuing()->SetParams({cont_param}); |
| b.Append(outer->Continuing(), [&] { |
| auto* cmp = b.GreaterThan(ty.bool_(), cont_param, 5_i); |
| b.BreakIf(outer, cmp, cont_param); |
| }); |
| |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %8 |
| %8 = OpLabel |
| %11 = OpPhi %int %int_1 %5 %13 %7 |
| OpLoopMerge %9 %7 None |
| OpBranch %6 |
| %6 = OpLabel |
| OpBranch %14 |
| %14 = OpLabel |
| OpBranch %17 |
| %17 = OpLabel |
| OpLoopMerge %18 %16 None |
| OpBranch %15 |
| %15 = OpLabel |
| OpBranch %16 |
| %16 = OpLabel |
| OpBranchConditional %true %18 %17 |
| %18 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| %13 = OpPhi %int %11 %18 |
| %21 = OpSGreaterThan %bool %13 %int_5 |
| OpBranchConditional %21 %9 %8 |
| %9 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| TEST_F(SpirvWriterTest, Loop_Phi_NestedIfWithResultAndImplicitFalse_InContinuing) { |
| auto* func = b.Function("foo", ty.void_()); |
| |
| b.Append(func->Block(), [&] { |
| auto* loop = b.Loop(); |
| |
| b.Append(loop->Body(), [&] { // |
| b.Continue(loop); |
| }); |
| |
| b.Append(loop->Continuing(), [&] { |
| auto* if_ = b.If(true); |
| auto* cond = b.InstructionResult(ty.bool_()); |
| if_->SetResults(Vector{cond}); |
| b.Append(if_->True(), [&] { // |
| b.ExitIf(if_, true); |
| }); |
| b.BreakIf(loop, cond); |
| }); |
| |
| b.Return(func); |
| }); |
| |
| ASSERT_TRUE(Generate()) << Error() << output_; |
| EXPECT_INST("%15 = OpUndef %bool"); |
| EXPECT_INST(R"( |
| %4 = OpLabel |
| OpBranch %7 |
| %7 = OpLabel |
| OpLoopMerge %8 %6 None |
| OpBranch %5 |
| %5 = OpLabel |
| OpBranch %6 |
| %6 = OpLabel |
| OpSelectionMerge %9 None |
| OpBranchConditional %true %10 %11 |
| %10 = OpLabel |
| OpBranch %9 |
| %11 = OpLabel |
| OpBranch %9 |
| %9 = OpLabel |
| %14 = OpPhi %bool %true %10 %15 %11 |
| OpBranchConditional %14 %8 %7 |
| %8 = OpLabel |
| OpReturn |
| OpFunctionEnd |
| )"); |
| } |
| |
| } // namespace |
| } // namespace tint::spirv::writer |