writer/spirv: Add tests for automatic OpReturnValue

As requested at:
https://dawn-review.googlesource.com/c/tint/+/71604/5/src/writer/spirv/builder_if_test.cc#476

Bug: tint:1302
Change-Id: I661f877beb44f999e17f2769b4aa46b22180bbb9
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/71682
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-12-03 19:19:33 +00:00 committed by Tint LUCI CQ
parent ea3eee9885
commit b9e8a0b87d
2 changed files with 80 additions and 0 deletions

View File

@ -503,6 +503,40 @@ OpReturnValue %5
)"); )");
} }
TEST_F(BuilderTest, IfElse_BothReturn) {
// if (true) {
// return true;
// } else {
// return true;
// }
auto* fn = Func("f", {}, ty.bool_(),
{
If(true, //
Block(Return(true)), //
Else(Block(Return(true)))),
});
spirv::Builder& b = Build();
EXPECT_TRUE(b.GenerateFunction(fn)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeBool
%1 = OpTypeFunction %2
%5 = OpConstantTrue %2
%9 = OpConstantNull %2
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(OpSelectionMerge %6 None
OpBranchConditional %5 %7 %8
%7 = OpLabel
OpReturnValue %5
%8 = OpLabel
OpReturnValue %5
%6 = OpLabel
OpReturnValue %9
)");
}
TEST_F(BuilderTest, If_WithNestedBlockReturnValue) { TEST_F(BuilderTest, If_WithNestedBlockReturnValue) {
// if (true) { // if (true) {
// { // {

View File

@ -400,6 +400,52 @@ OpFunctionEnd
)"); )");
} }
TEST_F(BuilderTest, Switch_AllReturn) {
// switch (1) {
// case 1: {
// return 1;
// }
// case 2: {
// fallthrough;
// }
// default: {
// return 3;
// }
// }
auto* fn = Func("f", {}, ty.i32(),
{
Switch(1, //
Case(Expr(1), Block(Return(1))), //
Case(Expr(2), Block(Fallthrough())), //
DefaultCase(Block(Return(3)))),
});
spirv::Builder& b = Build();
EXPECT_TRUE(b.GenerateFunction(fn)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "f"
%2 = OpTypeInt 32 1
%1 = OpTypeFunction %2
%6 = OpConstant %2 1
%10 = OpConstant %2 3
%11 = OpConstantNull %2
%3 = OpFunction %2 None %1
%4 = OpLabel
OpSelectionMerge %5 None
OpSwitch %6 %7 1 %8 2 %9
%8 = OpLabel
OpReturnValue %6
%9 = OpLabel
OpBranch %7
%7 = OpLabel
OpReturnValue %10
%5 = OpLabel
OpReturnValue %11
OpFunctionEnd
)");
}
} // namespace } // namespace
} // namespace spirv } // namespace spirv
} // namespace writer } // namespace writer