tint/resolver: Error when quantizeToF16 value is unrepresentable

...as a F16, for const-eval.

Fixed: tint:1744
Change-Id: I5eff05c6bc4727cbdfefb546602f3ee194d22612
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/108640
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2022-11-17 21:34:27 +00:00 committed by Dawn LUCI CQ
parent f6181d0365
commit 83cef52eb7
2 changed files with 16 additions and 21 deletions

View File

@ -2615,19 +2615,13 @@ ConstEval::Result ConstEval::unpack4x8unorm(const sem::Type* ty,
ConstEval::Result ConstEval::quantizeToF16(const sem::Type* ty, ConstEval::Result ConstEval::quantizeToF16(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args, utils::VectorRef<const sem::Constant*> args,
const Source&) { const Source& source) {
auto transform = [&](const sem::Constant* c) { auto transform = [&](const sem::Constant* c) -> ImplResult {
auto conv = CheckedConvert<f32>(f16(c->As<f32>())); auto value = c->As<f32>();
auto conv = CheckedConvert<f32>(f16(value));
if (!conv) { if (!conv) {
// https://www.w3.org/TR/WGSL/#quantizeToF16-builtin AddError(OverflowErrorMessage(value, "f16"), source);
// If e is outside the finite range of binary16, then the result is any value of type return utils::Failure;
// f32
switch (conv.Failure()) {
case ConversionFailure::kExceedsNegativeLimit:
return CreateElement(builder, c->Type(), f16(f16::kLowestValue));
case ConversionFailure::kExceedsPositiveLimit:
return CreateElement(builder, c->Type(), f16(f16::kHighestValue));
}
} }
return CreateElement(builder, c->Type(), conv.Get()); return CreateElement(builder, c->Type(), conv.Get());
}; };

View File

@ -1916,7 +1916,6 @@ INSTANTIATE_TEST_SUITE_P( //
testing::ValuesIn(Unpack2x16unormCases()))); testing::ValuesIn(Unpack2x16unormCases())));
std::vector<Case> QuantizeToF16Cases() { std::vector<Case> QuantizeToF16Cases() {
(void)E({Vec(0_f, 0_f)}, ""); // Currently unused, but will be soon.
return { return {
C({0_f}, 0_f), // C({0_f}, 0_f), //
C({-0_f}, -0_f), // C({-0_f}, -0_f), //
@ -1945,12 +1944,6 @@ std::vector<Case> QuantizeToF16Cases() {
C({0x0.06b7p-14_f}, 0x0.068p-14_f), // C({0x0.06b7p-14_f}, 0x0.068p-14_f), //
C({-0x0.06b7p-14_f}, -0x0.068p-14_f), // C({-0x0.06b7p-14_f}, -0x0.068p-14_f), //
// Value out of f16 range
C({65504.003_f}, 65504_f), //
C({-65504.003_f}, -65504_f), //
C({0x1.234p56_f}, 65504_f), //
C({-0x4.321p65_f}, -65504_f), //
// Vector tests // Vector tests
C({Vec(0_f, -0_f)}, Vec(0_f, -0_f)), // C({Vec(0_f, -0_f)}, Vec(0_f, -0_f)), //
C({Vec(1_f, -1_f)}, Vec(1_f, -1_f)), // C({Vec(1_f, -1_f)}, Vec(1_f, -1_f)), //
@ -1963,8 +1956,16 @@ std::vector<Case> QuantizeToF16Cases() {
C({Vec(0x0.034p-14_f, -0x0.034p-14_f, 0x0.068p-14_f, -0x0.068p-14_f)}, C({Vec(0x0.034p-14_f, -0x0.034p-14_f, 0x0.068p-14_f, -0x0.068p-14_f)},
Vec(0x0.034p-14_f, -0x0.034p-14_f, 0x0.068p-14_f, -0x0.068p-14_f)), Vec(0x0.034p-14_f, -0x0.034p-14_f, 0x0.068p-14_f, -0x0.068p-14_f)),
C({Vec(65504.003_f, 0x1.234p56_f)}, Vec(65504_f, 65504_f)), // Value out of f16 range
C({Vec(-0x1.234p56_f, -65504.003_f)}, Vec(-65504_f, -65504_f)), E({65504.003_f}, "12:34 error: value 65504.00390625 cannot be represented as 'f16'"),
E({-65504.003_f}, "12:34 error: value -65504.00390625 cannot be represented as 'f16'"),
E({0x1.234p56_f}, "12:34 error: value 81979586966978560 cannot be represented as 'f16'"),
E({0x4.321p65_f},
"12:34 error: value 1.5478871919272394752e+20 cannot be represented as 'f16'"),
E({Vec(65504.003_f, 0_f)},
"12:34 error: value 65504.00390625 cannot be represented as 'f16'"),
E({Vec(0_f, -0x4.321p65_f)},
"12:34 error: value -1.5478871919272394752e+20 cannot be represented as 'f16'"),
}; };
} }
INSTANTIATE_TEST_SUITE_P( // INSTANTIATE_TEST_SUITE_P( //