Skip to content

Commit 6b5a416

Browse files
fix(naga): Use wrapping arithmetic for u32 const eval (gfx-rs#8912)
Fixes one part of gfx-rs#8900
1 parent 046a1b4 commit 6b5a416

3 files changed

Lines changed: 10 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Bottom level categories:
6565

6666
- The validator checks that override-sized arrays have a positive size, if overrides have been resolved. By @andyleiserson in [#8822](https://github.com/gfx-rs/wgpu/pull/8822).
6767
- Fix some cases where f16 constants were not working. By @andyleiserson in [#8816](https://github.com/gfx-rs/wgpu/pull/8816).
68+
- Use wrapping arithmetic when evaluating constant expressions involving `u32`. By @andyleiserson in [#8912](https://github.com/gfx-rs/wgpu/pull/8912).
6869

6970
#### Naga
7071

cts_runner/test.lst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ webgpu:shader,validation,expression,access,array:early_eval_errors:case="overrid
217217
webgpu:shader,validation,expression,access,array:early_eval_errors:case="override_array_cnt_size_zero_signed"
218218
webgpu:shader,validation,expression,access,array:early_eval_errors:case="override_array_cnt_size_zero_unsigned"
219219
webgpu:shader,validation,expression,access,array:early_eval_errors:case="override_in_bounds"
220+
webgpu:shader,validation,expression,binary,add_sub_mul:scalar_vector_out_of_range:lhs="i32";*
221+
webgpu:shader,validation,expression,binary,add_sub_mul:scalar_vector_out_of_range:lhs="u32";*
220222
webgpu:shader,validation,expression,binary,short_circuiting_and_or:array_override:op="%26%26";a_val=1;b_val=1
221223
webgpu:shader,validation,expression,binary,short_circuiting_and_or:invalid_types:*
222224
webgpu:shader,validation,expression,binary,short_circuiting_and_or:scalar_vector:op="%26%26";lhs="bool";rhs="bool"

naga/src/proc/constant_evaluator.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,6 +2575,10 @@ impl<'a> ConstantEvaluator<'a> {
25752575
let left = self.eval_zero_value_and_splat(left, span)?;
25762576
let right = self.eval_zero_value_and_splat(right, span)?;
25772577

2578+
// Note: in most cases constant evaluation checks for overflow, but for
2579+
// i32/u32, it uses wrapping arithmetic. See
2580+
// <https://gpuweb.github.io/gpuweb/wgsl/#integer-types>.
2581+
25782582
let expr = match (&self.expressions[left], &self.expressions[right]) {
25792583
(&Expression::Literal(left_value), &Expression::Literal(right_value)) => {
25802584
let literal = match op {
@@ -2623,15 +2627,9 @@ impl<'a> ConstantEvaluator<'a> {
26232627
_ => return Err(ConstantEvaluatorError::InvalidBinaryOpArgs),
26242628
}),
26252629
(Literal::U32(a), Literal::U32(b)) => Literal::U32(match op {
2626-
BinaryOperator::Add => a.checked_add(b).ok_or_else(|| {
2627-
ConstantEvaluatorError::Overflow("addition".into())
2628-
})?,
2629-
BinaryOperator::Subtract => a.checked_sub(b).ok_or_else(|| {
2630-
ConstantEvaluatorError::Overflow("subtraction".into())
2631-
})?,
2632-
BinaryOperator::Multiply => a.checked_mul(b).ok_or_else(|| {
2633-
ConstantEvaluatorError::Overflow("multiplication".into())
2634-
})?,
2630+
BinaryOperator::Add => a.wrapping_add(b),
2631+
BinaryOperator::Subtract => a.wrapping_sub(b),
2632+
BinaryOperator::Multiply => a.wrapping_mul(b),
26352633
BinaryOperator::Divide => a
26362634
.checked_div(b)
26372635
.ok_or(ConstantEvaluatorError::DivisionByZero)?,

0 commit comments

Comments
 (0)