tint: Remove ast::CallExpression -> sem::Call implicit mapping

With abstract materialization, any ast::Expression may map to the new
sem::Materialize node. Because of this, we can't assume that an
ast::CallExpression maps to a sem::Call, as it might be wrapped by a
sem::Materialize.

Remove the mapping, and fix up all the code that was relying on this.

Fixes are done by either:
• Calling `UnwrapMaterialize()->As<sem::Call>()` on the semantic
  expression. This is done when the logic may assume it's possible for
  the expression to be a Materialize node.
• Using the explicit sem::Info::Get<sem::Call>() template argument to
  cast the semantic type to sem::Call. This is done when the logic
  either knows it is impossible for the expression to be a Materialize.

The backends have been stubbed, as we'll want to emit the constant value
for these nodes. It's likely that we'll just use the FoldConstants
transform to strip all Materialize nodes from the tree. For now, be
defensive.

Bug: tint:1504
Change-Id: If9231b300fc30c7fe886c17a804ead8ee2988285
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/90533
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton
2022-05-19 21:50:59 +00:00
committed by Dawn LUCI CQ
parent 7b921fb4c7
commit e5a67ac891
28 changed files with 120 additions and 75 deletions

View File

@@ -52,7 +52,7 @@ static void IterateArrayLengthOnStorageVar(CloneContext& ctx, F&& functor) {
continue;
}
auto* call = sem.Get(call_expr);
auto* call = sem.Get(call_expr)->UnwrapMaterialize()->As<sem::Call>();
auto* builtin = call->Target()->As<sem::Builtin>();
if (!builtin || builtin->Type() != sem::BuiltinType::kArrayLength) {
continue;

View File

@@ -123,7 +123,7 @@ void CalculateArrayLength::Run(CloneContext& ctx, const DataMap&, DataMap&) cons
// Find all the arrayLength() calls...
for (auto* node : ctx.src->ASTNodes().Objects()) {
if (auto* call_expr = node->As<ast::CallExpression>()) {
auto* call = sem.Get(call_expr);
auto* call = sem.Get(call_expr)->UnwrapMaterialize()->As<sem::Call>();
if (auto* builtin = call->Target()->As<sem::Builtin>()) {
if (builtin->Type() == sem::BuiltinType::kArrayLength) {
// We're dealing with an arrayLength() call

View File

@@ -220,7 +220,7 @@ struct CombineSamplers::State {
// sampler parameters to use the current function's combined samplers or
// the combined global samplers, as appropriate.
ctx.ReplaceAll([&](const ast::CallExpression* expr) -> const ast::Expression* {
if (auto* call = sem.Get(expr)) {
if (auto* call = sem.Get(expr)->UnwrapMaterialize()->As<sem::Call>()) {
ast::ExpressionList args;
// Replace all texture builtin calls.
if (auto* builtin = call->Target()->As<sem::Builtin>()) {

View File

@@ -882,7 +882,7 @@ void DecomposeMemoryAccess::Run(CloneContext& ctx, const DataMap&, DataMap&) con
}
if (auto* call_expr = node->As<ast::CallExpression>()) {
auto* call = sem.Get(call_expr);
auto* call = sem.Get(call_expr)->UnwrapMaterialize()->As<sem::Call>();
if (auto* builtin = call->Target()->As<sem::Builtin>()) {
if (builtin->Type() == sem::BuiltinType::kArrayLength) {
// arrayLength(X)

View File

@@ -115,7 +115,7 @@ void DecomposeStridedArray::Run(CloneContext& ctx, const DataMap&, DataMap&) con
// `array<strided_arr, 3>(strided_arr(1), strided_arr(2), strided_arr(3))`
ctx.ReplaceAll([&](const ast::CallExpression* expr) -> const ast::Expression* {
if (!expr->args.empty()) {
if (auto* call = sem.Get(expr)) {
if (auto* call = sem.Get(expr)->UnwrapMaterialize()->As<sem::Call>()) {
if (auto* ctor = call->Target()->As<sem::TypeConstructor>()) {
if (auto* arr = ctor->ReturnType()->As<sem::Array>()) {
// Begin by cloning the array constructor type or name

View File

@@ -184,7 +184,8 @@ struct MultiplanarExternalTexture::State {
// Transform the original textureLoad and textureSampleLevel calls into
// textureLoadExternal and textureSampleExternal calls.
ctx.ReplaceAll([&](const ast::CallExpression* expr) -> const ast::CallExpression* {
auto* builtin = sem.Get(expr)->Target()->As<sem::Builtin>();
auto* call = sem.Get(expr)->UnwrapMaterialize()->As<sem::Call>();
auto* builtin = call->Target()->As<sem::Builtin>();
if (builtin && !builtin->Parameters().empty() &&
builtin->Parameters()[0]->Type()->Is<sem::ExternalTexture>() &&
@@ -209,7 +210,7 @@ struct MultiplanarExternalTexture::State {
}
}
} else if (sem.Get(expr)->Target()->Is<sem::Function>()) {
} else if (call->Target()->Is<sem::Function>()) {
// The call expression may be to a user-defined function that
// contains a texture_external parameter. These need to be expanded
// out to multiple plane textures and the texture parameters

View File

@@ -33,7 +33,7 @@ void PromoteInitializersToConstVar::Run(CloneContext& ctx, const DataMap&, DataM
// Hoists array and structure initializers to a constant variable, declared
// just before the statement of usage.
auto type_ctor_to_let = [&](const ast::CallExpression* expr) {
auto* ctor = ctx.src->Sem().Get(expr);
auto* ctor = ctx.src->Sem().Get(expr)->UnwrapMaterialize()->As<sem::Call>();
if (!ctor->Target()->Is<sem::TypeConstructor>()) {
return true;
}

View File

@@ -86,12 +86,19 @@ void RemovePhonies::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
if (stmt->lhs->Is<ast::PhonyExpression>()) {
std::vector<const ast::Expression*> side_effects;
if (!ast::TraverseExpressions(
stmt->rhs, ctx.dst->Diagnostics(), [&](const ast::CallExpression* call) {
stmt->rhs, ctx.dst->Diagnostics(), [&](const ast::CallExpression* expr) {
// ast::CallExpression may map to a function or builtin call
// (both may have side-effects), or a type constructor or
// type conversion (both do not have side effects).
if (sem.Get(call)->Target()->IsAnyOf<sem::Function, sem::Builtin>()) {
side_effects.push_back(call);
auto* call = sem.Get<sem::Call>(expr);
if (!call) {
// Semantic node must be a Materialize, in which case the expression
// was creation-time (compile time), so could not have side effects.
// Just skip.
return ast::TraverseAction::Skip;
}
if (call->Target()->IsAnyOf<sem::Function, sem::Builtin>()) {
side_effects.push_back(expr);
return ast::TraverseAction::Skip;
}
return ast::TraverseAction::Descend;

View File

@@ -1278,7 +1278,7 @@ Output Renamer::Run(const Program* in, const DataMap& inputs) const {
}
}
} else if (auto* call = node->As<ast::CallExpression>()) {
auto* sem = in->Sem().Get(call);
auto* sem = in->Sem().Get(call)->UnwrapMaterialize()->As<sem::Call>();
if (!sem) {
TINT_ICE(Transform, out.Diagnostics()) << "CallExpression has no semantic info";
continue;

View File

@@ -206,7 +206,7 @@ struct Robustness::State {
/// @return the clamped replacement call expression, or nullptr if `expr`
/// should be cloned without changes.
const ast::CallExpression* Transform(const ast::CallExpression* expr) {
auto* call = ctx.src->Sem().Get(expr);
auto* call = ctx.src->Sem().Get(expr)->UnwrapMaterialize()->As<sem::Call>();
auto* call_target = call->Target();
auto* builtin = call_target->As<sem::Builtin>();
if (!builtin || !TextureBuiltinNeedsClamping(builtin->Type())) {

View File

@@ -49,7 +49,7 @@ void VectorizeScalarMatrixConstructors::Run(CloneContext& ctx, const DataMap&, D
std::unordered_map<const sem::Matrix*, Symbol> scalar_ctors;
ctx.ReplaceAll([&](const ast::CallExpression* expr) -> const ast::CallExpression* {
auto* call = ctx.src->Sem().Get(expr);
auto* call = ctx.src->Sem().Get(expr)->UnwrapMaterialize()->As<sem::Call>();
auto* ty_ctor = call->Target()->As<sem::TypeConstructor>();
if (!ty_ctor) {
return nullptr;

View File

@@ -83,7 +83,7 @@ void WrapArraysInStructs::Run(CloneContext& ctx, const DataMap&, DataMap&) const
// Fix up array constructors so `A(1,2)` becomes `tint_array_wrapper(A(1,2))`
ctx.ReplaceAll([&](const ast::CallExpression* expr) -> const ast::Expression* {
if (auto* call = sem.Get(expr)) {
if (auto* call = sem.Get(expr)->UnwrapMaterialize()->As<sem::Call>()) {
if (auto* ctor = call->Target()->As<sem::TypeConstructor>()) {
if (auto* array = ctor->ReturnType()->As<sem::Array>()) {
if (auto w = wrapper(array)) {