2021-03-31 17:44:27 +00:00
|
|
|
// Copyright 2021 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 "src/transform/canonicalize_entry_point_io.h"
|
|
|
|
|
2021-04-14 22:51:18 +00:00
|
|
|
#include <algorithm>
|
2021-03-31 17:44:27 +00:00
|
|
|
#include <utility>
|
2021-04-17 06:02:21 +00:00
|
|
|
#include <vector>
|
2021-03-31 17:44:27 +00:00
|
|
|
|
|
|
|
#include "src/program_builder.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/function.h"
|
|
|
|
#include "src/sem/statement.h"
|
2021-05-05 09:09:41 +00:00
|
|
|
#include "src/sem/struct.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/variable.h"
|
2021-03-31 17:44:27 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace transform {
|
|
|
|
|
|
|
|
CanonicalizeEntryPointIO::CanonicalizeEntryPointIO() = default;
|
|
|
|
CanonicalizeEntryPointIO::~CanonicalizeEntryPointIO() = default;
|
|
|
|
|
2021-04-14 22:51:18 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Comparison function used to reorder struct members such that all members with
|
|
|
|
// location attributes appear first (ordered by location slot), followed by
|
|
|
|
// those with builtin attributes.
|
|
|
|
bool StructMemberComparator(const ast::StructMember* a,
|
|
|
|
const ast::StructMember* b) {
|
|
|
|
auto* a_loc = ast::GetDecoration<ast::LocationDecoration>(a->decorations());
|
|
|
|
auto* b_loc = ast::GetDecoration<ast::LocationDecoration>(b->decorations());
|
|
|
|
auto* a_blt = ast::GetDecoration<ast::BuiltinDecoration>(a->decorations());
|
|
|
|
auto* b_blt = ast::GetDecoration<ast::BuiltinDecoration>(b->decorations());
|
|
|
|
if (a_loc) {
|
|
|
|
if (!b_loc) {
|
|
|
|
// `a` has location attribute and `b` does not: `a` goes first.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Both have location attributes: smallest goes first.
|
|
|
|
return a_loc->value() < b_loc->value();
|
|
|
|
} else {
|
|
|
|
if (b_loc) {
|
|
|
|
// `b` has location attribute and `a` does not: `b` goes first.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Both are builtins: order doesn't matter, just use enum value.
|
|
|
|
return a_blt->value() < b_blt->value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-04-16 08:35:24 +00:00
|
|
|
Output CanonicalizeEntryPointIO::Run(const Program* in, const DataMap&) {
|
2021-03-31 17:44:27 +00:00
|
|
|
ProgramBuilder out;
|
|
|
|
CloneContext ctx(&out, in);
|
|
|
|
|
|
|
|
// Strip entry point IO decorations from struct declarations.
|
|
|
|
// TODO(jrprice): This code is duplicated with the SPIR-V transform.
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* ty : ctx.src->AST().ConstructedTypes()) {
|
|
|
|
if (auto* struct_ty = ty->As<ast::Struct>()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
// Build new list of struct members without entry point IO decorations.
|
|
|
|
ast::StructMemberList new_struct_members;
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* member : struct_ty->members()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
ast::DecorationList new_decorations = RemoveDecorations(
|
|
|
|
&ctx, member->decorations(), [](const ast::Decoration* deco) {
|
|
|
|
return deco
|
|
|
|
->IsAnyOf<ast::BuiltinDecoration, ast::LocationDecoration>();
|
|
|
|
});
|
|
|
|
new_struct_members.push_back(
|
2021-04-07 11:16:01 +00:00
|
|
|
ctx.dst->Member(ctx.Clone(member->symbol()),
|
2021-03-31 17:44:27 +00:00
|
|
|
ctx.Clone(member->type()), new_decorations));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redeclare the struct.
|
2021-05-05 09:09:41 +00:00
|
|
|
auto new_struct_name = ctx.Clone(struct_ty->name());
|
2021-04-20 15:21:21 +00:00
|
|
|
auto* new_struct =
|
2021-05-05 09:09:41 +00:00
|
|
|
ctx.dst->create<ast::Struct>(new_struct_name, new_struct_members,
|
|
|
|
ctx.Clone(struct_ty->decorations()));
|
2021-03-31 17:44:27 +00:00
|
|
|
ctx.Replace(struct_ty, new_struct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* func_ast : ctx.src->AST().Functions()) {
|
|
|
|
if (!func_ast->IsEntryPoint()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
auto* func = ctx.src->Sem().Get(func_ast);
|
|
|
|
|
2021-03-31 17:44:27 +00:00
|
|
|
ast::VariableList new_parameters;
|
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
if (!func->Parameters().empty()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
// Collect all parameters and build a list of new struct members.
|
2021-04-29 12:59:14 +00:00
|
|
|
auto new_struct_param_symbol = ctx.dst->Sym();
|
2021-03-31 17:44:27 +00:00
|
|
|
ast::StructMemberList new_struct_members;
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* param : func->Parameters()) {
|
|
|
|
auto param_name = ctx.Clone(param->Declaration()->symbol());
|
|
|
|
auto* param_ty = param->Type();
|
|
|
|
auto* param_declared_ty = param->Declaration()->type();
|
2021-03-31 17:44:27 +00:00
|
|
|
|
2021-04-17 06:02:21 +00:00
|
|
|
std::function<ast::Expression*()> func_const_initializer;
|
2021-03-31 17:44:27 +00:00
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (auto* str = param_ty->As<sem::Struct>()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
// Pull out all struct members and build initializer list.
|
2021-04-17 06:02:21 +00:00
|
|
|
std::vector<Symbol> member_names;
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* member : str->Members()) {
|
2021-05-07 14:49:34 +00:00
|
|
|
if (member->Type()->UnwrapAll()->Is<sem::Struct>()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
TINT_ICE(ctx.dst->Diagnostics()) << "nested pipeline IO struct";
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::DecorationList new_decorations = RemoveDecorations(
|
2021-05-05 09:09:41 +00:00
|
|
|
&ctx, member->Declaration()->decorations(),
|
|
|
|
[](const ast::Decoration* deco) {
|
2021-03-31 17:44:27 +00:00
|
|
|
return !deco->IsAnyOf<ast::BuiltinDecoration,
|
|
|
|
ast::LocationDecoration>();
|
|
|
|
});
|
2021-05-05 09:09:41 +00:00
|
|
|
auto member_name = ctx.Clone(member->Declaration()->symbol());
|
|
|
|
auto* member_type = ctx.Clone(member->Declaration()->type());
|
|
|
|
new_struct_members.push_back(
|
|
|
|
ctx.dst->Member(member_name, member_type, new_decorations));
|
2021-04-17 06:02:21 +00:00
|
|
|
member_names.emplace_back(member_name);
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 06:02:21 +00:00
|
|
|
func_const_initializer = [&ctx, new_struct_param_symbol,
|
|
|
|
param_declared_ty, member_names]() {
|
|
|
|
ast::ExpressionList init_values;
|
|
|
|
for (auto name : member_names) {
|
|
|
|
init_values.push_back(
|
|
|
|
ctx.dst->MemberAccessor(new_struct_param_symbol, name));
|
|
|
|
}
|
|
|
|
return ctx.dst->Construct(ctx.Clone(param_declared_ty),
|
|
|
|
init_values);
|
|
|
|
};
|
2021-03-31 17:44:27 +00:00
|
|
|
} else {
|
|
|
|
ast::DecorationList new_decorations = RemoveDecorations(
|
2021-05-05 09:09:41 +00:00
|
|
|
&ctx, param->Declaration()->decorations(),
|
|
|
|
[](const ast::Decoration* deco) {
|
2021-03-31 17:44:27 +00:00
|
|
|
return !deco->IsAnyOf<ast::BuiltinDecoration,
|
|
|
|
ast::LocationDecoration>();
|
|
|
|
});
|
|
|
|
new_struct_members.push_back(ctx.dst->Member(
|
2021-04-13 13:32:33 +00:00
|
|
|
param_name, ctx.Clone(param_declared_ty), new_decorations));
|
2021-04-17 06:02:21 +00:00
|
|
|
func_const_initializer = [&ctx, new_struct_param_symbol,
|
|
|
|
param_name]() {
|
|
|
|
return ctx.dst->MemberAccessor(new_struct_param_symbol, param_name);
|
|
|
|
};
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 09:09:41 +00:00
|
|
|
if (func_ast->body()->empty()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
// Don't generate a function-scope const if the function is empty.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a function-scope const to replace the parameter.
|
|
|
|
// Initialize it with the value extracted from the new struct parameter.
|
2021-04-13 13:32:33 +00:00
|
|
|
auto* func_const = ctx.dst->Const(
|
2021-04-17 06:02:21 +00:00
|
|
|
param_name, ctx.Clone(param_declared_ty), func_const_initializer());
|
2021-05-05 09:09:41 +00:00
|
|
|
ctx.InsertBefore(func_ast->body()->statements(),
|
|
|
|
*func_ast->body()->begin(),
|
2021-03-31 17:44:27 +00:00
|
|
|
ctx.dst->WrapInStatement(func_const));
|
|
|
|
|
|
|
|
// Replace all uses of the function parameter with the function const.
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* user : param->Users()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
ctx.Replace<ast::Expression>(user->Declaration(),
|
2021-04-07 11:16:01 +00:00
|
|
|
ctx.dst->Expr(param_name));
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 22:51:18 +00:00
|
|
|
// Sort struct members to satisfy HLSL interfacing matching rules.
|
|
|
|
std::sort(new_struct_members.begin(), new_struct_members.end(),
|
|
|
|
StructMemberComparator);
|
|
|
|
|
2021-03-31 17:44:27 +00:00
|
|
|
// Create the new struct type.
|
2021-04-29 12:59:14 +00:00
|
|
|
auto in_struct_name = ctx.dst->Sym();
|
2021-05-05 09:09:41 +00:00
|
|
|
auto* in_struct = ctx.dst->create<ast::Struct>(
|
|
|
|
in_struct_name, new_struct_members, ast::DecorationList{});
|
|
|
|
ctx.InsertBefore(ctx.src->AST().GlobalDeclarations(), func_ast,
|
|
|
|
in_struct);
|
2021-03-31 17:44:27 +00:00
|
|
|
|
|
|
|
// Create a new function parameter using this struct type.
|
2021-05-05 09:09:41 +00:00
|
|
|
auto* struct_param = ctx.dst->Param(
|
|
|
|
new_struct_param_symbol, ctx.dst->ty.type_name(in_struct_name));
|
2021-03-31 17:44:27 +00:00
|
|
|
new_parameters.push_back(struct_param);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle return type.
|
2021-05-10 17:38:01 +00:00
|
|
|
auto* ret_type = func->ReturnType();
|
2021-05-05 09:09:41 +00:00
|
|
|
std::function<ast::Type*()> new_ret_type;
|
2021-04-19 22:51:23 +00:00
|
|
|
if (ret_type->Is<sem::Void>()) {
|
2021-05-05 09:09:41 +00:00
|
|
|
new_ret_type = [&ctx] { return ctx.dst->ty.void_(); };
|
2021-03-31 17:44:27 +00:00
|
|
|
} else {
|
|
|
|
ast::StructMemberList new_struct_members;
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
if (auto* str = ret_type->As<sem::Struct>()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
// Rebuild struct with only the entry point IO attributes.
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* member : str->Members()) {
|
2021-05-07 14:49:34 +00:00
|
|
|
if (member->Type()->UnwrapAll()->Is<sem::Struct>()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
TINT_ICE(ctx.dst->Diagnostics()) << "nested pipeline IO struct";
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::DecorationList new_decorations = RemoveDecorations(
|
2021-05-05 09:09:41 +00:00
|
|
|
&ctx, member->Declaration()->decorations(),
|
|
|
|
[](const ast::Decoration* deco) {
|
2021-03-31 17:44:27 +00:00
|
|
|
return !deco->IsAnyOf<ast::BuiltinDecoration,
|
|
|
|
ast::LocationDecoration>();
|
|
|
|
});
|
2021-05-05 09:09:41 +00:00
|
|
|
auto symbol = ctx.Clone(member->Declaration()->symbol());
|
|
|
|
auto* member_ty = ctx.Clone(member->Declaration()->type());
|
2021-04-07 11:16:01 +00:00
|
|
|
new_struct_members.push_back(
|
2021-05-05 09:09:41 +00:00
|
|
|
ctx.dst->Member(symbol, member_ty, new_decorations));
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-05 09:09:41 +00:00
|
|
|
auto* member_ty = ctx.Clone(func->Declaration()->return_type());
|
|
|
|
auto decos = ctx.Clone(func_ast->return_type_decorations());
|
2021-03-31 17:44:27 +00:00
|
|
|
new_struct_members.push_back(
|
2021-05-05 09:09:41 +00:00
|
|
|
ctx.dst->Member("value", member_ty, std::move(decos)));
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:51:18 +00:00
|
|
|
// Sort struct members to satisfy HLSL interfacing matching rules.
|
|
|
|
std::sort(new_struct_members.begin(), new_struct_members.end(),
|
|
|
|
StructMemberComparator);
|
|
|
|
|
2021-03-31 17:44:27 +00:00
|
|
|
// Create the new struct type.
|
2021-04-29 12:59:14 +00:00
|
|
|
auto out_struct_name = ctx.dst->Sym();
|
2021-05-05 09:09:41 +00:00
|
|
|
auto* out_struct = ctx.dst->create<ast::Struct>(
|
|
|
|
out_struct_name, new_struct_members, ast::DecorationList{});
|
|
|
|
ctx.InsertBefore(ctx.src->AST().GlobalDeclarations(), func_ast,
|
|
|
|
out_struct);
|
|
|
|
new_ret_type = [out_struct_name, &ctx] {
|
|
|
|
return ctx.dst->ty.type_name(out_struct_name);
|
|
|
|
};
|
2021-03-31 17:44:27 +00:00
|
|
|
|
|
|
|
// Replace all return statements.
|
2021-05-05 09:09:41 +00:00
|
|
|
for (auto* ret : func->ReturnStatements()) {
|
2021-03-31 21:00:26 +00:00
|
|
|
auto* ret_sem = ctx.src->Sem().Get(ret);
|
2021-03-31 17:44:27 +00:00
|
|
|
// Reconstruct the return value using the newly created struct.
|
2021-04-17 00:42:41 +00:00
|
|
|
std::function<ast::Expression*()> new_ret_value = [&ctx, ret] {
|
|
|
|
return ctx.Clone(ret->value());
|
|
|
|
};
|
|
|
|
|
2021-03-31 17:44:27 +00:00
|
|
|
ast::ExpressionList ret_values;
|
2021-05-07 14:49:34 +00:00
|
|
|
if (ret_type->Is<sem::Struct>()) {
|
2021-03-31 17:44:27 +00:00
|
|
|
if (!ret->value()->Is<ast::IdentifierExpression>()) {
|
|
|
|
// Create a const to hold the return value expression to avoid
|
|
|
|
// re-evaluating it multiple times.
|
2021-04-29 12:59:14 +00:00
|
|
|
auto temp = ctx.dst->Sym();
|
2021-05-05 09:09:41 +00:00
|
|
|
auto* ty = CreateASTTypeFor(&ctx, ret_type);
|
|
|
|
auto* temp_var =
|
|
|
|
ctx.dst->Decl(ctx.dst->Const(temp, ty, new_ret_value()));
|
2021-03-31 21:00:26 +00:00
|
|
|
ctx.InsertBefore(ret_sem->Block()->statements(), ret, temp_var);
|
2021-04-17 00:42:41 +00:00
|
|
|
new_ret_value = [&ctx, temp] { return ctx.dst->Expr(temp); };
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:51:18 +00:00
|
|
|
for (auto* member : new_struct_members) {
|
|
|
|
ret_values.push_back(
|
2021-04-17 00:42:41 +00:00
|
|
|
ctx.dst->MemberAccessor(new_ret_value(), member->symbol()));
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-17 00:42:41 +00:00
|
|
|
ret_values.push_back(new_ret_value());
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 13:50:53 +00:00
|
|
|
auto* new_ret =
|
2021-05-05 09:09:41 +00:00
|
|
|
ctx.dst->Return(ctx.dst->Construct(new_ret_type(), ret_values));
|
2021-03-31 17:44:27 +00:00
|
|
|
ctx.Replace(ret, new_ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite the function header with the new parameters.
|
|
|
|
auto* new_func = ctx.dst->create<ast::Function>(
|
2021-05-05 09:09:41 +00:00
|
|
|
func_ast->source(), ctx.Clone(func_ast->symbol()), new_parameters,
|
|
|
|
new_ret_type(), ctx.Clone(func_ast->body()),
|
|
|
|
ctx.Clone(func_ast->decorations()), ast::DecorationList{});
|
|
|
|
ctx.Replace(func_ast, new_func);
|
2021-03-31 17:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Clone();
|
|
|
|
return Output(Program(std::move(out)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace transform
|
|
|
|
} // namespace tint
|