[spirv-writer] Add as casts.

This CL adds the conversion of `as<f32>(b)` to SPIR-V.

Bug: tint:5
Change-Id: If1e04db2fe5520940527f4dcf52a89628b11b518
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23461
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-18 18:02:46 +00:00
parent d7868e34c2
commit 8b48b26f73
6 changed files with 131 additions and 0 deletions

View File

@ -689,6 +689,7 @@ source_set("tint_unittests_spv_writer_src") {
sources = [
"src/writer/spirv/binary_writer_test.cc",
"src/writer/spirv/builder_accessor_expression_test.cc",
"src/writer/spirv/builder_as_expression_test.cc",
"src/writer/spirv/builder_assign_test.cc",
"src/writer/spirv/builder_binary_expression_test.cc",
"src/writer/spirv/builder_call_test.cc",

View File

@ -416,6 +416,7 @@ if(${TINT_BUILD_SPV_WRITER})
list(APPEND TINT_TEST_SRCS
writer/spirv/binary_writer_test.cc
writer/spirv/builder_accessor_expression_test.cc
writer/spirv/builder_as_expression_test.cc
writer/spirv/builder_assign_test.cc
writer/spirv/builder_binary_expression_test.cc
writer/spirv/builder_call_test.cc

View File

@ -391,6 +391,10 @@ bool TypeDeterminer::DetermineArrayAccessor(
}
bool TypeDeterminer::DetermineAs(ast::AsExpression* expr) {
if (!DetermineResultType(expr->expr())) {
return false;
}
expr->set_result_type(expr->type());
return true;
}

View File

@ -19,6 +19,7 @@
#include "spirv/unified1/spirv.h"
#include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/binary_expression.h"
#include "src/ast/binding_decoration.h"
@ -339,6 +340,9 @@ uint32_t Builder::GenerateExpression(ast::Expression* expr) {
if (expr->IsArrayAccessor()) {
return GenerateAccessorExpression(expr->AsArrayAccessor());
}
if (expr->IsAs()) {
return GenerateAsExpression(expr->AsAs());
}
if (expr->IsBinary()) {
return GenerateBinaryExpression(expr->AsBinary());
}
@ -1354,6 +1358,36 @@ uint32_t Builder::GenerateIntrinsic(const std::string& name,
return result_id;
}
uint32_t Builder::GenerateAsExpression(ast::AsExpression* as) {
auto result = result_op();
auto result_id = result.to_i();
auto result_type_id = GenerateTypeIfNeeded(as->result_type());
if (result_type_id == 0) {
return 0;
}
auto val_id = GenerateExpression(as->expr());
if (val_id == 0) {
return 0;
}
val_id = GenerateLoadIfNeeded(as->expr()->result_type(), val_id);
// Bitcast does not allow same types, just emit a CopyObject
auto* to_type = as->result_type()->UnwrapPtrIfNeeded();
auto* from_type = as->expr()->result_type()->UnwrapPtrIfNeeded();
if (to_type->type_name() == from_type->type_name()) {
push_function_inst(spv::Op::OpCopyObject, {Operand::Int(result_type_id),
result, Operand::Int(val_id)});
return result_id;
}
push_function_inst(spv::Op::OpBitcast, {Operand::Int(result_type_id), result,
Operand::Int(val_id)});
return result_id;
}
uint32_t Builder::GenerateCastExpression(ast::CastExpression* cast) {
auto result = result_op();
auto result_id = result.to_i();

View File

@ -283,6 +283,10 @@ class Builder {
/// @param lit the literal to generate
/// @returns the ID on success or 0 on failure
uint32_t GenerateLiteralIfNeeded(ast::Literal* lit);
/// Generates an as expression
/// @param expr the expression to generate
/// @returns the expression ID on success or 0 otherwise
uint32_t GenerateAsExpression(ast::AsExpression* expr);
/// Generates a binary expression
/// @param expr the expression to generate
/// @returns the expression ID on success or 0 otherwise

View File

@ -0,0 +1,87 @@
// 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/as_expression.h"
#include "src/ast/float_literal.h"
#include "src/ast/module.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/u32_type.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, As) {
ast::type::U32Type u32;
ast::type::F32Type f32;
ast::AsExpression as(&u32,
std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.4)));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&as)) << td.error();
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateAsExpression(&as), 1u);
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
%3 = OpTypeFloat 32
%4 = OpConstant %3 2.4000001
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%1 = OpBitcast %2 %4
)");
}
TEST_F(BuilderTest, As_DuplicateType) {
ast::type::F32Type f32;
ast::AsExpression as(&f32,
std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 2.4)));
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ASSERT_TRUE(td.DetermineResultType(&as)) << td.error();
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateAsExpression(&as), 1u);
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%3 = OpConstant %2 2.4000001
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%1 = OpCopyObject %2 %3
)");
}
} // namespace
} // namespace spirv
} // namespace writer
} // namespace tint