tint/resolver: Change return type of EvaluateConstantValue()

These functions do not return an error - however they might return an
invalid Constant value (if a constant cannot be evaluated), which is
correctly handled by the resolver. So, drop the Result, just emit a
sem::Constant.

Change-Id: I0dd37125c0dba4ba6dcace5ee5f8b1c53cb34d33
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94326
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-06-21 22:56:33 +00:00 committed by Dawn LUCI CQ
parent b6c150d6e0
commit 44eb10814e
3 changed files with 25 additions and 58 deletions

View File

@ -1249,23 +1249,13 @@ const sem::Expression* Resolver::Materialize(const sem::Expression* expr,
return nullptr; return nullptr;
} }
auto expr_val = EvaluateConstantValue(decl, expr->Type()); auto expr_val = EvaluateConstantValue(decl, expr->Type());
if (!expr_val) { auto materialized_val = ConvertValue(std::move(expr_val), target_ty, decl->source);
return nullptr;
}
if (!expr_val->IsValid()) {
TINT_ICE(Resolver, builder_->Diagnostics())
<< decl->source
<< "EvaluateConstantValue() returned invalid value for materialized value of type: "
<< builder_->FriendlyName(src_ty);
return nullptr;
}
auto materialized_val = ConvertValue(expr_val.Get(), target_ty, decl->source);
if (!materialized_val) { if (!materialized_val) {
return nullptr; return nullptr;
} }
if (!materialized_val->IsValid()) { if (!materialized_val->IsValid()) {
TINT_ICE(Resolver, builder_->Diagnostics()) TINT_ICE(Resolver, builder_->Diagnostics())
<< decl->source << "ConvertValue(" << builder_->FriendlyName(expr_val->Type()) << decl->source << "ConvertValue(" << builder_->FriendlyName(expr_val.Type())
<< " -> " << builder_->FriendlyName(target_ty) << ") returned invalid value"; << " -> " << builder_->FriendlyName(target_ty) << ") returned invalid value";
return nullptr; return nullptr;
} }
@ -1368,11 +1358,8 @@ sem::Expression* Resolver::IndexAccessor(const ast::IndexAccessorExpression* exp
} }
auto val = EvaluateConstantValue(expr, ty); auto val = EvaluateConstantValue(expr, ty);
if (!val) {
return nullptr;
}
bool has_side_effects = idx->HasSideEffects() || obj->HasSideEffects(); bool has_side_effects = idx->HasSideEffects() || obj->HasSideEffects();
auto* sem = builder_->create<sem::Expression>(expr, ty, current_statement_, val.Get(), auto* sem = builder_->create<sem::Expression>(expr, ty, current_statement_, std::move(val),
has_side_effects, obj->SourceVariable()); has_side_effects, obj->SourceVariable());
sem->Behaviors() = idx->Behaviors() + obj->Behaviors(); sem->Behaviors() = idx->Behaviors() + obj->Behaviors();
return sem; return sem;
@ -1389,10 +1376,7 @@ sem::Expression* Resolver::Bitcast(const ast::BitcastExpression* expr) {
} }
auto val = EvaluateConstantValue(expr, ty); auto val = EvaluateConstantValue(expr, ty);
if (!val) { auto* sem = builder_->create<sem::Expression>(expr, ty, current_statement_, std::move(val),
return nullptr;
}
auto* sem = builder_->create<sem::Expression>(expr, ty, current_statement_, val.Get(),
inner->HasSideEffects()); inner->HasSideEffects());
sem->Behaviors() = inner->Behaviors(); sem->Behaviors() = inner->Behaviors();
@ -1440,11 +1424,8 @@ sem::Call* Resolver::Call(const ast::CallExpression* expr) {
return nullptr; return nullptr;
} }
auto val = EvaluateConstantValue(expr, call_target->ReturnType()); auto val = EvaluateConstantValue(expr, call_target->ReturnType());
if (!val) {
return nullptr;
}
return builder_->create<sem::Call>(expr, call_target, std::move(args), current_statement_, return builder_->create<sem::Call>(expr, call_target, std::move(args), current_statement_,
val.Get(), has_side_effects); std::move(val), has_side_effects);
}; };
// ct_ctor_or_conv is a helper for building either a sem::TypeConstructor or sem::TypeConversion // ct_ctor_or_conv is a helper for building either a sem::TypeConstructor or sem::TypeConversion
@ -1481,11 +1462,9 @@ sem::Call* Resolver::Call(const ast::CallExpression* expr) {
return nullptr; return nullptr;
} }
auto val = EvaluateConstantValue(expr, call_target->ReturnType()); auto val = EvaluateConstantValue(expr, call_target->ReturnType());
if (!val) {
return nullptr;
}
return builder_->create<sem::Call>(expr, call_target, std::move(args), return builder_->create<sem::Call>(expr, call_target, std::move(args),
current_statement_, val.Get(), has_side_effects); current_statement_, std::move(val),
has_side_effects);
}, },
[&](const sem::Struct* str) -> sem::Call* { [&](const sem::Struct* str) -> sem::Call* {
auto* call_target = utils::GetOrCreate( auto* call_target = utils::GetOrCreate(
@ -1506,11 +1485,9 @@ sem::Call* Resolver::Call(const ast::CallExpression* expr) {
return nullptr; return nullptr;
} }
auto val = EvaluateConstantValue(expr, call_target->ReturnType()); auto val = EvaluateConstantValue(expr, call_target->ReturnType());
if (!val) {
return nullptr;
}
return builder_->create<sem::Call>(expr, call_target, std::move(args), return builder_->create<sem::Call>(expr, call_target, std::move(args),
current_statement_, val.Get(), has_side_effects); current_statement_, std::move(val),
has_side_effects);
}, },
[&](Default) { [&](Default) {
AddError("type is not constructible", expr->source); AddError("type is not constructible", expr->source);
@ -1812,10 +1789,7 @@ sem::Expression* Resolver::Literal(const ast::LiteralExpression* literal) {
} }
auto val = EvaluateConstantValue(literal, ty); auto val = EvaluateConstantValue(literal, ty);
if (!val) { return builder_->create<sem::Expression>(literal, ty, current_statement_, std::move(val),
return nullptr;
}
return builder_->create<sem::Expression>(literal, ty, current_statement_, val.Get(),
/* has_side_effects */ false); /* has_side_effects */ false);
} }
@ -2027,12 +2001,9 @@ sem::Expression* Resolver::Binary(const ast::BinaryExpression* expr) {
} }
auto val = EvaluateConstantValue(expr, op.result); auto val = EvaluateConstantValue(expr, op.result);
if (!val) {
return nullptr;
}
bool has_side_effects = lhs->HasSideEffects() || rhs->HasSideEffects(); bool has_side_effects = lhs->HasSideEffects() || rhs->HasSideEffects();
auto* sem = builder_->create<sem::Expression>(expr, op.result, current_statement_, val.Get(), auto* sem = builder_->create<sem::Expression>(expr, op.result, current_statement_,
has_side_effects); std::move(val), has_side_effects);
sem->Behaviors() = lhs->Behaviors() + rhs->Behaviors(); sem->Behaviors() = lhs->Behaviors() + rhs->Behaviors();
return sem; return sem;
@ -2104,10 +2075,7 @@ sem::Expression* Resolver::UnaryOp(const ast::UnaryOpExpression* unary) {
} }
auto val = EvaluateConstantValue(unary, ty); auto val = EvaluateConstantValue(unary, ty);
if (!val) { auto* sem = builder_->create<sem::Expression>(unary, ty, current_statement_, std::move(val),
return nullptr;
}
auto* sem = builder_->create<sem::Expression>(unary, ty, current_statement_, val.Get(),
expr->HasSideEffects(), source_var); expr->HasSideEffects(), source_var);
sem->Behaviors() = expr->Behaviors(); sem->Behaviors() = expr->Behaviors();
return sem; return sem;

