[metal-writer] Emit binary operations.
This CL adds emission of binary operations to the Metal backend. Bug: tint:8 Change-Id: I797daadd238b718b081842b63ccefab3294bc20c Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23706 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
70c605fc47
commit
7f269e5bcb
3
BUILD.gn
3
BUILD.gn
|
@ -845,6 +845,7 @@ source_set("tint_unittests_wgsl_writer_src") {
|
||||||
"src/writer/wgsl/generator_impl_array_accessor_test.cc",
|
"src/writer/wgsl/generator_impl_array_accessor_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_as_test.cc",
|
"src/writer/wgsl/generator_impl_as_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_assign_test.cc",
|
"src/writer/wgsl/generator_impl_assign_test.cc",
|
||||||
|
"src/writer/wgsl/generator_impl_binary_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_break_test.cc",
|
"src/writer/wgsl/generator_impl_break_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_call_test.cc",
|
"src/writer/wgsl/generator_impl_call_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_case_test.cc",
|
"src/writer/wgsl/generator_impl_case_test.cc",
|
||||||
|
@ -861,7 +862,6 @@ source_set("tint_unittests_wgsl_writer_src") {
|
||||||
"src/writer/wgsl/generator_impl_kill_test.cc",
|
"src/writer/wgsl/generator_impl_kill_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_loop_test.cc",
|
"src/writer/wgsl/generator_impl_loop_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_member_accessor_test.cc",
|
"src/writer/wgsl/generator_impl_member_accessor_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_relational_test.cc",
|
|
||||||
"src/writer/wgsl/generator_impl_return_test.cc",
|
"src/writer/wgsl/generator_impl_return_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_switch_test.cc",
|
"src/writer/wgsl/generator_impl_switch_test.cc",
|
||||||
"src/writer/wgsl/generator_impl_test.cc",
|
"src/writer/wgsl/generator_impl_test.cc",
|
||||||
|
@ -886,6 +886,7 @@ source_set("tint_unittests_wgsl_writer_src") {
|
||||||
source_set("tint_unittests_msl_writer_src") {
|
source_set("tint_unittests_msl_writer_src") {
|
||||||
sources = [
|
sources = [
|
||||||
"src/writer/msl/generator_impl_assign_test.cc",
|
"src/writer/msl/generator_impl_assign_test.cc",
|
||||||
|
"src/writer/msl/generator_impl_binary_test.cc",
|
||||||
"src/writer/msl/generator_impl_function_test.cc",
|
"src/writer/msl/generator_impl_function_test.cc",
|
||||||
"src/writer/msl/generator_impl_identifier_test.cc",
|
"src/writer/msl/generator_impl_identifier_test.cc",
|
||||||
"src/writer/msl/generator_impl_return_test.cc",
|
"src/writer/msl/generator_impl_return_test.cc",
|
||||||
|
|
|
@ -465,6 +465,7 @@ if(${TINT_BUILD_WGSL_WRITER})
|
||||||
writer/wgsl/generator_impl_array_accessor_test.cc
|
writer/wgsl/generator_impl_array_accessor_test.cc
|
||||||
writer/wgsl/generator_impl_as_test.cc
|
writer/wgsl/generator_impl_as_test.cc
|
||||||
writer/wgsl/generator_impl_assign_test.cc
|
writer/wgsl/generator_impl_assign_test.cc
|
||||||
|
writer/wgsl/generator_impl_binary_test.cc
|
||||||
writer/wgsl/generator_impl_break_test.cc
|
writer/wgsl/generator_impl_break_test.cc
|
||||||
writer/wgsl/generator_impl_call_test.cc
|
writer/wgsl/generator_impl_call_test.cc
|
||||||
writer/wgsl/generator_impl_case_test.cc
|
writer/wgsl/generator_impl_case_test.cc
|
||||||
|
@ -481,7 +482,6 @@ if(${TINT_BUILD_WGSL_WRITER})
|
||||||
writer/wgsl/generator_impl_kill_test.cc
|
writer/wgsl/generator_impl_kill_test.cc
|
||||||
writer/wgsl/generator_impl_loop_test.cc
|
writer/wgsl/generator_impl_loop_test.cc
|
||||||
writer/wgsl/generator_impl_member_accessor_test.cc
|
writer/wgsl/generator_impl_member_accessor_test.cc
|
||||||
writer/wgsl/generator_impl_relational_test.cc
|
|
||||||
writer/wgsl/generator_impl_return_test.cc
|
writer/wgsl/generator_impl_return_test.cc
|
||||||
writer/wgsl/generator_impl_switch_test.cc
|
writer/wgsl/generator_impl_switch_test.cc
|
||||||
writer/wgsl/generator_impl_type_test.cc
|
writer/wgsl/generator_impl_type_test.cc
|
||||||
|
@ -494,6 +494,7 @@ endif()
|
||||||
if(${TINT_BUILD_MSL_WRITER})
|
if(${TINT_BUILD_MSL_WRITER})
|
||||||
list(APPEND TINT_TEST_SRCS
|
list(APPEND TINT_TEST_SRCS
|
||||||
writer/msl/generator_impl_assign_test.cc
|
writer/msl/generator_impl_assign_test.cc
|
||||||
|
writer/msl/generator_impl_binary_test.cc
|
||||||
writer/msl/generator_impl_function_test.cc
|
writer/msl/generator_impl_function_test.cc
|
||||||
writer/msl/generator_impl_identifier_test.cc
|
writer/msl/generator_impl_identifier_test.cc
|
||||||
writer/msl/generator_impl_return_test.cc
|
writer/msl/generator_impl_return_test.cc
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "src/writer/msl/generator_impl.h"
|
#include "src/writer/msl/generator_impl.h"
|
||||||
|
|
||||||
#include "src/ast/assignment_statement.h"
|
#include "src/ast/assignment_statement.h"
|
||||||
|
#include "src/ast/binary_expression.h"
|
||||||
#include "src/ast/function.h"
|
#include "src/ast/function.h"
|
||||||
#include "src/ast/identifier_expression.h"
|
#include "src/ast/identifier_expression.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
|
@ -66,7 +67,91 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
|
||||||
|
out_ << "(";
|
||||||
|
|
||||||
|
if (!EmitExpression(expr->lhs())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << " ";
|
||||||
|
|
||||||
|
switch (expr->op()) {
|
||||||
|
case ast::BinaryOp::kAnd:
|
||||||
|
out_ << "&";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kOr:
|
||||||
|
out_ << "|";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kXor:
|
||||||
|
out_ << "^";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kLogicalAnd:
|
||||||
|
out_ << "&&";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kLogicalOr:
|
||||||
|
out_ << "||";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kEqual:
|
||||||
|
out_ << "==";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kNotEqual:
|
||||||
|
out_ << "!=";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kLessThan:
|
||||||
|
out_ << "<";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kGreaterThan:
|
||||||
|
out_ << ">";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kLessThanEqual:
|
||||||
|
out_ << "<=";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kGreaterThanEqual:
|
||||||
|
out_ << ">=";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kShiftLeft:
|
||||||
|
out_ << "<<";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kShiftRight:
|
||||||
|
// TODO(dsinclair): MSL is based on C++14, and >> in C++14 has
|
||||||
|
// implementation-defined behaviour for negative LHS. We may have to
|
||||||
|
// generate extra code to implement WGSL-specified behaviour for negative
|
||||||
|
// LHS.
|
||||||
|
out_ << ">>";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kAdd:
|
||||||
|
out_ << "+";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kSubtract:
|
||||||
|
out_ << "-";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kMultiply:
|
||||||
|
out_ << "*";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kDivide:
|
||||||
|
out_ << "/";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kModulo:
|
||||||
|
out_ << "%";
|
||||||
|
break;
|
||||||
|
case ast::BinaryOp::kNone:
|
||||||
|
error_ = "missing binary operation type";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << " ";
|
||||||
|
|
||||||
|
if (!EmitExpression(expr->rhs())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_ << ")";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
||||||
|
if (expr->IsBinary()) {
|
||||||
|
return EmitBinary(expr->AsBinary());
|
||||||
|
}
|
||||||
if (expr->IsIdentifier()) {
|
if (expr->IsIdentifier()) {
|
||||||
return EmitIdentifier(expr->AsIdentifier());
|
return EmitIdentifier(expr->AsIdentifier());
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,6 +41,10 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param stmt the statement to emit
|
/// @param stmt the statement to emit
|
||||||
/// @returns true if the statement was emitted successfully
|
/// @returns true if the statement was emitted successfully
|
||||||
bool EmitAssign(ast::AssignmentStatement* stmt);
|
bool EmitAssign(ast::AssignmentStatement* stmt);
|
||||||
|
/// Handles generating a binary expression
|
||||||
|
/// @param expr the binary expression
|
||||||
|
/// @returns true if the expression was emitted, false otherwise
|
||||||
|
bool EmitBinary(ast::BinaryExpression* expr);
|
||||||
/// Handles generate an Expression
|
/// Handles generate an Expression
|
||||||
/// @param expr the expression
|
/// @param expr the expression
|
||||||
/// @returns true if the expression was emitted
|
/// @returns true if the expression was emitted
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
// 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 "gtest/gtest.h"
|
||||||
|
#include "src/ast/binary_expression.h"
|
||||||
|
#include "src/ast/identifier_expression.h"
|
||||||
|
#include "src/writer/msl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace msl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct BinaryData {
|
||||||
|
const char* result;
|
||||||
|
ast::BinaryOp op;
|
||||||
|
};
|
||||||
|
inline std::ostream& operator<<(std::ostream& out, BinaryData data) {
|
||||||
|
out << data.op;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
using MslBinaryTest = testing::TestWithParam<BinaryData>;
|
||||||
|
TEST_P(MslBinaryTest, Emit) {
|
||||||
|
auto params = GetParam();
|
||||||
|
|
||||||
|
auto left = std::make_unique<ast::IdentifierExpression>("left");
|
||||||
|
auto right = std::make_unique<ast::IdentifierExpression>("right");
|
||||||
|
|
||||||
|
ast::BinaryExpression expr(params.op, std::move(left), std::move(right));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), params.result);
|
||||||
|
}
|
||||||
|
INSTANTIATE_TEST_SUITE_P(
|
||||||
|
MslGeneratorImplTest,
|
||||||
|
MslBinaryTest,
|
||||||
|
testing::Values(
|
||||||
|
BinaryData{"(left & right)", ast::BinaryOp::kAnd},
|
||||||
|
BinaryData{"(left | right)", ast::BinaryOp::kOr},
|
||||||
|
BinaryData{"(left ^ right)", ast::BinaryOp::kXor},
|
||||||
|
BinaryData{"(left && right)", ast::BinaryOp::kLogicalAnd},
|
||||||
|
BinaryData{"(left || right)", ast::BinaryOp::kLogicalOr},
|
||||||
|
BinaryData{"(left == right)", ast::BinaryOp::kEqual},
|
||||||
|
BinaryData{"(left != right)", ast::BinaryOp::kNotEqual},
|
||||||
|
BinaryData{"(left < right)", ast::BinaryOp::kLessThan},
|
||||||
|
BinaryData{"(left > right)", ast::BinaryOp::kGreaterThan},
|
||||||
|
BinaryData{"(left <= right)", ast::BinaryOp::kLessThanEqual},
|
||||||
|
BinaryData{"(left >= right)", ast::BinaryOp::kGreaterThanEqual},
|
||||||
|
BinaryData{"(left << right)", ast::BinaryOp::kShiftLeft},
|
||||||
|
BinaryData{"(left >> right)", ast::BinaryOp::kShiftRight},
|
||||||
|
BinaryData{"(left + right)", ast::BinaryOp::kAdd},
|
||||||
|
BinaryData{"(left - right)", ast::BinaryOp::kSubtract},
|
||||||
|
BinaryData{"(left * right)", ast::BinaryOp::kMultiply},
|
||||||
|
BinaryData{"(left / right)", ast::BinaryOp::kDivide},
|
||||||
|
BinaryData{"(left % right)", ast::BinaryOp::kModulo}));
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace msl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
|
@ -32,8 +32,8 @@ inline std::ostream& operator<<(std::ostream& out, BinaryData data) {
|
||||||
out << data.op;
|
out << data.op;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
using BinaryTest = testing::TestWithParam<BinaryData>;
|
using WgslBinaryTest = testing::TestWithParam<BinaryData>;
|
||||||
TEST_P(BinaryTest, Emit) {
|
TEST_P(WgslBinaryTest, Emit) {
|
||||||
auto params = GetParam();
|
auto params = GetParam();
|
||||||
|
|
||||||
auto left = std::make_unique<ast::IdentifierExpression>("left");
|
auto left = std::make_unique<ast::IdentifierExpression>("left");
|
||||||
|
@ -47,7 +47,7 @@ TEST_P(BinaryTest, Emit) {
|
||||||
}
|
}
|
||||||
INSTANTIATE_TEST_SUITE_P(
|
INSTANTIATE_TEST_SUITE_P(
|
||||||
WgslGeneratorImplTest,
|
WgslGeneratorImplTest,
|
||||||
BinaryTest,
|
WgslBinaryTest,
|
||||||
testing::Values(
|
testing::Values(
|
||||||
BinaryData{"(left & right)", ast::BinaryOp::kAnd},
|
BinaryData{"(left & right)", ast::BinaryOp::kAnd},
|
||||||
BinaryData{"(left | right)", ast::BinaryOp::kOr},
|
BinaryData{"(left | right)", ast::BinaryOp::kOr},
|
Loading…
Reference in New Issue