blob: 5b02260dadd35e25f3ad259ac5af547dbe386eb7 [file] [log] [blame]
// Copyright 2022 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
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.h"
#include <utility>
#include "src/tint/lang/core/type/struct.h"
#include "src/tint/lang/wgsl/ast/transform/hoist_to_decl_before.h"
#include "src/tint/lang/wgsl/ast/traverse_expressions.h"
#include "src/tint/lang/wgsl/program/clone_context.h"
#include "src/tint/lang/wgsl/program/program_builder.h"
#include "src/tint/lang/wgsl/resolver/resolve.h"
#include "src/tint/lang/wgsl/sem/call.h"
#include "src/tint/lang/wgsl/sem/statement.h"
#include "src/tint/lang/wgsl/sem/value_constructor.h"
#include "src/tint/utils/containers/hashset.h"
namespace tint::ast::transform {
PromoteInitializersToLet::PromoteInitializersToLet() = default;
PromoteInitializersToLet::~PromoteInitializersToLet() = default;
Transform::ApplyResult PromoteInitializersToLet::Apply(const Program& src,
const DataMap&,
DataMap&) const {
ProgramBuilder b;
program::CloneContext ctx{&b, &src, /* auto_clone_symbols */ true};
// Returns true if the expression should be hoisted to a new let statement before the
// expression's statement.
auto should_hoist = [&](const sem::ValueExpression* expr) {
if (!expr->Type()->IsAnyOf<core::type::Array, core::type::Struct>()) {
// We only care about array and struct initializers
return false;
// Check whether the expression is an array or structure constructor
// Follow const-chains
auto* root_expr = expr;
if (expr->Stage() == core::EvaluationStage::kConstant) {
if (expr->Type()->HoldsAbstract()) {
// Do not hoist expressions that are not materialized, as doing so would cause
// premature materialization.
return false;
while (auto* user = root_expr->UnwrapMaterialize()->As<sem::VariableUser>()) {
root_expr = user->Variable()->Initializer();
auto* ctor = root_expr->UnwrapMaterialize()->As<sem::Call>();
if (!ctor || !ctor->Target()->Is<sem::ValueConstructor>()) {
// Root expression is not a value constructor. Not interested in this.
return false;
if (auto* src_var_decl = expr->Stmt()->Declaration()->As<VariableDeclStatement>()) {
if (src_var_decl->variable->initializer == expr->Declaration()) {
// This statement is just a variable declaration with the initializer as the
// initializer value. This is what we're attempting to transform to, and so
// ignore.
return false;
return true;
// A list of expressions that should be hoisted.
tint::Vector<const sem::ValueExpression*, 32> to_hoist;
// A set of expressions that are constant, which _may_ need to be hoisted.
Hashset<const Expression*, 32> const_chains;
// Walk the AST nodes. This order guarantees that leaf-expressions are visited first.
for (auto* node : src.ASTNodes().Objects()) {
if (auto* sem = src.Sem().GetVal(node)) {
auto* stmt = sem->Stmt();
if (!stmt) {
// Expression is outside of a statement. This usually means the expression is part
// of a global (module-scope) constant declaration. These must be constexpr, and so
// cannot contain the type of expressions that must be sanitized.
if (sem->Stage() == core::EvaluationStage::kConstant) {
// Expression is constant. We only need to hoist expressions if they're the
// outermost constant expression in a chain. Remove the immediate child nodes of the
// expression from const_chains, and add this expression to the const_chains. As we
// visit leaf-expressions first, this means the content of const_chains only
// contains the outer-most constant expressions.
auto* expr = sem->Declaration();
bool ok = TraverseExpressions(expr, [&](const Expression* child) {
return child == expr ? TraverseAction::Descend : TraverseAction::Skip;
if (!ok) {
return resolver::Resolve(b);
} else if (should_hoist(sem)) {
// After walking the full AST, const_chains only contains the outer-most constant expressions.
// Check if any of these need hoisting, and append those to to_hoist.
for (auto* expr : const_chains) {
if (auto* sem = src.Sem().GetVal(expr); should_hoist(sem)) {
if (to_hoist.IsEmpty()) {
// Nothing to do. Skip.
return SkipTransform;
// The order of to_hoist is currently undefined. Sort by AST node id, which will make this
// deterministic.
to_hoist.Sort([&](auto* expr_a, auto* expr_b) {
return expr_a->Declaration()->node_id < expr_b->Declaration()->node_id;
// Hoist all the expressions in to_hoist to a constant variable, declared just before the
// statement of usage.
HoistToDeclBefore hoist_to_decl_before(ctx);
for (auto* expr : to_hoist) {
if (!hoist_to_decl_before.Add(expr, expr->Declaration(),
HoistToDeclBefore::VariableKind::kLet)) {
return resolver::Resolve(b);
return resolver::Resolve(b);
} // namespace tint::ast::transform