mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-18 01:15:39 +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;
|
||||
}
|
||||
|
||||
int tint_div(int lhs, int rhs) {
|
||||
return (lhs / (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store(0u, asuint((asint(v.Load(0u)) / 2)));
|
||||
const int tint_symbol = tint_div(asint(v.Load(0u)), 2);
|
||||
v.Store(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -3,8 +3,13 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int tint_div(int lhs, int rhs) {
|
||||
return (lhs / (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store(0u, asuint((asint(v.Load(0u)) / 2)));
|
||||
const int tint_symbol = tint_div(asint(v.Load(0u)), 2);
|
||||
v.Store(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;
|
||||
}
|
||||
int tint_div(int lhs, int rhs) {
|
||||
return (lhs / (bool(uint((rhs == 0)) | uint(bool(uint((lhs == -2147483648)) & uint((rhs == -1))))) ? 1 : rhs));
|
||||
}
|
||||
|
||||
struct S {
|
||||
int a;
|
||||
};
|
||||
@@ -13,6 +17,7 @@ layout(binding = 0, std430) buffer v_block_ssbo {
|
||||
} v;
|
||||
|
||||
void foo() {
|
||||
v.inner.a = (v.inner.a / 2);
|
||||
int tint_symbol = tint_div(v.inner.a, 2);
|
||||
v.inner.a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#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)))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
/* 0x0000 */ int a;
|
||||
};
|
||||
|
||||
void foo(device S* const tint_symbol) {
|
||||
(*(tint_symbol)).a = ((*(tint_symbol)).a / 2);
|
||||
void foo(device S* const tint_symbol_1) {
|
||||
int const tint_symbol = tint_div((*(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: 20
|
||||
; Bound: 37
|
||||
; 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
|
||||
@@ -26,6 +29,12 @@
|
||||
%v = OpVariable %_ptr_StorageBuffer_v_block StorageBuffer
|
||||
%void = OpTypeVoid
|
||||
%6 = OpTypeFunction %void
|
||||
%10 = OpTypeFunction %int %int %int
|
||||
%16 = OpConstantNull %int
|
||||
%bool = OpTypeBool
|
||||
%int_n2147483648 = OpConstant %int -2147483648
|
||||
%int_n1 = OpConstant %int -1
|
||||
%int_1 = OpConstant %int 1
|
||||
%uint = OpTypeInt 32 0
|
||||
%uint_0 = OpConstant %uint 0
|
||||
%_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
|
||||
@@ -34,12 +43,25 @@
|
||||
%9 = OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%tint_div = OpFunction %int None %10
|
||||
%lhs = OpFunctionParameter %int
|
||||
%rhs = OpFunctionParameter %int
|
||||
%14 = OpLabel
|
||||
%17 = OpIEqual %bool %rhs %16
|
||||
%20 = OpIEqual %bool %lhs %int_n2147483648
|
||||
%22 = OpIEqual %bool %rhs %int_n1
|
||||
%23 = OpLogicalAnd %bool %20 %22
|
||||
%24 = OpLogicalOr %bool %17 %23
|
||||
%15 = OpSelect %int %24 %int_1 %rhs
|
||||
%26 = OpSDiv %int %lhs %15
|
||||
OpReturnValue %26
|
||||
OpFunctionEnd
|
||||
%foo = OpFunction %void None %6
|
||||
%11 = OpLabel
|
||||
%15 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
%16 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
%17 = OpLoad %int %16
|
||||
%19 = OpSDiv %int %17 %int_2
|
||||
OpStore %15 %19
|
||||
%28 = OpLabel
|
||||
%33 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
%34 = OpLoad %int %33
|
||||
%29 = OpFunctionCall %int %tint_div %34 %int_2
|
||||
%36 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
OpStore %36 %29
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
@@ -3,8 +3,13 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int tint_mod(int lhs, int rhs) {
|
||||
return (lhs % (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store(0u, asuint((asint(v.Load(0u)) % 2)));
|
||||
const int tint_symbol = tint_mod(asint(v.Load(0u)), 2);
|
||||
v.Store(0u, asuint(tint_symbol));
|
||||
}
|
||||
|
||||
@@ -3,8 +3,13 @@ void unused_entry_point() {
|
||||
return;
|
||||
}
|
||||
|
||||
int tint_mod(int lhs, int rhs) {
|
||||
return (lhs % (((rhs == 0) | ((lhs == -2147483648) & (rhs == -1))) ? 1 : rhs));
|
||||
}
|
||||
|
||||
RWByteAddressBuffer v : register(u0, space0);
|
||||
|
||||
void foo() {
|
||||
v.Store(0u, asuint((asint(v.Load(0u)) % 2)));
|
||||
const int tint_symbol = tint_mod(asint(v.Load(0u)), 2);
|
||||
v.Store(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;
|
||||
}
|
||||
int tint_mod(int lhs, int rhs) {
|
||||
return (lhs % (bool(uint((rhs == 0)) | uint(bool(uint((lhs == -2147483648)) & uint((rhs == -1))))) ? 1 : rhs));
|
||||
}
|
||||
|
||||
struct S {
|
||||
int a;
|
||||
};
|
||||
@@ -13,6 +17,7 @@ layout(binding = 0, std430) buffer v_block_ssbo {
|
||||
} v;
|
||||
|
||||
void foo() {
|
||||
v.inner.a = (v.inner.a % 2);
|
||||
int tint_symbol = tint_mod(v.inner.a, 2);
|
||||
v.inner.a = tint_symbol;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#include <metal_stdlib>
|
||||
|
||||
using namespace metal;
|
||||
int tint_mod(int lhs, int rhs) {
|
||||
return (lhs % select(rhs, 1, bool((rhs == 0) | bool((lhs == (-2147483647 - 1)) & (rhs == -1)))));
|
||||
}
|
||||
|
||||
struct S {
|
||||
/* 0x0000 */ int a;
|
||||
};
|
||||
|
||||
void foo(device S* const tint_symbol) {
|
||||
(*(tint_symbol)).a = ((*(tint_symbol)).a % 2);
|
||||
void foo(device S* const tint_symbol_1) {
|
||||
int 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: 20
|
||||
; Bound: 37
|
||||
; 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
|
||||
@@ -26,6 +29,12 @@
|
||||
%v = OpVariable %_ptr_StorageBuffer_v_block StorageBuffer
|
||||
%void = OpTypeVoid
|
||||
%6 = OpTypeFunction %void
|
||||
%10 = OpTypeFunction %int %int %int
|
||||
%16 = OpConstantNull %int
|
||||
%bool = OpTypeBool
|
||||
%int_n2147483648 = OpConstant %int -2147483648
|
||||
%int_n1 = OpConstant %int -1
|
||||
%int_1 = OpConstant %int 1
|
||||
%uint = OpTypeInt 32 0
|
||||
%uint_0 = OpConstant %uint 0
|
||||
%_ptr_StorageBuffer_int = OpTypePointer StorageBuffer %int
|
||||
@@ -34,12 +43,25 @@
|
||||
%9 = OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%tint_mod = OpFunction %int None %10
|
||||
%lhs = OpFunctionParameter %int
|
||||
%rhs = OpFunctionParameter %int
|
||||
%14 = OpLabel
|
||||
%17 = OpIEqual %bool %rhs %16
|
||||
%20 = OpIEqual %bool %lhs %int_n2147483648
|
||||
%22 = OpIEqual %bool %rhs %int_n1
|
||||
%23 = OpLogicalAnd %bool %20 %22
|
||||
%24 = OpLogicalOr %bool %17 %23
|
||||
%15 = OpSelect %int %24 %int_1 %rhs
|
||||
%26 = OpSMod %int %lhs %15
|
||||
OpReturnValue %26
|
||||
OpFunctionEnd
|
||||
%foo = OpFunction %void None %6
|
||||
%11 = OpLabel
|
||||
%15 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
%16 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
%17 = OpLoad %int %16
|
||||
%19 = OpSMod %int %17 %int_2
|
||||
OpStore %15 %19
|
||||
%28 = OpLabel
|
||||
%33 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
%34 = OpLoad %int %33
|
||||
%29 = OpFunctionCall %int %tint_mod %34 %int_2
|
||||
%36 = OpAccessChain %_ptr_StorageBuffer_int %v %uint_0 %uint_0
|
||||
OpStore %36 %29
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
|
||||
Reference in New Issue
Block a user