From 6366f6812135ef91c0db157aafc138290397e168 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 23 Jun 2020 18:22:21 +0000 Subject: [PATCH] [metal-writer] Emit constructors. This Cl adds the code to emit scalar and type constructors from the Metal backend. Bug: tint:8 Change-Id: I95c713568ae3a73b82f8c9e10119e29e3469893e Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23707 Reviewed-by: David Neto --- BUILD.gn | 1 + samples/main.cc | 2 +- src/CMakeLists.txt | 1 + src/writer/msl/generator.cc | 6 +- src/writer/msl/generator_impl.cc | 67 +++++ src/writer/msl/generator_impl.h | 19 ++ .../msl/generator_impl_constructor_test.cc | 238 ++++++++++++++++++ src/writer/wgsl/generator.cc | 6 +- test/function.wgsl | 17 ++ 9 files changed, 354 insertions(+), 3 deletions(-) create mode 100644 src/writer/msl/generator_impl_constructor_test.cc create mode 100644 test/function.wgsl diff --git a/BUILD.gn b/BUILD.gn index 677a1ccfa8..35ffa97efd 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -887,6 +887,7 @@ source_set("tint_unittests_msl_writer_src") { sources = [ "src/writer/msl/generator_impl_assign_test.cc", "src/writer/msl/generator_impl_binary_test.cc", + "src/writer/msl/generator_impl_constructor_test.cc", "src/writer/msl/generator_impl_function_test.cc", "src/writer/msl/generator_impl_identifier_test.cc", "src/writer/msl/generator_impl_return_test.cc", diff --git a/samples/main.cc b/samples/main.cc index bd1190f7d3..fcafe07512 100644 --- a/samples/main.cc +++ b/samples/main.cc @@ -452,7 +452,7 @@ int main(int argc, const char** argv) { } if (!writer->Generate()) { - std::cerr << "Failed to generate SPIR-V: " << writer->error() << std::endl; + std::cerr << "Failed to generate: " << writer->error() << std::endl; return 1; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6659416c9a..42c3ad2d7e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -495,6 +495,7 @@ if(${TINT_BUILD_MSL_WRITER}) list(APPEND TINT_TEST_SRCS writer/msl/generator_impl_assign_test.cc writer/msl/generator_impl_binary_test.cc + writer/msl/generator_impl_constructor_test.cc writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_identifier_test.cc writer/msl/generator_impl_return_test.cc diff --git a/src/writer/msl/generator.cc b/src/writer/msl/generator.cc index 6349ce8ff3..113c841a8d 100644 --- a/src/writer/msl/generator.cc +++ b/src/writer/msl/generator.cc @@ -25,7 +25,11 @@ Generator::Generator(ast::Module module) : Text(std::move(module)) {} Generator::~Generator() = default; bool Generator::Generate() { - return impl_.Generate(module_); + auto ret = impl_.Generate(module_); + if (!ret) { + error_ = impl_.error(); + } + return ret; } } // namespace msl diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index b399d45d42..ba4c935e1c 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -16,9 +16,12 @@ #include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" +#include "src/ast/bool_literal.h" +#include "src/ast/float_literal.h" #include "src/ast/function.h" #include "src/ast/identifier_expression.h" #include "src/ast/return_statement.h" +#include "src/ast/sint_literal.h" #include "src/ast/type/alias_type.h" #include "src/ast/type/array_type.h" #include "src/ast/type/bool_type.h" @@ -30,6 +33,7 @@ #include "src/ast/type/u32_type.h" #include "src/ast/type/vector_type.h" #include "src/ast/type/void_type.h" +#include "src/ast/uint_literal.h" namespace tint { namespace writer { @@ -148,10 +152,73 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { return true; } +bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) { + if (expr->IsScalarConstructor()) { + return EmitScalarConstructor(expr->AsScalarConstructor()); + } + return EmitTypeConstructor(expr->AsTypeConstructor()); +} + +bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) { + if (!EmitType(expr->type(), "")) { + return false; + } + + out_ << "("; + + bool first = true; + for (const auto& e : expr->values()) { + if (!first) { + out_ << ", "; + } + first = false; + + if (!EmitExpression(e.get())) { + return false; + } + } + + out_ << ")"; + return true; +} + +bool GeneratorImpl::EmitScalarConstructor( + ast::ScalarConstructorExpression* expr) { + return EmitLiteral(expr->literal()); +} + +bool GeneratorImpl::EmitLiteral(ast::Literal* lit) { + if (lit->IsBool()) { + out_ << (lit->AsBool()->IsTrue() ? "true" : "false"); + } else if (lit->IsFloat()) { + auto flags = out_.flags(); + auto precision = out_.precision(); + + out_.flags(flags | std::ios_base::showpoint); + out_.precision(std::numeric_limits::max_digits10); + + out_ << lit->AsFloat()->value() << "f"; + + out_.precision(precision); + out_.flags(flags); + } else if (lit->IsSint()) { + out_ << lit->AsSint()->value(); + } else if (lit->IsUint()) { + out_ << lit->AsUint()->value() << "u"; + } else { + error_ = "unknown literal type"; + return false; + } + return true; +} + bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsBinary()) { return EmitBinary(expr->AsBinary()); } + if (expr->IsConstructor()) { + return EmitConstructor(expr->AsConstructor()); + } if (expr->IsIdentifier()) { return EmitIdentifier(expr->AsIdentifier()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 5ef440701f..491f120e9c 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -18,7 +18,10 @@ #include #include +#include "src/ast/literal.h" #include "src/ast/module.h" +#include "src/ast/scalar_constructor_expression.h" +#include "src/ast/type_constructor_expression.h" #include "src/writer/text_generator.h" namespace tint { @@ -45,6 +48,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise bool EmitBinary(ast::BinaryExpression* expr); + /// Handles generating constructor expressions + /// @param expr the constructor expression + /// @returns true if the expression was emitted + bool EmitConstructor(ast::ConstructorExpression* expr); /// Handles generate an Expression /// @param expr the expression /// @returns true if the expression was emitted @@ -57,10 +64,18 @@ class GeneratorImpl : public TextGenerator { /// @param expr the identifier expression /// @returns true if the identifeir was emitted bool EmitIdentifier(ast::IdentifierExpression* expr); + /// Handles a literal + /// @param lit the literal to emit + /// @returns true if the literal was successfully emitted + bool EmitLiteral(ast::Literal* lit); /// Handles return statements /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted bool EmitReturn(ast::ReturnStatement* stmt); + /// Handles generating a scalar constructor + /// @param expr the scalar constructor expression + /// @returns true if the scalar constructor is emitted + bool EmitScalarConstructor(ast::ScalarConstructorExpression* expr); /// Handles a brace-enclosed list of statements. /// @param statements the statements to output /// @returns true if the statements were emitted @@ -78,6 +93,10 @@ class GeneratorImpl : public TextGenerator { /// @param name the name of the variable, only used for array emission /// @returns true if the type is emitted bool EmitType(ast::type::Type* type, const std::string& name); + /// Handles emitting a type constructor + /// @param expr the type constructor expression + /// @returns true if the constructor is emitted + bool EmitTypeConstructor(ast::TypeConstructorExpression* expr); }; } // namespace msl diff --git a/src/writer/msl/generator_impl_constructor_test.cc b/src/writer/msl/generator_impl_constructor_test.cc new file mode 100644 index 0000000000..2ebf533075 --- /dev/null +++ b/src/writer/msl/generator_impl_constructor_test.cc @@ -0,0 +1,238 @@ +// 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 "gtest/gtest.h" +#include "src/ast/bool_literal.h" +#include "src/ast/float_literal.h" +#include "src/ast/scalar_constructor_expression.h" +#include "src/ast/sint_literal.h" +#include "src/ast/type/array_type.h" +#include "src/ast/type/bool_type.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/i32_type.h" +#include "src/ast/type/matrix_type.h" +#include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" +#include "src/ast/type_constructor_expression.h" +#include "src/ast/uint_literal.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, EmitConstructor_Bool) { + ast::type::BoolType bool_type; + auto lit = std::make_unique(&bool_type, false); + ast::ScalarConstructorExpression expr(std::move(lit)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "false"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Int) { + ast::type::I32Type i32; + auto lit = std::make_unique(&i32, -12345); + ast::ScalarConstructorExpression expr(std::move(lit)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "-12345"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_UInt) { + ast::type::U32Type u32; + auto lit = std::make_unique(&u32, 56779); + ast::ScalarConstructorExpression expr(std::move(lit)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "56779u"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Float) { + ast::type::F32Type f32; + auto lit = std::make_unique(&f32, 1.5e27); + ast::ScalarConstructorExpression expr(std::move(lit)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "1.49999995e+27f"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Float) { + ast::type::F32Type f32; + + auto lit = std::make_unique(&f32, -1.2e-5); + ast::ExpressionList values; + values.push_back( + std::make_unique(std::move(lit))); + + ast::TypeConstructorExpression expr(&f32, std::move(values)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "float(-1.20000004e-05f)"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Bool) { + ast::type::BoolType b; + + auto lit = std::make_unique(&b, true); + ast::ExpressionList values; + values.push_back( + std::make_unique(std::move(lit))); + + ast::TypeConstructorExpression expr(&b, std::move(values)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "bool(true)"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Int) { + ast::type::I32Type i32; + + auto lit = std::make_unique(&i32, -12345); + ast::ExpressionList values; + values.push_back( + std::make_unique(std::move(lit))); + + ast::TypeConstructorExpression expr(&i32, std::move(values)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "int(-12345)"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Uint) { + ast::type::U32Type u32; + + auto lit = std::make_unique(&u32, 12345); + ast::ExpressionList values; + values.push_back( + std::make_unique(std::move(lit))); + + ast::TypeConstructorExpression expr(&u32, std::move(values)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "uint(12345u)"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Vec) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 3); + + auto lit1 = std::make_unique(&f32, 1.f); + auto lit2 = std::make_unique(&f32, 2.f); + auto lit3 = std::make_unique(&f32, 3.f); + ast::ExpressionList values; + values.push_back( + std::make_unique(std::move(lit1))); + values.push_back( + std::make_unique(std::move(lit2))); + values.push_back( + std::make_unique(std::move(lit3))); + + ast::TypeConstructorExpression expr(&vec, std::move(values)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), "float3(1.00000000f, 2.00000000f, 3.00000000f)"); +} + +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) { + ast::type::F32Type f32; + ast::type::MatrixType mat(&f32, 3, 2); + + ast::type::VectorType vec(&f32, 2); + + ast::ExpressionList mat_values; + + for (size_t i = 0; i < 3; i++) { + auto lit1 = std::make_unique( + &f32, static_cast(1 + (i * 2))); + auto lit2 = std::make_unique( + &f32, static_cast(2 + (i * 2))); + + ast::ExpressionList values; + values.push_back( + std::make_unique(std::move(lit1))); + values.push_back( + std::make_unique(std::move(lit2))); + + mat_values.push_back(std::make_unique( + &vec, std::move(values))); + } + + ast::TypeConstructorExpression expr(&mat, std::move(mat_values)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), + std::string("float2x3(float2(1.00000000f, 2.00000000f), ") + + "float2(3.00000000f, 4.00000000f), " + + "float2(5.00000000f, 6.00000000f))"); +} + +// TODO(dsinclair): Verify +TEST_F(MslGeneratorImplTest, DISABLED_EmitConstructor_Type_Array) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 3); + ast::type::ArrayType ary(&vec, 3); + + ast::ExpressionList ary_values; + + for (size_t i = 0; i < 3; i++) { + auto lit1 = std::make_unique( + &f32, static_cast(1 + (i * 3))); + auto lit2 = std::make_unique( + &f32, static_cast(2 + (i * 3))); + auto lit3 = std::make_unique( + &f32, static_cast(3 + (i * 3))); + + ast::ExpressionList values; + values.push_back( + std::make_unique(std::move(lit1))); + values.push_back( + std::make_unique(std::move(lit2))); + values.push_back( + std::make_unique(std::move(lit3))); + + ary_values.push_back(std::make_unique( + &vec, std::move(values))); + } + + ast::TypeConstructorExpression expr(&ary, std::move(ary_values)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error(); + EXPECT_EQ(g.result(), std::string("float3[3](") + + "float3(1.00000000f, 2.00000000f, 3.00000000f), " + + "float3(4.00000000f, 5.00000000f, 6.00000000f), " + + "float3(7.00000000f, 8.00000000f, 9.00000000f))"); +} + +// TODO(dsinclair): Add struct constructor test. +TEST_F(MslGeneratorImplTest, DISABLED_EmitConstructor_Type_Struct) {} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint diff --git a/src/writer/wgsl/generator.cc b/src/writer/wgsl/generator.cc index ec4c8f70e4..342688723e 100644 --- a/src/writer/wgsl/generator.cc +++ b/src/writer/wgsl/generator.cc @@ -25,7 +25,11 @@ Generator::Generator(ast::Module module) : Text(std::move(module)) {} Generator::~Generator() = default; bool Generator::Generate() { - return impl_.Generate(module_); + auto ret = impl_.Generate(module_); + if (!ret) { + error_ = impl_.error(); + } + return ret; } } // namespace wgsl diff --git a/test/function.wgsl b/test/function.wgsl new file mode 100644 index 0000000000..e8062f69b5 --- /dev/null +++ b/test/function.wgsl @@ -0,0 +1,17 @@ +# 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. + +fn main() -> f32 { + return ((2. * 3.) - 4.) / 5.; +}