mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-08-11 22:49:16 +00:00
[metal-writer] Emit functions
This Cl adds the code to emit functions from the metal writer. Note, this does not handle entry points yet. Bug: tint:8 Change-Id: Ie665771169261f6839de5eb1b66dc511bf47616a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23704 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
646fb23c1f
commit
e66d6a6745
1
BUILD.gn
1
BUILD.gn
@ -885,6 +885,7 @@ source_set("tint_unittests_wgsl_writer_src") {
|
|||||||
|
|
||||||
source_set("tint_unittests_msl_writer_src") {
|
source_set("tint_unittests_msl_writer_src") {
|
||||||
sources = [
|
sources = [
|
||||||
|
"src/writer/msl/generator_impl_function_test.cc",
|
||||||
"src/writer/msl/generator_impl_identifier_test.cc",
|
"src/writer/msl/generator_impl_identifier_test.cc",
|
||||||
"src/writer/msl/generator_impl_return_test.cc",
|
"src/writer/msl/generator_impl_return_test.cc",
|
||||||
"src/writer/msl/generator_impl_test.cc",
|
"src/writer/msl/generator_impl_test.cc",
|
||||||
|
@ -493,6 +493,7 @@ endif()
|
|||||||
|
|
||||||
if(${TINT_BUILD_MSL_WRITER})
|
if(${TINT_BUILD_MSL_WRITER})
|
||||||
list(APPEND TINT_TEST_SRCS
|
list(APPEND TINT_TEST_SRCS
|
||||||
|
writer/msl/generator_impl_function_test.cc
|
||||||
writer/msl/generator_impl_identifier_test.cc
|
writer/msl/generator_impl_identifier_test.cc
|
||||||
writer/msl/generator_impl_return_test.cc
|
writer/msl/generator_impl_return_test.cc
|
||||||
writer/msl/generator_impl_test.cc
|
writer/msl/generator_impl_test.cc
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#include "src/writer/msl/generator_impl.h"
|
#include "src/writer/msl/generator_impl.h"
|
||||||
|
|
||||||
|
#include "src/ast/function.h"
|
||||||
#include "src/ast/identifier_expression.h"
|
#include "src/ast/identifier_expression.h"
|
||||||
#include "src/ast/return_statement.h"
|
#include "src/ast/return_statement.h"
|
||||||
#include "src/ast/type/alias_type.h"
|
#include "src/ast/type/alias_type.h"
|
||||||
@ -36,7 +37,13 @@ GeneratorImpl::GeneratorImpl() = default;
|
|||||||
|
|
||||||
GeneratorImpl::~GeneratorImpl() = default;
|
GeneratorImpl::~GeneratorImpl() = default;
|
||||||
|
|
||||||
bool GeneratorImpl::Generate(const ast::Module&) {
|
bool GeneratorImpl::Generate(const ast::Module& module) {
|
||||||
|
for (const auto& func : module.functions()) {
|
||||||
|
if (!EmitFunction(func.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << std::endl;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,6 +56,35 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitFunction(ast::Function* func) {
|
||||||
|
make_indent();
|
||||||
|
|
||||||
|
if (!EmitType(func->return_type(), "")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_ << " " << func->name() << "(";
|
||||||
|
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& v : func->params()) {
|
||||||
|
if (!first) {
|
||||||
|
out_ << ", ";
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
|
||||||
|
if (!EmitType(v->type(), v->name())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Array name is output as part of the type
|
||||||
|
if (!v->type()->IsArray()) {
|
||||||
|
out_ << " " << v->name();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out_ << ")";
|
||||||
|
|
||||||
|
return EmitStatementBlockAndNewline(func->body());
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
|
bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
|
||||||
auto* ident = expr->AsIdentifier();
|
auto* ident = expr->AsIdentifier();
|
||||||
if (ident->has_path()) {
|
if (ident->has_path()) {
|
||||||
|
@ -41,6 +41,10 @@ class GeneratorImpl : public TextGenerator {
|
|||||||
/// @param expr the expression
|
/// @param expr the expression
|
||||||
/// @returns true if the expression was emitted
|
/// @returns true if the expression was emitted
|
||||||
bool EmitExpression(ast::Expression* expr);
|
bool EmitExpression(ast::Expression* expr);
|
||||||
|
/// Handles generating a function
|
||||||
|
/// @param func the function to generate
|
||||||
|
/// @returns true if the function was emitted
|
||||||
|
bool EmitFunction(ast::Function* func);
|
||||||
/// Handles generating an identifier expression
|
/// Handles generating an identifier expression
|
||||||
/// @param expr the identifier expression
|
/// @param expr the identifier expression
|
||||||
/// @returns true if the identifeir was emitted
|
/// @returns true if the identifeir was emitted
|
||||||
|
111
src/writer/msl/generator_impl_function_test.cc
Normal file
111
src/writer/msl/generator_impl_function_test.cc
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
// 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/function.h"
|
||||||
|
#include "src/ast/return_statement.h"
|
||||||
|
#include "src/ast/type/array_type.h"
|
||||||
|
#include "src/ast/type/f32_type.h"
|
||||||
|
#include "src/ast/type/i32_type.h"
|
||||||
|
#include "src/ast/type/void_type.h"
|
||||||
|
#include "src/ast/variable.h"
|
||||||
|
#include "src/writer/msl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace msl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using MslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(MslGeneratorImplTest, Emit_Function) {
|
||||||
|
ast::type::VoidType void_type;
|
||||||
|
|
||||||
|
ast::Function func("my_func", {}, &void_type);
|
||||||
|
|
||||||
|
ast::StatementList body;
|
||||||
|
body.push_back(std::make_unique<ast::ReturnStatement>());
|
||||||
|
func.set_body(std::move(body));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitFunction(&func));
|
||||||
|
EXPECT_EQ(g.result(), R"( void my_func() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MslGeneratorImplTest, Emit_Function_WithParams) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::type::I32Type i32;
|
||||||
|
|
||||||
|
ast::VariableList params;
|
||||||
|
params.push_back(
|
||||||
|
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32));
|
||||||
|
params.push_back(
|
||||||
|
std::make_unique<ast::Variable>("b", ast::StorageClass::kNone, &i32));
|
||||||
|
|
||||||
|
ast::type::VoidType void_type;
|
||||||
|
ast::Function func("my_func", std::move(params), &void_type);
|
||||||
|
|
||||||
|
ast::StatementList body;
|
||||||
|
body.push_back(std::make_unique<ast::ReturnStatement>());
|
||||||
|
func.set_body(std::move(body));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitFunction(&func));
|
||||||
|
EXPECT_EQ(g.result(), R"( void my_func(float a, int b) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MslGeneratorImplTest, Emit_Function_WithArrayParams) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::type::ArrayType ary(&f32, 5);
|
||||||
|
|
||||||
|
ast::VariableList params;
|
||||||
|
params.push_back(
|
||||||
|
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &ary));
|
||||||
|
|
||||||
|
ast::type::VoidType void_type;
|
||||||
|
auto func =
|
||||||
|
std::make_unique<ast::Function>("my_func", std::move(params), &void_type);
|
||||||
|
|
||||||
|
ast::StatementList body;
|
||||||
|
body.push_back(std::make_unique<ast::ReturnStatement>());
|
||||||
|
func->set_body(std::move(body));
|
||||||
|
|
||||||
|
ast::Module m;
|
||||||
|
m.AddFunction(std::move(func));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.Generate(m)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( void my_func(float a[5]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace msl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
Loading…
x
Reference in New Issue
Block a user