mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-07-29 16:35:43 +00:00
Add a new 'Target' to the ast::CallExpression, which can be either an Identifier or Type. The Identifier may resolve to a Type, if the Type is a structure or alias. The Resolver now resolves the CallExpression target to one of the following sem::CallTargets: * sem::Function * sem::Intrinsic * sem::TypeConstructor * sem::TypeCast This change will allow us to remove the type tracking logic from the WGSL parser, which is required for out-of-order module scope declarations. Bug: tint:888 Bug: tint:1266 Change-Id: I696f117115a50981fd5c102a0d7764641bb755dd Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/68525 Reviewed-by: David Neto <dneto@google.com> Reviewed-by: James Price <jrprice@google.com> Kokoro: Kokoro <noreply+kokoro@google.com>
100 lines
3.0 KiB
C++
100 lines
3.0 KiB
C++
// 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/fold_constants.h"
|
|
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "src/program_builder.h"
|
|
#include "src/sem/call.h"
|
|
#include "src/sem/expression.h"
|
|
#include "src/sem/type_constructor.h"
|
|
#include "src/sem/type_conversion.h"
|
|
|
|
TINT_INSTANTIATE_TYPEINFO(tint::transform::FoldConstants);
|
|
|
|
namespace tint {
|
|
namespace transform {
|
|
|
|
FoldConstants::FoldConstants() = default;
|
|
|
|
FoldConstants::~FoldConstants() = default;
|
|
|
|
void FoldConstants::Run(CloneContext& ctx, const DataMap&, DataMap&) {
|
|
ctx.ReplaceAll([&](const ast::Expression* expr) -> const ast::Expression* {
|
|
auto* call = ctx.src->Sem().Get<sem::Call>(expr);
|
|
if (!call) {
|
|
return nullptr;
|
|
}
|
|
|
|
auto value = call->ConstantValue();
|
|
if (!value.IsValid()) {
|
|
return nullptr;
|
|
}
|
|
|
|
auto* ty = call->Type();
|
|
|
|
if (!call->Target()->IsAnyOf<sem::TypeConversion, sem::TypeConstructor>()) {
|
|
return nullptr;
|
|
}
|
|
|
|
// If original ctor expression had no init values, don't replace the
|
|
// expression
|
|
if (call->Arguments().empty()) {
|
|
return nullptr;
|
|
}
|
|
|
|
if (auto* vec = ty->As<sem::Vector>()) {
|
|
uint32_t vec_size = static_cast<uint32_t>(vec->Width());
|
|
|
|
// We'd like to construct the new vector with the same number of
|
|
// constructor args that the original node had, but after folding
|
|
// constants, cases like the following are problematic:
|
|
//
|
|
// vec3<f32> = vec3<f32>(vec2<f32>, 1.0) // vec_size=3, ctor_size=2
|
|
//
|
|
// In this case, creating a vec3 with 2 args is invalid, so we should
|
|
// create it with 3. So what we do is construct with vec_size args,
|
|
// except if the original vector was single-value initialized, in
|
|
// which case, we only construct with one arg again.
|
|
uint32_t ctor_size = (call->Arguments().size() == 1) ? 1 : vec_size;
|
|
|
|
ast::ExpressionList ctors;
|
|
for (uint32_t i = 0; i < ctor_size; ++i) {
|
|
value.WithScalarAt(
|
|
i, [&](auto&& s) { ctors.emplace_back(ctx.dst->Expr(s)); });
|
|
}
|
|
|
|
auto* el_ty = CreateASTTypeFor(ctx, vec->type());
|
|
return ctx.dst->vec(el_ty, vec_size, ctors);
|
|
}
|
|
|
|
if (ty->is_scalar()) {
|
|
return value.WithScalarAt(0,
|
|
[&](auto&& s) -> const ast::LiteralExpression* {
|
|
return ctx.dst->Expr(s);
|
|
});
|
|
}
|
|
|
|
return nullptr;
|
|
});
|
|
|
|
ctx.Clone();
|
|
}
|
|
|
|
} // namespace transform
|
|
} // namespace tint
|