Refactor transformer tests

Transformers will be moving to a transform-on-copy model, instead of transform-in-place.
Rework the tests to handle this.

Bug: tint:390
Change-Id: Id53a0ba0bd365472940d116bd686e450a29e5028
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34571
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2020-12-03 20:25:29 +00:00 committed by Commit Bot service account
parent ad0b2cb841
commit 0f37afb74e
2 changed files with 545 additions and 597 deletions

File diff suppressed because it is too large Load Diff

View File

@ -19,49 +19,79 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/ast/builder.h" #include "src/ast/builder.h"
#include "src/ast/call_statement.h"
#include "src/ast/stage_decoration.h" #include "src/ast/stage_decoration.h"
#include "src/ast/variable_decl_statement.h"
#include "src/diagnostic/diagnostic.h"
#include "src/diagnostic/formatter.h"
#include "src/transform/manager.h" #include "src/transform/manager.h"
namespace tint { namespace tint {
namespace transform { namespace transform {
namespace { namespace {
class EmitVertexPointSizeTransformTest : public testing::Test, class EmitVertexPointSizeTransformTest : public testing::Test {
public ast::BuilderWithModule {
public: public:
EmitVertexPointSizeTransformTest() { struct Output {
auto transform = std::make_unique<EmitVertexPointSizeTransform>(mod); ast::Module module;
manager = std::make_unique<Manager>(); diag::List diagnostics;
manager->append(std::move(transform)); };
Output Transform(ast::Module mod) {
Manager manager;
manager.append(std::make_unique<EmitVertexPointSizeTransform>(&mod));
manager.Run(&mod);
Output out;
out.module = std::move(mod);
auto err = manager.error();
if (!err.empty()) {
diag::Diagnostic diag;
diag.message = err;
diag.severity = diag::Severity::Error;
out.diagnostics.add(std::move(diag));
}
return out;
}
};
struct ModuleBuilder : public ast::BuilderWithModule {
ModuleBuilder() {}
ast::Module Module() {
Build();
return std::move(*mod);
} }
std::unique_ptr<Manager> manager; protected:
virtual void Build() = 0;
}; };
TEST_F(EmitVertexPointSizeTransformTest, VertexStageBasic) { TEST_F(EmitVertexPointSizeTransformTest, VertexStageBasic) {
struct Builder : ModuleBuilder {
void Build() override {
auto* block = create<ast::BlockStatement>(Source{}); auto* block = create<ast::BlockStatement>(Source{});
block->append(create<ast::CallStatement>(create<ast::CallExpression>(
Source{},
create<ast::IdentifierExpression>(
Source{}, "builtin_assignments_should_happen_before_this"),
ast::ExpressionList{})));
mod->AddFunction(create<ast::Function>( block->append(create<ast::VariableDeclStatement>(
"non_entry_a", ast::VariableList{}, create<ast::type::Void>(), Var("builtin_assignments_should_happen_before_this",
tint::ast::StorageClass::kFunction, ty.f32)));
mod->AddFunction(
create<ast::Function>("non_entry_a", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}))); create<ast::BlockStatement>(Source{})));
auto* entry = create<ast::Function>("entry", ast::VariableList{}, auto* entry =
create<ast::type::Void>(), block); create<ast::Function>("entry", ast::VariableList{}, ty.void_, block);
entry->set_decorations( entry->set_decorations({create<ast::StageDecoration>(
{create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{})}); ast::PipelineStage::kVertex, Source{})});
mod->AddFunction(entry); mod->AddFunction(entry);
mod->AddFunction(create<ast::Function>( mod->AddFunction(
"non_entry_b", ast::VariableList{}, create<ast::type::Void>(), create<ast::Function>("non_entry_b", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}))); create<ast::BlockStatement>(Source{})));
}
};
manager->Run(mod); auto result = Transform(Builder{}.Module());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{ auto* expected = R"(Module{
DecoratedVariable{ DecoratedVariable{
@ -84,10 +114,12 @@ TEST_F(EmitVertexPointSizeTransformTest, VertexStageBasic) {
Identifier[__ptr_out__f32]{tint_pointsize} Identifier[__ptr_out__f32]{tint_pointsize}
ScalarConstructor[__f32]{1.000000} ScalarConstructor[__f32]{1.000000}
} }
Call[not set]{ VariableDeclStatement{
Identifier[not set]{builtin_assignments_should_happen_before_this} Variable{
( builtin_assignments_should_happen_before_this
) function
__f32
}
} }
} }
Function non_entry_b -> __void Function non_entry_b -> __void
@ -96,26 +128,32 @@ TEST_F(EmitVertexPointSizeTransformTest, VertexStageBasic) {
} }
} }
)"; )";
EXPECT_EQ(expected, mod->to_str()); EXPECT_EQ(expected, result.module.to_str());
} }
TEST_F(EmitVertexPointSizeTransformTest, VertexStageEmpty) { TEST_F(EmitVertexPointSizeTransformTest, VertexStageEmpty) {
mod->AddFunction(create<ast::Function>( struct Builder : ModuleBuilder {
"non_entry_a", ast::VariableList{}, create<ast::type::Void>(), void Build() override {
mod->AddFunction(
create<ast::Function>("non_entry_a", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}))); create<ast::BlockStatement>(Source{})));
auto* entry = create<ast::Function>("entry", ast::VariableList{}, auto* entry =
create<ast::type::Void>(), create<ast::Function>("entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{})); create<ast::BlockStatement>(Source{}));
entry->set_decorations( entry->set_decorations({create<ast::StageDecoration>(
{create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{})}); ast::PipelineStage::kVertex, Source{})});
mod->AddFunction(entry); mod->AddFunction(entry);
mod->AddFunction(create<ast::Function>( mod->AddFunction(
"non_entry_b", ast::VariableList{}, create<ast::type::Void>(), create<ast::Function>("non_entry_b", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{}))); create<ast::BlockStatement>(Source{})));
}
};
manager->Run(mod); auto result = Transform(Builder{}.Module());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{ auto* expected = R"(Module{
DecoratedVariable{ DecoratedVariable{
@ -145,25 +183,31 @@ TEST_F(EmitVertexPointSizeTransformTest, VertexStageEmpty) {
} }
} }
)"; )";
EXPECT_EQ(expected, mod->to_str()); EXPECT_EQ(expected, result.module.to_str());
} }
TEST_F(EmitVertexPointSizeTransformTest, NonVertexStage) { TEST_F(EmitVertexPointSizeTransformTest, NonVertexStage) {
auto* fragment_entry = create<ast::Function>( struct Builder : ModuleBuilder {
"fragment_entry", ast::VariableList{}, create<ast::type::Void>(), void Build() override {
auto* fragment_entry =
create<ast::Function>("fragment_entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{})); create<ast::BlockStatement>(Source{}));
fragment_entry->set_decorations( fragment_entry->set_decorations({create<ast::StageDecoration>(
{create<ast::StageDecoration>(ast::PipelineStage::kFragment, Source{})}); ast::PipelineStage::kFragment, Source{})});
mod->AddFunction(fragment_entry); mod->AddFunction(fragment_entry);
auto* compute_entry = create<ast::Function>( auto* compute_entry =
"compute_entry", ast::VariableList{}, create<ast::type::Void>(), create<ast::Function>("compute_entry", ast::VariableList{}, ty.void_,
create<ast::BlockStatement>(Source{})); create<ast::BlockStatement>(Source{}));
compute_entry->set_decorations( compute_entry->set_decorations({create<ast::StageDecoration>(
{create<ast::StageDecoration>(ast::PipelineStage::kCompute, Source{})}); ast::PipelineStage::kCompute, Source{})});
mod->AddFunction(compute_entry); mod->AddFunction(compute_entry);
}
};
manager->Run(mod); auto result = Transform(Builder{}.Module());
ASSERT_FALSE(result.diagnostics.contains_errors())
<< diag::Formatter().format(result.diagnostics);
auto* expected = R"(Module{ auto* expected = R"(Module{
Function fragment_entry -> __void Function fragment_entry -> __void
@ -178,7 +222,7 @@ TEST_F(EmitVertexPointSizeTransformTest, NonVertexStage) {
} }
} }
)"; )";
EXPECT_EQ(expected, mod->to_str()); EXPECT_EQ(expected, result.module.to_str());
} }
} // namespace } // namespace