mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-07-03 03:35:59 +00:00
[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>
This commit is contained in:
parent
9a7cc7c73c
commit
c9308dcb75
1
BUILD.gn
1
BUILD.gn
@ -567,6 +567,7 @@ source_set("tint_unittests_spv_writer_src") {
|
|||||||
"src/writer/spirv/builder_return_test.cc",
|
"src/writer/spirv/builder_return_test.cc",
|
||||||
"src/writer/spirv/builder_test.cc",
|
"src/writer/spirv/builder_test.cc",
|
||||||
"src/writer/spirv/builder_type_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/instruction_test.cc",
|
||||||
"src/writer/spirv/operand_test.cc",
|
"src/writer/spirv/operand_test.cc",
|
||||||
"src/writer/spirv/spv_dump.cc",
|
"src/writer/spirv/spv_dump.cc",
|
||||||
|
@ -440,6 +440,7 @@ if(${TINT_BUILD_SPV_WRITER})
|
|||||||
writer/spirv/builder_return_test.cc
|
writer/spirv/builder_return_test.cc
|
||||||
writer/spirv/builder_test.cc
|
writer/spirv/builder_test.cc
|
||||||
writer/spirv/builder_type_test.cc
|
writer/spirv/builder_type_test.cc
|
||||||
|
writer/spirv/builder_unary_op_expression_test.cc
|
||||||
writer/spirv/instruction_test.cc
|
writer/spirv/instruction_test.cc
|
||||||
writer/spirv/operand_test.cc
|
writer/spirv/operand_test.cc
|
||||||
writer/spirv/spv_dump.cc
|
writer/spirv/spv_dump.cc
|
||||||
|
@ -54,4 +54,4 @@ TEST_F(ExpressionTest, set_result_type_alias) {
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace ast
|
} // namespace ast
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "source/opt/constants.h"
|
#include "source/opt/constants.h"
|
||||||
|
@ -46,6 +46,7 @@
|
|||||||
#include "src/ast/type/vector_type.h"
|
#include "src/ast/type/vector_type.h"
|
||||||
#include "src/ast/type_constructor_expression.h"
|
#include "src/ast/type_constructor_expression.h"
|
||||||
#include "src/ast/uint_literal.h"
|
#include "src/ast/uint_literal.h"
|
||||||
|
#include "src/ast/unary_op_expression.h"
|
||||||
#include "src/ast/variable_decl_statement.h"
|
#include "src/ast/variable_decl_statement.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
@ -206,6 +207,9 @@ uint32_t Builder::GenerateExpression(ast::Expression* expr) {
|
|||||||
if (expr->IsIdentifier()) {
|
if (expr->IsIdentifier()) {
|
||||||
return GenerateIdentifierExpression(expr->AsIdentifier());
|
return GenerateIdentifierExpression(expr->AsIdentifier());
|
||||||
}
|
}
|
||||||
|
if (expr->IsUnaryOp()) {
|
||||||
|
return GenerateUnaryOpExpression(expr->AsUnaryOp());
|
||||||
|
}
|
||||||
|
|
||||||
error_ = "unknown expression type: " + expr->str();
|
error_ = "unknown expression type: " + expr->str();
|
||||||
return 0;
|
return 0;
|
||||||
@ -450,6 +454,40 @@ uint32_t Builder::GenerateIdentifierExpression(
|
|||||||
return val;
|
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) {
|
void Builder::GenerateImport(ast::Import* imp) {
|
||||||
auto result = result_op();
|
auto result = result_op();
|
||||||
auto id = result.to_i();
|
auto id = result.to_i();
|
||||||
|
@ -181,6 +181,10 @@ class Builder {
|
|||||||
/// @param expr the expresssion to generate
|
/// @param expr the expresssion to generate
|
||||||
/// @returns the id of the expression or 0 on failure
|
/// @returns the id of the expression or 0 on failure
|
||||||
uint32_t GenerateIdentifierExpression(ast::IdentifierExpression* expr);
|
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
|
/// Generates an if statement
|
||||||
/// @param stmt the statement to generate
|
/// @param stmt the statement to generate
|
||||||
/// @returns true on success
|
/// @returns true on success
|
||||||
|
114
src/writer/spirv/builder_unary_op_expression_test.cc
Normal file
114
src/writer/spirv/builder_unary_op_expression_test.cc
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user