[spirv-writer] Generate Unary Op expression.
This CL adds the code to generate the negation and not operators.
Bug: tint:5
Change-Id: Ibb4d374586e1415a2a678e375c64ba69bbc20367
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/20143
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index e3e4254..f088ced 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -567,6 +567,7 @@
"src/writer/spirv/builder_return_test.cc",
"src/writer/spirv/builder_test.cc",
"src/writer/spirv/builder_type_test.cc",
+ "src/writer/spirv/builder_unary_op_expression_test.cc",
"src/writer/spirv/instruction_test.cc",
"src/writer/spirv/operand_test.cc",
"src/writer/spirv/spv_dump.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 12bbea8..84280dd 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -440,6 +440,7 @@
writer/spirv/builder_return_test.cc
writer/spirv/builder_test.cc
writer/spirv/builder_type_test.cc
+ writer/spirv/builder_unary_op_expression_test.cc
writer/spirv/instruction_test.cc
writer/spirv/operand_test.cc
writer/spirv/spv_dump.cc
diff --git a/src/ast/expression_test.cc b/src/ast/expression_test.cc
index de72167..41a7ee6 100644
--- a/src/ast/expression_test.cc
+++ b/src/ast/expression_test.cc
@@ -54,4 +54,4 @@
} // namespace
} // namespace ast
-} // namespace tint
\ No newline at end of file
+} // namespace tint
diff --git a/src/reader/spirv/parser_impl.h b/src/reader/spirv/parser_impl.h
index 2412398..4811e78 100644
--- a/src/reader/spirv/parser_impl.h
+++ b/src/reader/spirv/parser_impl.h
@@ -21,6 +21,7 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
+#include <utility>
#include <vector>
#include "source/opt/constants.h"
diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc
index 47e8a36..1928777 100644
--- a/src/writer/spirv/builder.cc
+++ b/src/writer/spirv/builder.cc
@@ -46,6 +46,7 @@
#include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h"
#include "src/ast/uint_literal.h"
+#include "src/ast/unary_op_expression.h"
#include "src/ast/variable_decl_statement.h"
namespace tint {
@@ -206,6 +207,9 @@
if (expr->IsIdentifier()) {
return GenerateIdentifierExpression(expr->AsIdentifier());
}
+ if (expr->IsUnaryOp()) {
+ return GenerateUnaryOpExpression(expr->AsUnaryOp());
+ }
error_ = "unknown expression type: " + expr->str();
return 0;
@@ -450,6 +454,40 @@
return val;
}
+uint32_t Builder::GenerateUnaryOpExpression(ast::UnaryOpExpression* expr) {
+ auto result = result_op();
+ auto result_id = result.to_i();
+
+ auto val_id = GenerateExpression(expr->expr());
+ if (val_id == 0) {
+ return 0;
+ }
+
+ auto type_id = GenerateTypeIfNeeded(expr->result_type());
+ if (type_id == 0) {
+ return 0;
+ }
+
+ spv::Op op = spv::Op::OpNop;
+ if (expr->op() == ast::UnaryOp::kNegation) {
+ if (expr->result_type()->is_float_scalar_or_vector()) {
+ op = spv::Op::OpFNegate;
+ } else {
+ op = spv::Op::OpSNegate;
+ }
+ } else if (expr->op() == ast::UnaryOp::kNot) {
+ op = spv::Op::OpNot;
+ }
+ if (op == spv::Op::OpNop) {
+ error_ = "invalid unary op type";
+ return 0;
+ }
+
+ push_function_inst(op, {Operand::Int(type_id), result, Operand::Int(val_id)});
+
+ return result_id;
+}
+
void Builder::GenerateImport(ast::Import* imp) {
auto result = result_op();
auto id = result.to_i();
diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h
index ae8377d..02e3a19 100644
--- a/src/writer/spirv/builder.h
+++ b/src/writer/spirv/builder.h
@@ -181,6 +181,10 @@
/// @param expr the expresssion to generate
/// @returns the id of the expression or 0 on failure
uint32_t GenerateIdentifierExpression(ast::IdentifierExpression* expr);
+ /// Generates a unary op expression
+ /// @param expr the expression to generate
+ /// @returns the id of the expression or 0 on failure
+ uint32_t GenerateUnaryOpExpression(ast::UnaryOpExpression* expr);
/// Generates an if statement
/// @param stmt the statement to generate
/// @returns true on success
diff --git a/src/writer/spirv/builder_unary_op_expression_test.cc b/src/writer/spirv/builder_unary_op_expression_test.cc
new file mode 100644
index 0000000..1065115
--- /dev/null
+++ b/src/writer/spirv/builder_unary_op_expression_test.cc
@@ -0,0 +1,114 @@
+// 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/float_literal.h"
+#include "src/ast/identifier_expression.h"
+#include "src/ast/int_literal.h"
+#include "src/ast/scalar_constructor_expression.h"
+#include "src/ast/type/f32_type.h"
+#include "src/ast/type/i32_type.h"
+#include "src/ast/unary_op_expression.h"
+#include "src/context.h"
+#include "src/type_determiner.h"
+#include "src/writer/spirv/builder.h"
+#include "src/writer/spirv/spv_dump.h"
+
+namespace tint {
+namespace writer {
+namespace spirv {
+namespace {
+
+using BuilderTest = testing::Test;
+
+TEST_F(BuilderTest, UnaryOp_Negation_Integer) {
+ ast::type::I32Type i32;
+
+ ast::UnaryOpExpression expr(
+ ast::UnaryOp::kNegation,
+ std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::IntLiteral>(&i32, 1)));
+
+ Context ctx;
+ ast::Module mod;
+ TypeDeterminer td(&ctx, &mod);
+
+ ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
+
+ Builder b(&mod);
+ b.push_function(Function{});
+ EXPECT_EQ(b.GenerateUnaryOpExpression(&expr), 1u) << b.error();
+ EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
+%3 = OpConstant %2 1
+)");
+ EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
+ R"(%1 = OpSNegate %2 %3
+)");
+}
+
+TEST_F(BuilderTest, UnaryOp_Negation_Float) {
+ ast::type::F32Type f32;
+
+ ast::UnaryOpExpression expr(
+ ast::UnaryOp::kNegation,
+ std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::FloatLiteral>(&f32, 1)));
+
+ Context ctx;
+ ast::Module mod;
+ TypeDeterminer td(&ctx, &mod);
+
+ ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
+
+ Builder b(&mod);
+ b.push_function(Function{});
+ EXPECT_EQ(b.GenerateUnaryOpExpression(&expr), 1u) << b.error();
+ EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
+%3 = OpConstant %2 1
+)");
+ EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
+ R"(%1 = OpFNegate %2 %3
+)");
+}
+
+TEST_F(BuilderTest, UnaryOp_Not) {
+ ast::type::I32Type i32;
+
+ ast::UnaryOpExpression expr(
+ ast::UnaryOp::kNot, std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::IntLiteral>(&i32, 1)));
+
+ Context ctx;
+ ast::Module mod;
+ TypeDeterminer td(&ctx, &mod);
+
+ ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
+
+ Builder b(&mod);
+ b.push_function(Function{});
+ EXPECT_EQ(b.GenerateUnaryOpExpression(&expr), 1u) << b.error();
+ EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
+%3 = OpConstant %2 1
+)");
+ EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
+ R"(%1 = OpNot %2 %3
+)");
+}
+
+} // namespace
+} // namespace spirv
+} // namespace writer
+} // namespace tint