Emit UnaryMethod expressions.

This CL updates the WGSL generator to output UnaryMethodExpression
nodes.

Bug: tint:4
Change-Id: If43c0e5c12a967f08cc23e816d292353490404cf
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17160
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:01:38 +00:00 committed by dan sinclair
parent cbabfa8ca1
commit 09671ab6f1
4 changed files with 140 additions and 1 deletions

View File

@ -417,6 +417,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_relational_test.cc writer/wgsl/generator_impl_relational_test.cc
writer/wgsl/generator_impl_type_test.cc writer/wgsl/generator_impl_type_test.cc
writer/wgsl/generator_impl_unary_derivative_test.cc writer/wgsl/generator_impl_unary_derivative_test.cc
writer/wgsl/generator_impl_unary_method_test.cc
writer/wgsl/generator_impl_variable_test.cc writer/wgsl/generator_impl_variable_test.cc
) )
endif() endif()

View File

@ -45,6 +45,7 @@
#include "src/ast/type_initializer_expression.h" #include "src/ast/type_initializer_expression.h"
#include "src/ast/uint_literal.h" #include "src/ast/uint_literal.h"
#include "src/ast/unary_derivative_expression.h" #include "src/ast/unary_derivative_expression.h"
#include "src/ast/unary_method_expression.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -148,6 +149,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsUnaryDerivative()) { if (expr->IsUnaryDerivative()) {
return EmitUnaryDerivative(expr->AsUnaryDerivative()); return EmitUnaryDerivative(expr->AsUnaryDerivative());
} }
if (expr->IsUnaryMethod()) {
return EmitUnaryMethod(expr->AsUnaryMethod());
}
error_ = "unknown expression type"; error_ = "unknown expression type";
return false; return false;
@ -566,6 +570,51 @@ bool GeneratorImpl::EmitUnaryDerivative(ast::UnaryDerivativeExpression* expr) {
return true; return true;
} }
bool GeneratorImpl::EmitUnaryMethod(ast::UnaryMethodExpression* expr) {
switch (expr->op()) {
case ast::UnaryMethod::kAny:
out_ << "any";
break;
case ast::UnaryMethod::kAll:
out_ << "all";
break;
case ast::UnaryMethod::kIsNan:
out_ << "is_nan";
break;
case ast::UnaryMethod::kIsInf:
out_ << "is_inf";
break;
case ast::UnaryMethod::kIsFinite:
out_ << "is_finite";
break;
case ast::UnaryMethod::kIsNormal:
out_ << "is_normal";
break;
case ast::UnaryMethod::kDot:
out_ << "dot";
break;
case ast::UnaryMethod::kOuterProduct:
out_ << "outer_product";
break;
}
out_ << "(";
bool first = true;
for (const auto& param : expr->params()) {
if (!first) {
out_ << ", ";
}
first = false;
if (!EmitExpression(param.get())) {
return false;
}
}
out_ << ")";
return true;
}
} // namespace wgsl } // namespace wgsl
} // namespace writer } // namespace writer
} // namespace tint } // namespace tint

View File

@ -130,6 +130,10 @@ class GeneratorImpl {
/// @param expr the expression to emit /// @param expr the expression to emit
/// @returns true if the expression was emitted /// @returns true if the expression was emitted
bool EmitUnaryDerivative(ast::UnaryDerivativeExpression* expr); bool EmitUnaryDerivative(ast::UnaryDerivativeExpression* expr);
/// Handles a unary method expression
/// @param expr the expression to emit
/// @returns true if the expression was emitted
bool EmitUnaryMethod(ast::UnaryMethodExpression* expr);
/// Handles generating a variable /// Handles generating a variable
/// @param var the variable to generate /// @param var the variable to generate
/// @returns true if the variable was emitted /// @returns true if the variable was emitted

View File

@ -0,0 +1,85 @@
// 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/unary_method_expression.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
struct UnaryMethodData {
const char* name;
ast::UnaryMethod method;
};
inline std::ostream& operator<<(std::ostream& out, UnaryMethodData data) {
out << data.method;
return out;
}
using UnaryMethodTest = testing::TestWithParam<UnaryMethodData>;
TEST_P(UnaryMethodTest, Emit) {
auto params = GetParam();
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
std::vector<std::unique_ptr<ast::Expression>> ops;
ops.push_back(std::move(expr));
ast::UnaryMethodExpression method(params.method, std::move(ops));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&method)) << g.error();
EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
}
INSTANTIATE_TEST_SUITE_P(
GeneratorImplTest,
UnaryMethodTest,
testing::Values(UnaryMethodData{"any", ast::UnaryMethod::kAny},
UnaryMethodData{"all", ast::UnaryMethod::kAll},
UnaryMethodData{"is_nan", ast::UnaryMethod::kIsNan},
UnaryMethodData{"is_finite", ast::UnaryMethod::kIsFinite},
UnaryMethodData{"is_normal", ast::UnaryMethod::kIsNormal}));
using UnaryMethodTest_MultiParam = testing::TestWithParam<UnaryMethodData>;
TEST_P(UnaryMethodTest_MultiParam, Emit) {
auto params = GetParam();
auto expr1 = std::make_unique<ast::IdentifierExpression>("expr1");
auto expr2 = std::make_unique<ast::IdentifierExpression>("expr2");
std::vector<std::unique_ptr<ast::Expression>> ops;
ops.push_back(std::move(expr1));
ops.push_back(std::move(expr2));
ast::UnaryMethodExpression method(params.method, std::move(ops));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&method)) << g.error();
EXPECT_EQ(g.result(), std::string(params.name) + "(expr1, expr2)");
}
INSTANTIATE_TEST_SUITE_P(
GeneratorImplTest,
UnaryMethodTest_MultiParam,
testing::Values(UnaryMethodData{"dot", ast::UnaryMethod::kDot},
UnaryMethodData{"outer_product",
ast::UnaryMethod::kOuterProduct}));
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint