[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 <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-23 18:22:21 +00:00
parent 7f269e5bcb
commit 6366f68121
9 changed files with 354 additions and 3 deletions

View File

@ -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",

View File

@ -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;
}

View File

@ -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

View File

@ -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

View File

@ -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<float>::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());
}

View File

@ -18,7 +18,10 @@
#include <sstream>
#include <string>
#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

View File

@ -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<ast::BoolLiteral>(&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<ast::SintLiteral>(&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<ast::UintLiteral>(&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<ast::FloatLiteral>(&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<ast::FloatLiteral>(&f32, -1.2e-5);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(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<ast::BoolLiteral>(&b, true);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(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<ast::SintLiteral>(&i32, -12345);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(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<ast::UintLiteral>(&u32, 12345);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(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<ast::FloatLiteral>(&f32, 1.f);
auto lit2 = std::make_unique<ast::FloatLiteral>(&f32, 2.f);
auto lit3 = std::make_unique<ast::FloatLiteral>(&f32, 3.f);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(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<ast::FloatLiteral>(
&f32, static_cast<float>(1 + (i * 2)));
auto lit2 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(2 + (i * 2)));
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
mat_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
&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<ast::FloatLiteral>(
&f32, static_cast<float>(1 + (i * 3)));
auto lit2 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(2 + (i * 3)));
auto lit3 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(3 + (i * 3)));
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit3)));
ary_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
&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

View File

@ -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

17
test/function.wgsl Normal file
View File

@ -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.;
}