mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-19 09:55:26 +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
@@ -56,6 +56,21 @@ void FoldConstants::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto build_scalar = [&](sem::Constant::Scalar s) {
|
||||
return Switch(
|
||||
value.ElementType(), //
|
||||
[&](const sem::I32*) { return ctx.dst->Expr(i32(std::get<AInt>(s).value)); },
|
||||
[&](const sem::U32*) { return ctx.dst->Expr(u32(std::get<AInt>(s).value)); },
|
||||
[&](const sem::F32*) { return ctx.dst->Expr(f32(std::get<AFloat>(s).value)); },
|
||||
[&](const sem::Bool*) { return ctx.dst->Expr(std::get<bool>(s)); },
|
||||
[&](Default) {
|
||||
TINT_ICE(Transform, ctx.dst->Diagnostics())
|
||||
<< "unhandled Constant::Scalar type: "
|
||||
<< value.ElementType()->FriendlyName(ctx.src->Symbols());
|
||||
return nullptr;
|
||||
});
|
||||
};
|
||||
|
||||
if (auto* vec = ty->As<sem::Vector>()) {
|
||||
uint32_t vec_size = static_cast<uint32_t>(vec->Width());
|
||||
|
||||
@@ -73,7 +88,7 @@ void FoldConstants::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
|
||||
|
||||
ast::ExpressionList ctors;
|
||||
for (uint32_t i = 0; i < ctor_size; ++i) {
|
||||
value.WithScalarAt(i, [&](auto&& s) { ctors.emplace_back(ctx.dst->Expr(s)); });
|
||||
ctors.emplace_back(build_scalar(value.Elements()[i]));
|
||||
}
|
||||
|
||||
auto* el_ty = CreateASTTypeFor(ctx, vec->type());
|
||||
@@ -81,8 +96,7 @@ void FoldConstants::Run(CloneContext& ctx, const DataMap&, DataMap&) const {
|
||||
}
|
||||
|
||||
if (ty->is_scalar()) {
|
||||
return value.WithScalarAt(
|
||||
0, [&](auto&& s) -> const ast::LiteralExpression* { return ctx.dst->Expr(s); });
|
||||
return build_scalar(value.Elements()[0]);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
|
||||
@@ -123,10 +123,10 @@ struct Robustness::State {
|
||||
if (auto idx_constant = idx_sem->ConstantValue()) {
|
||||
// Constant value index
|
||||
if (idx_constant.Type()->Is<sem::I32>()) {
|
||||
idx.i32 = idx_constant.Elements()[0].i32;
|
||||
idx.i32 = static_cast<int32_t>(idx_constant.Element<AInt>(0).value);
|
||||
idx.is_signed = true;
|
||||
} else if (idx_constant.Type()->Is<sem::U32>()) {
|
||||
idx.u32 = idx_constant.Elements()[0].u32;
|
||||
idx.u32 = static_cast<uint32_t>(idx_constant.Element<AInt>(0).value);
|
||||
idx.is_signed = false;
|
||||
} else {
|
||||
TINT_ICE(Transform, b.Diagnostics()) << "unsupported constant value for accessor "
|
||||
|
||||
@@ -359,13 +359,8 @@ struct ZeroInitWorkgroupMemory::State {
|
||||
}
|
||||
auto* sem = ctx.src->Sem().Get(expr);
|
||||
if (auto c = sem->ConstantValue()) {
|
||||
if (c.ElementType()->Is<sem::I32>()) {
|
||||
workgroup_size_const *= static_cast<uint32_t>(c.Elements()[0].i32);
|
||||
continue;
|
||||
} else if (c.ElementType()->Is<sem::U32>()) {
|
||||
workgroup_size_const *= c.Elements()[0].u32;
|
||||
continue;
|
||||
}
|
||||
workgroup_size_const *= c.Element<AInt>(0).value;
|
||||
continue;
|
||||
}
|
||||
// Constant value could not be found. Build expression instead.
|
||||
workgroup_size_expr = [this, expr, size = workgroup_size_expr] {
|
||||
|
||||
Reference in New Issue
Block a user