View File

@ -391,10 +391,10 @@ class Resolver {
ConstantResult ConvertValue(const sem::Constant& value, ConstantResult ConvertValue(const sem::Constant& value,
const sem::Type* target_type, const sem::Type* target_type,
const Source& source); const Source& source);
ConstantResult EvaluateConstantValue(const ast::Expression* expr, const sem::Type* type); sem::Constant EvaluateConstantValue(const ast::Expression* expr, const sem::Type* type);
ConstantResult EvaluateConstantValue(const ast::LiteralExpression* literal, sem::Constant EvaluateConstantValue(const ast::LiteralExpression* literal,
const sem::Type* type); const sem::Type* type);
ConstantResult EvaluateConstantValue(const ast::CallExpression* call, const sem::Type* type); sem::Constant EvaluateConstantValue(const ast::CallExpression* call, const sem::Type* type);
/// @returns true if the symbol is the name of a builtin function. /// @returns true if the symbol is the name of a builtin function.
bool IsBuiltin(Symbol) const; bool IsBuiltin(Symbol) const;

View File

@ -151,19 +151,18 @@ utils::Result<sem::Constant::Elements> MaterializeElements(const sem::Constant::
} // namespace } // namespace
utils::Result<sem::Constant> Resolver::EvaluateConstantValue(const ast::Expression* expr, sem::Constant Resolver::EvaluateConstantValue(const ast::Expression* expr, const sem::Type* type) {
const sem::Type* type) {
if (auto* e = expr->As<ast::LiteralExpression>()) { if (auto* e = expr->As<ast::LiteralExpression>()) {
return EvaluateConstantValue(e, type); return EvaluateConstantValue(e, type);
} }
if (auto* e = expr->As<ast::CallExpression>()) { if (auto* e = expr->As<ast::CallExpression>()) {
return EvaluateConstantValue(e, type); return EvaluateConstantValue(e, type);
} }
return sem::Constant{}; return {};
} }
utils::Result<sem::Constant> Resolver::EvaluateConstantValue(const ast::LiteralExpression* literal, sem::Constant Resolver::EvaluateConstantValue(const ast::LiteralExpression* literal,
const sem::Type* type) { const sem::Type* type) {
return Switch( return Switch(
literal, literal,
[&](const ast::BoolLiteralExpression* lit) { [&](const ast::BoolLiteralExpression* lit) {
@ -177,8 +176,8 @@ utils::Result<sem::Constant> Resolver::EvaluateConstantValue(const ast::LiteralE
}); });
} }
utils::Result<sem::Constant> Resolver::EvaluateConstantValue(const ast::CallExpression* call, sem::Constant Resolver::EvaluateConstantValue(const ast::CallExpression* call,
const sem::Type* ty) { const sem::Type* ty) {
uint32_t result_size = 0; uint32_t result_size = 0;
auto* el_ty = sem::Type::ElementOf(ty, &result_size); auto* el_ty = sem::Type::ElementOf(ty, &result_size);
if (!el_ty) { if (!el_ty) {
@ -212,11 +211,11 @@ utils::Result<sem::Constant> Resolver::EvaluateConstantValue(const ast::CallExpr
for (auto* expr : call->args) { for (auto* expr : call->args) {
auto* arg = builder_->Sem().Get(expr); auto* arg = builder_->Sem().Get(expr);
if (!arg) { if (!arg) {
return sem::Constant{}; return {};
} }
auto value = arg->ConstantValue(); auto value = arg->ConstantValue();
if (!value) { if (!value) {
return sem::Constant{}; return {};
} }
// Convert the elements to the desired type. // Convert the elements to the desired type.