tint: polyfill remainder to handle negative operands

Bug: tint:1802
Change-Id: Ie9baa045feda08523e5ca4f5ce94b6db7d4477e5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/119100
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Antonio Maiorano
2023-02-10 15:01:02 +00:00
committed by Dawn LUCI CQ
parent 78583a14e6
commit ec20758675
103 changed files with 1660 additions and 554 deletions

View File

@@ -6,7 +6,12 @@ void unused_entry_point() {
RWByteAddressBuffer v : register(u0, space0);
int tint_mod(int lhs, int rhs) {
return (lhs % (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
const int rhs_or_one = (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs);
if (any(((uint((lhs | rhs_or_one)) & 2147483648u) != 0u))) {
return (lhs - ((lhs / rhs_or_one) * rhs_or_one));
} else {
return (lhs % rhs_or_one);
}
}
void foo() {

View File

@@ -6,7 +6,12 @@ void unused_entry_point() {
RWByteAddressBuffer v : register(u0, space0);
int tint_mod(int lhs, int rhs) {
return (lhs % (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
const int rhs_or_one = (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs);
if (any(((uint((lhs | rhs_or_one)) & 2147483648u) != 0u))) {
return (lhs - ((lhs / rhs_or_one) * rhs_or_one));
} else {
return (lhs % rhs_or_one);
}
}
void foo() {

View File

@@ -13,7 +13,12 @@ layout(binding = 0, std430) buffer v_block_ssbo {
} v;
int tint_mod(int lhs, int rhs) {
return (lhs % (bool(uint((rhs == 0)) | uint(bool(uint((lhs == -2147483648)) & uint((rhs == -1))))) ? 1 : rhs));
int rhs_or_one = (bool(uint((rhs == 0)) | uint(bool(uint((lhs == -2147483648)) & uint((rhs == -1))))) ? 1 : rhs);
if (((uint((lhs | rhs_or_one)) & 2147483648u) != 0u)) {
return (lhs - ((lhs / rhs_or_one) * rhs_or_one));
} else {
return (lhs % rhs_or_one);
}
}
void foo() {

View File

@@ -6,7 +6,12 @@ struct S {
};
int tint_mod(int lhs, int rhs) {
return (lhs % select(rhs, 1, bool((rhs == 0) | bool((lhs == (-2147483647 - 1)) & (rhs == -1)))));
int const rhs_or_one = select(rhs, 1, bool((rhs == 0) | bool((lhs == (-2147483647 - 1)) & (rhs == -1))));
if (any(((uint((lhs | rhs_or_one)) & 2147483648u) != 0u))) {
return as_type<int>((as_type<uint>(lhs) - as_type<uint>(as_type<int>((as_type<uint>((lhs / rhs_or_one)) * as_type<uint>(rhs_or_one))))));
} else {
return (lhs % rhs_or_one);
}
}
void foo(device S* const tint_symbol_1) {

View File

@@ -1,7 +1,7 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 37
; Bound: 57
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
@@ -16,6 +16,8 @@
OpName %tint_mod "tint_mod"
OpName %lhs "lhs"
OpName %rhs "rhs"
OpName %tint_return_flag "tint_return_flag"
OpName %tint_return_value "tint_return_value"
OpName %foo "foo"
OpDecorate %v_block Block
OpMemberDecorate %v_block 0 Offset 0
@@ -30,12 +32,18 @@
%void = OpTypeVoid
%6 = OpTypeFunction %void
%10 = OpTypeFunction %int %int %int
%16 = OpConstantNull %int
%bool = OpTypeBool
%_ptr_Function_bool = OpTypePointer Function %bool
%18 = OpConstantNull %bool
%_ptr_Function_int = OpTypePointer Function %int
%21 = OpConstantNull %int
%int_n2147483648 = OpConstant %int -2147483648
%int_n1 = OpConstant %int -1
%int_1 = OpConstant %int 1
%uint = OpTypeInt 32 0
%uint_2147483648 = OpConstant %uint 2147483648
%37 = OpConstantNull %uint
%true = OpConstantTrue %bool
%uint_0 = OpConstant %uint 0
%_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
%int_2 = OpConstant %int 2
@@ -47,21 +55,42 @@
%lhs = OpFunctionParameter %int
%rhs = OpFunctionParameter %int
%14 = OpLabel
%17 = OpIEqual %bool %rhs %16
%20 = OpIEqual %bool %lhs %int_n2147483648
%22 = OpIEqual %bool %rhs %int_n1
%23 = OpLogicalAnd %bool %20 %22
%24 = OpLogicalOr %bool %17 %23
%15 = OpSelect %int %24 %int_1 %rhs
%26 = OpSRem %int %lhs %15
OpReturnValue %26
%tint_return_flag = OpVariable %_ptr_Function_bool Function %18
%tint_return_value = OpVariable %_ptr_Function_int Function %21
%23 = OpIEqual %bool %rhs %21
%25 = OpIEqual %bool %lhs %int_n2147483648
%27 = OpIEqual %bool %rhs %int_n1
%28 = OpLogicalAnd %bool %25 %27
%29 = OpLogicalOr %bool %23 %28
%22 = OpSelect %int %29 %int_1 %rhs
%34 = OpBitwiseOr %int %lhs %22
%32 = OpBitcast %uint %34
%36 = OpBitwiseAnd %uint %32 %uint_2147483648
%38 = OpINotEqual %bool %36 %37
OpSelectionMerge %39 None
OpBranchConditional %38 %40 %41
%40 = OpLabel
OpStore %tint_return_flag %true
%43 = OpSDiv %int %lhs %22
%44 = OpIMul %int %43 %22
%45 = OpISub %int %lhs %44
OpStore %tint_return_value %45
OpBranch %39
%41 = OpLabel
OpStore %tint_return_flag %true
%46 = OpSRem %int %lhs %22
OpStore %tint_return_value %46
OpBranch %39
%39 = OpLabel
%47 = OpLoad %int %tint_return_value
OpReturnValue %47
OpFunctionEnd
%foo = OpFunction %void None %6
%28 = OpLabel
%33 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
%34 = OpLoad %int %33
%29 = OpFunctionCall %int %tint_mod %34 %int_2
%36 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
OpStore %36 %29
%49 = OpLabel
%53 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
%54 = OpLoad %int %53
%50 = OpFunctionCall %int %tint_mod %54 %int_2
%56 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
OpStore %56 %50
OpReturn
OpFunctionEnd