spirv-writer: Generate load for return value, when needed
spirv-writer: treat function parameter as const Bug: tint:274 Change-Id: Iba4d90ea10a70ce1f551b7b8da6efcc01fe97412 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/30924 Commit-Queue: dan sinclair <dsinclair@chromium.org> Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
parent
f81c1081ea
commit
1bafdc3c45
|
@ -2151,6 +2151,7 @@ bool Builder::GenerateReturnStatement(ast::ReturnStatement* stmt) {
|
||||||
if (val_id == 0) {
|
if (val_id == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
val_id = GenerateLoadIfNeeded(stmt->value()->result_type(), val_id);
|
||||||
push_function_inst(spv::Op::OpReturnValue, {Operand::Int(val_id)});
|
push_function_inst(spv::Op::OpReturnValue, {Operand::Int(val_id)});
|
||||||
} else {
|
} else {
|
||||||
push_function_inst(spv::Op::OpReturn, {});
|
push_function_inst(spv::Op::OpReturn, {});
|
||||||
|
|
|
@ -24,6 +24,8 @@
|
||||||
#include "src/ast/type/i32_type.h"
|
#include "src/ast/type/i32_type.h"
|
||||||
#include "src/ast/type/void_type.h"
|
#include "src/ast/type/void_type.h"
|
||||||
#include "src/ast/variable.h"
|
#include "src/ast/variable.h"
|
||||||
|
#include "src/context.h"
|
||||||
|
#include "src/type_determiner.h"
|
||||||
#include "src/writer/spirv/builder.h"
|
#include "src/writer/spirv/builder.h"
|
||||||
#include "src/writer/spirv/spv_dump.h"
|
#include "src/writer/spirv/spv_dump.h"
|
||||||
|
|
||||||
|
@ -60,10 +62,14 @@ TEST_F(BuilderTest, Function_WithParams) {
|
||||||
ast::type::I32Type i32;
|
ast::type::I32Type i32;
|
||||||
|
|
||||||
ast::VariableList params;
|
ast::VariableList params;
|
||||||
params.push_back(
|
auto var_a =
|
||||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32));
|
std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
|
||||||
params.push_back(
|
var_a->set_is_const(true);
|
||||||
std::make_unique<ast::Variable>("b", ast::StorageClass::kFunction, &i32));
|
params.push_back(std::move(var_a));
|
||||||
|
auto var_b =
|
||||||
|
std::make_unique<ast::Variable>("b", ast::StorageClass::kFunction, &i32);
|
||||||
|
var_b->set_is_const(true);
|
||||||
|
params.push_back(std::move(var_b));
|
||||||
|
|
||||||
ast::Function func("a_func", std::move(params), &f32);
|
ast::Function func("a_func", std::move(params), &f32);
|
||||||
|
|
||||||
|
@ -72,7 +78,13 @@ TEST_F(BuilderTest, Function_WithParams) {
|
||||||
std::make_unique<ast::IdentifierExpression>("a")));
|
std::make_unique<ast::IdentifierExpression>("a")));
|
||||||
func.set_body(std::move(body));
|
func.set_body(std::move(body));
|
||||||
|
|
||||||
|
Context ctx;
|
||||||
ast::Module mod;
|
ast::Module mod;
|
||||||
|
TypeDeterminer td(&ctx, &mod);
|
||||||
|
td.RegisterVariableForTesting(func.params()[0].get());
|
||||||
|
td.RegisterVariableForTesting(func.params()[1].get());
|
||||||
|
EXPECT_TRUE(td.DetermineFunction(&func));
|
||||||
|
|
||||||
Builder b(&mod);
|
Builder b(&mod);
|
||||||
ASSERT_TRUE(b.GenerateFunction(&func));
|
ASSERT_TRUE(b.GenerateFunction(&func));
|
||||||
EXPECT_EQ(DumpBuilder(b), R"(OpName %4 "a_func"
|
EXPECT_EQ(DumpBuilder(b), R"(OpName %4 "a_func"
|
||||||
|
@ -87,7 +99,7 @@ OpName %6 "b"
|
||||||
%7 = OpLabel
|
%7 = OpLabel
|
||||||
OpReturnValue %5
|
OpReturnValue %5
|
||||||
OpFunctionEnd
|
OpFunctionEnd
|
||||||
)");
|
)") << DumpBuilder(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(BuilderTest, Function_WithBody) {
|
TEST_F(BuilderTest, Function_WithBody) {
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "src/ast/float_literal.h"
|
#include "src/ast/float_literal.h"
|
||||||
|
#include "src/ast/identifier_expression.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
#include "src/ast/scalar_constructor_expression.h"
|
#include "src/ast/scalar_constructor_expression.h"
|
||||||
#include "src/ast/type/f32_type.h"
|
#include "src/ast/type/f32_type.h"
|
||||||
|
@ -84,6 +85,39 @@ TEST_F(BuilderTest, Return_WithValue) {
|
||||||
)");
|
)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(BuilderTest, Return_WithValue_GeneratesLoad) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
ast::Variable var("param", ast::StorageClass::kFunction, &f32);
|
||||||
|
|
||||||
|
ast::ReturnStatement ret(
|
||||||
|
std::make_unique<ast::IdentifierExpression>("param"));
|
||||||
|
|
||||||
|
Context ctx;
|
||||||
|
ast::Module mod;
|
||||||
|
TypeDeterminer td(&ctx, &mod);
|
||||||
|
td.RegisterVariableForTesting(&var);
|
||||||
|
EXPECT_TRUE(td.DetermineResultType(&ret)) << td.error();
|
||||||
|
|
||||||
|
Builder b(&mod);
|
||||||
|
b.push_function(Function{});
|
||||||
|
EXPECT_TRUE(b.GenerateFunctionVariable(&var)) << b.error();
|
||||||
|
EXPECT_TRUE(b.GenerateReturnStatement(&ret)) << b.error();
|
||||||
|
ASSERT_FALSE(b.has_error()) << b.error();
|
||||||
|
|
||||||
|
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
|
||||||
|
%2 = OpTypePointer Function %3
|
||||||
|
%4 = OpConstantNull %3
|
||||||
|
)");
|
||||||
|
EXPECT_EQ(DumpInstructions(b.functions()[0].variables()),
|
||||||
|
R"(%1 = OpVariable %2 Function %4
|
||||||
|
)");
|
||||||
|
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
|
||||||
|
R"(%5 = OpLoad %3 %1
|
||||||
|
OpReturnValue %5
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace spirv
|
} // namespace spirv
|
||||||
} // namespace writer
|
} // namespace writer
|
||||||
|
|
Loading…
Reference in New Issue