diff --git a/BUILD.gn b/BUILD.gn index 0d68e4121e..80d4a4fdd8 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1213,6 +1213,7 @@ source_set("tint_unittests_msl_writer_src") { source_set("tint_unittests_hlsl_writer_src") { sources = [ + "src/transform/hlsl_test.cc", "src/writer/hlsl/generator_impl_alias_type_test.cc", "src/writer/hlsl/generator_impl_array_accessor_test.cc", "src/writer/hlsl/generator_impl_assign_test.cc", @@ -1237,6 +1238,7 @@ source_set("tint_unittests_hlsl_writer_src") { "src/writer/hlsl/generator_impl_member_accessor_test.cc", "src/writer/hlsl/generator_impl_module_constant_test.cc", "src/writer/hlsl/generator_impl_return_test.cc", + "src/writer/hlsl/generator_impl_sanitizer_tests.cc", "src/writer/hlsl/generator_impl_switch_test.cc", "src/writer/hlsl/generator_impl_test.cc", "src/writer/hlsl/generator_impl_type_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d1648b9972..008ce14002 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -732,6 +732,7 @@ if(${TINT_BUILD_TESTS}) if (${TINT_BUILD_HLSL_WRITER}) list(APPEND TINT_TEST_SRCS + transform/hlsl_test.cc writer/hlsl/generator_impl_alias_type_test.cc writer/hlsl/generator_impl_array_accessor_test.cc writer/hlsl/generator_impl_assign_test.cc @@ -756,6 +757,7 @@ if(${TINT_BUILD_TESTS}) writer/hlsl/generator_impl_member_accessor_test.cc writer/hlsl/generator_impl_module_constant_test.cc writer/hlsl/generator_impl_return_test.cc + writer/hlsl/generator_impl_sanitizer_tests.cc writer/hlsl/generator_impl_switch_test.cc writer/hlsl/generator_impl_test.cc writer/hlsl/generator_impl_type_test.cc diff --git a/src/clone_context.h b/src/clone_context.h index 0442b3ec9d..39edc4e3c0 100644 --- a/src/clone_context.h +++ b/src/clone_context.h @@ -108,14 +108,14 @@ class CloneContext { // version instead of making yet another copy. auto it = cloned_.find(a); if (it != cloned_.end()) { - return CheckedCast(it->second); + return CheckedCast(it->second); } // First time clone and no replacer transforms matched. // Clone with T::Clone(). auto* c = a->Clone(this); cloned_.emplace(a, c); - return CheckedCast(c); + return CheckedCast(c); } /// Clones the Source `s` into `dst` diff --git a/src/transform/hlsl.cc b/src/transform/hlsl.cc index acaba88508..c75eae506d 100644 --- a/src/transform/hlsl.cc +++ b/src/transform/hlsl.cc @@ -1,4 +1,4 @@ -// Copyright 2020 The Tint Authors. +// Copyright 2021 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. @@ -16,7 +16,11 @@ #include +#include "src/ast/variable_decl_statement.h" #include "src/program_builder.h" +#include "src/semantic/expression.h" +#include "src/semantic/statement.h" +#include "src/type/array_type.h" namespace tint { namespace transform { @@ -26,9 +30,76 @@ Hlsl::~Hlsl() = default; Transform::Output Hlsl::Run(const Program* in) { ProgramBuilder out; - CloneContext(&out, in).Clone(); + CloneContext ctx(&out, in); + PromoteArrayInitializerToConstVar(ctx); + ctx.Clone(); return Output{Program(std::move(out))}; } +void Hlsl::PromoteArrayInitializerToConstVar(CloneContext& ctx) const { + // Scan the AST nodes for array initializers which need to be promoted to + // their own constant declaration. + + // Note: Correct handling of arrays-of-arrays is guaranteed due to the + // depth-first traversal of the ast::Node::Clone() methods: + // + // The inner-most array initializers are traversed first, and they are hoisted + // to const variables declared just above the statement of use. The outer + // array initializer will then be hoisted, inserting themselves between the + // inner array declaration and the statement of use. This pattern applies + // correctly to any nested depth. + // + // Depth-first traversal of the AST is guaranteed because AST nodes are fully + // immutable and require their children to be constructed first so their + // pointer can be passed to the parent's constructor. + + for (auto* src_node : ctx.src->ASTNodes().Objects()) { + if (auto* src_init = src_node->As()) { + if (auto* src_sem_expr = ctx.src->Sem().Get(src_init)) { + auto* src_sem_stmt = src_sem_expr->Stmt(); + if (!src_sem_stmt) { + // Expression is outside of a statement. This usually means the + // expression is part of a global (module-scope) constant declaration. + // These must be constexpr, and so cannot contain the type of + // expressions that must be sanitized. + continue; + } + auto* src_stmt = src_sem_stmt->Declaration(); + + if (auto* src_var_decl = src_stmt->As()) { + if (src_var_decl->variable()->constructor() == src_init) { + // This statement is just a variable declaration with the array + // initializer as the constructor value. This is what we're + // attempting to transform to, and so ignore. + continue; + } + } + + if (auto* src_array_ty = src_sem_expr->Type()->As()) { + // Create a new symbol for the constant + auto dst_symbol = ctx.dst->Symbols().New(); + // Clone the array type + auto* dst_array_ty = ctx.Clone(src_array_ty); + // Clone the array initializer + auto* dst_init = ctx.Clone(src_init); + // Construct the constant that holds the array + auto* dst_var = ctx.dst->Const( + dst_symbol, ast::StorageClass::kFunction, dst_array_ty, dst_init); + // Construct the variable declaration statement + auto* dst_var_decl = + ctx.dst->create(dst_var); + // Construct the identifier for referencing the constant + auto* dst_ident = ctx.dst->Expr(dst_symbol); + + // Insert the constant before the usage + ctx.InsertBefore(src_stmt, dst_var_decl); + // Replace the inlined array with a reference to the constant + ctx.Replace(src_init, dst_ident); + } + } + } + } +} + } // namespace transform } // namespace tint diff --git a/src/transform/hlsl.h b/src/transform/hlsl.h index c8a04cc21a..193e54bc30 100644 --- a/src/transform/hlsl.h +++ b/src/transform/hlsl.h @@ -18,6 +18,10 @@ #include "src/transform/transform.h" namespace tint { + +// Forward declarations +class CloneContext; + namespace transform { /// Hlsl is a transform used to sanitize a Program for use with the Hlsl writer. @@ -36,6 +40,12 @@ class Hlsl : public Transform { /// @param program the source program to transform /// @returns the transformation result Output Run(const Program* program) override; + + private: + /// Hoists the array initializer to a constant variable, declared just before + /// the array usage statement. + /// See crbug.com/tint/406 for more details + void PromoteArrayInitializerToConstVar(CloneContext& ctx) const; }; } // namespace transform diff --git a/src/transform/hlsl_test.cc b/src/transform/hlsl_test.cc new file mode 100644 index 0000000000..38a4e69dae --- /dev/null +++ b/src/transform/hlsl_test.cc @@ -0,0 +1,154 @@ +// Copyright 2021 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 "src/transform/hlsl.h" + +#include +#include +#include + +#include "src/transform/test_helper.h" + +namespace tint { +namespace transform { +namespace { + +using HlslTest = TransformTest; + +TEST_F(HlslTest, PromoteArrayInitializerToConstVar_Basic) { + auto* src = R"( +[[stage(vertex)]] +fn main() -> void { + var f0 : f32 = 1.0; + var f1 : f32 = 2.0; + var f2 : f32 = 3.0; + var f3 : f32 = 4.0; + var i : i32 = array(f0, f1, f2, f3)[2]; +} +)"; + + auto* expect = R"( +[[stage(vertex)]] +fn main() -> void { + var f0 : f32 = 1.0; + var f1 : f32 = 2.0; + var f2 : f32 = 3.0; + var f3 : f32 = 4.0; + const tint_symbol_1 : array = array(f0, f1, f2, f3); + var i : i32 = tint_symbol_1[2]; +} +)"; + + auto got = Transform(src); + + EXPECT_EQ(expect, got); +} + +TEST_F(HlslTest, PromoteArrayInitializerToConstVar_ArrayInArray) { + auto* src = R"( +[[stage(vertex)]] +fn main() -> void { + var i : i32 = array, 2>(array(1.0, 2.0), array(3.0, 4.0))[0][1]; +} +)"; + + auto* expect = R"( +[[stage(vertex)]] +fn main() -> void { + const tint_symbol_1 : array = array(1.0, 2.0); + const tint_symbol_2 : array = array(3.0, 4.0); + const tint_symbol_3 : array, 2> = array, 2>(tint_symbol_1, tint_symbol_2); + var i : i32 = tint_symbol_3[0][1]; +} +)"; + + auto got = Transform(src); + + EXPECT_EQ(expect, got); +} + +TEST_F(HlslTest, PromoteArrayInitializerToConstVar_NoChangeOnArrayVarDecl) { + auto* src = R"( +[[stage(vertex)]] +fn main() -> void { + var local_arr : array = array(0.0, 1.0, 2.0, 3.0); +} + +const module_arr : array = array(0.0, 1.0, 2.0, 3.0); +)"; + + auto* expect = src; + + auto got = Transform(src); + + EXPECT_EQ(expect, got); +} + +TEST_F(HlslTest, PromoteArrayInitializerToConstVar_Bug406) { + // See crbug.com/tint/406 + auto* src = R"( +[[block]] +struct Uniforms { + [[offset(0)]] + transform : mat2x2; +}; + +[[group(0), binding(0)]] var ubo : Uniforms; + +[[builtin(vertex_index)]] var vertex_index : u32; + +[[builtin(position)]] var position : vec4; + +[[stage(vertex)]] +fn main() -> void { + const transform : mat2x2 = ubo.transform; + var coord : array, 3> = array, 3>( + vec2(-1.0, 1.0), + vec2( 1.0, 1.0), + vec2(-1.0, -1.0) + )[vertex_index]; + position = vec4(transform * coord, 0.0, 1.0); +} +)"; + + auto* expect = R"( +[[block]] +struct Uniforms { + [[offset(0)]] + transform : mat2x2; +}; + +[[group(0), binding(0)]] var ubo : Uniforms; + +[[builtin(vertex_index)]] var vertex_index : u32; + +[[builtin(position)]] var position : vec4; + +[[stage(vertex)]] +fn main() -> void { + const transform : mat2x2 = ubo.transform; + const tint_symbol_1 : array, 3> = array, 3>(vec2(-1.0, 1.0), vec2(1.0, 1.0), vec2(-1.0, -1.0)); + var coord : array, 3> = tint_symbol_1[vertex_index]; + position = vec4((transform * coord), 0.0, 1.0); +} +)"; + + auto got = Transform(src); + + EXPECT_EQ(expect, got); +} + +} // namespace +} // namespace transform +} // namespace tint diff --git a/src/writer/hlsl/generator_impl_sanitizer_tests.cc b/src/writer/hlsl/generator_impl_sanitizer_tests.cc new file mode 100644 index 0000000000..b0952efa54 --- /dev/null +++ b/src/writer/hlsl/generator_impl_sanitizer_tests.cc @@ -0,0 +1,63 @@ +// Copyright 2021 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 +#include + +#include "src/ast/identifier_expression.h" +#include "src/ast/stage_decoration.h" +#include "src/ast/unary_op_expression.h" +#include "src/ast/variable_decl_statement.h" +#include "src/program.h" +#include "src/writer/hlsl/test_helper.h" + +namespace tint { +namespace writer { +namespace hlsl { +namespace { + +using HlslSanitizerTest = TestHelper; + +TEST_F(HlslSanitizerTest, PromoteArrayInitializerToConstVar) { + auto* array_init = array(1, 2, 3, 4); + auto* array_index = IndexAccessor(array_init, 3); + auto* pos = Var("pos", ast::StorageClass::kFunction, ty.i32(), array_index); + + Func("main", ast::VariableList{}, ty.void_(), + ast::StatementList{ + create(pos), + }, + ast::FunctionDecorationList{ + create(ast::PipelineStage::kVertex), + }); + + GeneratorImpl& gen = SanitizeAndBuild(); + + ASSERT_TRUE(gen.Generate(out)) << gen.error(); + + auto got = result(); + auto* expect = R"(void main() { + const int tint_symbol_1[4] = {1, 2, 3, 4}; + int pos = tint_symbol_1[3]; + return; +} + +)"; + EXPECT_EQ(expect, got); +} + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint diff --git a/src/writer/hlsl/test_helper.h b/src/writer/hlsl/test_helper.h index 01a3905d5a..b49b8e495b 100644 --- a/src/writer/hlsl/test_helper.h +++ b/src/writer/hlsl/test_helper.h @@ -23,6 +23,7 @@ #include "gtest/gtest.h" #include "src/diagnostic/formatter.h" #include "src/program_builder.h" +#include "src/transform/hlsl.h" #include "src/type_determiner.h" #include "src/writer/hlsl/generator_impl.h" @@ -37,7 +38,7 @@ class TestHelperBase : public BODY, public ProgramBuilder { TestHelperBase() = default; ~TestHelperBase() override = default; - /// Builds and returns a GeneratorImpl from the program. + /// Builds the program and returns a GeneratorImpl from the program. /// @note The generator is only built once. Multiple calls to Build() will /// return the same GeneratorImpl without rebuilding. /// @return the built generator @@ -45,19 +46,49 @@ class TestHelperBase : public BODY, public ProgramBuilder { if (gen_) { return *gen_; } + diag::Formatter formatter; [&]() { ASSERT_TRUE(IsValid()) << "Builder program is not valid\n" - << diag::Formatter().format(Diagnostics()); + << formatter.format(Diagnostics()); }(); program = std::make_unique(std::move(*this)); [&]() { ASSERT_TRUE(program->IsValid()) - << diag::Formatter().format(program->Diagnostics()); + << formatter.format(program->Diagnostics()); }(); gen_ = std::make_unique(program.get()); return *gen_; } + /// Builds the program, runs the program through the transform::Hlsl sanitizer + /// and returns a GeneratorImpl from the sanitized program. + /// @note The generator is only built once. Multiple calls to Build() will + /// return the same GeneratorImpl without rebuilding. + /// @return the built generator + GeneratorImpl& SanitizeAndBuild() { + if (gen_) { + return *gen_; + } + diag::Formatter formatter; + [&]() { + ASSERT_TRUE(IsValid()) << "Builder program is not valid\n" + << formatter.format(Diagnostics()); + }(); + program = std::make_unique(std::move(*this)); + [&]() { + ASSERT_TRUE(program->IsValid()) + << formatter.format(program->Diagnostics()); + }(); + auto result = transform::Hlsl().Run(program.get()); + [&]() { + ASSERT_FALSE(result.diagnostics.contains_errors()) + << formatter.format(result.diagnostics); + }(); + *program = std::move(result.program); + gen_ = std::make_unique(program.get()); + return *gen_; + } + /// @returns the result string std::string result() const { return out.str(); }