tint/transform: Implement div / mod polyfill

Prevents UB for divide-by-zero and integer overflow when dividing

Fixed: tint:1349
Change-Id: Ieef66d27d7aec3011628ced076b2bccc7770a8af
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/108925
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton
2022-11-09 22:04:11 +00:00
committed by Dawn LUCI CQ
parent 9418152d08
commit 46ee63933c
427 changed files with 7255 additions and 3789 deletions

View File

@@ -1,7 +1,11 @@
int tint_div(int lhs, int rhs) {
return (lhs / (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
}
[numthreads(1, 1, 1)]
void f() {
const int a = 1;
const int b = 2;
const int r = (a / (b == 0 ? 1 : b));
const int r = tint_div(a, b);
return;
}

View File

@@ -1,7 +1,11 @@
int tint_div(int lhs, int rhs) {
return (lhs / (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
}
[numthreads(1, 1, 1)]
void f() {
const int a = 1;
const int b = 2;
const int r = (a / (b == 0 ? 1 : b));
const int r = tint_div(a, b);
return;
}

View File

@@ -1,9 +1,13 @@
#version 310 es
int tint_div(int lhs, int rhs) {
return (lhs / (bool(uint((rhs == 0)) | uint(bool(uint((lhs == -2147483648)) & uint((rhs == -1))))) ? 1 : rhs));
}
void f() {
int a = 1;
int b = 2;
int r = (a / b);
int r = tint_div(a, b);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

View File

@@ -1,10 +1,14 @@
#include <metal_stdlib>
using namespace metal;
int tint_div(int lhs, int rhs) {
return (lhs / select(rhs, 1, bool((rhs == 0) | bool((lhs == (-2147483647 - 1)) & (rhs == -1)))));
}
kernel void f() {
int const a = 1;
int const b = 2;
int const r = (a / b);
int const r = tint_div(a, b);
return;
}

View File

@@ -1,20 +1,41 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 9
; Bound: 25
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %f "f"
OpExecutionMode %f LocalSize 1 1 1
OpName %tint_div "tint_div"
OpName %lhs "lhs"
OpName %rhs "rhs"
OpName %f "f"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%int = OpTypeInt 32 1
%1 = OpTypeFunction %int %int %int
%8 = OpConstantNull %int
%bool = OpTypeBool
%int_n2147483648 = OpConstant %int -2147483648
%int_n1 = OpConstant %int -1
%int_1 = OpConstant %int 1
%void = OpTypeVoid
%19 = OpTypeFunction %void
%int_2 = OpConstant %int 2
%f = OpFunction %void None %1
%4 = OpLabel
%8 = OpSDiv %int %int_1 %int_2
%tint_div = OpFunction %int None %1
%lhs = OpFunctionParameter %int
%rhs = OpFunctionParameter %int
%6 = OpLabel
%9 = OpIEqual %bool %rhs %8
%12 = OpIEqual %bool %lhs %int_n2147483648
%14 = OpIEqual %bool %rhs %int_n1
%15 = OpLogicalAnd %bool %12 %14
%16 = OpLogicalOr %bool %9 %15
%7 = OpSelect %int %16 %int_1 %rhs
%18 = OpSDiv %int %lhs %7
OpReturnValue %18
OpFunctionEnd
%f = OpFunction %void None %19
%22 = OpLabel
%24 = OpFunctionCall %int %tint_div %int_1 %int_2
OpReturn
OpFunctionEnd

View File

@@ -1,7 +1,11 @@
uint tint_div(uint lhs, uint rhs) {
return (lhs / ((rhs == 0u) ? 1u : rhs));
}
[numthreads(1, 1, 1)]
void f() {
const uint a = 1u;
const uint b = 2u;
const uint r = (a / (b == 0u ? 1u : b));
const uint r = tint_div(a, b);
return;
}

View File

@@ -1,7 +1,11 @@
uint tint_div(uint lhs, uint rhs) {
return (lhs / ((rhs == 0u) ? 1u : rhs));
}
[numthreads(1, 1, 1)]
void f() {
const uint a = 1u;
const uint b = 2u;
const uint r = (a / (b == 0u ? 1u : b));
const uint r = tint_div(a, b);
return;
}

View File

@@ -1,9 +1,13 @@
#version 310 es
uint tint_div(uint lhs, uint rhs) {
return (lhs / ((rhs == 0u) ? 1u : rhs));
}
void f() {
uint a = 1u;
uint b = 2u;
uint r = (a / b);
uint r = tint_div(a, b);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

View File

@@ -1,10 +1,14 @@
#include <metal_stdlib>
using namespace metal;
uint tint_div(uint lhs, uint rhs) {
return (lhs / select(rhs, 1u, (rhs == 0u)));
}
kernel void f() {
uint const a = 1u;
uint const b = 2u;
uint const r = (a / b);
uint const r = tint_div(a, b);
return;
}

View File

@@ -1,20 +1,35 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 9
; Bound: 19
; Schema: 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %f "f"
OpExecutionMode %f LocalSize 1 1 1
OpName %tint_div "tint_div"
OpName %lhs "lhs"
OpName %rhs "rhs"
OpName %f "f"
%void = OpTypeVoid
%1 = OpTypeFunction %void
%uint = OpTypeInt 32 0
%1 = OpTypeFunction %uint %uint %uint
%8 = OpConstantNull %uint
%bool = OpTypeBool
%uint_1 = OpConstant %uint 1
%void = OpTypeVoid
%13 = OpTypeFunction %void
%uint_2 = OpConstant %uint 2
%f = OpFunction %void None %1
%4 = OpLabel
%8 = OpUDiv %uint %uint_1 %uint_2
%tint_div = OpFunction %uint None %1
%lhs = OpFunctionParameter %uint
%rhs = OpFunctionParameter %uint
%6 = OpLabel
%9 = OpIEqual %bool %rhs %8
%7 = OpSelect %uint %9 %uint_1 %rhs
%12 = OpUDiv %uint %lhs %7
OpReturnValue %12
OpFunctionEnd
%f = OpFunction %void None %13
%16 = OpLabel
%18 = OpFunctionCall %uint %tint_div %uint_1 %uint_2
OpReturn
OpFunctionEnd