[hlsl-writer] Add loop support.
This CL adds support for the LoopStatement emission in HLSL. Bug: tint:7 Change-Id: Ie42b24abff3a69c9cbfe3d3c8ab6fb9b1823e61d Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25849 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
97bc4ad8d6
commit
8278f2c6f0
1
BUILD.gn
1
BUILD.gn
|
@ -1054,6 +1054,7 @@ source_set("tint_unittests_hlsl_writer_src") {
|
||||||
"src/writer/hlsl/generator_impl_discard_test.cc",
|
"src/writer/hlsl/generator_impl_discard_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_identifier_test.cc",
|
"src/writer/hlsl/generator_impl_identifier_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_if_test.cc",
|
"src/writer/hlsl/generator_impl_if_test.cc",
|
||||||
|
"src/writer/hlsl/generator_impl_loop_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_member_accessor_test.cc",
|
"src/writer/hlsl/generator_impl_member_accessor_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_return_test.cc",
|
"src/writer/hlsl/generator_impl_return_test.cc",
|
||||||
"src/writer/hlsl/generator_impl_switch_test.cc",
|
"src/writer/hlsl/generator_impl_switch_test.cc",
|
||||||
|
|
|
@ -562,6 +562,7 @@ if (${TINT_BUILD_HLSL_WRITER})
|
||||||
writer/hlsl/generator_impl_discard_test.cc
|
writer/hlsl/generator_impl_discard_test.cc
|
||||||
writer/hlsl/generator_impl_identifier_test.cc
|
writer/hlsl/generator_impl_identifier_test.cc
|
||||||
writer/hlsl/generator_impl_if_test.cc
|
writer/hlsl/generator_impl_if_test.cc
|
||||||
|
writer/hlsl/generator_impl_loop_test.cc
|
||||||
writer/hlsl/generator_impl_member_accessor_test.cc
|
writer/hlsl/generator_impl_member_accessor_test.cc
|
||||||
writer/hlsl/generator_impl_return_test.cc
|
writer/hlsl/generator_impl_return_test.cc
|
||||||
writer/hlsl/generator_impl_switch_test.cc
|
writer/hlsl/generator_impl_switch_test.cc
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include "src/ast/float_literal.h"
|
#include "src/ast/float_literal.h"
|
||||||
#include "src/ast/identifier_expression.h"
|
#include "src/ast/identifier_expression.h"
|
||||||
#include "src/ast/if_statement.h"
|
#include "src/ast/if_statement.h"
|
||||||
|
#include "src/ast/loop_statement.h"
|
||||||
#include "src/ast/member_accessor_expression.h"
|
#include "src/ast/member_accessor_expression.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
#include "src/ast/sint_literal.h"
|
#include "src/ast/sint_literal.h"
|
||||||
|
@ -529,6 +530,60 @@ bool GeneratorImpl::EmitZeroValue(ast::type::Type* type) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
|
||||||
|
loop_emission_counter_++;
|
||||||
|
|
||||||
|
std::string guard = namer_.NameFor("tint_hlsl_is_first_" +
|
||||||
|
std::to_string(loop_emission_counter_));
|
||||||
|
|
||||||
|
if (stmt->has_continuing()) {
|
||||||
|
make_indent();
|
||||||
|
|
||||||
|
// Continuing variables get their own scope.
|
||||||
|
out_ << "{" << std::endl;
|
||||||
|
increment_indent();
|
||||||
|
|
||||||
|
make_indent();
|
||||||
|
out_ << "bool " << guard << " = true;" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
make_indent();
|
||||||
|
out_ << "for(;;) {" << std::endl;
|
||||||
|
increment_indent();
|
||||||
|
|
||||||
|
if (stmt->has_continuing()) {
|
||||||
|
make_indent();
|
||||||
|
out_ << "if (!" << guard << ") ";
|
||||||
|
|
||||||
|
if (!EmitBlockAndNewline(stmt->continuing())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
make_indent();
|
||||||
|
out_ << guard << " = false;" << std::endl;
|
||||||
|
out_ << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& s : *(stmt->body())) {
|
||||||
|
if (!EmitStatement(s.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
decrement_indent();
|
||||||
|
make_indent();
|
||||||
|
out_ << "}" << std::endl;
|
||||||
|
|
||||||
|
// Close the scope for any continuing variables.
|
||||||
|
if (stmt->has_continuing()) {
|
||||||
|
decrement_indent();
|
||||||
|
make_indent();
|
||||||
|
out_ << "}" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) {
|
bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) {
|
||||||
if (!EmitExpression(expr->structure())) {
|
if (!EmitExpression(expr->structure())) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -581,6 +636,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
if (stmt->IsIf()) {
|
if (stmt->IsIf()) {
|
||||||
return EmitIf(stmt->AsIf());
|
return EmitIf(stmt->AsIf());
|
||||||
}
|
}
|
||||||
|
if (stmt->IsLoop()) {
|
||||||
|
return EmitLoop(stmt->AsLoop());
|
||||||
|
}
|
||||||
if (stmt->IsReturn()) {
|
if (stmt->IsReturn()) {
|
||||||
return EmitReturn(stmt->AsReturn());
|
return EmitReturn(stmt->AsReturn());
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,10 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param lit the literal to emit
|
/// @param lit the literal to emit
|
||||||
/// @returns true if the literal was successfully emitted
|
/// @returns true if the literal was successfully emitted
|
||||||
bool EmitLiteral(ast::Literal* lit);
|
bool EmitLiteral(ast::Literal* lit);
|
||||||
|
/// Handles a loop statement
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was emitted
|
||||||
|
bool EmitLoop(ast::LoopStatement* stmt);
|
||||||
/// Handles generating an identifier expression
|
/// Handles generating an identifier expression
|
||||||
/// @param expr the identifier expression
|
/// @param expr the identifier expression
|
||||||
/// @returns true if the identifeir was emitted
|
/// @returns true if the identifeir was emitted
|
||||||
|
@ -171,6 +175,7 @@ class GeneratorImpl : public TextGenerator {
|
||||||
ast::Module* module_ = nullptr;
|
ast::Module* module_ = nullptr;
|
||||||
std::string current_ep_name_;
|
std::string current_ep_name_;
|
||||||
bool generating_entry_point_ = false;
|
bool generating_entry_point_ = false;
|
||||||
|
uint32_t loop_emission_counter_ = 0;
|
||||||
ScopeStack<ast::Variable*> global_variables_;
|
ScopeStack<ast::Variable*> global_variables_;
|
||||||
std::unordered_map<std::string, EntryPointData> ep_name_to_in_data_;
|
std::unordered_map<std::string, EntryPointData> ep_name_to_in_data_;
|
||||||
std::unordered_map<std::string, EntryPointData> ep_name_to_out_data_;
|
std::unordered_map<std::string, EntryPointData> ep_name_to_out_data_;
|
||||||
|
|
|
@ -0,0 +1,184 @@
|
||||||
|
// 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/module.h"
|
||||||
|
#include "src/ast/return_statement.h"
|
||||||
|
#include "src/ast/type/f32_type.h"
|
||||||
|
#include "src/ast/variable.h"
|
||||||
|
#include "src/ast/variable_decl_statement.h"
|
||||||
|
#include "src/writer/hlsl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace hlsl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using HlslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_Loop) {
|
||||||
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
|
body->append(std::make_unique<ast::DiscardStatement>());
|
||||||
|
|
||||||
|
ast::LoopStatement l(std::move(body), {});
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&l)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( for(;;) {
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_LoopWithContinuing) {
|
||||||
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
|
body->append(std::make_unique<ast::DiscardStatement>());
|
||||||
|
|
||||||
|
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||||
|
continuing->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
|
ast::LoopStatement l(std::move(body), std::move(continuing));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&l)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( {
|
||||||
|
bool tint_hlsl_is_first_1 = true;
|
||||||
|
for(;;) {
|
||||||
|
if (!tint_hlsl_is_first_1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tint_hlsl_is_first_1 = false;
|
||||||
|
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(HlslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
|
body->append(std::make_unique<ast::DiscardStatement>());
|
||||||
|
|
||||||
|
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||||
|
continuing->append(std::make_unique<ast::ReturnStatement>());
|
||||||
|
|
||||||
|
auto inner = std::make_unique<ast::LoopStatement>(std::move(body),
|
||||||
|
std::move(continuing));
|
||||||
|
|
||||||
|
body = std::make_unique<ast::BlockStatement>();
|
||||||
|
body->append(std::move(inner));
|
||||||
|
|
||||||
|
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
|
||||||
|
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
|
||||||
|
|
||||||
|
continuing = std::make_unique<ast::BlockStatement>();
|
||||||
|
continuing->append(std::make_unique<ast::AssignmentStatement>(
|
||||||
|
std::move(lhs), std::move(rhs)));
|
||||||
|
|
||||||
|
ast::LoopStatement outer(std::move(body), std::move(continuing));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&outer)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( {
|
||||||
|
bool tint_hlsl_is_first_1 = true;
|
||||||
|
for(;;) {
|
||||||
|
if (!tint_hlsl_is_first_1) {
|
||||||
|
lhs = rhs;
|
||||||
|
}
|
||||||
|
tint_hlsl_is_first_1 = false;
|
||||||
|
|
||||||
|
{
|
||||||
|
bool tint_hlsl_is_first_2 = true;
|
||||||
|
for(;;) {
|
||||||
|
if (!tint_hlsl_is_first_2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tint_hlsl_is_first_2 = false;
|
||||||
|
|
||||||
|
discard;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(dsinclair): Handle pulling declared variables up and out of the for() if
|
||||||
|
// there is a continuing block.
|
||||||
|
TEST_F(HlslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
auto var = std::make_unique<ast::Variable>(
|
||||||
|
"lhs", ast::StorageClass::kFunction, &f32);
|
||||||
|
var->set_constructor(std::make_unique<ast::ScalarConstructorExpression>(
|
||||||
|
std::make_unique<ast::FloatLiteral>(&f32, 2.4)));
|
||||||
|
|
||||||
|
auto body = std::make_unique<ast::BlockStatement>();
|
||||||
|
body->append(std::make_unique<ast::VariableDeclStatement>(std::move(var)));
|
||||||
|
body->append(std::make_unique<ast::VariableDeclStatement>(
|
||||||
|
std::make_unique<ast::Variable>("other", ast::StorageClass::kFunction,
|
||||||
|
&f32)));
|
||||||
|
|
||||||
|
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
|
||||||
|
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
|
||||||
|
|
||||||
|
auto continuing = std::make_unique<ast::BlockStatement>();
|
||||||
|
continuing->append(std::make_unique<ast::AssignmentStatement>(
|
||||||
|
std::move(lhs), std::move(rhs)));
|
||||||
|
|
||||||
|
ast::LoopStatement outer(std::move(body), std::move(continuing));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
GeneratorImpl g(&m);
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitStatement(&outer)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( {
|
||||||
|
float lhs;
|
||||||
|
bool tint_hlsl_is_first_1 = true;
|
||||||
|
for(;;) {
|
||||||
|
if (!tint_hlsl_is_first_1) {
|
||||||
|
lhs = rhs;
|
||||||
|
}
|
||||||
|
tint_hlsl_is_first_1 = false;
|
||||||
|
|
||||||
|
lhs = 2.40000010f;
|
||||||
|
float other;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace hlsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
|
@ -132,7 +132,8 @@ TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
|
||||||
)");
|
)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(dsinclair): Make this work when we have variable declarations.
|
// TODO(dsinclair): Handle pulling declared variables up and out of the for() if
|
||||||
|
// there is a continuing block.
|
||||||
TEST_F(MslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
TEST_F(MslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
||||||
ast::type::F32Type f32;
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
@ -170,7 +171,7 @@ TEST_F(MslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
||||||
}
|
}
|
||||||
tint_msl_is_first_1 = false;
|
tint_msl_is_first_1 = false;
|
||||||
|
|
||||||
lhs = 2.4;
|
lhs = 2.40000010f;
|
||||||
float other;
|
float other;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue