From 3dc03bc23f5c0f2275b7545d604a38bbe5d0c2d0 Mon Sep 17 00:00:00 2001 From: Brendon Daugherty Date: Tue, 3 Mar 2026 07:06:18 -0700 Subject: [PATCH] fix(naga): Use wrapping arithmetic for dot on concrete ints (#9142) --- CHANGELOG.md | 1 + cts_runner/test.lst | 9 +++++++++ naga/src/proc/constant_evaluator.rs | 16 ++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8f911876..42c297d33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -186,6 +186,7 @@ depth_stencil: Some(wgpu::DepthStencilState::stencil( - Fixed constant evaluation for `sign()` builtin to return zero when the argument is zero. By @mandryskowski in [#8942](https://github.com/gfx-rs/wgpu/pull/8942). - Allow array generation to compile with the macOS 10.12 Metal compiler. By @madsmtm in [#8953](https://github.com/gfx-rs/wgpu/pull/8953) - Naga now detects bitwise shifts by a constant exceeding the operand bit width at compile time, and disallows scalar-by-vector and vector-by-scalar shifts in constant evaluation. By @andyleiserson in [#8907](https://github.com/gfx-rs/wgpu/pull/8907). +- Naga uses wrapping arithmetic when evaluating dot products on concrete integer types (`u32` and `i32`). By @BKDaugherty in [#9142](https://github.com/gfx-rs/wgpu/pull/9142). #### Validation diff --git a/cts_runner/test.lst b/cts_runner/test.lst index 09cd70645..fee1de757 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -299,6 +299,15 @@ webgpu:api,validation,texture,rg11b10ufloat_renderable:* webgpu:shader,execution,expression,call,builtin,atan2:f16:* webgpu:shader,execution,expression,call,builtin,atan2:f32:* +webgpu:shader,execution,expression,call,builtin,dot:abstract_int_vec2:* +webgpu:shader,execution,expression,call,builtin,dot:abstract_int_vec3:* +webgpu:shader,execution,expression,call,builtin,dot:abstract_int_vec4:* +webgpu:shader,execution,expression,call,builtin,dot:i32_vec2:* +webgpu:shader,execution,expression,call,builtin,dot:i32_vec3:* +webgpu:shader,execution,expression,call,builtin,dot:i32_vec4:* +webgpu:shader,execution,expression,call,builtin,dot:u32_vec2:* +webgpu:shader,execution,expression,call,builtin,dot:u32_vec3:* +webgpu:shader,execution,expression,call,builtin,dot:u32_vec4:* //FAIL: webgpu:shader,execution,expression,call,builtin,select:* // - Fails with `const`/abstract int cases on all platforms because of . // - Fails with `vec3` & `f16` cases on macOS because of . diff --git a/naga/src/proc/constant_evaluator.rs b/naga/src/proc/constant_evaluator.rs index e30cc33ef..6c7e14daa 100644 --- a/naga/src/proc/constant_evaluator.rs +++ b/naga/src/proc/constant_evaluator.rs @@ -1718,7 +1718,7 @@ impl<'a> ConstantEvaluator<'a> { return Err(ConstantEvaluatorError::InvalidMathArg); } - fn int_dot

(a: &[P], b: &[P]) -> Result + fn int_dot_checked

(a: &[P], b: &[P]) -> Result where P: num_traits::PrimInt + num_traits::CheckedAdd + num_traits::CheckedMul, { @@ -1737,9 +1737,21 @@ impl<'a> ConstantEvaluator<'a> { )) } + fn int_dot_wrapping

(a: &[P], b: &[P]) -> P + where + P: num_traits::PrimInt + num_traits::WrappingAdd + num_traits::WrappingMul, + { + a.iter() + .zip(b.iter()) + .map(|(&aa, bb)| aa.wrapping_mul(bb)) + .fold(P::zero(), |acc, x| acc.wrapping_add(&x)) + } + let result = match_literal_vector!(match (e1, e2) => Literal { Float => |e1, e2| { e1.iter().zip(e2.iter()).map(|(&aa, &bb)| aa * bb).sum() }, - Integer => |e1, e2| { int_dot(e1, e2)? }, + AbstractInt => |e1, e2 | { int_dot_checked(e1, e2)? }, + I32 => |e1, e2| { int_dot_wrapping(e1, e2) }, + U32 => |e1, e2| { int_dot_wrapping(e1, e2) }, })?; self.register_evaluated_expr(Expression::Literal(result), span) }