Move constant MatchWidth to the ir::Builder. This CL moves the `MatchWidth` method to the ir::Builder and removes the various implementations of the method. Bug: 350978931 Change-Id: I9d75212596854beb344921f3c8585746b000b0f5 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/198354 Commit-Queue: dan sinclair <dsinclair@chromium.org> Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/lang/core/ir/builder.h b/src/tint/lang/core/ir/builder.h index defcab7..bd6a9d5 100644 --- a/src/tint/lang/core/ir/builder.h +++ b/src/tint/lang/core/ir/builder.h
@@ -400,6 +400,20 @@ return ir.constant_values.Get(v); } + /// Return a constant that has the same number of vector components as `match`, each with the + /// `value`. If `match` is scalar just return `value` as a constant. + /// @param value the value + /// @param match the type to match + /// @returns the new constant + template <typename ARG> + ir::Constant* MatchWidth(ARG&& value, const core::type::Type* match) { + auto* element = Constant(std::forward<ARG>(value)); + if (match->Is<core::type::Vector>()) { + return Splat(ir.Types().match_width(element->Type(), match), element); + } + return element; + } + /// Creates a new ir::Constant /// @param ty the splat type /// @param value the splat value
diff --git a/src/tint/lang/core/ir/transform/binary_polyfill.cc b/src/tint/lang/core/ir/transform/binary_polyfill.cc index 7f74802..db8fa5f 100644 --- a/src/tint/lang/core/ir/transform/binary_polyfill.cc +++ b/src/tint/lang/core/ir/transform/binary_polyfill.cc
@@ -27,8 +27,6 @@ #include "src/tint/lang/core/ir/transform/binary_polyfill.h" -#include <utility> - #include "src/tint/lang/core/ir/builder.h" #include "src/tint/lang/core/ir/module.h" #include "src/tint/lang/core/ir/validator.h" @@ -106,18 +104,6 @@ } } - /// Return a constant that has the same number of vector components as @p match, each with the - /// value @p element. If @p match is scalar just return @p element. - /// @param element the value to extend - /// @param match the type to match the component count of - /// @returns a value with the same number of vector components as @p match - ir::Constant* MatchWidth(ir::Constant* element, const core::type::Type* match) { - if (match->Is<core::type::Vector>()) { - return b.Splat(ty.match_width(element->Type(), match), element); - } - return element; - } - /// Replace an integer divide or modulo with a call to helper function that prevents /// divide-by-zero and signed integer overflow. /// @param binary the binary instruction @@ -147,11 +133,11 @@ ir::Constant* one = nullptr; ir::Constant* zero = nullptr; if (is_signed) { - one = MatchWidth(b.Constant(1_i), result_ty); - zero = MatchWidth(b.Constant(0_i), result_ty); + one = b.MatchWidth(1_i, result_ty); + zero = b.MatchWidth(0_i, result_ty); } else { - one = MatchWidth(b.Constant(1_u), result_ty); - zero = MatchWidth(b.Constant(0_u), result_ty); + one = b.MatchWidth(1_u, result_ty); + zero = b.MatchWidth(0_u, result_ty); } // Select either the RHS or a constant one value if the RHS is zero. @@ -159,8 +145,8 @@ auto* bool_ty = ty.match_width(ty.bool_(), result_ty); auto* cond = b.Equal(bool_ty, rhs, zero); if (is_signed) { - auto* lowest = MatchWidth(b.Constant(i32::Lowest()), result_ty); - auto* minus_one = MatchWidth(b.Constant(-1_i), result_ty); + auto* lowest = b.MatchWidth(i32::Lowest(), result_ty); + auto* minus_one = b.MatchWidth(-1_i, result_ty); auto* lhs_is_lowest = b.Equal(bool_ty, lhs, lowest); auto* rhs_is_minus_one = b.Equal(bool_ty, rhs, minus_one); cond = b.Or(bool_ty, cond, b.And(bool_ty, lhs_is_lowest, rhs_is_minus_one)); @@ -205,8 +191,8 @@ void MaskShiftAmount(ir::CoreBinary* binary) { auto* lhs = binary->LHS(); auto* rhs = binary->RHS(); - auto* mask = b.Constant(u32(lhs->Type()->DeepestElement()->Size() * 8 - 1)); - auto* masked = b.And(rhs->Type(), rhs, MatchWidth(mask, rhs->Type())); + auto mask = u32(lhs->Type()->DeepestElement()->Size() * 8 - 1); + auto* masked = b.And(rhs->Type(), rhs, b.MatchWidth(mask, rhs->Type())); masked->InsertBefore(binary); binary->SetOperand(ir::CoreBinary::kRhsOperandOffset, masked->Result(0)); }
diff --git a/src/tint/lang/core/ir/transform/builtin_polyfill.cc b/src/tint/lang/core/ir/transform/builtin_polyfill.cc index 99d787d..97433c0 100644 --- a/src/tint/lang/core/ir/transform/builtin_polyfill.cc +++ b/src/tint/lang/core/ir/transform/builtin_polyfill.cc
@@ -27,8 +27,6 @@ #include "src/tint/lang/core/ir/transform/builtin_polyfill.h" -#include <utility> - #include "src/tint/lang/core/ir/builder.h" #include "src/tint/lang/core/ir/module.h" #include "src/tint/lang/core/ir/validator.h" @@ -227,18 +225,6 @@ } } - /// Return a constant that has the same number of vector components as @p match, each with the - /// value @p element. If @p match is scalar just return @p element. - /// @param element the value to extend - /// @param match the type to match the component count of - /// @returns a value with the same number of vector components as @p match - ir::Constant* MatchWidth(ir::Constant* element, const core::type::Type* match) { - if (match->Is<core::type::Vector>()) { - return b.Splat(ty.match_width(element->Type(), match), element); - } - return element; - } - /// Polyfill a `clamp()` builtin call for integers. /// @param call the builtin call instruction void ClampInt(ir::CoreBuiltinCall* call) { @@ -263,7 +249,7 @@ auto* bool_ty = ty.match_width(ty.bool_(), result_ty); // Make an u32 constant with the same component count as result_ty. - auto V = [&](uint32_t u) { return MatchWidth(b.Constant(u32(u)), result_ty); }; + auto V = [&](uint32_t u) { return b.MatchWidth(u32(u), result_ty); }; b.InsertBefore(call, [&] { // %x = %input; @@ -325,7 +311,7 @@ auto* bool_ty = ty.match_width(ty.bool_(), result_ty); // Make an u32 constant with the same component count as result_ty. - auto V = [&](uint32_t u) { return MatchWidth(b.Constant(u32(u)), result_ty); }; + auto V = [&](uint32_t u) { return b.MatchWidth(u32(u), result_ty); }; b.InsertBefore(call, [&] { // %x = %input; @@ -431,7 +417,7 @@ auto* bool_ty = ty.match_width(ty.bool_(), result_ty); // Make an u32 constant with the same component count as result_ty. - auto V = [&](uint32_t u) { return MatchWidth(b.Constant(u32(u)), result_ty); }; + auto V = [&](uint32_t u) { return b.MatchWidth(u32(u), result_ty); }; b.InsertBefore(call, [&] { // %x = %input; @@ -493,7 +479,7 @@ auto* bool_ty = ty.match_width(ty.bool_(), result_ty); // Make an u32 constant with the same component count as result_ty. - auto V = [&](uint32_t u) { return MatchWidth(b.Constant(u32(u)), result_ty); }; + auto V = [&](uint32_t u) { return b.MatchWidth(u32(u), result_ty); }; b.InsertBefore(call, [&] { // %x = %input; @@ -596,11 +582,11 @@ ir::Constant* zero = nullptr; ir::Constant* one = nullptr; if (type->DeepestElement()->Is<core::type::F32>()) { - zero = MatchWidth(b.Constant(0_f), type); - one = MatchWidth(b.Constant(1_f), type); + zero = b.MatchWidth(0_f, type); + one = b.MatchWidth(1_f, type); } else if (type->DeepestElement()->Is<core::type::F16>()) { - zero = MatchWidth(b.Constant(0_h), type); - one = MatchWidth(b.Constant(1_h), type); + zero = b.MatchWidth(0_h, type); + one = b.MatchWidth(1_h, type); } auto* clamp = b.CallWithResult(call->DetachResult(), core::BuiltinFn::kClamp, Vector{call->Args()[0], zero, one});
diff --git a/src/tint/lang/core/ir/transform/conversion_polyfill.cc b/src/tint/lang/core/ir/transform/conversion_polyfill.cc index fd35229..5c3e80d 100644 --- a/src/tint/lang/core/ir/transform/conversion_polyfill.cc +++ b/src/tint/lang/core/ir/transform/conversion_polyfill.cc
@@ -121,11 +121,11 @@ // Integer limits. if (res_ty->is_signed_integer_scalar_or_vector()) { - limits.low_limit_i = MatchWidth(b.Constant(i32(INT32_MIN)), res_ty); - limits.high_limit_i = MatchWidth(b.Constant(i32(INT32_MAX)), res_ty); + limits.low_limit_i = b.MatchWidth(i32(INT32_MIN), res_ty); + limits.high_limit_i = b.MatchWidth(i32(INT32_MAX), res_ty); } else { - limits.low_limit_i = MatchWidth(b.Constant(u32(0)), res_ty); - limits.high_limit_i = MatchWidth(b.Constant(u32(UINT32_MAX)), res_ty); + limits.low_limit_i = b.MatchWidth(u32(0), res_ty); + limits.high_limit_i = b.MatchWidth(u32(UINT32_MAX), res_ty); } // Largest integers representable in the source floating point format. @@ -135,23 +135,23 @@ // INT32_MAX is (2^31 - 1), which is not exactly representable as an f32, so we // instead use the next highest integer value in the f32 domain. const float kMaxI32AsF32 = std::nexttowardf(0x1p+31f, 0.0L); - limits.low_limit_f = MatchWidth(b.Constant(f32(INT32_MIN)), res_ty); - limits.high_limit_f = MatchWidth(b.Constant(f32(kMaxI32AsF32)), res_ty); + limits.low_limit_f = b.MatchWidth(f32(INT32_MIN), res_ty); + limits.high_limit_f = b.MatchWidth(f32(kMaxI32AsF32), res_ty); } else { // UINT32_MAX is (2^32 - 1), which is not exactly representable as an f32, so we // instead use the next highest integer value in the f32 domain. const float kMaxU32AsF32 = std::nexttowardf(0x1p+32f, 0.0L); - limits.low_limit_f = MatchWidth(b.Constant(f32(0)), res_ty); - limits.high_limit_f = MatchWidth(b.Constant(f32(kMaxU32AsF32)), res_ty); + limits.low_limit_f = b.MatchWidth(f32(0), res_ty); + limits.high_limit_f = b.MatchWidth(f32(kMaxU32AsF32), res_ty); } } else if (src_el_ty->Is<type::F16>()) { constexpr float MAX_F16 = 65504; if (res_ty->is_signed_integer_scalar_or_vector()) { - limits.low_limit_f = MatchWidth(b.Constant(f16(-MAX_F16)), res_ty); - limits.high_limit_f = MatchWidth(b.Constant(f16(MAX_F16)), res_ty); + limits.low_limit_f = b.MatchWidth(f16(-MAX_F16), res_ty); + limits.high_limit_f = b.MatchWidth(f16(MAX_F16), res_ty); } else { - limits.low_limit_f = MatchWidth(b.Constant(f16(0)), res_ty); - limits.high_limit_f = MatchWidth(b.Constant(f16(MAX_F16)), res_ty); + limits.low_limit_f = b.MatchWidth(f16(0), res_ty); + limits.high_limit_f = b.MatchWidth(f16(MAX_F16), res_ty); } } else { TINT_UNIMPLEMENTED() << "unhandled floating-point type"; @@ -184,18 +184,6 @@ auto* call = b.CallWithResult(convert->DetachResult(), helper, convert->Args()[0]); call->InsertBefore(convert); } - - /// Return a constant that has the same number of vector components as @p match, each with the - /// value @p element. If @p match is scalar just return @p element. - /// @param element the value to extend - /// @param match the type to match the component count of - /// @returns a value with the same number of vector components as @p match - ir::Constant* MatchWidth(ir::Constant* element, const core::type::Type* match) { - if (match->Is<core::type::Vector>()) { - return b.Splat(ty.match_width(element->Type(), match), element); - } - return element; - } }; } // namespace
diff --git a/src/tint/lang/core/ir/transform/demote_to_helper.cc b/src/tint/lang/core/ir/transform/demote_to_helper.cc index bb7cdfe..d903f25 100644 --- a/src/tint/lang/core/ir/transform/demote_to_helper.cc +++ b/src/tint/lang/core/ir/transform/demote_to_helper.cc
@@ -27,8 +27,6 @@ #include "src/tint/lang/core/ir/transform/demote_to_helper.h" -#include <utility> - #include "src/tint/lang/core/ir/builder.h" #include "src/tint/lang/core/ir/module.h" #include "src/tint/lang/core/ir/validator.h"
diff --git a/src/tint/lang/core/ir/transform/robustness.cc b/src/tint/lang/core/ir/transform/robustness.cc index c76327f..a9aa2b7 100644 --- a/src/tint/lang/core/ir/transform/robustness.cc +++ b/src/tint/lang/core/ir/transform/robustness.cc
@@ -28,7 +28,6 @@ #include "src/tint/lang/core/ir/transform/robustness.h" #include <algorithm> -#include <utility> #include "src/tint/lang/core/ir/builder.h" #include "src/tint/lang/core/ir/module.h" @@ -287,12 +286,9 @@ // Helper for clamping the coordinates. auto clamp_coords = [&](uint32_t idx) { - const type::Type* type = ty.u32(); - auto* one = b.Constant(1_u); - if (auto* vec = args[idx]->Type()->As<type::Vector>()) { - type = ty.vec(type, vec->Width()); - one = b.Splat(type, one); - } + auto* arg_ty = args[idx]->Type(); + const type::Type* type = ty.match_width(ty.u32(), arg_ty); + auto* one = b.MatchWidth(1_u, arg_ty); auto* dims = clamped_level ? b.Call(type, core::BuiltinFn::kTextureDimensions, args[0], clamped_level) : b.Call(type, core::BuiltinFn::kTextureDimensions, args[0]);
diff --git a/src/tint/lang/core/type/manager.h b/src/tint/lang/core/type/manager.h index 8ed8e94..e46ca98 100644 --- a/src/tint/lang/core/type/manager.h +++ b/src/tint/lang/core/type/manager.h
@@ -40,7 +40,6 @@ #include "src/tint/lang/core/type/type.h" #include "src/tint/lang/core/type/unique_node.h" #include "src/tint/utils/containers/unique_allocator.h" -#include "src/tint/utils/math/hash.h" #include "src/tint/utils/symbol/symbol.h" // Forward declarations
diff --git a/src/tint/lang/msl/writer/raise/builtin_polyfill.cc b/src/tint/lang/msl/writer/raise/builtin_polyfill.cc index 5111265..5fda95e 100644 --- a/src/tint/lang/msl/writer/raise/builtin_polyfill.cc +++ b/src/tint/lang/msl/writer/raise/builtin_polyfill.cc
@@ -460,14 +460,9 @@ // Calls to `sign` with an integer argument are replaced with select operations: // result = select(select(-1, 1, arg > 0), 0, arg == 0); if (type->is_integer_scalar_or_vector()) { - core::ir::Value* pos_one = b.Constant(i32(1)); - core::ir::Value* neg_one = b.Constant(i32(-1)); + core::ir::Value* pos_one = b.MatchWidth(i32(1), type); + core::ir::Value* neg_one = b.MatchWidth(i32(-1), type); const core::type::Type* bool_type = ty.match_width(ty.bool_(), type); - if (type->Is<core::type::Vector>()) { - pos_one = b.Splat(type, i32(1)); - neg_one = b.Splat(type, i32(-1)); - } - auto* zero = b.Zero(type); auto* sign = b.Call(type, core::BuiltinFn::kSelect, neg_one, pos_one, b.GreaterThan(bool_type, arg, zero));
diff --git a/src/tint/lang/wgsl/ast/transform/demote_to_helper.cc b/src/tint/lang/wgsl/ast/transform/demote_to_helper.cc index 30b3982..5f8de69 100644 --- a/src/tint/lang/wgsl/ast/transform/demote_to_helper.cc +++ b/src/tint/lang/wgsl/ast/transform/demote_to_helper.cc
@@ -29,7 +29,6 @@ #include <unordered_map> #include <unordered_set> -#include <utility> #include "src/tint/lang/core/type/reference.h" #include "src/tint/lang/wgsl/ast/transform/hoist_to_decl_before.h" @@ -40,7 +39,6 @@ #include "src/tint/lang/wgsl/sem/call.h" #include "src/tint/lang/wgsl/sem/function.h" #include "src/tint/lang/wgsl/sem/statement.h" -#include "src/tint/utils/containers/map.h" #include "src/tint/utils/rtti/switch.h" TINT_INSTANTIATE_TYPEINFO(tint::ast::transform::DemoteToHelper);
diff --git a/src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.cc b/src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.cc index 82e4e83..b67da7a 100644 --- a/src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.cc +++ b/src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.cc
@@ -27,8 +27,6 @@ #include "src/tint/lang/wgsl/ast/transform/promote_initializers_to_let.h" -#include <utility> - #include "src/tint/lang/core/type/struct.h" #include "src/tint/lang/wgsl/ast/transform/hoist_to_decl_before.h" #include "src/tint/lang/wgsl/ast/traverse_expressions.h"