From 6522837acbf96682583ada4ead5c8e86f28df437 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 20 May 2022 17:18:50 +0000 Subject: [PATCH] tint: Add `enable_abstract_numerics` flag on Resolver When enabled, unsuffixed literals will be treated as abstract numerics. By having this disabled by default, we can build up the tests without the risk of breaking production code. This flag is enabled for resolver unit tests. This can safely be done with no change in tested behavior, as all tests use the '_i' or '_u' literal suffixes, so currently there are no tests that exercise abstract numerics. Bug: tint:1504 Change-Id: I39523ff6e235a12533b1dd98587b580bed98300f Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/91025 Commit-Queue: Ben Clayton Reviewed-by: David Neto --- src/tint/resolver/resolver.cc | 21 ++++++++++++++++----- src/tint/resolver/resolver.h | 7 ++++++- src/tint/resolver/resolver_constants.cc | 10 ++++++++++ src/tint/resolver/resolver_test_helper.cc | 3 ++- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/tint/resolver/resolver.cc b/src/tint/resolver/resolver.cc index d9be7f8880..270acf1777 100644 --- a/src/tint/resolver/resolver.cc +++ b/src/tint/resolver/resolver.cc @@ -51,6 +51,8 @@ #include "src/tint/ast/vector.h" #include "src/tint/ast/workgroup_attribute.h" #include "src/tint/resolver/uniformity.h" +#include "src/tint/sem/abstract_float.h" +#include "src/tint/sem/abstract_int.h" #include "src/tint/sem/array.h" #include "src/tint/sem/atomic.h" #include "src/tint/sem/call.h" @@ -82,12 +84,13 @@ namespace tint::resolver { -Resolver::Resolver(ProgramBuilder* builder) +Resolver::Resolver(ProgramBuilder* builder, bool enable_abstract_numerics) : builder_(builder), diagnostics_(builder->Diagnostics()), intrinsic_table_(IntrinsicTable::Create(*builder)), sem_(builder, dependencies_), - validator_(builder, sem_) {} + validator_(builder, sem_), + enable_abstract_numerics_(enable_abstract_numerics) {} Resolver::~Resolver() = default; @@ -1486,8 +1489,10 @@ sem::Expression* Resolver::Literal(const ast::LiteralExpression* literal) { [&](const ast::IntLiteralExpression* i) -> sem::Type* { switch (i->suffix) { case ast::IntLiteralExpression::Suffix::kNone: - // TODO(crbug.com/tint/1504): This will need to become abstract-int. - // For now, treat as 'i32'. + if (enable_abstract_numerics_) { + return builder_->create(); + } + return builder_->create(); case ast::IntLiteralExpression::Suffix::kI: return builder_->create(); case ast::IntLiteralExpression::Suffix::kU: @@ -1495,7 +1500,13 @@ sem::Expression* Resolver::Literal(const ast::LiteralExpression* literal) { } return nullptr; }, - [&](const ast::FloatLiteralExpression*) { return builder_->create(); }, + [&](const ast::FloatLiteralExpression* f) -> sem::Type* { + if (f->suffix == ast::FloatLiteralExpression::Suffix::kNone && + enable_abstract_numerics_) { + return builder_->create(); + } + return builder_->create(); + }, [&](const ast::BoolLiteralExpression*) { return builder_->create(); }, [&](Default) { return nullptr; }); diff --git a/src/tint/resolver/resolver.h b/src/tint/resolver/resolver.h index 5f7e4ca9f4..52689b44d2 100644 --- a/src/tint/resolver/resolver.h +++ b/src/tint/resolver/resolver.h @@ -75,7 +75,10 @@ class Resolver { public: /// Constructor /// @param builder the program builder - explicit Resolver(ProgramBuilder* builder); + /// @param enable_abstract_numerics if true, then treat unsuffixed integers as abstract integers + /// and unsuffixed floats as abstract floats. This is a temporary flag while abstract numerics + /// are being developed. Once complete, this will be permanently enabled. + explicit Resolver(ProgramBuilder* builder, bool enable_abstract_numerics = false); /// Destructor ~Resolver(); @@ -365,6 +368,8 @@ class Resolver { sem::Statement* current_statement_ = nullptr; sem::CompoundStatement* current_compound_statement_ = nullptr; sem::BlockStatement* current_block_ = nullptr; + + bool enable_abstract_numerics_ = false; }; } // namespace tint::resolver diff --git a/src/tint/resolver/resolver_constants.cc b/src/tint/resolver/resolver_constants.cc index 66094cf65d..4e620eec35 100644 --- a/src/tint/resolver/resolver_constants.cc +++ b/src/tint/resolver/resolver_constants.cc @@ -14,6 +14,8 @@ #include "src/tint/resolver/resolver.h" +#include "src/tint/sem/abstract_float.h" +#include "src/tint/sem/abstract_int.h" #include "src/tint/sem/constant.h" #include "src/tint/sem/type_constructor.h" #include "src/tint/utils/map.h" @@ -64,6 +66,12 @@ sem::Constant Resolver::EvaluateConstantValue(const ast::CallExpression* call, using Scalars = sem::Constant::Scalars; auto constant = Switch( elem_type, + [&](const sem::AbstractInt*) { + return sem::Constant(type, Scalars(result_size, AInt(0))); + }, + [&](const sem::AbstractFloat*) { + return sem::Constant(type, Scalars(result_size, AFloat(0))); + }, [&](const sem::I32*) { return sem::Constant(type, Scalars(result_size, AInt(0))); }, [&](const sem::U32*) { return sem::Constant(type, Scalars(result_size, AInt(0))); }, [&](const sem::F32*) { return sem::Constant(type, Scalars(result_size, AFloat(0))); }, @@ -107,6 +115,8 @@ sem::Constant Resolver::ConstantCast(const sem::Constant& value, // TODO(crbug.com/tint/1504): Check that value fits in new type elems.emplace_back(Switch( target_elem_type, // + [&](const sem::AbstractInt*) { return value.ElementAs(i); }, + [&](const sem::AbstractFloat*) { return value.ElementAs(i); }, [&](const sem::I32*) { return value.ElementAs(i); }, [&](const sem::U32*) { return value.ElementAs(i); }, [&](const sem::F32*) { return value.ElementAs(i); }, diff --git a/src/tint/resolver/resolver_test_helper.cc b/src/tint/resolver/resolver_test_helper.cc index dd590d9261..75a66982ae 100644 --- a/src/tint/resolver/resolver_test_helper.cc +++ b/src/tint/resolver/resolver_test_helper.cc @@ -18,7 +18,8 @@ namespace tint::resolver { -TestHelper::TestHelper() : resolver_(std::make_unique(this)) {} +TestHelper::TestHelper() + : resolver_(std::make_unique(this, /* enable_abstract_numerics */ true)) {} TestHelper::~TestHelper() = default;