Move constants into constant namespace.

This CL moves the constants into the constant namespace.

Bug: tint:1718
Change-Id: Id8f98ca6c16085136c06d98bbd8b755b36cb2409
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/114161
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2022-12-14 19:22:19 +00:00 committed by Dawn LUCI CQ
parent 0890380d10
commit 8626c9ee87
8 changed files with 54 additions and 48 deletions

View File

@ -16,9 +16,9 @@
#include <utility>
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Composite);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Composite);
namespace tint::resolver {
namespace tint::constant {
Composite::Composite(const type::Type* t,
utils::VectorRef<const constant::Constant*> els,
@ -28,4 +28,4 @@ Composite::Composite(const type::Type* t,
Composite::~Composite() = default;
} // namespace tint::resolver
} // namespace tint::constant

View File

@ -22,7 +22,7 @@
#include "src/tint/utils/hash.h"
#include "src/tint/utils/vector.h"
namespace tint::resolver {
namespace tint::constant {
/// Composite holds a number of mixed child Constant values.
/// Composite may be of a vector, matrix or array type.
@ -75,6 +75,6 @@ class Composite : public Castable<Composite, constant::Constant> {
}
};
} // namespace tint::resolver
} // namespace tint::constant
#endif // SRC_TINT_CONSTANT_COMPOSITE_H_

View File

@ -14,10 +14,10 @@
#include "src/tint/constant/scalar.h"
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Scalar<tint::AInt>);
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Scalar<tint::AFloat>);
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Scalar<tint::i32>);
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Scalar<tint::u32>);
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Scalar<tint::f16>);
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Scalar<tint::f32>);
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Scalar<bool>);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Scalar<tint::AInt>);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Scalar<tint::AFloat>);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Scalar<tint::i32>);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Scalar<tint::u32>);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Scalar<tint::f16>);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Scalar<tint::f32>);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Scalar<bool>);

View File

