[hlsl-writer] Emit break, return and continue.

This CL updates the HLSL writer to emit break, return and continue
statements.

Bug: tint:7
Change-Id: I03eafc343e57e9e8d1efaf930023099d6f85fc57
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25220
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-21 15:58:29 +00:00 committed by dan sinclair
parent dee9136c20
commit abfdd22acc
7 changed files with 216 additions and 0 deletions

View File

@ -1019,7 +1019,10 @@ source_set("tint_unittests_msl_writer_src") {
source_set("tint_unittests_hlsl_writer_src") {
sources = [
"src/writer/hlsl/generator_impl_binary_test.cc",
"src/writer/hlsl/generator_impl_break_test.cc",
"src/writer/hlsl/generator_impl_continue_test.cc",
"src/writer/hlsl/generator_impl_identifier_test.cc",
"src/writer/hlsl/generator_impl_return_test.cc",
"src/writer/hlsl/generator_impl_test.cc",
"src/writer/hlsl/namer_test.cc",
]

View File

@ -543,7 +543,10 @@ endif()
if (${TINT_BUILD_HLSL_WRITER})
list(APPEND TINT_TEST_SRCS
writer/hlsl/generator_impl_binary_test.cc
writer/hlsl/generator_impl_break_test.cc
writer/hlsl/generator_impl_continue_test.cc
writer/hlsl/generator_impl_identifier_test.cc
writer/hlsl/generator_impl_return_test.cc
writer/hlsl/generator_impl_test.cc
writer/hlsl/namer_test.cc
)

View File

@ -16,6 +16,7 @@
#include "src/ast/binary_expression.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/return_statement.h"
namespace tint {
namespace writer {
@ -137,6 +138,18 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
return true;
}
bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
make_indent();
out_ << "break;" << std::endl;
return true;
}
bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
make_indent();
out_ << "continue;" << std::endl;
return true;
}
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsBinary()) {
return EmitBinary(expr->AsBinary());
@ -180,6 +193,41 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
return true;
}
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
make_indent();
out_ << "return";
if (generating_entry_point_) {
auto out_data = ep_name_to_out_data_.find(current_ep_name_);
if (out_data != ep_name_to_out_data_.end()) {
out_ << " " << out_data->second.var_name;
}
} else if (stmt->has_value()) {
out_ << " ";
if (!EmitExpression(stmt->value())) {
return false;
}
}
out_ << ";" << std::endl;
return true;
}
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsBreak()) {
return EmitBreak(stmt->AsBreak());
}
if (stmt->IsContinue()) {
return EmitContinue(stmt->AsContinue());
}
if (stmt->IsReturn()) {
return EmitReturn(stmt->AsReturn());
}
error_ = "unknown statement type: " + stmt->str();
return false;
}
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -39,6 +39,14 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the binary expression
/// @returns true if the expression was emitted, false otherwise
bool EmitBinary(ast::BinaryExpression* expr);
/// Handles a break statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBreak(ast::BreakStatement* stmt);
/// Handles a continue statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitContinue(ast::ContinueStatement* stmt);
/// Handles generate an Expression
/// @param expr the expression
/// @returns true if the expression was emitted
@ -47,6 +55,14 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the identifier expression
/// @returns true if the identifeir was emitted
bool EmitIdentifier(ast::IdentifierExpression* expr);
/// Handles return statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitReturn(ast::ReturnStatement* stmt);
/// Handles statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitStatement(ast::Statement* stmt);
/// Checks if the global variable is in an input or output struct
/// @param var the variable to check
@ -66,6 +82,7 @@ class GeneratorImpl : public TextGenerator {
Namer namer_;
ast::Module* module_ = nullptr;
std::string current_ep_name_;
bool generating_entry_point_ = false;
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_out_data_;

View File

@ -0,0 +1,44 @@
// 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 <vector>
#include "gtest/gtest.h"
#include "src/ast/break_statement.h"
#include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslGeneratorImplTest = testing::Test;
TEST_F(HlslGeneratorImplTest, Emit_Break) {
ast::BreakStatement b;
ast::Module m;
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
EXPECT_EQ(g.result(), " break;\n");
}
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -0,0 +1,44 @@
// 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 <vector>
#include "gtest/gtest.h"
#include "src/ast/continue_statement.h"
#include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslGeneratorImplTest = testing::Test;
TEST_F(HlslGeneratorImplTest, Emit_Continue) {
ast::ContinueStatement c;
ast::Module m;
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
EXPECT_EQ(g.result(), " continue;\n");
}
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -0,0 +1,57 @@
// 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 <vector>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/module.h"
#include "src/ast/return_statement.h"
#include "src/writer/hlsl/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslGeneratorImplTest = testing::Test;
TEST_F(HlslGeneratorImplTest, Emit_Return) {
ast::ReturnStatement r;
ast::Module m;
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
EXPECT_EQ(g.result(), " return;\n");
}
TEST_F(HlslGeneratorImplTest, Emit_ReturnWithValue) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::ReturnStatement r(std::move(expr));
ast::Module m;
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
EXPECT_EQ(g.result(), " return expr;\n");
}
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint