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

View File

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

View File

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

View File

@@ -1,9 +1,14 @@
#version 310 es
ivec3 tint_div(int lhs, ivec3 rhs) {
ivec3 l = ivec3(lhs);
return (l / mix(rhs, ivec3(1), bvec3(uvec3(equal(rhs, ivec3(0))) | uvec3(bvec3(uvec3(equal(l, ivec3(-2147483648))) & uvec3(equal(rhs, ivec3(-1))))))));
}
void f() {
int a = 4;
ivec3 b = ivec3(1, 2, 3);
ivec3 r = (a / b);
ivec3 r = tint_div(a, b);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

View File

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

View File

@@ -1,28 +1,50 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 17
; Bound: 34
; 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
%int_4 = OpConstant %int 4
%v3int = OpTypeVector %int 3
%1 = OpTypeFunction %v3int %int %v3int
%10 = OpConstantNull %v3int
%bool = OpTypeBool
%v3bool = OpTypeVector %bool 3
%int_n2147483648 = OpConstant %int -2147483648
%15 = OpConstantComposite %v3int %int_n2147483648 %int_n2147483648 %int_n2147483648
%int_n1 = OpConstant %int -1
%18 = OpConstantComposite %v3int %int_n1 %int_n1 %int_n1
%int_1 = OpConstant %int 1
%23 = OpConstantComposite %v3int %int_1 %int_1 %int_1
%void = OpTypeVoid
%25 = OpTypeFunction %void
%int_4 = OpConstant %int 4
%int_2 = OpConstant %int 2
%int_3 = OpConstant %int 3
%11 = OpConstantComposite %v3int %int_1 %int_2 %int_3
%_ptr_Function_v3int = OpTypePointer Function %v3int
%15 = OpConstantNull %v3int
%f = OpFunction %void None %1
%4 = OpLabel
%13 = OpVariable %_ptr_Function_v3int Function %15
%16 = OpCompositeConstruct %v3int %int_4 %int_4 %int_4
%12 = OpSDiv %v3int %16 %11
%32 = OpConstantComposite %v3int %int_1 %int_2 %int_3
%tint_div = OpFunction %v3int None %1
%lhs = OpFunctionParameter %int
%rhs = OpFunctionParameter %v3int
%7 = OpLabel
%8 = OpCompositeConstruct %v3int %lhs %lhs %lhs
%11 = OpIEqual %v3bool %rhs %10
%16 = OpIEqual %v3bool %8 %15
%19 = OpIEqual %v3bool %rhs %18
%20 = OpLogicalAnd %v3bool %16 %19
%21 = OpLogicalOr %v3bool %11 %20
%9 = OpSelect %v3int %21 %23 %rhs
%24 = OpSDiv %v3int %8 %9
OpReturnValue %24
OpFunctionEnd
%f = OpFunction %void None %25
%28 = OpLabel
%33 = OpFunctionCall %v3int %tint_div %int_4 %32
OpReturn
OpFunctionEnd

View File

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

View File

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

View File

@@ -1,9 +1,14 @@
#version 310 es
uvec3 tint_div(uint lhs, uvec3 rhs) {
uvec3 l = uvec3(lhs);
return (l / mix(rhs, uvec3(1u), equal(rhs, uvec3(0u))));
}
void f() {
uint a = 4u;
uvec3 b = uvec3(1u, 2u, 3u);
uvec3 r = (a / b);
uvec3 r = tint_div(a, b);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

View File

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

View File

@@ -1,28 +1,42 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 17
; Bound: 26
; 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
%uint_4 = OpConstant %uint 4
%v3uint = OpTypeVector %uint 3
%1 = OpTypeFunction %v3uint %uint %v3uint
%10 = OpConstantNull %v3uint
%bool = OpTypeBool
%v3bool = OpTypeVector %bool 3
%uint_1 = OpConstant %uint 1
%15 = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_1
%void = OpTypeVoid
%17 = OpTypeFunction %void
%uint_4 = OpConstant %uint 4
%uint_2 = OpConstant %uint 2
%uint_3 = OpConstant %uint 3
%11 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3
%_ptr_Function_v3uint = OpTypePointer Function %v3uint
%15 = OpConstantNull %v3uint
%f = OpFunction %void None %1
%4 = OpLabel
%13 = OpVariable %_ptr_Function_v3uint Function %15
%16 = OpCompositeConstruct %v3uint %uint_4 %uint_4 %uint_4
%12 = OpUDiv %v3uint %16 %11
%24 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3
%tint_div = OpFunction %v3uint None %1
%lhs = OpFunctionParameter %uint
%rhs = OpFunctionParameter %v3uint
%7 = OpLabel
%8 = OpCompositeConstruct %v3uint %lhs %lhs %lhs
%11 = OpIEqual %v3bool %rhs %10
%9 = OpSelect %v3uint %11 %15 %rhs
%16 = OpUDiv %v3uint %8 %9
OpReturnValue %16
OpFunctionEnd
%f = OpFunction %void None %17
%20 = OpLabel
%25 = OpFunctionCall %v3uint %tint_div %uint_4 %24
OpReturn
OpFunctionEnd

View File

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

View File

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

View File

@@ -1,9 +1,14 @@
#version 310 es
ivec3 tint_div(ivec3 lhs, int rhs) {
ivec3 r = ivec3(rhs);
return (lhs / mix(r, ivec3(1), bvec3(uvec3(equal(r, ivec3(0))) | uvec3(bvec3(uvec3(equal(lhs, ivec3(-2147483648))) & uvec3(equal(r, ivec3(-1))))))));
}
void f() {
ivec3 a = ivec3(1, 2, 3);
int b = 4;
ivec3 r = (a / b);
ivec3 r = tint_div(a, b);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

View File

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

View File

@@ -1,28 +1,50 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 17
; Bound: 34
; 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
%v3int = OpTypeVector %int 3
%1 = OpTypeFunction %v3int %v3int %int
%10 = OpConstantNull %v3int
%bool = OpTypeBool
%v3bool = OpTypeVector %bool 3
%int_n2147483648 = OpConstant %int -2147483648
%15 = OpConstantComposite %v3int %int_n2147483648 %int_n2147483648 %int_n2147483648
%int_n1 = OpConstant %int -1
%18 = OpConstantComposite %v3int %int_n1 %int_n1 %int_n1
%int_1 = OpConstant %int 1
%23 = OpConstantComposite %v3int %int_1 %int_1 %int_1
%void = OpTypeVoid
%25 = OpTypeFunction %void
%int_2 = OpConstant %int 2
%int_3 = OpConstant %int 3
%10 = OpConstantComposite %v3int %int_1 %int_2 %int_3
%31 = OpConstantComposite %v3int %int_1 %int_2 %int_3
%int_4 = OpConstant %int 4
%_ptr_Function_v3int = OpTypePointer Function %v3int
%15 = OpConstantNull %v3int
%f = OpFunction %void None %1
%4 = OpLabel
%13 = OpVariable %_ptr_Function_v3int Function %15
%16 = OpCompositeConstruct %v3int %int_4 %int_4 %int_4
%12 = OpSDiv %v3int %10 %16
%tint_div = OpFunction %v3int None %1
%lhs = OpFunctionParameter %v3int
%rhs = OpFunctionParameter %int
%7 = OpLabel
%8 = OpCompositeConstruct %v3int %rhs %rhs %rhs
%11 = OpIEqual %v3bool %8 %10
%16 = OpIEqual %v3bool %lhs %15
%19 = OpIEqual %v3bool %8 %18
%20 = OpLogicalAnd %v3bool %16 %19
%21 = OpLogicalOr %v3bool %11 %20
%9 = OpSelect %v3int %21 %23 %8
%24 = OpSDiv %v3int %lhs %9
OpReturnValue %24
OpFunctionEnd
%f = OpFunction %void None %25
%28 = OpLabel
%33 = OpFunctionCall %v3int %tint_div %31 %int_4
OpReturn
OpFunctionEnd

View File

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

View File

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

View File

@@ -1,9 +1,14 @@
#version 310 es
uvec3 tint_div(uvec3 lhs, uint rhs) {
uvec3 r = uvec3(rhs);
return (lhs / mix(r, uvec3(1u), equal(r, uvec3(0u))));
}
void f() {
uvec3 a = uvec3(1u, 2u, 3u);
uint b = 4u;
uvec3 r = (a / b);
uvec3 r = tint_div(a, b);
}
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

View File

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

View File

@@ -1,28 +1,42 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 17
; Bound: 26
; 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
%v3uint = OpTypeVector %uint 3
%1 = OpTypeFunction %v3uint %v3uint %uint
%10 = OpConstantNull %v3uint
%bool = OpTypeBool
%v3bool = OpTypeVector %bool 3
%uint_1 = OpConstant %uint 1
%15 = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_1
%void = OpTypeVoid
%17 = OpTypeFunction %void
%uint_2 = OpConstant %uint 2
%uint_3 = OpConstant %uint 3
%10 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3
%23 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3
%uint_4 = OpConstant %uint 4
%_ptr_Function_v3uint = OpTypePointer Function %v3uint
%15 = OpConstantNull %v3uint
%f = OpFunction %void None %1
%4 = OpLabel
%13 = OpVariable %_ptr_Function_v3uint Function %15
%16 = OpCompositeConstruct %v3uint %uint_4 %uint_4 %uint_4
%12 = OpUDiv %v3uint %10 %16
%tint_div = OpFunction %v3uint None %1
%lhs = OpFunctionParameter %v3uint
%rhs = OpFunctionParameter %uint
%7 = OpLabel
%8 = OpCompositeConstruct %v3uint %rhs %rhs %rhs
%11 = OpIEqual %v3bool %8 %10
%9 = OpSelect %v3uint %11 %15 %8
%16 = OpUDiv %v3uint %lhs %9
OpReturnValue %16
OpFunctionEnd
%f = OpFunction %void None %17
%20 = OpLabel
%25 = OpFunctionCall %v3uint %tint_div %23 %uint_4
OpReturn
OpFunctionEnd

View File

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

View File

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

View File

@@ -1,9 +1,13 @@
#version 310 es
ivec3 tint_div(ivec3 lhs, ivec3 rhs) {
return (lhs / mix(rhs, ivec3(1), bvec3(uvec3(equal(rhs, ivec3(0))) | uvec3(bvec3(uvec3(equal(lhs, ivec3(-2147483648))) & uvec3(equal(rhs, ivec3(-1))))))));
}
void f() {
ivec3 a = ivec3(1, 2, 3);
ivec3 b = ivec3(4, 5, 6);
ivec3 r = (a / b);
ivec3 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;
int3 tint_div(int3 lhs, int3 rhs) {
return (lhs / select(rhs, int3(1), ((rhs == int3(0)) | ((lhs == int3((-2147483647 - 1))) & (rhs == int3(-1))))));
}
kernel void f() {
int3 const a = int3(1, 2, 3);
int3 const b = int3(4, 5, 6);
int3 const r = (a / b);
int3 const r = tint_div(a, b);
return;
}

View File

@@ -1,27 +1,52 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 16
; Bound: 36
; 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
%v3int = OpTypeVector %int 3
%1 = OpTypeFunction %v3int %v3int %v3int
%9 = OpConstantNull %v3int
%bool = OpTypeBool
%v3bool = OpTypeVector %bool 3
%int_n2147483648 = OpConstant %int -2147483648
%14 = OpConstantComposite %v3int %int_n2147483648 %int_n2147483648 %int_n2147483648
%int_n1 = OpConstant %int -1
%17 = OpConstantComposite %v3int %int_n1 %int_n1 %int_n1
%int_1 = OpConstant %int 1
%22 = OpConstantComposite %v3int %int_1 %int_1 %int_1
%void = OpTypeVoid
%24 = OpTypeFunction %void
%int_2 = OpConstant %int 2
%int_3 = OpConstant %int 3
%10 = OpConstantComposite %v3int %int_1 %int_2 %int_3
%30 = OpConstantComposite %v3int %int_1 %int_2 %int_3
%int_4 = OpConstant %int 4
%int_5 = OpConstant %int 5
%int_6 = OpConstant %int 6
%14 = OpConstantComposite %v3int %int_4 %int_5 %int_6
%f = OpFunction %void None %1
%4 = OpLabel
%15 = OpSDiv %v3int %10 %14
%34 = OpConstantComposite %v3int %int_4 %int_5 %int_6
%tint_div = OpFunction %v3int None %1
%lhs = OpFunctionParameter %v3int
%rhs = OpFunctionParameter %v3int
%7 = OpLabel
%10 = OpIEqual %v3bool %rhs %9
%15 = OpIEqual %v3bool %lhs %14
%18 = OpIEqual %v3bool %rhs %17
%19 = OpLogicalAnd %v3bool %15 %18
%20 = OpLogicalOr %v3bool %10 %19
%8 = OpSelect %v3int %20 %22 %rhs
%23 = OpSDiv %v3int %lhs %8
OpReturnValue %23
OpFunctionEnd
%f = OpFunction %void None %24
%27 = OpLabel
%35 = OpFunctionCall %v3int %tint_div %30 %34
OpReturn
OpFunctionEnd

View File

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

View File

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

View File

@@ -1,9 +1,13 @@
#version 310 es
uvec3 tint_div(uvec3 lhs, uvec3 rhs) {
return (lhs / mix(rhs, uvec3(1u), equal(rhs, uvec3(0u))));
}
void f() {
uvec3 a = uvec3(1u, 2u, 3u);
uvec3 b = uvec3(4u, 5u, 6u);
uvec3 r = (a / b);
uvec3 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;
uint3 tint_div(uint3 lhs, uint3 rhs) {
return (lhs / select(rhs, uint3(1u), (rhs == uint3(0u))));
}
kernel void f() {
uint3 const a = uint3(1u, 2u, 3u);
uint3 const b = uint3(4u, 5u, 6u);
uint3 const r = (a / b);
uint3 const r = tint_div(a, b);
return;
}

View File

@@ -1,27 +1,44 @@
; SPIR-V
; Version: 1.3
; Generator: Google Tint Compiler; 0
; Bound: 16
; Bound: 28
; 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
%v3uint = OpTypeVector %uint 3
%1 = OpTypeFunction %v3uint %v3uint %v3uint
%9 = OpConstantNull %v3uint
%bool = OpTypeBool
%v3bool = OpTypeVector %bool 3
%uint_1 = OpConstant %uint 1
%14 = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_1
%void = OpTypeVoid
%16 = OpTypeFunction %void
%uint_2 = OpConstant %uint 2
%uint_3 = OpConstant %uint 3
%10 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3
%22 = OpConstantComposite %v3uint %uint_1 %uint_2 %uint_3
%uint_4 = OpConstant %uint 4
%uint_5 = OpConstant %uint 5
%uint_6 = OpConstant %uint 6
%14 = OpConstantComposite %v3uint %uint_4 %uint_5 %uint_6
%f = OpFunction %void None %1
%4 = OpLabel
%15 = OpUDiv %v3uint %10 %14
%26 = OpConstantComposite %v3uint %uint_4 %uint_5 %uint_6
%tint_div = OpFunction %v3uint None %1
%lhs = OpFunctionParameter %v3uint
%rhs = OpFunctionParameter %v3uint
%7 = OpLabel
%10 = OpIEqual %v3bool %rhs %9
%8 = OpSelect %v3uint %10 %14 %rhs
%15 = OpUDiv %v3uint %lhs %8
OpReturnValue %15
OpFunctionEnd
%f = OpFunction %void None %16
%19 = OpLabel
%27 = OpFunctionCall %v3uint %tint_div %22 %26
OpReturn
OpFunctionEnd