[spirv-writer] Emit function signatures.

This CL updates the SPIR-V writer to emit the OpTypeFunction and
OpFunction instructions.

Bug: tint:5
Change-Id: I85ead161ca37304a977213257a825ff268d29f2d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17741
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-25 19:43:20 +00:00 committed by dan sinclair
parent 9981b63fa4
commit 33d34650e8
11 changed files with 193 additions and 4 deletions

View File

@ -408,6 +408,7 @@ if(${TINT_BUILD_SPV_WRITER})
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_entry_point_test.cc writer/spirv/builder_entry_point_test.cc
writer/spirv/builder_function_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

View File

@ -14,6 +14,8 @@
#include "src/ast/function.h" #include "src/ast/function.h"
#include <sstream>
namespace tint { namespace tint {
namespace ast { namespace ast {
@ -83,5 +85,16 @@ void Function::to_str(std::ostream& out, size_t indent) const {
out << "}" << std::endl; out << "}" << std::endl;
} }
std::string Function::type_name() const {
std::ostringstream out;
out << "__func" + return_type_->type_name();
for (const auto& param : params_) {
out << param->type()->type_name();
}
return out.str();
}
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -94,6 +94,9 @@ class Function : public Node {
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(std::ostream& out, size_t indent) const override;
/// @returns the type name for this function
std::string type_name() const;
private: private:
Function(const Function&) = delete; Function(const Function&) = delete;

View File

@ -16,6 +16,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/ast/nop_statement.h" #include "src/ast/nop_statement.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/ast/type/void_type.h" #include "src/ast/type/void_type.h"
#include "src/ast/variable.h" #include "src/ast/variable.h"
@ -202,5 +203,27 @@ TEST_F(FunctionTest, ToStr_WithParams) {
)"); )");
} }
TEST_F(FunctionTest, TypeName) {
type::VoidType void_type;
Function f("func", {}, &void_type);
EXPECT_EQ(f.type_name(), "__func__void");
}
TEST_F(FunctionTest, TypeName_WithParams) {
type::VoidType void_type;
type::I32Type i32;
type::F32Type f32;
std::vector<std::unique_ptr<Variable>> params;
params.push_back(
std::make_unique<Variable>("var1", StorageClass::kNone, &i32));
params.push_back(
std::make_unique<Variable>("var2", StorageClass::kNone, &f32));
Function f("func", std::move(params), &void_type);
EXPECT_EQ(f.type_name(), "__func__void__i32__f32");
}
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -200,7 +200,7 @@ TEST_F(SpvNamerTest, SuggestSanitizedMemberName_TakeSanitizedSuggestion) {
TEST_F( TEST_F(
SpvNamerTest, SpvNamerTest,
SuggestSanitizedMemberName_TakeSuggestionWhenNoConflictAfterSuggestionForLowerMember) { SuggestSanitizedMemberName_TakeSuggestionWhenNoConflictAfterSuggestionForLowerMember) { // NOLINT
Namer namer(fail_stream_); Namer namer(fail_stream_);
EXPECT_TRUE(namer.SuggestSanitizedMemberName(1, 7, "mother")); EXPECT_TRUE(namer.SuggestSanitizedMemberName(1, 7, "mother"));
EXPECT_THAT(namer.GetMemberName(1, 2), Eq("")); EXPECT_THAT(namer.GetMemberName(1, 2), Eq(""));

View File

@ -238,7 +238,7 @@ void ParserImpl::ResetInternalModule() {
bool ParserImpl::ParseInternalModule() { bool ParserImpl::ParseInternalModule() {
if (!success_) { if (!success_) {
return false; return false;
}; }
if (!RegisterExtendedInstructionImports()) { if (!RegisterExtendedInstructionImports()) {
return false; return false;
} }

View File

@ -14,6 +14,8 @@
#include "src/writer/spirv/builder.h" #include "src/writer/spirv/builder.h"
#include <utility>
#include "spirv/unified1/spirv.h" #include "spirv/unified1/spirv.h"
#include "src/ast/struct.h" #include "src/ast/struct.h"
#include "src/ast/struct_member.h" #include "src/ast/struct_member.h"
@ -74,6 +76,12 @@ bool Builder::Build(const ast::Module& m) {
{Operand::Int(SpvAddressingModelLogical), {Operand::Int(SpvAddressingModelLogical),
Operand::Int(SpvMemoryModelVulkanKHR)}); Operand::Int(SpvMemoryModelVulkanKHR)});
for (const auto& func : m.functions()) {
if (!GenerateFunction(func.get())) {
return false;
}
}
for (const auto& ep : m.entry_points()) { for (const auto& ep : m.entry_points()) {
if (!GenerateEntryPoint(ep.get())) { if (!GenerateEntryPoint(ep.get())) {
return false; return false;
@ -142,6 +150,58 @@ bool Builder::GenerateEntryPoint(ast::EntryPoint* ep) {
return true; return true;
} }
bool Builder::GenerateFunction(ast::Function* func) {
uint32_t func_type_id = GenerateFunctionTypeIfNeeded(func);
if (func_type_id == 0) {
return false;
}
auto func_op = result_op();
auto func_id = func_op.to_i();
push_debug(spv::Op::OpName,
{Operand::Int(func_id), Operand::String(func->name())});
auto ret_id = GenerateTypeIfNeeded(func->return_type());
if (ret_id == 0) {
return false;
}
// TODO(dsinclair): Handle parameters
push_inst(spv::Op::OpFunction, {Operand::Int(ret_id), func_op,
Operand::Int(SpvFunctionControlMaskNone),
Operand::Int(func_type_id)});
push_inst(spv::Op::OpLabel, {result_op()});
// TODO(dsinclair): Function body ...
push_inst(spv::Op::OpFunctionEnd, {});
func_name_to_id_[func->name()] = func_id;
return true;
}
uint32_t Builder::GenerateFunctionTypeIfNeeded(ast::Function* func) {
auto val = type_name_to_id_.find(func->type_name());
if (val != type_name_to_id_.end()) {
return val->second;
}
auto func_op = result_op();
auto func_type_id = func_op.to_i();
auto ret_id = GenerateTypeIfNeeded(func->return_type());
if (ret_id == 0) {
return 0;
}
// TODO(dsinclair): Handle parameters
push_type(spv::Op::OpTypeFunction, {func_op, Operand::Int(ret_id)});
type_name_to_id_[func->type_name()] = func_type_id;
return func_type_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();

View File

@ -110,7 +110,7 @@ class Builder {
instructions_.push_back(Instruction{op, operands}); instructions_.push_back(Instruction{op, operands});
} }
/// @returns the instruction list /// @returns the instruction list
const std::vector<Instruction>& inst() const { return instructions_; } const std::vector<Instruction>& instructions() const { return instructions_; }
/// Adds an instruction to the annotations /// Adds an instruction to the annotations
/// @param op the op to set /// @param op the op to set
/// @param operands the operands for the instruction /// @param operands the operands for the instruction
@ -124,6 +124,14 @@ class Builder {
/// @param ep the entry point /// @param ep the entry point
/// @returns true if the instruction was generated, false otherwise /// @returns true if the instruction was generated, false otherwise
bool GenerateEntryPoint(ast::EntryPoint* ep); bool GenerateEntryPoint(ast::EntryPoint* ep);
/// Generates the instructions for a function
/// @param func the function to generate
/// @returns true if the instructions were generated
bool GenerateFunction(ast::Function* func);
/// Generates a function type if not already created
/// @param func the function to generate for
/// @returns the ID to use for the function type. Returns 0 on failure.
uint32_t GenerateFunctionTypeIfNeeded(ast::Function* func);
/// Generates an import instruction /// Generates an import instruction
/// @param imp the import /// @param imp the import
void GenerateImport(ast::Import* imp); void GenerateImport(ast::Import* imp);

View File

@ -85,7 +85,7 @@ TEST_P(EntryPointStageTest, Emit) {
ASSERT_EQ(preamble.size(), 1); ASSERT_EQ(preamble.size(), 1);
EXPECT_EQ(preamble[0].opcode(), spv::Op::OpEntryPoint); EXPECT_EQ(preamble[0].opcode(), spv::Op::OpEntryPoint);
ASSERT_TRUE(preamble[0].operands().size() >= 3); ASSERT_GE(preamble[0].operands().size(), 3);
EXPECT_EQ(preamble[0].operands()[0].to_i(), params.model); EXPECT_EQ(preamble[0].operands()[0].to_i(), params.model);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(

View File

@ -0,0 +1,80 @@
// 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 <string>
#include "gtest/gtest.h"
#include "spirv/unified1/spirv.h"
#include "spirv/unified1/spirv.hpp11"
#include "src/ast/function.h"
#include "src/ast/type/void_type.h"
#include "src/writer/spirv/builder.h"
#include "src/writer/spirv/spv_dump.h"
namespace tint {
namespace writer {
namespace spirv {
using BuilderTest = testing::Test;
TEST_F(BuilderTest, Function_Empty) {
ast::type::VoidType void_type;
ast::Function func("a_func", {}, &void_type);
Builder b;
ASSERT_TRUE(b.GenerateFunction(&func));
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "a_func"
)");
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeVoid
%1 = OpTypeFunction %2
)");
EXPECT_EQ(DumpInstructions(b.instructions()), R"(%3 = OpFunction %2 None %1
%4 = OpLabel
OpFunctionEnd
)");
}
TEST_F(BuilderTest, DISABLED_Function_WithParams) {}
TEST_F(BuilderTest, DISABLED_Function_WithBody) {}
TEST_F(BuilderTest, FunctionType) {
ast::type::VoidType void_type;
ast::Function func("a_func", {}, &void_type);
Builder b;
ASSERT_TRUE(b.GenerateFunction(&func));
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeVoid
%1 = OpTypeFunction %2
)");
}
TEST_F(BuilderTest, FunctionType_DeDuplicate) {
ast::type::VoidType void_type;
ast::Function func1("a_func", {}, &void_type);
ast::Function func2("b_func", {}, &void_type);
Builder b;
ASSERT_TRUE(b.GenerateFunction(&func1));
ASSERT_TRUE(b.GenerateFunction(&func2));
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeVoid
%1 = OpTypeFunction %2
)");
}
} // namespace spirv
} // namespace writer
} // namespace tint

View File

@ -16,6 +16,7 @@
#define SRC_WRITER_SPIRV_SPV_DUMP_H_ #define SRC_WRITER_SPIRV_SPV_DUMP_H_
#include <string> #include <string>
#include <vector>
#include "src/writer/spirv/builder.h" #include "src/writer/spirv/builder.h"
#include "src/writer/spirv/instruction.h" #include "src/writer/spirv/instruction.h"