[spirv-writer] Move module to SPIR-V builder constructor

This CL moves the ast::Module to the constructor of the SPIR-V builder
class. This allows access to things like the ast::Import from the
builder class.

Bug: tint:5
Change-Id: I0d61b164c1274d006e124dde2cc11f41a2385fc5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/19922
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-04-20 14:18:44 +00:00 committed by dan sinclair
parent c954788b59
commit 6ad9f5d1ed
17 changed files with 213 additions and 113 deletions

View File

@ -28,7 +28,8 @@ namespace {
using BinaryWriterTest = testing::Test;
TEST_F(BinaryWriterTest, Preamble) {
Builder b;
ast::Module mod;
Builder b(&mod);
BinaryWriter bw;
bw.WriteHeader(5);
@ -42,7 +43,8 @@ TEST_F(BinaryWriterTest, Preamble) {
}
TEST_F(BinaryWriterTest, Float) {
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_preamble(spv::Op::OpNop, {Operand::Float(2.4f)});
BinaryWriter bw;
bw.WriteBuilder(b);
@ -55,7 +57,8 @@ TEST_F(BinaryWriterTest, Float) {
}
TEST_F(BinaryWriterTest, Int) {
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_preamble(spv::Op::OpNop, {Operand::Int(2)});
BinaryWriter bw;
bw.WriteBuilder(b);
@ -66,7 +69,8 @@ TEST_F(BinaryWriterTest, Int) {
}
TEST_F(BinaryWriterTest, String) {
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_preamble(spv::Op::OpNop, {Operand::String("my_string")});
BinaryWriter bw;
bw.WriteBuilder(b);
@ -90,7 +94,8 @@ TEST_F(BinaryWriterTest, String) {
}
TEST_F(BinaryWriterTest, String_Multiple4Length) {
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_preamble(spv::Op::OpNop, {Operand::String("mystring")});
BinaryWriter bw;
bw.WriteBuilder(b);

View File

@ -82,16 +82,16 @@ uint32_t pipeline_stage_to_execution_model(ast::PipelineStage stage) {
} // namespace
Builder::Builder() : scope_stack_({}) {}
Builder::Builder(ast::Module* mod) : mod_(mod), scope_stack_({}) {}
Builder::~Builder() = default;
bool Builder::Build(const ast::Module& m) {
bool Builder::Build() {
push_preamble(spv::Op::OpCapability, {Operand::Int(SpvCapabilityShader)});
push_preamble(spv::Op::OpCapability,
{Operand::Int(SpvCapabilityVulkanMemoryModel)});
for (const auto& imp : m.imports()) {
for (const auto& imp : mod_->imports()) {
GenerateImport(imp.get());
}
@ -99,19 +99,19 @@ bool Builder::Build(const ast::Module& m) {
{Operand::Int(SpvAddressingModelLogical),
Operand::Int(SpvMemoryModelVulkanKHR)});
for (const auto& var : m.global_variables()) {
for (const auto& var : mod_->global_variables()) {
if (!GenerateGlobalVariable(var.get())) {
return false;
}
}
for (const auto& func : m.functions()) {
for (const auto& func : mod_->functions()) {
if (!GenerateFunction(func.get())) {
return false;
}
}
for (const auto& ep : m.entry_points()) {
for (const auto& ep : mod_->entry_points()) {
if (!GenerateEntryPoint(ep.get())) {
return false;
}

View File

@ -38,13 +38,13 @@ namespace spirv {
class Builder {
public:
/// Constructor
Builder();
/// @param mod the module to generate from
explicit Builder(ast::Module* mod);
~Builder();
/// Generates the SPIR-V instructions for the given module
/// @param module the module to generate from
/// @returns true if the SPIR-V was successfully built
bool Build(const ast::Module& module);
bool Build();
/// @returns the error string or blank if no error was reported.
const std::string& error() const { return error_; }
@ -280,6 +280,7 @@ class Builder {
/// automatically.
Operand result_op();
ast::Module* mod_;
std::string error_;
uint32_t next_id_ = 1;
std::vector<Instruction> preamble_;

View File

@ -37,7 +37,8 @@ TEST_F(BuilderTest, Assign_Var) {
ast::Variable v("var", ast::StorageClass::kOutput, &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();

View File

@ -64,7 +64,8 @@ TEST_P(BinaryArithSignedIntegerTest, Scalar) {
TypeDeterminer td(&ctx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 4u) << b.error();
@ -107,7 +108,8 @@ TEST_P(BinaryArithSignedIntegerTest, Vector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -152,7 +154,8 @@ TEST_P(BinaryArithUnsignedIntegerTest, Scalar) {
TypeDeterminer td(&ctx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 4u) << b.error();
@ -195,7 +198,8 @@ TEST_P(BinaryArithUnsignedIntegerTest, Vector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -240,7 +244,8 @@ TEST_P(BinaryArithFloatTest, Scalar) {
TypeDeterminer td(&ctx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 4u) << b.error();
@ -284,7 +289,8 @@ TEST_P(BinaryArithFloatTest, Vector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -322,7 +328,8 @@ TEST_P(BinaryCompareUnsignedIntegerTest, Scalar) {
TypeDeterminer td(&ctx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 4u) << b.error();
@ -367,7 +374,8 @@ TEST_P(BinaryCompareUnsignedIntegerTest, Vector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -409,7 +417,8 @@ TEST_P(BinaryCompareSignedIntegerTest, Scalar) {
TypeDeterminer td(&ctx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 4u) << b.error();
@ -454,7 +463,8 @@ TEST_P(BinaryCompareSignedIntegerTest, Vector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -496,7 +506,8 @@ TEST_P(BinaryCompareFloatTest, Scalar) {
TypeDeterminer td(&ctx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 4u) << b.error();
@ -541,7 +552,8 @@ TEST_P(BinaryCompareFloatTest, Vector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -591,7 +603,8 @@ TEST_F(BuilderTest, Binary_Multiply_VectorScalar) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -629,7 +642,8 @@ TEST_F(BuilderTest, Binary_Multiply_ScalarVector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateBinaryExpression(&expr), 5u) << b.error();
@ -661,7 +675,8 @@ TEST_F(BuilderTest, Binary_Multiply_MatrixScalar) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();
@ -698,7 +713,8 @@ TEST_F(BuilderTest, Binary_Multiply_ScalarMatrix) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();
@ -744,7 +760,8 @@ TEST_F(BuilderTest, Binary_Multiply_MatrixVector) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();
@ -792,7 +809,8 @@ TEST_F(BuilderTest, Binary_Multiply_VectorMatrix) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();
@ -830,7 +848,8 @@ TEST_F(BuilderTest, Binary_Multiply_MatrixMatrix) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();

View File

@ -38,7 +38,8 @@ TEST_F(BuilderTest, Constructor_Const) {
auto fl = std::make_unique<ast::FloatLiteral>(&f32, 42.2f);
ast::ScalarConstructorExpression c(std::move(fl));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateConstructorExpression(&c, true), 2u);
ASSERT_FALSE(b.has_error()) << b.error();
@ -61,7 +62,8 @@ TEST_F(BuilderTest, Constructor_Type) {
ast::TypeConstructorExpression t(&vec, std::move(vals));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateConstructorExpression(&t, true), 5u);
ASSERT_FALSE(b.has_error()) << b.error();
@ -87,7 +89,8 @@ TEST_F(BuilderTest, Constructor_Type_Dedups) {
ast::TypeConstructorExpression t(&vec, std::move(vals));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateConstructorExpression(&t, true), 5u);
EXPECT_EQ(b.GenerateConstructorExpression(&t, true), 5u);
ASSERT_FALSE(b.has_error()) << b.error();
@ -110,7 +113,8 @@ TEST_F(BuilderTest, Constructor_NonConst_Type_Fails) {
ast::TypeConstructorExpression t(&vec, std::move(vals));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateConstructorExpression(&t, true), 0u);
EXPECT_TRUE(b.has_error());
EXPECT_EQ(b.error(), R"(constructor must be a constant expression)");

View File

@ -32,7 +32,8 @@ using BuilderTest = testing::Test;
TEST_F(BuilderTest, EntryPoint) {
ast::EntryPoint ep(ast::PipelineStage::kFragment, "main", "frag_main");
Builder b;
ast::Module mod;
Builder b(&mod);
b.set_func_name_to_id("frag_main", 2);
ASSERT_TRUE(b.GenerateEntryPoint(&ep));
@ -45,7 +46,8 @@ TEST_F(BuilderTest, EntryPoint) {
TEST_F(BuilderTest, EntryPoint_WithoutName) {
ast::EntryPoint ep(ast::PipelineStage::kCompute, "", "compute_main");
Builder b;
ast::Module mod;
Builder b(&mod);
b.set_func_name_to_id("compute_main", 3);
ASSERT_TRUE(b.GenerateEntryPoint(&ep));
@ -59,7 +61,8 @@ TEST_F(BuilderTest, EntryPoint_WithoutName) {
TEST_F(BuilderTest, EntryPoint_BadFunction) {
ast::EntryPoint ep(ast::PipelineStage::kFragment, "main", "frag_main");
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_FALSE(b.GenerateEntryPoint(&ep));
EXPECT_EQ(b.error(), "unable to find ID for function: frag_main");
}
@ -78,7 +81,8 @@ TEST_P(EntryPointStageTest, Emit) {
ast::EntryPoint ep(params.stage, "", "main");
Builder b;
ast::Module mod;
Builder b(&mod);
b.set_func_name_to_id("main", 3);
ASSERT_TRUE(b.GenerateEntryPoint(&ep));

View File

@ -33,7 +33,8 @@ TEST_F(BuilderTest, Function_Empty) {
ast::type::VoidType void_type;
ast::Function func("a_func", {}, &void_type);
Builder b;
ast::Module mod;
Builder b(&mod);
ASSERT_TRUE(b.GenerateFunction(&func));
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "a_func"
@ -56,7 +57,8 @@ TEST_F(BuilderTest, FunctionType) {
ast::type::VoidType void_type;
ast::Function func("a_func", {}, &void_type);
Builder b;
ast::Module mod;
Builder b(&mod);
ASSERT_TRUE(b.GenerateFunction(&func));
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeVoid
%1 = OpTypeFunction %2
@ -68,7 +70,8 @@ TEST_F(BuilderTest, FunctionType_DeDuplicate) {
ast::Function func1("a_func", {}, &void_type);
ast::Function func2("b_func", {}, &void_type);
Builder b;
ast::Module mod;
Builder b(&mod);
ASSERT_TRUE(b.GenerateFunction(&func1));
ASSERT_TRUE(b.GenerateFunction(&func2));
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeVoid

View File

@ -44,7 +44,8 @@ TEST_F(BuilderTest, FunctionVar_NoStorageClass) {
ast::type::F32Type f32;
ast::Variable v("var", ast::StorageClass::kNone, &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
@ -76,7 +77,8 @@ TEST_F(BuilderTest, FunctionVar_WithConstantConstructor) {
ast::Variable v("var", ast::StorageClass::kOutput, &f32);
v.set_constructor(std::move(init));
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();
@ -120,7 +122,8 @@ TEST_F(BuilderTest, DISABLED_FunctionVar_WithNonConstantConstructor) {
ast::Variable v("var", ast::StorageClass::kOutput, &f32);
v.set_constructor(std::move(init));
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();
@ -162,7 +165,8 @@ TEST_F(BuilderTest, FunctionVar_Const) {
v.set_constructor(std::move(init));
v.set_is_const(true);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();

View File

@ -43,7 +43,8 @@ TEST_F(BuilderTest, GlobalVar_NoStorageClass) {
ast::type::F32Type f32;
ast::Variable v("var", ast::StorageClass::kNone, &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)");
@ -57,7 +58,8 @@ TEST_F(BuilderTest, GlobalVar_WithStorageClass) {
ast::type::F32Type f32;
ast::Variable v("var", ast::StorageClass::kOutput, &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)");
@ -85,7 +87,8 @@ TEST_F(BuilderTest, GlobalVar_WithConstructor) {
ast::Variable v("var", ast::StorageClass::kOutput, &f32);
v.set_constructor(std::move(init));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();
@ -120,7 +123,8 @@ TEST_F(BuilderTest, GlobalVar_Const) {
v.set_constructor(std::move(init));
v.set_is_const(true);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();
@ -142,7 +146,8 @@ TEST_F(BuilderTest, GlobalVar_WithLocation) {
ast::DecoratedVariable dv(std::move(v));
dv.set_decorations(std::move(decos));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&dv)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
OpDecorate %1 Location 5
@ -164,7 +169,8 @@ TEST_F(BuilderTest, GlobalVar_WithBindingAndSet) {
ast::DecoratedVariable dv(std::move(v));
dv.set_decorations(std::move(decos));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&dv)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
OpDecorate %1 Binding 2
@ -187,7 +193,8 @@ TEST_F(BuilderTest, GlobalVar_WithBuiltin) {
ast::DecoratedVariable dv(std::move(v));
dv.set_decorations(std::move(decos));
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&dv)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
OpDecorate %1 BuiltIn Position
@ -210,7 +217,8 @@ using BuiltinDataTest = testing::TestWithParam<BuiltinData>;
TEST_P(BuiltinDataTest, Convert) {
auto params = GetParam();
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.ConvertBuiltin(params.builtin), params.result);
}
INSTANTIATE_TEST_SUITE_P(

View File

@ -56,7 +56,8 @@ TEST_F(BuilderTest, IdentifierExpression_GlobalConst) {
v.set_constructor(std::move(init));
v.set_is_const(true);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();
@ -75,7 +76,8 @@ TEST_F(BuilderTest, IdentifierExpression_GlobalVar) {
ast::type::F32Type f32;
ast::Variable v("var", ast::StorageClass::kOutput, &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateGlobalVariable(&v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)");
@ -107,7 +109,8 @@ TEST_F(BuilderTest, IdentifierExpression_FunctionConst) {
v.set_constructor(std::move(init));
v.set_is_const(true);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error();
@ -126,7 +129,8 @@ TEST_F(BuilderTest, IdentifierExpression_FunctionVar) {
ast::type::F32Type f32;
ast::Variable v("var", ast::StorageClass::kNone, &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
@ -161,7 +165,8 @@ TEST_F(BuilderTest, IdentifierExpression_Load) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(&var)) << b.error();
@ -198,7 +203,8 @@ TEST_F(BuilderTest, IdentifierExpression_NoLoadConst) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(&var)) << b.error();

View File

@ -50,7 +50,8 @@ TEST_F(BuilderTest, If_Empty) {
TypeDeterminer td(&ctx);
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_TRUE(b.GenerateIfStatement(&expr)) << b.error();
@ -93,7 +94,8 @@ TEST_F(BuilderTest, If_WithStatements) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();
@ -155,7 +157,8 @@ TEST_F(BuilderTest, If_WithElse) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();
@ -224,7 +227,8 @@ TEST_F(BuilderTest, If_WithElseIf) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();
@ -317,7 +321,8 @@ TEST_F(BuilderTest, If_WithMultiple) {
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error();

View File

@ -36,7 +36,8 @@ TEST_F(BuilderTest, Literal_Bool_True) {
ast::type::BoolType bool_type;
ast::BoolLiteral b_true(&bool_type, true);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateLiteralIfNeeded(&b_true);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(2u, id);
@ -50,7 +51,8 @@ TEST_F(BuilderTest, Literal_Bool_False) {
ast::type::BoolType bool_type;
ast::BoolLiteral b_false(&bool_type, false);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateLiteralIfNeeded(&b_false);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(2u, id);
@ -65,7 +67,8 @@ TEST_F(BuilderTest, Literal_Bool_Dedup) {
ast::BoolLiteral b_true(&bool_type, true);
ast::BoolLiteral b_false(&bool_type, false);
Builder b;
ast::Module mod;
Builder b(&mod);
ASSERT_NE(b.GenerateLiteralIfNeeded(&b_true), 0u);
ASSERT_FALSE(b.has_error()) << b.error();
ASSERT_NE(b.GenerateLiteralIfNeeded(&b_false), 0u);
@ -83,7 +86,8 @@ TEST_F(BuilderTest, Literal_I32) {
ast::type::I32Type i32;
ast::IntLiteral i(&i32, -23);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateLiteralIfNeeded(&i);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(2u, id);
@ -98,7 +102,8 @@ TEST_F(BuilderTest, Literal_I32_Dedup) {
ast::IntLiteral i1(&i32, -23);
ast::IntLiteral i2(&i32, -23);
Builder b;
ast::Module mod;
Builder b(&mod);
ASSERT_NE(b.GenerateLiteralIfNeeded(&i1), 0u);
ASSERT_NE(b.GenerateLiteralIfNeeded(&i2), 0u);
ASSERT_FALSE(b.has_error()) << b.error();
@ -112,7 +117,8 @@ TEST_F(BuilderTest, Literal_U32) {
ast::type::U32Type u32;
ast::UintLiteral i(&u32, 23);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateLiteralIfNeeded(&i);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(2u, id);
@ -127,7 +133,8 @@ TEST_F(BuilderTest, Literal_U32_Dedup) {
ast::UintLiteral i1(&u32, 23);
ast::UintLiteral i2(&u32, 23);
Builder b;
ast::Module mod;
Builder b(&mod);
ASSERT_NE(b.GenerateLiteralIfNeeded(&i1), 0u);
ASSERT_NE(b.GenerateLiteralIfNeeded(&i2), 0u);
ASSERT_FALSE(b.has_error()) << b.error();
@ -141,7 +148,8 @@ TEST_F(BuilderTest, Literal_F32) {
ast::type::F32Type f32;
ast::FloatLiteral i(&f32, 23.245f);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateLiteralIfNeeded(&i);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(2u, id);
@ -156,7 +164,8 @@ TEST_F(BuilderTest, Literal_F32_Dedup) {
ast::FloatLiteral i1(&f32, 23.245f);
ast::FloatLiteral i2(&f32, 23.245f);
Builder b;
ast::Module mod;
Builder b(&mod);
ASSERT_NE(b.GenerateLiteralIfNeeded(&i1), 0u);
ASSERT_NE(b.GenerateLiteralIfNeeded(&i2), 0u);
ASSERT_FALSE(b.has_error()) << b.error();

View File

@ -34,7 +34,8 @@ using BuilderTest = testing::Test;
TEST_F(BuilderTest, Return) {
ast::ReturnStatement ret;
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_TRUE(b.GenerateReturnStatement(&ret));
ASSERT_FALSE(b.has_error()) << b.error();
@ -60,7 +61,8 @@ TEST_F(BuilderTest, Return_WithValue) {
ast::ReturnStatement ret(std::move(val));
Builder b;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_TRUE(b.GenerateReturnStatement(&ret));
ASSERT_FALSE(b.has_error()) << b.error();

View File

@ -34,8 +34,8 @@ TEST_F(BuilderTest, InsertsPreambleWithImport) {
ast::Module m;
m.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "glsl"));
Builder b;
ASSERT_TRUE(b.Build(m));
Builder b(&m);
ASSERT_TRUE(b.Build());
ASSERT_EQ(b.preamble().size(), 4u);
EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader
@ -47,8 +47,8 @@ OpMemoryModel Logical Vulkan
TEST_F(BuilderTest, InsertsPreambleWithoutImport) {
ast::Module m;
Builder b;
ASSERT_TRUE(b.Build(m));
Builder b(&m);
ASSERT_TRUE(b.Build());
EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader
OpCapability VulkanMemoryModel
OpMemoryModel Logical Vulkan
@ -56,7 +56,8 @@ OpMemoryModel Logical Vulkan
}
TEST_F(BuilderTest, TracksIdBounds) {
Builder b;
ast::Module mod;
Builder b(&mod);
for (size_t i = 0; i < 5; i++) {
EXPECT_EQ(b.next_id(), i + 1);

View File

@ -43,7 +43,8 @@ TEST_F(BuilderTest_Type, GenerateAlias) {
ast::type::F32Type f32;
ast::type::AliasType alias_type("my_type", &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&alias_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -58,7 +59,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedAlias) {
ast::type::F32Type f32;
ast::type::AliasType alias_type("my_type", &f32);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&alias_type), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&i32), 2u);
@ -73,7 +75,8 @@ TEST_F(BuilderTest_Type, GenerateRuntimeArray) {
ast::type::I32Type i32;
ast::type::ArrayType ary(&i32);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&ary);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(1u, id);
@ -87,7 +90,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedRuntimeArray) {
ast::type::I32Type i32;
ast::type::ArrayType ary(&i32);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&ary), 1u);
EXPECT_EQ(b.GenerateTypeIfNeeded(&ary), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
@ -101,7 +105,8 @@ TEST_F(BuilderTest_Type, GenerateArray) {
ast::type::I32Type i32;
ast::type::ArrayType ary(&i32, 4);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&ary);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(1u, id);
@ -117,7 +122,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedArray) {
ast::type::I32Type i32;
ast::type::ArrayType ary(&i32, 4);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&ary), 1u);
EXPECT_EQ(b.GenerateTypeIfNeeded(&ary), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
@ -132,7 +138,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedArray) {
TEST_F(BuilderTest_Type, GenerateBool) {
ast::type::BoolType bool_type;
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&bool_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -146,7 +153,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedBool) {
ast::type::I32Type i32;
ast::type::BoolType bool_type;
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&bool_type), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&i32), 2u);
@ -158,7 +166,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedBool) {
TEST_F(BuilderTest_Type, GenerateF32) {
ast::type::F32Type f32;
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&f32);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -172,7 +181,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedF32) {
ast::type::I32Type i32;
ast::type::F32Type f32;
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&f32), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&i32), 2u);
@ -184,7 +194,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedF32) {
TEST_F(BuilderTest_Type, GenerateI32) {
ast::type::I32Type i32;
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&i32);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -198,7 +209,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedI32) {
ast::type::I32Type i32;
ast::type::F32Type f32;
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&i32), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&f32), 2u);
@ -211,7 +223,8 @@ TEST_F(BuilderTest_Type, GenerateMatrix) {
ast::type::F32Type f32;
ast::type::MatrixType mat_type(&f32, 3, 2);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&mat_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -227,7 +240,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedMatrix) {
ast::type::I32Type i32;
ast::type::MatrixType mat_type(&i32, 3, 4);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&mat_type), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&i32), 3u);
@ -240,7 +254,8 @@ TEST_F(BuilderTest_Type, GeneratePtr) {
ast::type::I32Type i32;
ast::type::PointerType ptr(&i32, ast::StorageClass::kOutput);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&ptr);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(1u, id);
@ -254,7 +269,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedPtr) {
ast::type::I32Type i32;
ast::type::PointerType ptr(&i32, ast::StorageClass::kOutput);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&ptr), 1u);
EXPECT_EQ(b.GenerateTypeIfNeeded(&ptr), 1u);
}
@ -263,7 +279,8 @@ TEST_F(BuilderTest_Type, GenerateStruct_Empty) {
auto s = std::make_unique<ast::Struct>();
ast::type::StructType s_type(std::move(s));
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&s_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -287,7 +304,8 @@ TEST_F(BuilderTest_Type, GenerateStruct) {
ast::type::StructType s_type(std::move(s));
s_type.set_name("my_struct");
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&s_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -313,7 +331,8 @@ TEST_F(BuilderTest_Type, GenerateStruct_Decorated) {
ast::type::StructType s_type(std::move(s));
s_type.set_name("my_struct");
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&s_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -346,7 +365,8 @@ TEST_F(BuilderTest_Type, GenerateStruct_DecoratedMembers) {
std::move(members));
ast::type::StructType s_type(std::move(s));
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&s_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -365,7 +385,8 @@ OpMemberDecorate %1 1 Offset 8
TEST_F(BuilderTest_Type, GenerateU32) {
ast::type::U32Type u32;
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&u32);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -379,7 +400,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedU32) {
ast::type::U32Type u32;
ast::type::F32Type f32;
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&u32), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&f32), 2u);
@ -392,7 +414,8 @@ TEST_F(BuilderTest_Type, GenerateVector) {
ast::type::F32Type f32;
ast::type::VectorType vec_type(&f32, 3);
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&vec_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -407,7 +430,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedVector) {
ast::type::I32Type i32;
ast::type::VectorType vec_type(&i32, 3);
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&vec_type), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&i32), 2u);
@ -419,7 +443,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedVector) {
TEST_F(BuilderTest_Type, GenerateVoid) {
ast::type::VoidType void_type;
Builder b;
ast::Module mod;
Builder b(&mod);
auto id = b.GenerateTypeIfNeeded(&void_type);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(id, 1u);
@ -433,7 +458,8 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedVoid) {
ast::type::I32Type i32;
ast::type::VoidType void_type;
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.GenerateTypeIfNeeded(&void_type), 1u);
ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(b.GenerateTypeIfNeeded(&i32), 2u);
@ -454,7 +480,8 @@ using PtrDataTest = testing::TestWithParam<PtrData>;
TEST_P(PtrDataTest, ConvertStorageClass) {
auto params = GetParam();
Builder b;
ast::Module mod;
Builder b(&mod);
EXPECT_EQ(b.ConvertStorageClass(params.ast_class), params.result);
}
INSTANTIATE_TEST_SUITE_P(

View File

@ -20,12 +20,13 @@ namespace tint {
namespace writer {
namespace spirv {
Generator::Generator(ast::Module module) : writer::Writer(std::move(module)) {}
Generator::Generator(ast::Module module)
: writer::Writer(std::move(module)), builder_(&module_) {}
Generator::~Generator() = default;
bool Generator::Generate() {
if (!builder_.Build(module_)) {
if (!builder_.Build()) {
set_error(builder_.error());
return false;
}