@ -21,7 +21,7 @@
#include "src/tint/type/type.h"
#include "src/tint/utils/hash.h"
namespace tint::resolver {
namespace tint::constant {
/// Scalar holds a single scalar or abstract-numeric value.
/// Scalar implements the Constant interface.
@ -36,7 +36,7 @@ class Scalar : public Castable<Scalar<T>, constant::Constant> {
/// @param v the scalar value
Scalar(const type::Type* t, T v) : type(t), value(v) {
if constexpr (IsFloatingPoint<T>) {
TINT_ASSERT(Resolver, std::isfinite(v.value));
TINT_ASSERT(Constant, std::isfinite(v.value));
}
}
~Scalar() override = default;
@ -79,6 +79,6 @@ class Scalar : public Castable<Scalar<T>, constant::Constant> {
const T value;
};
} // namespace tint::resolver
} // namespace tint::constant
#endif // SRC_TINT_CONSTANT_SCALAR_H_

View File

@ -14,13 +14,13 @@
#include "src/tint/constant/splat.h"
TINT_INSTANTIATE_TYPEINFO(tint::resolver::Splat);
TINT_INSTANTIATE_TYPEINFO(tint::constant::Splat);
namespace tint::resolver {
namespace tint::constant {
Splat::Splat(const type::Type* t, const constant::Constant* e, size_t n)
: type(t), el(e), count(n) {}
Splat::~Splat() = default;
} // namespace tint::resolver
} // namespace tint::constant

View File

@ -20,7 +20,7 @@
#include "src/tint/type/type.h"
#include "src/tint/utils/vector.h"
namespace tint::resolver {
namespace tint::constant {
/// Splat holds a single Constant value, duplicated as all children.
/// Splat is used for zero-initializers, 'splat' initializers, or initializers where each element is
@ -64,6 +64,6 @@ class Splat : public Castable<Splat, constant::Constant> {
const size_t count;
};
} // namespace tint::resolver
} // namespace tint::constant
#endif // SRC_TINT_CONSTANT_SPLAT_H_

View File

@ -37,6 +37,7 @@ inline bool operator>=(Severity a, Severity b) {
enum class System {
AST,
Clone,
Constant,
Inspector,
IR,
Program,

View File

@ -241,7 +241,7 @@ const constant::Constant* CreateComposite(ProgramBuilder& builder,
utils::VectorRef<const constant::Constant*> elements);
template <typename T>
ImplResult ScalarConvert(const Scalar<T>* scalar,
ImplResult ScalarConvert(const constant::Scalar<T>* scalar,
ProgramBuilder& builder,
const type::Type* target_ty,
const Source& source) {
@ -258,13 +258,13 @@ ImplResult ScalarConvert(const Scalar<T>* scalar,
using FROM = T;
if constexpr (std::is_same_v<TO, bool>) {
// [x -> bool]
return builder.create<Scalar<TO>>(target_ty, !scalar->IsPositiveZero());
return builder.create<constant::Scalar<TO>>(target_ty, !scalar->IsPositiveZero());
} else if constexpr (std::is_same_v<FROM, bool>) {
// [bool -> x]
return builder.create<Scalar<TO>>(target_ty, TO(scalar->value ? 1 : 0));
return builder.create<constant::Scalar<TO>>(target_ty, TO(scalar->value ? 1 : 0));
} else if (auto conv = CheckedConvert<TO>(scalar->value)) {
// Conversion success
return builder.create<Scalar<TO>>(target_ty, conv.Get());
return builder.create<constant::Scalar<TO>>(target_ty, conv.Get());
// --- Below this point are the failure cases ---
} else if constexpr (IsAbstract<FROM>) {
// [abstract-numeric -> x] - materialization failure
@ -284,14 +284,14 @@ ImplResult ScalarConvert(const Scalar<T>* scalar,
// https://www.w3.org/TR/WGSL/#floating-point-conversion
switch (conv.Failure()) {
case ConversionFailure::kExceedsNegativeLimit:
return builder.create<Scalar<TO>>(target_ty, TO::Lowest());
return builder.create<constant::Scalar<TO>>(target_ty, TO::Lowest());
case ConversionFailure::kExceedsPositiveLimit:
return builder.create<Scalar<TO>>(target_ty, TO::Highest());
return builder.create<constant::Scalar<TO>>(target_ty, TO::Highest());
}
} else if constexpr (IsIntegral<FROM>) {
// [integer -> integer] - number not exactly representable
// Static cast
return builder.create<Scalar<TO>>(target_ty, static_cast<TO>(scalar->value));
return builder.create<constant::Scalar<TO>>(target_ty, static_cast<TO>(scalar->value));
}
return nullptr; // Expression is not constant.
});
@ -304,7 +304,7 @@ ImplResult ConvertInternal(const constant::Constant* c,
const type::Type* target_ty,
const Source& source);
ImplResult SplatConvert(const Splat* splat,
ImplResult SplatConvert(const constant::Splat* splat,
ProgramBuilder& builder,
const type::Type* target_ty,
const Source& source) {
@ -316,10 +316,10 @@ ImplResult SplatConvert(const Splat* splat,
if (!conv_el.Get()) {
return nullptr;
}
return builder.create<Splat>(target_ty, conv_el.Get(), splat->count);
return builder.create<constant::Splat>(target_ty, conv_el.Get(), splat->count);
}
ImplResult CompositeConvert(const Composite* composite,
ImplResult CompositeConvert(const constant::Composite* composite,
ProgramBuilder& builder,
const type::Type* target_ty,
const Source& source) {
@ -359,30 +359,34 @@ ImplResult ConvertInternal(const constant::Constant* c,
const Source& source) {
return Switch(
c,
[&](const Scalar<tint::AFloat>* val) {
[&](const constant::Scalar<tint::AFloat>* val) {
return ScalarConvert(val, builder, target_ty, source);
},
[&](const Scalar<tint::AInt>* val) {
[&](const constant::Scalar<tint::AInt>* val) {
return ScalarConvert(val, builder, target_ty, source);
},
[&](const Scalar<tint::u32>* val) {
[&](const constant::Scalar<tint::u32>* val) {
return ScalarConvert(val, builder, target_ty, source);
},
[&](const Scalar<tint::i32>* val) {
[&](const constant::Scalar<tint::i32>* val) {
return ScalarConvert(val, builder, target_ty, source);
},
[&](const Scalar<tint::f32>* val) {
[&](const constant::Scalar<tint::f32>* val) {
return ScalarConvert(val, builder, target_ty, source);
},
[&](const Scalar<tint::f16>* val) {
[&](const constant::Scalar<tint::f16>* val) {
return ScalarConvert(val, builder, target_ty, source);
},
[&](const Scalar<bool>* val) { return ScalarConvert(val, builder, target_ty, source); },
[&](const Splat* val) { return SplatConvert(val, builder, target_ty, source); },
[&](const Composite* val) { return CompositeConvert(val, builder, target_ty, source); });
[&](const constant::Scalar<bool>* val) {
return ScalarConvert(val, builder, target_ty, source);
},
[&](const constant::Splat* val) { return SplatConvert(val, builder, target_ty, source); },
[&](const constant::Composite* val) {
return CompositeConvert(val, builder, target_ty, source);
});
}
/// CreateScalar constructs and returns an Scalar<T>.
/// CreateScalar constructs and returns an constant::Scalar<T>.
template <typename T>
ImplResult CreateScalar(ProgramBuilder& builder, const Source& source, const type::Type* t, T v) {
static_assert(IsNumber<T> || std::is_same_v<T, bool>, "T must be a Number or bool");
@ -395,7 +399,7 @@ ImplResult CreateScalar(ProgramBuilder& builder, const Source& source, const typ
return utils::Failure;
}
}
return builder.create<Scalar<T>>(t, v);
return builder.create<constant::Scalar<T>>(t, v);
}
/// ZeroValue returns a Constant for the zero-value of the type `type`.
@ -404,16 +408,16 @@ const constant::Constant* ZeroValue(ProgramBuilder& builder, const type::Type* t
type, //
[&](const type::Vector* v) -> const constant::Constant* {
auto* zero_el = ZeroValue(builder, v->type());
return builder.create<Splat>(type, zero_el, v->Width());
return builder.create<constant::Splat>(type, zero_el, v->Width());
},
[&](const type::Matrix* m) -> const constant::Constant* {
auto* zero_el = ZeroValue(builder, m->ColumnType());
return builder.create<Splat>(type, zero_el, m->columns());
return builder.create<constant::Splat>(type, zero_el, m->columns());
},
[&](const type::Array* a) -> const constant::Constant* {
if (auto n = a->ConstantCount()) {
if (auto* zero_el = ZeroValue(builder, a->ElemType())) {
return builder.create<Splat>(type, zero_el, n.value());
return builder.create<constant::Splat>(type, zero_el, n.value());
}
}
return nullptr;
@ -432,7 +436,7 @@ const constant::Constant* ZeroValue(ProgramBuilder& builder, const type::Type* t
}
if (zero_by_type.Count() == 1) {
// All members were of the same type, so the zero value is the same for all members.
return builder.create<Splat>(type, zeros[0], s->Members().Length());
return builder.create<constant::Splat>(type, zeros[0], s->Members().Length());
}
return CreateComposite(builder, s, std::move(zeros));
},
@ -531,9 +535,9 @@ const constant::Constant* CreateComposite(ProgramBuilder& builder,
}
}
if (all_equal) {
return builder.create<Splat>(type, elements[0], elements.Length());
return builder.create<constant::Splat>(type, elements[0], elements.Length());
} else {
return builder.create<Composite>(type, std::move(elements), all_zero, any_zero);
return builder.create<constant::Composite>(type, std::move(elements), all_zero, any_zero);
}
}
@ -1298,7 +1302,8 @@ ConstEval::Result ConstEval::VecSplat(const type::Type* ty,
utils::VectorRef<const constant::Constant*> args,
const Source&) {
if (auto* arg = args[0]) {
return builder.create<Splat>(ty, arg, static_cast<const type::Vector*>(ty)->Width());
return builder.create<constant::Splat>(ty, arg,
static_cast<const type::Vector*>(ty)->Width());
}
return nullptr;
}