mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-10 05:57:51 +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,11 @@
|
||||
int3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
int3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#version 310 es
|
||||
|
||||
ivec3 tint_mod(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_mod(a, b);
|
||||
}
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
int3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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_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 %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 = OpSMod %v3int %10 %14
|
||||
%34 = OpConstantComposite %v3int %int_4 %int_5 %int_6
|
||||
%tint_mod = 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 = OpSMod %v3int %lhs %8
|
||||
OpReturnValue %23
|
||||
OpFunctionEnd
|
||||
%f = OpFunction %void None %24
|
||||
%27 = OpLabel
|
||||
%35 = OpFunctionCall %v3int %tint_mod %30 %34
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
uint3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
uint3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#version 310 es
|
||||
|
||||
uvec3 tint_mod(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_mod(a, b);
|
||||
}
|
||||
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
uint3 tint_mod(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_mod(a, b);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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_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 %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 = OpUMod %v3uint %10 %14
|
||||
%26 = OpConstantComposite %v3uint %uint_4 %uint_5 %uint_6
|
||||
%tint_mod = OpFunction %v3uint None %1
|
||||
%lhs = OpFunctionParameter %v3uint
|
||||
%rhs = OpFunctionParameter %v3uint
|
||||
%7 = OpLabel
|
||||
%10 = OpIEqual %v3bool %rhs %9
|
||||
%8 = OpSelect %v3uint %10 %14 %rhs
|
||||
%15 = OpUMod %v3uint %lhs %8
|
||||
OpReturnValue %15
|
||||
OpFunctionEnd
|
||||
%f = OpFunction %void None %16
|
||||
%19 = OpLabel
|
||||
%27 = OpFunctionCall %v3uint %tint_mod %22 %26
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
Reference in New Issue
Block a user