mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-07-15 09:35:57 +00:00
Make private all TypeDeterminer::DetermineXXX() methods, forcing all tests to use the root-level TypeDeterminer::Determine() method. Remove TypeDeterminer::RegisterVariableForTesting(). The main use for calling the TypeDeterminer::DetermineXXX() methods was to perform type determination on a partial AST. This was messy and often resulting in multiple calls into TypeDeterminer. Most tests already perform a full TypeDeterminer::Determine() call when the program is built, so many of these were redundant. The exposure of these internal methods for testing also makes refactoring the TypeDeterminer extremely difficult. Add a number of ProgramBuilder helper methods for attaching the partial AST in these tests to the root of the AST, greatly simplifying the use of the TypeDeterminer: * ProgramBuilder::Global() and ProgramBuilder::GlobalConst() are helpers that register the variable returned by ProgramBuilder::Var() and ProgramBuilder::Const(), respectively. * ProgramBuilder::WrapInFunction() is a variadic function that accepts variables, expressions and statements, attaching these to the root of the AST via a dummy function. Most test classes now no longer use their own TypeDeterminer, and instead properly depend on the automatic type determination performed at Program build time. Bug: tint:390 Change-Id: Ie901890420c5de170cdf2a7aaef9b96fc3bebd60 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40062 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: David Neto <dneto@google.com>
197 lines
4.8 KiB
C++
197 lines
4.8 KiB
C++
// Copyright 2020 The Tint Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include <memory>
|
|
|
|
#include "gtest/gtest.h"
|
|
#include "src/ast/assignment_statement.h"
|
|
#include "src/ast/discard_statement.h"
|
|
#include "src/ast/float_literal.h"
|
|
#include "src/ast/identifier_expression.h"
|
|
#include "src/ast/loop_statement.h"
|
|
#include "src/ast/return_statement.h"
|
|
#include "src/ast/variable.h"
|
|
#include "src/ast/variable_decl_statement.h"
|
|
#include "src/program.h"
|
|
#include "src/type/f32_type.h"
|
|
#include "src/writer/msl/generator_impl.h"
|
|
#include "src/writer/msl/test_helper.h"
|
|
|
|
namespace tint {
|
|
namespace writer {
|
|
namespace msl {
|
|
namespace {
|
|
|
|
using MslGeneratorImplTest = TestHelper;
|
|
|
|
TEST_F(MslGeneratorImplTest, Emit_Loop) {
|
|
auto* body = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::DiscardStatement>(),
|
|
});
|
|
auto* l = create<ast::LoopStatement>(body, nullptr);
|
|
WrapInFunction(l);
|
|
|
|
GeneratorImpl& gen = Build();
|
|
|
|
gen.increment_indent();
|
|
|
|
ASSERT_TRUE(gen.EmitStatement(l)) << gen.error();
|
|
EXPECT_EQ(gen.result(), R"( for(;;) {
|
|
discard_fragment();
|
|
}
|
|
)");
|
|
}
|
|
|
|
TEST_F(MslGeneratorImplTest, Emit_LoopWithContinuing) {
|
|
auto* body = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::DiscardStatement>(),
|
|
});
|
|
auto* continuing = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::ReturnStatement>(),
|
|
});
|
|
auto* l = create<ast::LoopStatement>(body, continuing);
|
|
WrapInFunction(l);
|
|
|
|
GeneratorImpl& gen = Build();
|
|
|
|
gen.increment_indent();
|
|
|
|
ASSERT_TRUE(gen.EmitStatement(l)) << gen.error();
|
|
EXPECT_EQ(gen.result(), R"( {
|
|
bool tint_msl_is_first_1 = true;
|
|
for(;;) {
|
|
if (!tint_msl_is_first_1) {
|
|
return;
|
|
}
|
|
tint_msl_is_first_1 = false;
|
|
|
|
discard_fragment();
|
|
}
|
|
}
|
|
)");
|
|
}
|
|
|
|
TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
|
|
auto* body = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::DiscardStatement>(),
|
|
});
|
|
auto* continuing = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::ReturnStatement>(),
|
|
});
|
|
auto* inner = create<ast::LoopStatement>(body, continuing);
|
|
|
|
body = create<ast::BlockStatement>(ast::StatementList{
|
|
inner,
|
|
});
|
|
|
|
continuing = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::AssignmentStatement>(Expr("lhs"), Expr("rhs")),
|
|
});
|
|
|
|
auto* outer = create<ast::LoopStatement>(body, continuing);
|
|
WrapInFunction(outer);
|
|
|
|
GeneratorImpl& gen = Build();
|
|
|
|
gen.increment_indent();
|
|
|
|
ASSERT_TRUE(gen.EmitStatement(outer)) << gen.error();
|
|
EXPECT_EQ(gen.result(), R"( {
|
|
bool tint_msl_is_first_1 = true;
|
|
for(;;) {
|
|
if (!tint_msl_is_first_1) {
|
|
lhs = rhs;
|
|
}
|
|
tint_msl_is_first_1 = false;
|
|
|
|
{
|
|
bool tint_msl_is_first_2 = true;
|
|
for(;;) {
|
|
if (!tint_msl_is_first_2) {
|
|
return;
|
|
}
|
|
tint_msl_is_first_2 = false;
|
|
|
|
discard_fragment();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
)");
|
|
}
|
|
|
|
TEST_F(MslGeneratorImplTest, Emit_LoopWithVarUsedInContinuing) {
|
|
// loop {
|
|
// var lhs : f32 = 2.4;
|
|
// var other : f32;
|
|
// continuing {
|
|
// lhs = rhs
|
|
// }
|
|
// }
|
|
//
|
|
// ->
|
|
// {
|
|
// float lhs;
|
|
// float other;
|
|
// for (;;) {
|
|
// if (continuing) {
|
|
// lhs = rhs;
|
|
// }
|
|
// lhs = 2.4f;
|
|
// other = 0.0f;
|
|
// }
|
|
// }
|
|
|
|
auto* var = Var("lhs", ast::StorageClass::kFunction, ty.f32(), Expr(2.4f),
|
|
ast::VariableDecorationList{});
|
|
|
|
auto* body = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::VariableDeclStatement>(var),
|
|
create<ast::VariableDeclStatement>(
|
|
Var("other", ast::StorageClass::kFunction, ty.f32()))});
|
|
|
|
auto* continuing = create<ast::BlockStatement>(ast::StatementList{
|
|
create<ast::AssignmentStatement>(Expr("lhs"), Expr("rhs")),
|
|
});
|
|
|
|
auto* outer = create<ast::LoopStatement>(body, continuing);
|
|
WrapInFunction(outer);
|
|
|
|
GeneratorImpl& gen = Build();
|
|
|
|
gen.increment_indent();
|
|
|
|
ASSERT_TRUE(gen.EmitStatement(outer)) << gen.error();
|
|
EXPECT_EQ(gen.result(), R"( {
|
|
bool tint_msl_is_first_1 = true;
|
|
float lhs;
|
|
float other;
|
|
for(;;) {
|
|
if (!tint_msl_is_first_1) {
|
|
lhs = rhs;
|
|
}
|
|
tint_msl_is_first_1 = false;
|
|
|
|
lhs = 2.400000095f;
|
|
other = 0.0f;
|
|
}
|
|
}
|
|
)");
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace msl
|
|
} // namespace writer
|
|
} // namespace tint
|