mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-14 15:46:28 +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
@@ -3,8 +3,13 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int4 tint_div(int4 lhs, int4 rhs) {
|
||||
return (lhs / (((rhs == (0).xxxx) | ((lhs == (-2147483648).xxxx) & (rhs == (-1).xxxx))) ? (1).xxxx : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store4(0u, asuint((asint(v.Load4(0u)) / (2).xxxx)));
|
||||
const int4 tint_symbol = tint_div(asint(v.Load4(0u)), (2).xxxx);
|
||||
v.Store4(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -3,8 +3,13 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int4 tint_div(int4 lhs, int4 rhs) {
|
||||
return (lhs / (((rhs == (0).xxxx) | ((lhs == (-2147483648).xxxx) & (rhs == (-1).xxxx))) ? (1).xxxx : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store4(0u, asuint((asint(v.Load4(0u)) / (2).xxxx)));
|
||||
const int4 tint_symbol = tint_div(asint(v.Load4(0u)), (2).xxxx);
|
||||
v.Store4(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,10 @@ layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
ivec4 tint_div(ivec4 lhs, ivec4 rhs) {
|
||||
return (lhs / mix(rhs, ivec4(1), bvec4(uvec4(equal(rhs, ivec4(0))) | uvec4(bvec4(uvec4(equal(lhs, ivec4(-2147483648))) & uvec4(equal(rhs, ivec4(-1))))))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
ivec4 a;
|
||||
};
|
||||
@@ -13,6 +17,7 @@ layout(binding = 0, std430) buffer v_block_ssbo {
|
||||
} v;
|
||||
|
||||
void foo() {
|
||||
v.inner.a = (v.inner.a / ivec4(2));
|
||||
ivec4 tint_symbol = tint_div(v.inner.a, ivec4(2));
|
||||
v.inner.a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
int4 tint_div(int4 lhs, int4 rhs) {
|
||||
return (lhs / select(rhs, int4(1), ((rhs == int4(0)) | ((lhs == int4((-2147483647 - 1))) & (rhs == int4(-1))))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
/* 0x0000 */ int4 a;
|
||||
};
|
||||
|
||||
void foo(device S* const tint_symbol) {
|
||||
(*(tint_symbol)).a = ((*(tint_symbol)).a / int4(2));
|
||||
void foo(device S* const tint_symbol_1) {
|
||||
int4 const tint_symbol = tint_div((*(tint_symbol_1)).a, int4(2));
|
||||
(*(tint_symbol_1)).a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
; SPIR-V
|
||||
; Version: 1.3
|
||||
; Generator: Google Tint Compiler; 0
|
||||
; Bound: 22
|
||||
; Bound: 43
|
||||
; Schema: 0
|
||||
OpCapability Shader
|
||||
OpMemoryModel Logical GLSL450
|
||||
@@ -13,6 +13,9 @@
|
||||
OpMemberName %S 0 "a"
|
||||
OpName %v "v"
|
||||
OpName %unused_entry_point "unused_entry_point"
|
||||
OpName %tint_div "tint_div"
|
||||
OpName %lhs "lhs"
|
||||
OpName %rhs "rhs"
|
||||
OpName %foo "foo"
|
||||
OpDecorate %v_block Block
|
||||
OpMemberDecorate %v_block 0 Offset 0
|
||||
@@ -27,21 +30,44 @@
|
||||
%v = OpVariable %_ptr_StorageBuffer_v_block StorageBuffer
|
||||
%void = OpTypeVoid
|
||||
%7 = OpTypeFunction %void
|
||||
%11 = OpTypeFunction %v4int %v4int %v4int
|
||||
%17 = OpConstantNull %v4int
|
||||
%bool = OpTypeBool
|
||||
%v4bool = OpTypeVector %bool 4
|
||||
%int_n2147483648 = OpConstant %int -2147483648
|
||||
%22 = OpConstantComposite %v4int %int_n2147483648 %int_n2147483648 %int_n2147483648 %int_n2147483648
|
||||
%int_n1 = OpConstant %int -1
|
||||
%25 = OpConstantComposite %v4int %int_n1 %int_n1 %int_n1 %int_n1
|
||||
%int_1 = OpConstant %int 1
|
||||
%30 = OpConstantComposite %v4int %int_1 %int_1 %int_1 %int_1
|
||||
%uint = OpTypeInt 32 0
|
||||
%uint_0 = OpConstant %uint 0
|
||||
%_ptr_StorageBuffer_v4int = OpTypePointer StorageBuffer %v4int
|
||||
%int_2 = OpConstant %int 2
|
||||
%20 = OpConstantComposite %v4int %int_2 %int_2 %int_2 %int_2
|
||||
%41 = OpConstantComposite %v4int %int_2 %int_2 %int_2 %int_2
|
||||
%unused_entry_point = OpFunction %void None %7
|
||||
%10 = OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%tint_div = OpFunction %v4int None %11
|
||||
%lhs = OpFunctionParameter %v4int
|
||||
%rhs = OpFunctionParameter %v4int
|
||||
%15 = OpLabel
|
||||
%18 = OpIEqual %v4bool %rhs %17
|
||||
%23 = OpIEqual %v4bool %lhs %22
|
||||
%26 = OpIEqual %v4bool %rhs %25
|
||||
%27 = OpLogicalAnd %v4bool %23 %26
|
||||
%28 = OpLogicalOr %v4bool %18 %27
|
||||
%16 = OpSelect %v4int %28 %30 %rhs
|
||||
%31 = OpSDiv %v4int %lhs %16
|
||||
OpReturnValue %31
|
||||
OpFunctionEnd
|
||||
%foo = OpFunction %void None %7
|
||||
%12 = OpLabel
|
||||
%16 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%17 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%18 = OpLoad %v4int %17
|
||||
%21 = OpSDiv %v4int %18 %20
|
||||
OpStore %16 %21
|
||||
%33 = OpLabel
|
||||
%38 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%39 = OpLoad %v4int %38
|
||||
%34 = OpFunctionCall %v4int %tint_div %39 %41
|
||||
%42 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
OpStore %42 %34
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
@@ -3,8 +3,14 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int4 tint_mod(int4 lhs, int rhs) {
|
||||
const int4 r = int4((rhs).xxxx);
|
||||
return (lhs % (((r == (0).xxxx) | ((lhs == (-2147483648).xxxx) & (r == (-1).xxxx))) ? (1).xxxx : r));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store4(0u, asuint((asint(v.Load4(0u)) % 2)));
|
||||
const int4 tint_symbol = tint_mod(asint(v.Load4(0u)), 2);
|
||||
v.Store4(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -3,8 +3,14 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int4 tint_mod(int4 lhs, int rhs) {
|
||||
const int4 r = int4((rhs).xxxx);
|
||||
return (lhs % (((r == (0).xxxx) | ((lhs == (-2147483648).xxxx) & (r == (-1).xxxx))) ? (1).xxxx : r));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store4(0u, asuint((asint(v.Load4(0u)) % 2)));
|
||||
const int4 tint_symbol = tint_mod(asint(v.Load4(0u)), 2);
|
||||
v.Store4(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@ layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
ivec4 tint_mod(ivec4 lhs, int rhs) {
|
||||
ivec4 r = ivec4(rhs);
|
||||
return (lhs % mix(r, ivec4(1), bvec4(uvec4(equal(r, ivec4(0))) | uvec4(bvec4(uvec4(equal(lhs, ivec4(-2147483648))) & uvec4(equal(r, ivec4(-1))))))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
ivec4 a;
|
||||
};
|
||||
@@ -13,6 +18,7 @@ layout(binding = 0, std430) buffer v_block_ssbo {
|
||||
} v;
|
||||
|
||||
void foo() {
|
||||
v.inner.a = (v.inner.a % 2);
|
||||
ivec4 tint_symbol = tint_mod(v.inner.a, 2);
|
||||
v.inner.a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
int4 tint_mod(int4 lhs, int rhs) {
|
||||
int4 const r = int4(rhs);
|
||||
return (lhs % select(r, int4(1), ((r == int4(0)) | ((lhs == int4((-2147483647 - 1))) & (r == int4(-1))))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
/* 0x0000 */ int4 a;
|
||||
};
|
||||
|
||||
void foo(device S* const tint_symbol) {
|
||||
(*(tint_symbol)).a = ((*(tint_symbol)).a % 2);
|
||||
void foo(device S* const tint_symbol_1) {
|
||||
int4 const tint_symbol = tint_mod((*(tint_symbol_1)).a, 2);
|
||||
(*(tint_symbol_1)).a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
; SPIR-V
|
||||
; Version: 1.3
|
||||
; Generator: Google Tint Compiler; 0
|
||||
; Bound: 25
|
||||
; Bound: 43
|
||||
; Schema: 0
|
||||
OpCapability Shader
|
||||
OpMemoryModel Logical GLSL450
|
||||
@@ -13,6 +13,9 @@
|
||||
OpMemberName %S 0 "a"
|
||||
OpName %v "v"
|
||||
OpName %unused_entry_point "unused_entry_point"
|
||||
OpName %tint_mod "tint_mod"
|
||||
OpName %lhs "lhs"
|
||||
OpName %rhs "rhs"
|
||||
OpName %foo "foo"
|
||||
OpDecorate %v_block Block
|
||||
OpMemberDecorate %v_block 0 Offset 0
|
||||
@@ -27,24 +30,44 @@
|
||||
%v = OpVariable %_ptr_StorageBuffer_v_block StorageBuffer
|
||||
%void = OpTypeVoid
|
||||
%7 = OpTypeFunction %void
|
||||
%11 = OpTypeFunction %v4int %v4int %int
|
||||
%18 = OpConstantNull %v4int
|
||||
%bool = OpTypeBool
|
||||
%v4bool = OpTypeVector %bool 4
|
||||
%int_n2147483648 = OpConstant %int -2147483648
|
||||
%23 = OpConstantComposite %v4int %int_n2147483648 %int_n2147483648 %int_n2147483648 %int_n2147483648
|
||||
%int_n1 = OpConstant %int -1
|
||||
%26 = OpConstantComposite %v4int %int_n1 %int_n1 %int_n1 %int_n1
|
||||
%int_1 = OpConstant %int 1
|
||||
%31 = OpConstantComposite %v4int %int_1 %int_1 %int_1 %int_1
|
||||
%uint = OpTypeInt 32 0
|
||||
%uint_0 = OpConstant %uint 0
|
||||
%_ptr_StorageBuffer_v4int = OpTypePointer StorageBuffer %v4int
|
||||
%int_2 = OpConstant %int 2
|
||||
%_ptr_Function_v4int = OpTypePointer Function %v4int
|
||||
%23 = OpConstantNull %v4int
|
||||
%unused_entry_point = OpFunction %void None %7
|
||||
%10 = OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%tint_mod = OpFunction %v4int None %11
|
||||
%lhs = OpFunctionParameter %v4int
|
||||
%rhs = OpFunctionParameter %int
|
||||
%15 = OpLabel
|
||||
%16 = OpCompositeConstruct %v4int %rhs %rhs %rhs %rhs
|
||||
%19 = OpIEqual %v4bool %16 %18
|
||||
%24 = OpIEqual %v4bool %lhs %23
|
||||
%27 = OpIEqual %v4bool %16 %26
|
||||
%28 = OpLogicalAnd %v4bool %24 %27
|
||||
%29 = OpLogicalOr %v4bool %19 %28
|
||||
%17 = OpSelect %v4int %29 %31 %16
|
||||
%32 = OpSMod %v4int %lhs %17
|
||||
OpReturnValue %32
|
||||
OpFunctionEnd
|
||||
%foo = OpFunction %void None %7
|
||||
%12 = OpLabel
|
||||
%21 = OpVariable %_ptr_Function_v4int Function %23
|
||||
%16 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%17 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%18 = OpLoad %v4int %17
|
||||
%24 = OpCompositeConstruct %v4int %int_2 %int_2 %int_2 %int_2
|
||||
%20 = OpSMod %v4int %18 %24
|
||||
OpStore %16 %20
|
||||
%34 = OpLabel
|
||||
%39 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%40 = OpLoad %v4int %39
|
||||
%35 = OpFunctionCall %v4int %tint_mod %40 %int_2
|
||||
%42 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
OpStore %42 %35
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
@@ -3,8 +3,13 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int4 tint_mod(int4 lhs, int4 rhs) {
|
||||
return (lhs % (((rhs == (0).xxxx) | ((lhs == (-2147483648).xxxx) & (rhs == (-1).xxxx))) ? (1).xxxx : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store4(0u, asuint((asint(v.Load4(0u)) % (2).xxxx)));
|
||||
const int4 tint_symbol = tint_mod(asint(v.Load4(0u)), (2).xxxx);
|
||||
v.Store4(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -3,8 +3,13 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int4 tint_mod(int4 lhs, int4 rhs) {
|
||||
return (lhs % (((rhs == (0).xxxx) | ((lhs == (-2147483648).xxxx) & (rhs == (-1).xxxx))) ? (1).xxxx : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store4(0u, asuint((asint(v.Load4(0u)) % (2).xxxx)));
|
||||
const int4 tint_symbol = tint_mod(asint(v.Load4(0u)), (2).xxxx);
|
||||
v.Store4(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -4,6 +4,10 @@ layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
ivec4 tint_mod(ivec4 lhs, ivec4 rhs) {
|
||||
return (lhs % mix(rhs, ivec4(1), bvec4(uvec4(equal(rhs, ivec4(0))) | uvec4(bvec4(uvec4(equal(lhs, ivec4(-2147483648))) & uvec4(equal(rhs, ivec4(-1))))))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
ivec4 a;
|
||||
};
|
||||
@@ -13,6 +17,7 @@ layout(binding = 0, std430) buffer v_block_ssbo {
|
||||
} v;
|
||||
|
||||
void foo() {
|
||||
v.inner.a = (v.inner.a % ivec4(2));
|
||||
ivec4 tint_symbol = tint_mod(v.inner.a, ivec4(2));
|
||||
v.inner.a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
int4 tint_mod(int4 lhs, int4 rhs) {
|
||||
return (lhs % select(rhs, int4(1), ((rhs == int4(0)) | ((lhs == int4((-2147483647 - 1))) & (rhs == int4(-1))))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
/* 0x0000 */ int4 a;
|
||||
};
|
||||
|
||||
void foo(device S* const tint_symbol) {
|
||||
(*(tint_symbol)).a = ((*(tint_symbol)).a % int4(2));
|
||||
void foo(device S* const tint_symbol_1) {
|
||||
int4 const tint_symbol = tint_mod((*(tint_symbol_1)).a, int4(2));
|
||||
(*(tint_symbol_1)).a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
; SPIR-V
|
||||
; Version: 1.3
|
||||
; Generator: Google Tint Compiler; 0
|
||||
; Bound: 22
|
||||
; Bound: 43
|
||||
; Schema: 0
|
||||
OpCapability Shader
|
||||
OpMemoryModel Logical GLSL450
|
||||
@@ -13,6 +13,9 @@
|
||||
OpMemberName %S 0 "a"
|
||||
OpName %v "v"
|
||||
OpName %unused_entry_point "unused_entry_point"
|
||||
OpName %tint_mod "tint_mod"
|
||||
OpName %lhs "lhs"
|
||||
OpName %rhs "rhs"
|
||||
OpName %foo "foo"
|
||||
OpDecorate %v_block Block
|
||||
OpMemberDecorate %v_block 0 Offset 0
|
||||
@@ -27,21 +30,44 @@
|
||||
%v = OpVariable %_ptr_StorageBuffer_v_block StorageBuffer
|
||||
%void = OpTypeVoid
|
||||
%7 = OpTypeFunction %void
|
||||
%11 = OpTypeFunction %v4int %v4int %v4int
|
||||
%17 = OpConstantNull %v4int
|
||||
%bool = OpTypeBool
|
||||
%v4bool = OpTypeVector %bool 4
|
||||
%int_n2147483648 = OpConstant %int -2147483648
|
||||
%22 = OpConstantComposite %v4int %int_n2147483648 %int_n2147483648 %int_n2147483648 %int_n2147483648
|
||||
%int_n1 = OpConstant %int -1
|
||||
%25 = OpConstantComposite %v4int %int_n1 %int_n1 %int_n1 %int_n1
|
||||
%int_1 = OpConstant %int 1
|
||||
%30 = OpConstantComposite %v4int %int_1 %int_1 %int_1 %int_1
|
||||
%uint = OpTypeInt 32 0
|
||||
%uint_0 = OpConstant %uint 0
|
||||
%_ptr_StorageBuffer_v4int = OpTypePointer StorageBuffer %v4int
|
||||
%int_2 = OpConstant %int 2
|
||||
%20 = OpConstantComposite %v4int %int_2 %int_2 %int_2 %int_2
|
||||
%41 = OpConstantComposite %v4int %int_2 %int_2 %int_2 %int_2
|
||||
%unused_entry_point = OpFunction %void None %7
|
||||
%10 = OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%tint_mod = OpFunction %v4int None %11
|
||||
%lhs = OpFunctionParameter %v4int
|
||||
%rhs = OpFunctionParameter %v4int
|
||||
%15 = OpLabel
|
||||
%18 = OpIEqual %v4bool %rhs %17
|
||||
%23 = OpIEqual %v4bool %lhs %22
|
||||
%26 = OpIEqual %v4bool %rhs %25
|
||||
%27 = OpLogicalAnd %v4bool %23 %26
|
||||
%28 = OpLogicalOr %v4bool %18 %27
|
||||
%16 = OpSelect %v4int %28 %30 %rhs
|
||||
%31 = OpSMod %v4int %lhs %16
|
||||
OpReturnValue %31
|
||||
OpFunctionEnd
|
||||
%foo = OpFunction %void None %7
|
||||
%12 = OpLabel
|
||||
%16 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%17 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%18 = OpLoad %v4int %17
|
||||
%21 = OpSMod %v4int %18 %20
|
||||
OpStore %16 %21
|
||||
%33 = OpLabel
|
||||
%38 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
%39 = OpLoad %v4int %38
|
||||
%34 = OpFunctionCall %v4int %tint_mod %39 %41
|
||||
%42 = OpAccessChain %_ptr_StorageBuffer_v4int %v %uint_0 %uint_0
|
||||
OpStore %42 %34
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
Reference in New Issue
Block a user