From 88f6e9b8f82d3ef42240643b63b89e1fbb600c24 Mon Sep 17 00:00:00 2001 From: Jamie Nicol Date: Fri, 24 Jan 2025 14:07:30 +0000 Subject: [PATCH] [naga hlsl-out] Avoid undefined behaviour for signed integer addition, subtraction, and multiplication Though not explicitly specified one way or the other, we have been informed by DirectX engineers that signed integer overflow may be undefined behaviour in some cases. To avoid this, we therefore bitcast signed operands to unsigned prior to performing addition, subtraction, or multiplication, then bitcast the result back to signed. As signed types are represented as two's complement, this gives the correct result whilst avoid any potential undefined behaviour. Unfortunately HLSL's bitcast functions asint() and asuint() only work for the 32-bit int and uint types. We therefore only apply this workaround for 32-bit signed arithmetic. Support for other bit widths could be added in the future, but extra care must be taken when converting from unsigned to signed to avoid undefined or implemented-defined behaviour. --- naga/src/back/hlsl/writer.rs | 26 ++++++++++++++ naga/tests/out/hlsl/access.hlsl | 8 ++--- naga/tests/out/hlsl/boids.hlsl | 4 +-- naga/tests/out/hlsl/empty-global-name.hlsl | 2 +- naga/tests/out/hlsl/image.hlsl | 8 ++--- naga/tests/out/hlsl/int64.hlsl | 4 +-- naga/tests/out/hlsl/operators.hlsl | 40 +++++++++++----------- 7 files changed, 59 insertions(+), 33 deletions(-) diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index 3e5d2a7f1..27445165e 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -2757,6 +2757,32 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { )?; } Expression::Override(_) => return Err(Error::Override), + // Avoid undefined behaviour for addition, subtraction, and + // multiplication of signed integers by casting operands to + // unsigned, performing the operation, then casting the result back + // to signed. This relies on the asint/asuint functions which only + // work for 32-bit types. + Expression::Binary { + op: + op @ crate::BinaryOperator::Add + | op @ crate::BinaryOperator::Subtract + | op @ crate::BinaryOperator::Multiply, + left, + right, + } if matches!( + func_ctx.resolve_type(expr, &module.types).scalar(), + Some(Scalar { + kind: ScalarKind::Sint, + width: 4 + }) + ) => + { + write!(self.out, "asint(asuint(",)?; + self.write_expr(module, left, func_ctx)?; + write!(self.out, ") {} asuint(", back::binary_operation_str(op))?; + self.write_expr(module, right, func_ctx)?; + write!(self.out, "))")?; + } // All of the multiplication can be expressed as `mul`, // except vector * vector, which needs to use the "*" operator. Expression::Binary { diff --git a/naga/tests/out/hlsl/access.hlsl b/naga/tests/out/hlsl/access.hlsl index d0a1e9b21..167046fb3 100644 --- a/naga/tests/out/hlsl/access.hlsl +++ b/naga/tests/out/hlsl/access.hlsl @@ -139,7 +139,7 @@ void test_matrix_within_struct_accesses() Baz t = ConstructBaz(float3x2((1.0).xx, (2.0).xx, (3.0).xx)); int _e3 = idx; - idx = (_e3 - int(1)); + idx = asint(asuint(_e3) - asuint(int(1))); float3x2 l0_ = GetMatmOnBaz(baz); float2 l1_ = GetMatmOnBaz(baz)[0]; int _e14 = idx; @@ -153,7 +153,7 @@ void test_matrix_within_struct_accesses() int _e38 = idx; float l6_ = GetMatmOnBaz(baz)[_e36][_e38]; int _e51 = idx; - idx = (_e51 + int(1)); + idx = asint(asuint(_e51) + asuint(int(1))); SetMatmOnBaz(t, float3x2((6.0).xx, (5.0).xx, (4.0).xx)); t.m_0 = (9.0).xx; int _e66 = idx; @@ -186,7 +186,7 @@ void test_matrix_within_array_within_struct_accesses() MatCx2InArray t_1 = ConstructMatCx2InArray(ZeroValuearray2_float4x2_()); int _e3 = idx_1; - idx_1 = (_e3 - int(1)); + idx_1 = asint(asuint(_e3) - asuint(int(1))); float4x2 l0_1[2] = ((float4x2[2])nested_mat_cx2_.am); float4x2 l1_1 = ((float4x2)nested_mat_cx2_.am[0]); float2 l2_1 = nested_mat_cx2_.am[0]._0; @@ -201,7 +201,7 @@ void test_matrix_within_array_within_struct_accesses() int _e48 = idx_1; float l7_ = __get_col_of_mat4x2(nested_mat_cx2_.am[0], _e46)[_e48]; int _e55 = idx_1; - idx_1 = (_e55 + int(1)); + idx_1 = asint(asuint(_e55) + asuint(int(1))); t_1.am = (__mat4x2[2])ZeroValuearray2_float4x2_(); t_1.am[0] = (__mat4x2)float4x2((8.0).xx, (7.0).xx, (6.0).xx, (5.0).xx); t_1.am[0]._0 = (9.0).xx; diff --git a/naga/tests/out/hlsl/boids.hlsl b/naga/tests/out/hlsl/boids.hlsl index b6bc4ed79..58d444706 100644 --- a/naga/tests/out/hlsl/boids.hlsl +++ b/naga/tests/out/hlsl/boids.hlsl @@ -73,7 +73,7 @@ void main(uint3 global_invocation_id : SV_DispatchThreadID) float2 _e61 = pos; cMass = (_e60 + _e61); int _e63 = cMassCount; - cMassCount = (_e63 + int(1)); + cMassCount = asint(asuint(_e63) + asuint(int(1))); } float2 _e66 = pos; float2 _e67 = vPos; @@ -92,7 +92,7 @@ void main(uint3 global_invocation_id : SV_DispatchThreadID) float2 _e86 = vel; cVel = (_e85 + _e86); int _e88 = cVelCount; - cVelCount = (_e88 + int(1)); + cVelCount = asint(asuint(_e88) + asuint(int(1))); } } int _e94 = cMassCount; diff --git a/naga/tests/out/hlsl/empty-global-name.hlsl b/naga/tests/out/hlsl/empty-global-name.hlsl index 1d7208ee9..4f45d9474 100644 --- a/naga/tests/out/hlsl/empty-global-name.hlsl +++ b/naga/tests/out/hlsl/empty-global-name.hlsl @@ -7,7 +7,7 @@ RWByteAddressBuffer unnamed : register(u0); void function() { int _e3 = asint(unnamed.Load(0)); - unnamed.Store(0, asuint((_e3 + int(1)))); + unnamed.Store(0, asuint(asint(asuint(_e3) + asuint(int(1))))); return; } diff --git a/naga/tests/out/hlsl/image.hlsl b/naga/tests/out/hlsl/image.hlsl index 57d25f207..1aac36f1b 100644 --- a/naga/tests/out/hlsl/image.hlsl +++ b/naga/tests/out/hlsl/image.hlsl @@ -39,14 +39,14 @@ void main(uint3 local_id : SV_GroupThreadID) uint4 value1_ = image_mipmapped_src.Load(int3(itc, int(local_id.z))); uint4 value2_ = image_multisampled_src.Load(itc, int(local_id.z)); uint4 value4_ = image_storage_src.Load(itc); - uint4 value5_ = image_array_src.Load(int4(itc, local_id.z, (int(local_id.z) + int(1)))); - uint4 value6_ = image_array_src.Load(int4(itc, int(local_id.z), (int(local_id.z) + int(1)))); + uint4 value5_ = image_array_src.Load(int4(itc, local_id.z, asint(asuint(int(local_id.z)) + asuint(int(1))))); + uint4 value6_ = image_array_src.Load(int4(itc, int(local_id.z), asint(asuint(int(local_id.z)) + asuint(int(1))))); uint4 value7_ = image_1d_src.Load(int2(int(local_id.x), int(local_id.z))); uint4 value1u = image_mipmapped_src.Load(int3(uint2(itc), int(local_id.z))); uint4 value2u = image_multisampled_src.Load(uint2(itc), int(local_id.z)); uint4 value4u = image_storage_src.Load(uint2(itc)); - uint4 value5u = image_array_src.Load(int4(uint2(itc), local_id.z, (int(local_id.z) + int(1)))); - uint4 value6u = image_array_src.Load(int4(uint2(itc), int(local_id.z), (int(local_id.z) + int(1)))); + uint4 value5u = image_array_src.Load(int4(uint2(itc), local_id.z, asint(asuint(int(local_id.z)) + asuint(int(1))))); + uint4 value6u = image_array_src.Load(int4(uint2(itc), int(local_id.z), asint(asuint(int(local_id.z)) + asuint(int(1))))); uint4 value7u = image_1d_src.Load(int2(uint(local_id.x), int(local_id.z))); image_dst[itc.x] = ((((value1_ + value2_) + value4_) + value5_) + value6_); image_dst[uint(itc.x)] = ((((value1u + value2u) + value4u) + value5u) + value6u); diff --git a/naga/tests/out/hlsl/int64.hlsl b/naga/tests/out/hlsl/int64.hlsl index 26441380b..84ff64307 100644 --- a/naga/tests/out/hlsl/int64.hlsl +++ b/naga/tests/out/hlsl/int64.hlsl @@ -75,7 +75,7 @@ int64_t int64_function(int64_t x) int _e26 = input_uniform.val_i32_; int64_t _e27 = val; int64_t _e31 = val; - val = (_e31 + int64_t((_e26 + int(_e27)))); + val = (_e31 + int64_t(asint(asuint(_e26) + asuint(int(_e27))))); float _e35 = input_uniform.val_f32_; int64_t _e36 = val; int64_t _e40 = val; @@ -162,7 +162,7 @@ uint64_t uint64_function(uint64_t x_1) int _e26 = input_uniform.val_i32_; uint64_t _e27 = val_1; uint64_t _e31 = val_1; - val_1 = (_e31 + uint64_t((_e26 + int(_e27)))); + val_1 = (_e31 + uint64_t(asint(asuint(_e26) + asuint(int(_e27))))); float _e35 = input_uniform.val_f32_; uint64_t _e36 = val_1; uint64_t _e40 = val_1; diff --git a/naga/tests/out/hlsl/operators.hlsl b/naga/tests/out/hlsl/operators.hlsl index 55562830a..f6b074039 100644 --- a/naga/tests/out/hlsl/operators.hlsl +++ b/naga/tests/out/hlsl/operators.hlsl @@ -13,7 +13,7 @@ float4 builtins() float b1_ = asfloat(int(1)); float4 b2_ = asfloat(v_i32_one); int4 v_i32_zero = int4(int(0), int(0), int(0), int(0)); - return (((((float4(((s1_).xxxx + v_i32_zero)) + s2_) + m1_) + m2_) + (b1_).xxxx) + b2_); + return (((((float4(asint(asuint((s1_).xxxx) + asuint(v_i32_zero))) + s2_) + m1_) + m2_) + (b1_).xxxx) + b2_); } float4 splat(float m, int n) @@ -73,22 +73,22 @@ void arithmetic() float neg0_1 = -(1.0); int2 neg1_1 = -((int(1)).xx); float2 neg2_ = -((1.0).xx); - int add0_ = (int(2) + int(1)); + int add0_ = asint(asuint(int(2)) + asuint(int(1))); uint add1_ = (2u + 1u); float add2_ = (2.0 + 1.0); - int2 add3_ = ((int(2)).xx + (int(1)).xx); + int2 add3_ = asint(asuint((int(2)).xx) + asuint((int(1)).xx)); uint3 add4_ = ((2u).xxx + (1u).xxx); float4 add5_ = ((2.0).xxxx + (1.0).xxxx); - int sub0_ = (int(2) - int(1)); + int sub0_ = asint(asuint(int(2)) - asuint(int(1))); uint sub1_ = (2u - 1u); float sub2_ = (2.0 - 1.0); - int2 sub3_ = ((int(2)).xx - (int(1)).xx); + int2 sub3_ = asint(asuint((int(2)).xx) - asuint((int(1)).xx)); uint3 sub4_ = ((2u).xxx - (1u).xxx); float4 sub5_ = ((2.0).xxxx - (1.0).xxxx); - int mul0_ = (int(2) * int(1)); + int mul0_ = asint(asuint(int(2)) * asuint(int(1))); uint mul1_ = (2u * 1u); float mul2_ = (2.0 * 1.0); - int2 mul3_ = ((int(2)).xx * (int(1)).xx); + int2 mul3_ = asint(asuint((int(2)).xx) * asuint((int(1)).xx)); uint3 mul4_ = ((2u).xxx * (1u).xxx); float4 mul5_ = ((2.0).xxxx * (1.0).xxxx); int div0_ = (int(2) / int(1)); @@ -104,20 +104,20 @@ void arithmetic() uint3 rem4_ = ((2u).xxx % (1u).xxx); float4 rem5_ = fmod((2.0).xxxx, (1.0).xxxx); { - int2 add0_1 = ((int(2)).xx + (int(1)).xx); - int2 add1_1 = ((int(2)).xx + (int(1)).xx); + int2 add0_1 = asint(asuint((int(2)).xx) + asuint((int(1)).xx)); + int2 add1_1 = asint(asuint((int(2)).xx) + asuint((int(1)).xx)); uint2 add2_1 = ((2u).xx + (1u).xx); uint2 add3_1 = ((2u).xx + (1u).xx); float2 add4_1 = ((2.0).xx + (1.0).xx); float2 add5_1 = ((2.0).xx + (1.0).xx); - int2 sub0_1 = ((int(2)).xx - (int(1)).xx); - int2 sub1_1 = ((int(2)).xx - (int(1)).xx); + int2 sub0_1 = asint(asuint((int(2)).xx) - asuint((int(1)).xx)); + int2 sub1_1 = asint(asuint((int(2)).xx) - asuint((int(1)).xx)); uint2 sub2_1 = ((2u).xx - (1u).xx); uint2 sub3_1 = ((2u).xx - (1u).xx); float2 sub4_1 = ((2.0).xx - (1.0).xx); float2 sub5_1 = ((2.0).xx - (1.0).xx); - int2 mul0_1 = ((int(2)).xx * int(1)); - int2 mul1_1 = (int(2) * (int(1)).xx); + int2 mul0_1 = asint(asuint((int(2)).xx) * asuint(int(1))); + int2 mul1_1 = asint(asuint(int(2)) * asuint((int(1)).xx)); uint2 mul2_1 = ((2u).xx * 1u); uint2 mul3_1 = (2u * (1u).xx); float2 mul4_1 = ((2.0).xx * 1.0); @@ -226,12 +226,12 @@ void assignment() a_1 = int(1); int _e5 = a_1; - a_1 = (_e5 + int(1)); + a_1 = asint(asuint(_e5) + asuint(int(1))); int _e7 = a_1; - a_1 = (_e7 - int(1)); + a_1 = asint(asuint(_e7) - asuint(int(1))); int _e9 = a_1; int _e10 = a_1; - a_1 = (_e10 * _e9); + a_1 = asint(asuint(_e10) * asuint(_e9)); int _e12 = a_1; int _e13 = a_1; a_1 = (_e13 / _e12); @@ -248,13 +248,13 @@ void assignment() int _e25 = a_1; a_1 = (_e25 >> 1u); int _e28 = a_1; - a_1 = (_e28 + int(1)); + a_1 = asint(asuint(_e28) + asuint(int(1))); int _e31 = a_1; - a_1 = (_e31 - int(1)); + a_1 = asint(asuint(_e31) - asuint(int(1))); int _e37 = vec0_[int(1)]; - vec0_[int(1)] = (_e37 + int(1)); + vec0_[int(1)] = asint(asuint(_e37) + asuint(int(1))); int _e41 = vec0_[int(1)]; - vec0_[int(1)] = (_e41 - int(1)); + vec0_[int(1)] = asint(asuint(_e41) - asuint(int(1))); return; }