From 7cf19796673f427b59ca3603dde74568e7aa19f7 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 29 Jul 2020 19:11:15 +0000 Subject: [PATCH] [hlsl-writer] Add AsExpression to the HLSL backend. This CL adds as casts to the HLSL backend. Bug: tint:7 Change-Id: I599527d665a3ec1ab6cf80b4f550f7aee8fdf294 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25843 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/hlsl/generator_impl.cc | 23 ++++++++ src/writer/hlsl/generator_impl.h | 4 ++ src/writer/hlsl/generator_impl_as_test.cc | 69 +++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 src/writer/hlsl/generator_impl_as_test.cc diff --git a/BUILD.gn b/BUILD.gn index bd007cc573..cfbbe40d83 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1043,6 +1043,7 @@ source_set("tint_unittests_hlsl_writer_src") { sources = [ "src/writer/hlsl/generator_impl_alias_type_test.cc", "src/writer/hlsl/generator_impl_array_accessor_test.cc", + "src/writer/hlsl/generator_impl_as_test.cc", "src/writer/hlsl/generator_impl_assign_test.cc", "src/writer/hlsl/generator_impl_binary_test.cc", "src/writer/hlsl/generator_impl_block_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7af9c12db2..959c6748aa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -551,6 +551,7 @@ if (${TINT_BUILD_HLSL_WRITER}) list(APPEND TINT_TEST_SRCS writer/hlsl/generator_impl_alias_type_test.cc writer/hlsl/generator_impl_array_accessor_test.cc + writer/hlsl/generator_impl_as_test.cc writer/hlsl/generator_impl_assign_test.cc writer/hlsl/generator_impl_binary_test.cc writer/hlsl/generator_impl_block_test.cc diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index a431001ba0..2288d2d48d 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -15,6 +15,7 @@ #include "src/writer/hlsl/generator_impl.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/bool_literal.h" @@ -117,6 +118,25 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { return true; } +bool GeneratorImpl::EmitAs(ast::AsExpression* expr) { + if (!expr->type()->IsF32() && !expr->type()->IsI32() && + !expr->type()->IsU32()) { + error_ = "Unable to do as cast to type " + expr->type()->type_name(); + return false; + } + + out_ << "as"; + if (!EmitType(expr->type(), "")) { + return false; + } + out_ << "("; + if (!EmitExpression(expr->expr())) { + return false; + } + out_ << ")"; + return true; +} + bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { make_indent(); @@ -368,6 +388,9 @@ bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) { } bool GeneratorImpl::EmitExpression(ast::Expression* expr) { + if (expr->IsAs()) { + return EmitAs(expr->AsAs()); + } if (expr->IsArrayAccessor()) { return EmitArrayAccessor(expr->AsArrayAccessor()); } diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index 24fadb8b4c..2b06729af2 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -46,6 +46,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the expression to emit /// @returns true if the array accessor was emitted bool EmitArrayAccessor(ast::ArrayAccessorExpression* expr); + /// Handles generating an as expression + /// @param expr the as expression + /// @returns true if the as was emitted + bool EmitAs(ast::AsExpression* expr); /// Handles an assignment statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully diff --git a/src/writer/hlsl/generator_impl_as_test.cc b/src/writer/hlsl/generator_impl_as_test.cc new file mode 100644 index 0000000000..c7975ce83e --- /dev/null +++ b/src/writer/hlsl/generator_impl_as_test.cc @@ -0,0 +1,69 @@ +// 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 + +#include "gtest/gtest.h" +#include "src/ast/as_expression.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/module.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/i32_type.h" +#include "src/ast/type/u32_type.h" +#include "src/writer/hlsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace hlsl { +namespace { + +using HlslGeneratorImplTest = testing::Test; + +TEST_F(HlslGeneratorImplTest, EmitExpression_As_Float) { + ast::type::F32Type f32; + auto id = std::make_unique("id"); + ast::AsExpression as(&f32, std::move(id)); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitExpression(&as)) << g.error(); + EXPECT_EQ(g.result(), "asfloat(id)"); +} + +TEST_F(HlslGeneratorImplTest, EmitExpression_As_Int) { + ast::type::I32Type i32; + auto id = std::make_unique("id"); + ast::AsExpression as(&i32, std::move(id)); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitExpression(&as)) << g.error(); + EXPECT_EQ(g.result(), "asint(id)"); +} + +TEST_F(HlslGeneratorImplTest, EmitExpression_As_Uint) { + ast::type::U32Type u32; + auto id = std::make_unique("id"); + ast::AsExpression as(&u32, std::move(id)); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitExpression(&as)) << g.error(); + EXPECT_EQ(g.result(), "asuint(id)"); +} + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint