From a9696b511fabac24a6acc7e22faea7d55f8c840f Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Sat, 25 Jul 2020 14:32:54 +0000 Subject: [PATCH] [msl-writer] Emit DiscardStatement. This CL adds the DiscardStatement to the MSL backend. Bug: tint:164 Change-Id: I1e201d3b6a167690af7bb799e1c93e48232f4a0a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25602 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/msl/generator_impl.cc | 11 +++++ src/writer/msl/generator_impl.h | 4 ++ src/writer/msl/generator_impl_discard_test.cc | 41 +++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 src/writer/msl/generator_impl_discard_test.cc diff --git a/BUILD.gn b/BUILD.gn index fa52285d71..c329fa01ba 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -994,6 +994,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_cast_test.cc", "src/writer/msl/generator_impl_constructor_test.cc", "src/writer/msl/generator_impl_continue_test.cc", + "src/writer/msl/generator_impl_discard_test.cc", "src/writer/msl/generator_impl_entry_point_test.cc", "src/writer/msl/generator_impl_function_test.cc", "src/writer/msl/generator_impl_identifier_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f35b61f7b..aa92225c00 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -525,6 +525,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_cast_test.cc writer/msl/generator_impl_constructor_test.cc writer/msl/generator_impl_continue_test.cc + writer/msl/generator_impl_discard_test.cc writer/msl/generator_impl_entry_point_test.cc writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_identifier_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 4a4119ee9f..248a33936e 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -1389,6 +1389,14 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { return true; } +bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) { + make_indent(); + // TODO(dsinclair): Verify this is correct when the discard semantics are + // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) + out_ << "discard_fragment();" << std::endl; + return true; +} + bool GeneratorImpl::EmitKill(ast::KillStatement*) { make_indent(); // TODO(dsinclair): Verify this is correct when the kill semantics are defined @@ -1509,6 +1517,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsContinue()) { return EmitContinue(stmt->AsContinue()); } + if (stmt->IsDiscard()) { + return EmitDiscard(stmt->AsDiscard()); + } if (stmt->IsFallthrough()) { make_indent(); out_ << "/* fallthrough */" << std::endl; diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index e2a0e3844b..5f5b31c1e1 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -97,6 +97,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully bool EmitContinue(ast::ContinueStatement* stmt); + /// Handles generating a discard statement + /// @param stmt the discard statement + /// @returns true if the statement was successfully emitted + bool EmitDiscard(ast::DiscardStatement* stmt); /// Handles generating an else statement /// @param stmt the statement to emit /// @returns true if the statement was emitted diff --git a/src/writer/msl/generator_impl_discard_test.cc b/src/writer/msl/generator_impl_discard_test.cc new file mode 100644 index 0000000000..5d3ab30559 --- /dev/null +++ b/src/writer/msl/generator_impl_discard_test.cc @@ -0,0 +1,41 @@ +// 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/discard_statement.h" +#include "src/ast/module.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Discard) { + ast::DiscardStatement stmt; + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), " discard_fragment();\n"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint