writer/spirv: Fix trailing emission of OpReturn

writer::spirv::Function attempted to append a trailing OpReturn if the
function does not end with a terminating instruction. This wasn't
considering functions that have a non-void return type.

This has now been moved to spirv::Builder::GenerateFunction(),
where we can actually examine the function return type, and generate a
zero-expression to return if we need to.

Note: this was masked by WGSL validation that required all functions to
end with a return statement.

Bug: tint:1302
Change-Id: Iddfeda25a956622c318b8235dc6fc093a2a5c26d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/71604
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-12-03 17:51:48 +00:00
parent 2ec1393f31
commit c270322884
7 changed files with 561 additions and 629 deletions

View File

@ -653,6 +653,15 @@ bool Builder::GenerateFunction(const ast::Function* func_ast) {
}
}
if (!LastIsTerminator(func_ast->body)) {
if (func->ReturnType()->Is<sem::Void>()) {
push_function_inst(spv::Op::OpReturn, {});
} else {
auto zero = GenerateConstantNullIfNeeded(func->ReturnType());
push_function_inst(spv::Op::OpReturnValue, {Operand::Int(zero)});
}
}
if (func_ast->IsEntryPoint()) {
if (!GenerateEntryPoint(func_ast, func_id)) {
return false;

View File

@ -787,6 +787,7 @@ TEST_F(BuilderTest, IndexAccessor_Of_Vec) {
EXPECT_EQ(DumpInstructions(b.functions()[0].variables()), R"()");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%18 = OpCompositeExtract %6 %16 1
OpReturn
)");
Validate(b);
@ -832,6 +833,7 @@ TEST_F(BuilderTest, IndexAccessor_Of_Array_Of_f32) {
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%18 = OpCompositeExtract %6 %16 2
%20 = OpCompositeExtract %7 %18 1
OpReturn
)");
Validate(b);
@ -931,6 +933,7 @@ TEST_F(BuilderTest, IndexAccessor_Array_Literal) {
EXPECT_EQ(DumpInstructions(b.functions()[0].variables()), "");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%15 = OpCompositeExtract %6 %12 2
OpReturn
)");
Validate(b);
@ -979,6 +982,7 @@ TEST_F(BuilderTest, IndexAccessor_Array_Dynamic) {
%20 = OpLoad %18 %16
%22 = OpAccessChain %21 %13 %20
%23 = OpLoad %6 %22
OpReturn
)");
Validate(b);
@ -1031,6 +1035,7 @@ TEST_F(BuilderTest, IndexAccessor_Matrix_Dynamic) {
%22 = OpLoad %20 %18
%24 = OpAccessChain %23 %15 %22
%25 = OpLoad %6 %24
OpReturn
)");
Validate(b);

View File

@ -29,23 +29,15 @@ TEST_F(BuilderTest, Expression_Call) {
func_params.push_back(Param("a", ty.f32()));
func_params.push_back(Param("b", ty.f32()));
auto* a_func =
Func("a_func", func_params, ty.f32(),
ast::StatementList{Return(Add("a", "b"))}, ast::DecorationList{});
auto* a_func = Func("a_func", func_params, ty.f32(), {Return(Add("a", "b"))});
auto* func =
Func("main", {}, ty.void_(), ast::StatementList{}, ast::DecorationList{});
auto* expr = Call("a_func", 1.f, 1.f);
WrapInFunction(expr);
Func("main", {}, ty.void_(), {Assign(Phony(), Call("a_func", 1.f, 1.f))});
spirv::Builder& b = Build();
ASSERT_TRUE(b.GenerateFunction(a_func)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_EQ(b.GenerateCallExpression(expr), 12u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "a_func"
OpName %4 "a"
OpName %5 "b"
@ -75,23 +67,16 @@ TEST_F(BuilderTest, Statement_Call) {
func_params.push_back(Param("a", ty.f32()));
func_params.push_back(Param("b", ty.f32()));
auto* a_func =
Func("a_func", func_params, ty.f32(),
ast::StatementList{Return(Add("a", "b"))}, ast::DecorationList{});
auto* a_func = Func("a_func", func_params, ty.f32(), {Return(Add("a", "b"))});
auto* func =
Func("main", {}, ty.void_(), ast::StatementList{}, ast::DecorationList{});
auto* expr = CallStmt(Call("a_func", 1.f, 1.f));
WrapInFunction(expr);
Func("main", {}, ty.void_(), {CallStmt(Call("a_func", 1.f, 1.f))});
spirv::Builder& b = Build();
ASSERT_TRUE(b.GenerateFunction(a_func)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "a_func"
OpName %4 "a"
OpName %5 "b"

View File

@ -452,26 +452,24 @@ TEST_F(BuilderTest, If_WithReturn) {
// if (true) {
// return;
// }
auto* if_body = Block(Return());
auto* expr =
create<ast::IfStatement>(Expr(true), if_body, ast::ElseStatementList{});
WrapInFunction(expr);
auto* fn = Func("f", {}, ty.void_(), {If(true, Block(Return()))});
spirv::Builder& b = Build();
b.push_function(Function{});
EXPECT_TRUE(b.GenerateIfStatement(expr)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeBool
%2 = OpConstantTrue %1
EXPECT_TRUE(b.GenerateFunction(fn)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeVoid
%1 = OpTypeFunction %2
%5 = OpTypeBool
%6 = OpConstantTrue %5
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(OpSelectionMerge %3 None
OpBranchConditional %2 %4 %3
%4 = OpLabel
R"(OpSelectionMerge %7 None
OpBranchConditional %6 %8 %7
%8 = OpLabel
OpReturn
%7 = OpLabel
OpReturn
%3 = OpLabel
)");
}
@ -480,24 +478,28 @@ TEST_F(BuilderTest, If_WithReturnValue) {
// return false;
// }
// return true;
auto* if_body = Block(Return(false));
auto* expr = If(Expr(true), if_body);
Func("test", {}, ty.bool_(), {expr, Return(true)}, {});
auto* fn = Func("f", {}, ty.bool_(),
{
If(true, Block(Return(false))),
Return(true),
});
spirv::Builder& b = Build();
b.push_function(Function{});
EXPECT_TRUE(b.GenerateIfStatement(expr)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeBool
%2 = OpConstantTrue %1
%5 = OpConstantFalse %1
EXPECT_TRUE(b.GenerateFunction(fn)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeBool
%1 = OpTypeFunction %2
%5 = OpConstantTrue %2
%8 = OpConstantFalse %2
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(OpSelectionMerge %3 None
OpBranchConditional %2 %4 %3
%4 = OpLabel
R"(OpSelectionMerge %6 None
OpBranchConditional %5 %7 %6
%7 = OpLabel
OpReturnValue %8
%6 = OpLabel
OpReturnValue %5
%3 = OpLabel
)");
}
@ -512,24 +514,28 @@ TEST_F(BuilderTest, If_WithNestedBlockReturnValue) {
// }
// }
// return true;
auto* if_body = Block(Block(Block(Block(Return(false)))));
auto* expr = If(Expr(true), if_body);
Func("test", {}, ty.bool_(), {expr, Return(true)}, {});
auto* fn = Func("f", {}, ty.bool_(),
{
If(true, Block(Block(Block(Block(Return(false)))))),
Return(true),
});
spirv::Builder& b = Build();
b.push_function(Function{});
EXPECT_TRUE(b.GenerateIfStatement(expr)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeBool
%2 = OpConstantTrue %1
%5 = OpConstantFalse %1
EXPECT_TRUE(b.GenerateFunction(fn)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeBool
%1 = OpTypeFunction %2
%5 = OpConstantTrue %2
%8 = OpConstantFalse %2
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(OpSelectionMerge %3 None
OpBranchConditional %2 %4 %3
%4 = OpLabel
R"(OpSelectionMerge %6 None
OpBranchConditional %5 %7 %6
%7 = OpLabel
OpReturnValue %8
%6 = OpLabel
OpReturnValue %5
%3 = OpLabel
)");
}
@ -539,29 +545,27 @@ TEST_F(BuilderTest, If_WithLoad_Bug327) {
// }
auto* var = Global("a", ty.bool_(), ast::StorageClass::kPrivate);
auto* expr =
create<ast::IfStatement>(Expr("a"), Block(), ast::ElseStatementList{});
WrapInFunction(expr);
auto* fn = Func("f", {}, ty.void_(), {If("a", Block())});
spirv::Builder& b = Build();
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
EXPECT_TRUE(b.GenerateIfStatement(expr)) << b.error();
EXPECT_TRUE(b.GenerateFunction(fn)) << b.error();
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeBool
%2 = OpTypePointer Private %3
%4 = OpConstantNull %3
%1 = OpVariable %2 Private %4
%6 = OpTypeVoid
%5 = OpTypeFunction %6
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%5 = OpLoad %3 %1
OpSelectionMerge %6 None
OpBranchConditional %5 %7 %6
%7 = OpLabel
OpBranch %6
%6 = OpLabel
R"(%9 = OpLoad %3 %1
OpSelectionMerge %10 None
OpBranchConditional %9 %11 %10
%11 = OpLabel
OpBranch %10
%10 = OpLabel
OpReturn
)");
}

File diff suppressed because it is too large Load Diff

View File

@ -60,13 +60,13 @@ TEST_F(BuilderTest, Switch_WithCase) {
auto* v = Global("v", ty.i32(), ast::StorageClass::kPrivate);
auto* a = Global("a", ty.i32(), ast::StorageClass::kPrivate);
auto* expr = Switch("a", /**/
Case(Expr(1), Block(Assign("v", 1))),
Case(Expr(2), Block(Assign("v", 2))), DefaultCase());
WrapInFunction(expr);
auto* func = Func("a_func", {}, ty.void_(), ast::StatementList{},
ast::DecorationList{});
auto* func = Func("a_func", {}, ty.void_(),
{
Switch("a", //
Case(Expr(1), Block(Assign("v", 1))), //
Case(Expr(2), Block(Assign("v", 2))), //
DefaultCase()),
});
spirv::Builder& b = Build();
@ -74,8 +74,6 @@ TEST_F(BuilderTest, Switch_WithCase) {
ASSERT_TRUE(b.GenerateGlobalVariable(a)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "a"
OpName %8 "a_func"
@ -119,13 +117,13 @@ TEST_F(BuilderTest, Switch_WithCase_Unsigned) {
auto* v = Global("v", ty.i32(), ast::StorageClass::kPrivate);
auto* a = Global("a", ty.u32(), ast::StorageClass::kPrivate);
auto* expr = Switch("a", Case(Expr(1u), Block(Assign("v", 1))),
Case(Expr(2u), Block(Assign("v", 2))), DefaultCase());
WrapInFunction(expr);
auto* func = Func("a_func", {}, ty.void_(), ast::StatementList{},
ast::DecorationList{});
auto* func = Func("a_func", {}, ty.void_(),
{
Switch("a", //
Case(Expr(1u), Block(Assign("v", 1))), //
Case(Expr(2u), Block(Assign("v", 2))), //
DefaultCase()),
});
spirv::Builder& b = Build();
@ -133,8 +131,6 @@ TEST_F(BuilderTest, Switch_WithCase_Unsigned) {
ASSERT_TRUE(b.GenerateGlobalVariable(a)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "a"
OpName %11 "a_func"
@ -178,18 +174,11 @@ TEST_F(BuilderTest, Switch_WithDefault) {
auto* v = Global("v", ty.i32(), ast::StorageClass::kPrivate);
auto* a = Global("a", ty.i32(), ast::StorageClass::kPrivate);
auto* default_body = Block(Assign("v", 1));
ast::CaseStatementList cases;
cases.push_back(
create<ast::CaseStatement>(ast::CaseSelectorList{}, default_body));
auto* expr = create<ast::SwitchStatement>(Expr("a"), cases);
WrapInFunction(expr);
auto* func = Func("a_func", {}, ty.void_(), ast::StatementList{},
ast::DecorationList{});
auto* func = Func("a_func", {}, ty.void_(),
{
Switch("a", //
DefaultCase(Block(Assign("v", 1)))), //
});
spirv::Builder& b = Build();
@ -197,8 +186,6 @@ TEST_F(BuilderTest, Switch_WithDefault) {
ASSERT_TRUE(b.GenerateGlobalVariable(a)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "a"
OpName %8 "a_func"
@ -237,31 +224,15 @@ TEST_F(BuilderTest, Switch_WithCaseAndDefault) {
auto* v = Global("v", ty.i32(), ast::StorageClass::kPrivate);
auto* a = Global("a", ty.i32(), ast::StorageClass::kPrivate);
auto* case_1_body = Block(Assign("v", Expr(1)));
auto* case_2_body = Block(Assign("v", Expr(2)));
auto* default_body = Block(Assign("v", Expr(3)));
ast::CaseSelectorList selector_1;
selector_1.push_back(Expr(1));
ast::CaseSelectorList selector_2;
selector_2.push_back(Expr(2));
selector_2.push_back(Expr(3));
ast::CaseStatementList cases;
cases.push_back(create<ast::CaseStatement>(selector_1, case_1_body));
cases.push_back(create<ast::CaseStatement>(selector_2, case_2_body));
cases.push_back(
create<ast::CaseStatement>(ast::CaseSelectorList{}, default_body));
auto* expr = create<ast::SwitchStatement>(Expr("a"), cases);
WrapInFunction(expr);
auto* func = Func("a_func", {}, ty.void_(), ast::StatementList{},
ast::DecorationList{});
auto* func = Func("a_func", {}, ty.void_(),
{
Switch(Expr("a"), //
Case(Expr(1), //
Block(Assign("v", 1))), //
Case({Expr(2), Expr(3)}, //
Block(Assign("v", 2))), //
DefaultCase(Block(Assign("v", 3)))),
});
spirv::Builder& b = Build();
@ -269,8 +240,6 @@ TEST_F(BuilderTest, Switch_WithCaseAndDefault) {
ASSERT_TRUE(b.GenerateGlobalVariable(a)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "a"
OpName %8 "a_func"
@ -318,31 +287,15 @@ TEST_F(BuilderTest, Switch_CaseWithFallthrough) {
auto* v = Global("v", ty.i32(), ast::StorageClass::kPrivate);
auto* a = Global("a", ty.i32(), ast::StorageClass::kPrivate);
auto* case_1_body =
Block(Assign("v", Expr(1)), create<ast::FallthroughStatement>());
auto* case_2_body = Block(Assign("v", Expr(2)));
auto* default_body = Block(Assign("v", Expr(3)));
ast::CaseSelectorList selector_1;
selector_1.push_back(Expr(1));
ast::CaseSelectorList selector_2;
selector_2.push_back(Expr(2));
ast::CaseStatementList cases;
cases.push_back(create<ast::CaseStatement>(selector_1, case_1_body));
cases.push_back(create<ast::CaseStatement>(selector_2, case_2_body));
cases.push_back(
create<ast::CaseStatement>(ast::CaseSelectorList{}, default_body));
auto* expr = create<ast::SwitchStatement>(Expr("a"), cases);
WrapInFunction(expr);
auto* func = Func("a_func", {}, ty.void_(), ast::StatementList{},
ast::DecorationList{});
auto* func = Func("a_func", {}, ty.void_(),
{
Switch(Expr("a"), //
Case(Expr(1), //
Block(Assign("v", 1), Fallthrough())), //
Case(Expr(2), //
Block(Assign("v", 2))), //
DefaultCase(Block(Assign("v", 3)))),
});
spirv::Builder& b = Build();
@ -350,8 +303,6 @@ TEST_F(BuilderTest, Switch_CaseWithFallthrough) {
ASSERT_TRUE(b.GenerateGlobalVariable(a)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "a"
OpName %8 "a_func"
@ -398,17 +349,16 @@ TEST_F(BuilderTest, Switch_WithNestedBreak) {
auto* v = Global("v", ty.i32(), ast::StorageClass::kPrivate);
auto* a = Global("a", ty.i32(), ast::StorageClass::kPrivate);
auto* expr = Switch(
"a", /**/
Case(Expr(1), Block(/**/
auto* func = Func(
"a_func", {}, ty.void_(),
{
Switch("a", //
Case(Expr(1), //
Block( //
If(Expr(true), Block(create<ast::BreakStatement>())),
Assign("v", 1))),
DefaultCase());
WrapInFunction(expr);
auto* func = Func("a_func", {}, ty.void_(), ast::StatementList{},
ast::DecorationList{});
DefaultCase()),
});
spirv::Builder& b = Build();
@ -416,8 +366,6 @@ TEST_F(BuilderTest, Switch_WithNestedBreak) {
ASSERT_TRUE(b.GenerateGlobalVariable(a)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "a"
OpName %8 "a_func"

View File

@ -17,15 +17,6 @@
namespace tint {
namespace writer {
namespace spirv {
namespace {
// Returns true if the given Op is a function terminator
bool OpIsFunctionTerminator(spv::Op op) {
return op == spv::Op::OpReturn || op == spv::Op::OpReturnValue ||
op == spv::Op::OpKill;
}
} // namespace
Function::Function()
: declaration_(Instruction{spv::Op::OpNop, {}}),
@ -56,17 +47,6 @@ void Function::iterate(std::function<void(const Instruction&)> cb) const {
cb(inst);
}
bool needs_terminator = false;
if (instructions_.empty()) {
needs_terminator = true;
} else {
const auto& last = instructions_.back();
needs_terminator = !OpIsFunctionTerminator(last.opcode());
}
if (needs_terminator) {
cb(Instruction{spv::Op::OpReturn, {}});
}
cb(Instruction{spv::Op::OpFunctionEnd, {}});
}