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 <bclayton@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2022-05-20 17:18:50 +00:00 committed by Dawn LUCI CQ
parent 249dcd8f87
commit 6522837acb
4 changed files with 34 additions and 7 deletions

View File

@ -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<sem::AbstractInt>();
}
return builder_->create<sem::I32>();
case ast::IntLiteralExpression::Suffix::kI:
return builder_->create<sem::I32>();
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<sem::F32>(); },
[&](const ast::FloatLiteralExpression* f) -> sem::Type* {
if (f->suffix == ast::FloatLiteralExpression::Suffix::kNone &&
enable_abstract_numerics_) {
return builder_->create<sem::AbstractFloat>();
}
return builder_->create<sem::F32>();
},
[&](const ast::BoolLiteralExpression*) { return builder_->create<sem::Bool>(); },
[&](Default) { return nullptr; });

View File

@ -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

View File

@ -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<sem::Constant::Scalar>(
target_elem_type, //
[&](const sem::AbstractInt*) { return value.ElementAs<AInt>(i); },
[&](const sem::AbstractFloat*) { return value.ElementAs<AFloat>(i); },
[&](const sem::I32*) { return value.ElementAs<AInt>(i); },
[&](const sem::U32*) { return value.ElementAs<AInt>(i); },
[&](const sem::F32*) { return value.ElementAs<AFloat>(i); },

View File

@ -18,7 +18,8 @@
namespace tint::resolver {
TestHelper::TestHelper() : resolver_(std::make_unique<Resolver>(this)) {}
TestHelper::TestHelper()
: resolver_(std::make_unique<Resolver>(this, /* enable_abstract_numerics */ true)) {}
TestHelper::~TestHelper() = default;