[ir][spirv-writer] Add support for return values

Bug: tint:1906
Change-Id: I73eb7ed2479c0f8051939163416c660c2f4faf05
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/134200
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2023-05-24 13:58:34 +00:00 committed by Dawn LUCI CQ
parent 62bea2cf43
commit f5a62539f4
2 changed files with 23 additions and 3 deletions

View File

@ -332,11 +332,14 @@ void GeneratorImplIr::EmitBranch(const ir::Branch* b) {
b->To(), b->To(),
[&](const ir::Block* blk) { current_function_.push_inst(spv::Op::OpBranch, {Label(blk)}); }, [&](const ir::Block* blk) { current_function_.push_inst(spv::Op::OpBranch, {Label(blk)}); },
[&](const ir::FunctionTerminator*) { [&](const ir::FunctionTerminator*) {
// TODO(jrprice): Handle the return value, which will be a branch argument.
if (!b->Args().IsEmpty()) { if (!b->Args().IsEmpty()) {
TINT_ICE(Writer, diagnostics_) << "unimplemented return value"; TINT_ASSERT(Writer, b->Args().Length() == 1u);
OperandList operands;
operands.push_back(Value(b->Args()[0]));
current_function_.push_inst(spv::Op::OpReturnValue, operands);
} else {
current_function_.push_inst(spv::Op::OpReturn, {});
} }
current_function_.push_inst(spv::Op::OpReturn, {});
}, },
[&](Default) { [&](Default) {
// A block may not have an outward branch (e.g. an unreachable merge // A block may not have an outward branch (e.g. an unreachable merge

View File

@ -138,5 +138,22 @@ OpFunctionEnd
)"); )");
} }
TEST_F(SpvGeneratorImplTest, Function_ReturnValue) {
auto* func = b.CreateFunction("foo", mod.types.i32());
func->StartTarget()->SetInstructions(
utils::Vector{b.Branch(func->EndTarget(), utils::Vector{b.Constant(i32(42))})});
generator_.EmitFunction(func);
EXPECT_EQ(DumpModule(generator_.Module()), R"(OpName %1 "foo"
%2 = OpTypeInt 32 1
%3 = OpTypeFunction %2
%5 = OpConstant %2 42
%1 = OpFunction %2 None %3
%4 = OpLabel
OpReturnValue %5
OpFunctionEnd
)");
}
} // namespace } // namespace
} // namespace tint::writer::spirv } // namespace tint::writer::spirv