mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-10 14:08:04 +00:00
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:
committed by
Dawn LUCI CQ
parent
9418152d08
commit
46ee63933c
@@ -1,7 +1,12 @@
|
||||
int3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
int3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#version 310 es
|
||||
|
||||
ivec3 tint_mod(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_mod(a, b);
|
||||
}
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
int3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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_mod "tint_mod"
|
||||
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 = OpSMod %v3int %10 %16
|
||||
%tint_mod = 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 = OpSMod %v3int %lhs %9
|
||||
OpReturnValue %24
|
||||
OpFunctionEnd
|
||||
%f = OpFunction %void None %25
|
||||
%28 = OpLabel
|
||||
%33 = OpFunctionCall %v3int %tint_mod %31 %int_4
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
uint3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
uint3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#version 310 es
|
||||
|
||||
uvec3 tint_mod(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_mod(a, b);
|
||||
}
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
uint3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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_mod "tint_mod"
|
||||
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 = OpUMod %v3uint %10 %16
|
||||
%tint_mod = 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 = OpUMod %v3uint %lhs %9
|
||||
OpReturnValue %16
|
||||
OpFunctionEnd
|
||||
%f = OpFunction %void None %17
|
||||
%20 = OpLabel
|
||||
%25 = OpFunctionCall %v3uint %tint_mod %23 %uint_4
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
Reference in New Issue
Block a user