[spirv-writer] Add DiscardStatement support.

This CL updates the spirv-writer to emit the DiscardStatement as an
OpKill.

Bug: tint:163
Change-Id: Ic2514ee8a4ef7ef0220fc2e1145f8df0c3d32069
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25641
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-25 14:32:44 +00:00 committed by dan sinclair
parent b4374c271e
commit 6a61d4127e
5 changed files with 62 additions and 2 deletions

View File

@ -803,6 +803,7 @@ source_set("tint_unittests_spv_writer_src") {
"src/writer/spirv/builder_call_test.cc",
"src/writer/spirv/builder_cast_expression_test.cc",
"src/writer/spirv/builder_constructor_expression_test.cc",
"src/writer/spirv/builder_discard_test.cc",
"src/writer/spirv/builder_entry_point_test.cc",
"src/writer/spirv/builder_function_test.cc",
"src/writer/spirv/builder_function_variable_test.cc",

View File

@ -457,6 +457,7 @@ if(${TINT_BUILD_SPV_WRITER})
writer/spirv/builder_call_test.cc
writer/spirv/builder_cast_expression_test.cc
writer/spirv/builder_constructor_expression_test.cc
writer/spirv/builder_discard_test.cc
writer/spirv/builder_entry_point_test.cc
writer/spirv/builder_function_test.cc
writer/spirv/builder_function_variable_test.cc

View File

@ -105,8 +105,8 @@ bool LastIsTerminator(const ast::StatementList& stmts) {
}
auto* last = stmts.back().get();
return last->IsBreak() || last->IsContinue() || last->IsReturn() ||
last->IsKill() || last->IsFallthrough();
return last->IsBreak() || last->IsContinue() || last->IsDiscard() ||
last->IsReturn() || last->IsKill() || last->IsFallthrough();
}
uint32_t IndexFromName(char name) {
@ -273,6 +273,14 @@ bool Builder::GenerateContinueStatement(ast::ContinueStatement*) {
return true;
}
// TODO(dsinclair): This is generating an OpKill but the semantics of kill
// haven't been defined for WGSL yet. So, this may need to change.
// https://github.com/gpuweb/gpuweb/issues/676
bool Builder::GenerateDiscardStatement(ast::DiscardStatement*) {
push_function_inst(spv::Op::OpKill, {});
return true;
}
// TODO(dsinclair): This is generating an OpKill but the semantics of kill
// haven't been defined for WGSL yet. So, this may need to change.
// https://github.com/gpuweb/gpuweb/issues/676
@ -1816,6 +1824,9 @@ bool Builder::GenerateStatement(ast::Statement* stmt) {
if (stmt->IsContinue()) {
return GenerateContinueStatement(stmt->AsContinue());
}
if (stmt->IsDiscard()) {
return GenerateDiscardStatement(stmt->AsDiscard());
}
if (stmt->IsFallthrough()) {
// Do nothing here, the fallthrough gets handled by the switch code.
return true;

View File

@ -175,6 +175,10 @@ class Builder {
/// @param stmt the statement to generate
/// @returns true if the statement was successfully generated
bool GenerateContinueStatement(ast::ContinueStatement* stmt);
/// Generates a discard statement
/// @param stmt the statement to generate
/// @returns true if the statement was successfully generated
bool GenerateDiscardStatement(ast::DiscardStatement* stmt);
/// Generates a kill statement
/// @param stmt the statement to generate
/// @returns true if the statement was successfully generated

View File

@ -0,0 +1,43 @@
// 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/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, Discard) {
ast::DiscardStatement expr;
ast::Module mod;
Builder b(&mod);
b.push_function(Function{});
EXPECT_EQ(b.GenerateStatement(&expr), 1u) << b.error();
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"(OpKill
)");
}
} // namespace
} // namespace spirv
} // namespace writer
} // namespace tint