tint: fix signed overflow in const eval modulo

Bug: chromium:1395241
Bug: tint:1581
Change-Id: I6f6084749d9bc5c0d493476b251eeec5543d8621
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112701
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Auto-Submit: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Antonio Maiorano 2022-12-05 17:16:15 +00:00 committed by Dawn LUCI CQ
parent af7b4d0d9e
commit 875d116a87
9 changed files with 99 additions and 6 deletions

View File

@ -547,7 +547,12 @@ namespace detail {
/// @returns the remainder of e1 / e2 /// @returns the remainder of e1 / e2
template <typename T> template <typename T>
inline T Mod(T e1, T e2) { inline T Mod(T e1, T e2) {
return e1 - e2 * static_cast<T>(std::trunc(e1 / e2)); if constexpr (IsIntegral<T>) {
return e1 % e2;
} else {
return e1 - e2 * std::trunc(e1 / e2);
}
} }
} // namespace detail } // namespace detail

View File

@ -704,6 +704,15 @@ INSTANTIATE_TEST_SUITE_P(
{AInt(2), AInt(10), AInt(4)}, {AInt(2), AInt(10), AInt(4)},
{AInt(0), AInt::Highest(), AInt::Highest()}, {AInt(0), AInt::Highest(), AInt::Highest()},
{AInt(0), AInt::Lowest(), AInt::Lowest()}, {AInt(0), AInt::Lowest(), AInt::Lowest()},
{AInt(2), AInt::Highest(), AInt(5)},
{AInt(1), AInt::Highest(), AInt(6)},
{AInt(0), AInt::Highest(), AInt(7)},
{-AInt{1}, -AInt{10}, AInt{3}},
{-AInt{2}, -AInt{10}, AInt{4}},
{AInt{1}, AInt{10}, -AInt{3}},
{AInt{2}, AInt{10}, -AInt{4}},
{-AInt{1}, -AInt{10}, -AInt{3}},
{-AInt{2}, -AInt{10}, -AInt{4}},
{OVERFLOW, AInt::Highest(), AInt(0)}, {OVERFLOW, AInt::Highest(), AInt(0)},
{OVERFLOW, AInt::Lowest(), AInt(0)}, {OVERFLOW, AInt::Lowest(), AInt(0)},
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
@ -725,11 +734,22 @@ TEST_P(CheckedModTest_Float, Test) {
template <typename T> template <typename T>
std::vector<BinaryCheckedCase_Float> CheckedModTest_FloatCases() { std::vector<BinaryCheckedCase_Float> CheckedModTest_FloatCases() {
return { return {
{T(0.5), T(10.5), T(1)}, {T(0.5), T(10.5), T(2)}, {T(0.5), T(10.5), T(1)}, //
{T(1.5), T(10.5), T(3)}, {T(2.5), T(10.5), T(4)}, {T(0.5), T(10.5), T(2)}, //
{T(0.5), T(10.5), T(5)}, {T(0), T::Highest(), T::Highest()}, {T(1.5), T(10.5), T(3)}, //
{T(0), T::Lowest(), T::Lowest()}, {Overflow<T>, T(123), T(0)}, {T(2.5), T(10.5), T(4)}, //
{Overflow<T>, T(123), T(-0)}, {Overflow<T>, T(-123), T(0)}, {T(0.5), T(10.5), T(5)}, //
{T(0), T::Highest(), T::Highest()}, //
{T(0), T::Lowest(), T::Lowest()}, //
{-T{1}, -T{10}, T{3}}, //
{-T{2}, -T{10}, T{4}}, //
{T{1}, T{10}, -T{3}}, //
{T{2}, T{10}, -T{4}}, //
{-T{1}, -T{10}, -T{3}}, //
{-T{2}, -T{10}, -T{4}}, //
{Overflow<T>, T(123), T(0)}, //
{Overflow<T>, T(123), T(-0)}, //
{Overflow<T>, T(-123), T(0)}, //
{Overflow<T>, T(-123), T(-0)}, {Overflow<T>, T(-123), T(-0)},
}; };
} }

View File

@ -0,0 +1,4 @@
fn fr6snorm() {
var bzbxttch=~~~~~~~~~~~~ ~~~~~-~~~~~~~~~~~~~~~~~~~~~ ~~~-~~~~~~~~~~~~~~~~~~~~~~~~~~~9223372036854775807% (
5);
}

View File

@ -0,0 +1,8 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
void fr6snorm() {
int bzbxttch = 2;
}

View File

@ -0,0 +1,8 @@
[numthreads(1, 1, 1)]
void unused_entry_point() {
return;
}
void fr6snorm() {
int bzbxttch = 2;
}

View File

@ -0,0 +1,10 @@
#version 310 es
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void unused_entry_point() {
return;
}
void fr6snorm() {
int bzbxttch = 2;
}

View File

@ -0,0 +1,7 @@
#include <metal_stdlib>
using namespace metal;
void fr6snorm() {
int bzbxttch = 2;
}

View File

@ -0,0 +1,28 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 12
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
OpExecutionMode %unused_entry_point LocalSize 1 1 1
OpName %unused_entry_point "unused_entry_point"
OpName %fr6snorm "fr6snorm"
OpName %bzbxttch "bzbxttch"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%int_2 = OpConstant %int 2
%_ptr_Function_int = OpTypePointer Function %int
%11 = OpConstantNull %int
%unused_entry_point = OpFunction %void None %1
%4 = OpLabel
OpReturn
OpFunctionEnd
%fr6snorm = OpFunction %void None %1
%6 = OpLabel
%bzbxttch = OpVariable %_ptr_Function_int Function %11
OpStore %bzbxttch %int_2
OpReturn
OpFunctionEnd

View File

@ -0,0 +1,3 @@
fn fr6snorm() {
var bzbxttch = (~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(-(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(-(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(~(9223372036854775807)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) % 5);
}