mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-20 18:29:23 +00:00
tint: Simplify sem::Constant::Scalar
Migrate from a hand-rolled tagged-union of [i32, u32, f32, f16, bool] types. Instead use a std::variant of [AInt, AFloat, bool]. The Constant holds the actual type, so no information is lost with the reduced types. Note: Currently integer constants are still limited to 32-bits in size. This is enforced by the frontend. Bug: tint:1504 Change-Id: I316957787649c454fffb532334159d726cd1fb2d Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/90643 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: James Price <jrprice@google.com> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
committed by
Dawn LUCI CQ
parent
c590373cfb
commit
aaa9ba3043
@@ -60,16 +60,12 @@ Constant::~Constant() = default;
|
||||
Constant& Constant::operator=(const Constant& rhs) = default;
|
||||
|
||||
bool Constant::AnyZero() const {
|
||||
for (size_t i = 0; i < Elements().size(); ++i) {
|
||||
if (WithScalarAt(i, [&](auto&& s) {
|
||||
// Use std::equal_to to work around -Wfloat-equal warnings
|
||||
using T = std::remove_reference_t<decltype(s)>;
|
||||
auto equal_to = std::equal_to<T>{};
|
||||
if (equal_to(s, T(0))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})) {
|
||||
for (auto scalar : elems_) {
|
||||
auto is_zero = [&](auto&& s) {
|
||||
using T = std::remove_reference_t<decltype(s)>;
|
||||
return s == T(0);
|
||||
};
|
||||
if (std::visit(is_zero, scalar)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#ifndef SRC_TINT_SEM_CONSTANT_H_
|
||||
#define SRC_TINT_SEM_CONSTANT_H_
|
||||
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#include "src/tint/program_builder.h"
|
||||
@@ -26,40 +27,8 @@ namespace tint::sem {
|
||||
/// list of scalar values. Value may be of a scalar or vector type.
|
||||
class Constant {
|
||||
public:
|
||||
/// Scalar holds a single constant scalar value, as a union of an i32, u32,
|
||||
/// f32 or boolean.
|
||||
union Scalar {
|
||||
/// The scalar value as a i32
|
||||
tint::i32 i32;
|
||||
/// The scalar value as a u32
|
||||
tint::u32 u32;
|
||||
/// The scalar value as a f32
|
||||
tint::f32 f32;
|
||||
/// The scalar value as a f16, internally stored as float
|
||||
tint::f16 f16;
|
||||
/// The scalar value as a bool
|
||||
bool bool_;
|
||||
|
||||
/// Constructs the scalar with the i32 value `v`
|
||||
/// @param v the value of the Scalar
|
||||
Scalar(tint::i32 v) : i32(v) {} // NOLINT
|
||||
|
||||
/// Constructs the scalar with the u32 value `v`
|
||||
/// @param v the value of the Scalar
|
||||
Scalar(tint::u32 v) : u32(v) {} // NOLINT
|
||||
|
||||
/// Constructs the scalar with the f32 value `v`
|
||||
/// @param v the value of the Scalar
|
||||
Scalar(tint::f32 v) : f32(v) {} // NOLINT
|
||||
|
||||
/// Constructs the scalar with the f16 value `v`
|
||||
/// @param v the value of the Scalar
|
||||
Scalar(tint::f16 v) : f16({v}) {} // NOLINT
|
||||
|
||||
/// Constructs the scalar with the bool value `v`
|
||||
/// @param v the value of the Scalar
|
||||
Scalar(bool v) : bool_(v) {} // NOLINT
|
||||
};
|
||||
/// Scalar holds a single constant scalar value - one of: AInt, AFloat or bool.
|
||||
using Scalar = std::variant<AInt, AFloat, bool>;
|
||||
|
||||
/// Scalars is a list of scalar values
|
||||
using Scalars = std::vector<Scalar>;
|
||||
@@ -101,33 +70,18 @@ class Constant {
|
||||
/// @returns true if any scalar element is zero
|
||||
bool AnyZero() const;
|
||||
|
||||
/// Calls `func(s)` with s being the current scalar value at `index`.
|
||||
/// `func` is typically a lambda of the form '[](auto&& s)'.
|
||||
/// @param index the index of the scalar value
|
||||
/// @param func a function with signature `T(S)`
|
||||
/// @return the value returned by func.
|
||||
template <typename Func>
|
||||
auto WithScalarAt(size_t index, Func&& func) const {
|
||||
return Switch(
|
||||
ElementType(), //
|
||||
[&](const I32*) { return func(elems_[index].i32); },
|
||||
[&](const U32*) { return func(elems_[index].u32); },
|
||||
[&](const F16*) { return func(elems_[index].f16); },
|
||||
[&](const F32*) { return func(elems_[index].f32); },
|
||||
[&](const Bool*) { return func(elems_[index].bool_); },
|
||||
[&](Default) {
|
||||
diag::List diags;
|
||||
TINT_UNREACHABLE(Semantic, diags)
|
||||
<< "invalid scalar type " << type_->TypeInfo().name;
|
||||
return func(u32(0u));
|
||||
});
|
||||
/// @return the value of the scalar at `index`, which must be of type `T`.
|
||||
template <typename T>
|
||||
T Element(size_t index) const {
|
||||
return std::get<T>(elems_[index]);
|
||||
}
|
||||
|
||||
/// @param index the index of the scalar value
|
||||
/// @return the value of the scalar `static_cast` to type T.
|
||||
template <typename T>
|
||||
T ElementAs(size_t index) const {
|
||||
return WithScalarAt(index, [](auto val) { return static_cast<T>(val); });
|
||||
return std::visit([](auto val) { return static_cast<T>(val); }, elems_[index]);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user