tint: make RemovePhonies remove call statements to builtins that return a constant value

If the return value of a builtin is a constant value, it may be an
abstract number, and our backends currently do not deal with abstract
numbers. As these builtins have no side-effects, and the return value is
unused, we can just drop the call altogether.

This is needed for the follow-up CL that fixes calls to builtins with
abstract args of different type.

Bug: chromium:1350147
Change-Id: Iebd853372fdb9242fe0f28706944eabe9df03459
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/98542
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Antonio Maiorano 2022-08-09 18:14:12 +00:00 committed by Dawn LUCI CQ
parent 8830991b41
commit f7e2de7971
3 changed files with 83 additions and 64 deletions

View File

@ -72,6 +72,11 @@ bool RemovePhonies::ShouldRun(const Program* program, const DataMap&) const {
if (node->Is<ast::PhonyExpression>()) {
return true;
}
if (auto* stmt = node->As<ast::CallStatement>()) {
if (program->Sem().Get(stmt->expr)->ConstantValue() != nullptr) {
return true;
}
}
}
return false;
}
@ -82,19 +87,22 @@ void RemovePhonies::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
std::unordered_map<SinkSignature, Symbol, SinkSignature::Hasher> sinks;
for (auto* node : ctx.src->ASTNodes().Objects()) {
if (auto* stmt = node->As<ast::AssignmentStatement>()) {
Switch(
node,
[&](const ast::AssignmentStatement* stmt) {
if (stmt->lhs->Is<ast::PhonyExpression>()) {
std::vector<const ast::Expression*> side_effects;
if (!ast::TraverseExpressions(
stmt->rhs, ctx.dst->Diagnostics(), [&](const ast::CallExpression* expr) {
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).
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.
// 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>() &&
@ -111,15 +119,16 @@ void RemovePhonies::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
// Phony assignment with no side effects.
// Just remove it.
RemoveStatement(ctx, stmt);
continue;
return;
}
if (side_effects.size() == 1) {
if (auto* call = side_effects[0]->As<ast::CallExpression>()) {
// Phony assignment with single call side effect.
// Replace phony assignment with call.
ctx.Replace(stmt, [&, call] { return ctx.dst->CallStmt(ctx.Clone(call)); });
continue;
ctx.Replace(stmt,
[&, call] { return ctx.dst->CallStmt(ctx.Clone(call)); });
return;
}
}
@ -149,7 +158,15 @@ void RemovePhonies::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
return ctx.dst->CallStmt(ctx.dst->Call(sink, args));
});
}
},
[&](const ast::CallStatement* stmt) {
// Remove call statements to const value-returning functions.
// TODO(crbug.com/tint/1637): Remove if `stmt->expr` has no side-effects.
auto* sem_expr = sem.Get(stmt->expr);
if ((sem_expr->ConstantValue() != nullptr) && !sem_expr->HasSideEffects()) {
ctx.Remove(sem.Get(stmt)->Block()->Declaration()->statements, stmt);
}
});
}
ctx.Clone();

View File

@ -24,7 +24,7 @@ namespace tint::transform {
/// RemovePhonies is a Transform that removes all phony-assignment statements,
/// while preserving function call expressions in the RHS of the assignment that
/// may have side-effects.
/// may have side-effects. It also removes calls to builtins that return a constant value.
class RemovePhonies final : public Castable<RemovePhonies, Transform> {
public:
/// Constructor

View File

@ -67,6 +67,8 @@ fn f() {
_ = mat2x2<f32>(9.0, 10.0, 11.0, 12.0);
_ = atan2(1.0, 2.0);
_ = clamp(1.0, 2.0, 3.0);
atan2(1.0, 2.0);
clamp(1.0, 2.0, 3.0);
}
)";