tint/resolver: Propagate constant values between 'const's

Required for 'const' values that use other 'const' values in their
expressions.

Bug: tint:1580
Change-Id: I1d86281bec19340fdc0c60dc5b22eb3d6dc04cf5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94606
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-06-26 08:40:00 +00:00 committed by Dawn LUCI CQ
parent e3834c4760
commit 410a3bd19a
3 changed files with 13 additions and 4 deletions

View File

@ -210,6 +210,8 @@ class Resolver {
/// nodes are guaranteed to have been already resolved and any constant values calculated.
////////////////////////////////////////////////////////////////////////////////////////////////
sem::Constant EvaluateConstantValue(const ast::Expression* expr, const sem::Type* type);
sem::Constant EvaluateConstantValue(const ast::IdentifierExpression* ident,
const sem::Type* type);
sem::Constant EvaluateConstantValue(const ast::LiteralExpression* literal,
const sem::Type* type);
sem::Constant EvaluateConstantValue(const ast::CallExpression* call, const sem::Type* type);

View File

@ -154,11 +154,20 @@ utils::Result<sem::Constant::Elements> MaterializeElements(const sem::Constant::
sem::Constant Resolver::EvaluateConstantValue(const ast::Expression* expr, const sem::Type* type) {
return Switch(
expr, //
[&](const ast::IdentifierExpression* e) { return EvaluateConstantValue(e, type); },
[&](const ast::LiteralExpression* e) { return EvaluateConstantValue(e, type); },
[&](const ast::CallExpression* e) { return EvaluateConstantValue(e, type); },
[&](const ast::IndexAccessorExpression* e) { return EvaluateConstantValue(e, type); });
}
sem::Constant Resolver::EvaluateConstantValue(const ast::IdentifierExpression* ident,
const sem::Type*) {
if (auto* sem = builder_->Sem().Get(ident)) {
return sem->ConstantValue();
}
return {};
}
sem::Constant Resolver::EvaluateConstantValue(const ast::LiteralExpression* literal,
const sem::Type* type) {
return Switch(

View File

@ -1052,8 +1052,7 @@ TEST_F(ResolverVariableTest, LocalConst_ImplicitType_Decls) {
EXPECT_EQ(Sem().Get(c_maf32)->ConstantValue().ElementCount(), 9u);
}
// Enable when constants propagate between 'const' variables
TEST_F(ResolverVariableTest, DISABLED_LocalConst_PropagateConstValue) {
TEST_F(ResolverVariableTest, LocalConst_PropagateConstValue) {
auto* a = Const("a", nullptr, Expr(42_i));
auto* b = Const("b", nullptr, Expr("a"));
auto* c = Const("c", nullptr, Expr("b"));
@ -1256,8 +1255,7 @@ TEST_F(ResolverVariableTest, GlobalConst_ImplicitType_Decls) {
EXPECT_EQ(Sem().Get(c_maf32)->ConstantValue().ElementCount(), 9u);
}
// Enable when constants propagate between 'const' variables
TEST_F(ResolverVariableTest, DISABLED_GlobalConst_PropagateConstValue) {
TEST_F(ResolverVariableTest, GlobalConst_PropagateConstValue) {
GlobalConst("b", nullptr, Expr("a"));
auto* c = GlobalConst("c", nullptr, Expr("b"));
GlobalConst("a", nullptr, Expr(42_i));