From 5a7601baf4ecbf5db82cf6cea3e21c6c4301c8fe Mon Sep 17 00:00:00 2001 From: Ferdon <81517360+notferdon@users.noreply.github.com> Date: Mon, 28 Sep 2026 07:18:12 +0200 Subject: [PATCH] Reject unsigned inputs to sign (#10451) Co-authored-by: Ferdon --- CHANGELOG.md | 1 + naga/src/proc/overloads/mathfunction.rs | 3 +- naga/src/proc/overloads/scalar_set.rs | 4 ++ naga/tests/naga/validation.rs | 70 +++++++++++++++++++++++++ naga/tests/naga/wgsl_errors.rs | 55 +++++++++++++++++++ 5 files changed, 132 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71cf1ef45..222581276 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -198,6 +198,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). #### naga +- Reject unsigned integer arguments to WGSL `sign()` during Naga validation instead of allowing them to reach and panic in the SPIR-V backend. By @notferdon in [#10451](https://github.com/gfx-rs/wgpu/pull/10451). - Fix panics when shader `var` size is larger than 256 bytes. By @beicause in [#9725](https://github.com/gfx-rs/wgpu/pull/9725). - Fix a panic in the SPIR-V frontend when a subgroup collective operation (e.g. `OpGroupNonUniformUMin`) or `OpGroupNonUniformBallot` used an argument whose value needed to be spilled to a temporary variable, such as when the argument was computed inside a loop. By @nazar-pc in [#9957](https://github.com/gfx-rs/wgpu/issues/9957). - Lower `@builtin(instance_index)` in `@any_hit` and `@closest_hit` entry points to SPIR-V's `InstanceId` rather than `InstanceIndex`, which Vulkan only permits in the vertex stage. By @JMS55 in [10154](https://github.com/gfx-rs/wgpu/pull/10154). diff --git a/naga/src/proc/overloads/mathfunction.rs b/naga/src/proc/overloads/mathfunction.rs index d2f70ad5c..9f3fae6c2 100644 --- a/naga/src/proc/overloads/mathfunction.rs +++ b/naga/src/proc/overloads/mathfunction.rs @@ -17,7 +17,8 @@ impl ir::MathFunction { let set: AnyOverloadSet = match self { // Component-wise unary numeric operations - Mf::Abs | Mf::Sign => regular!(1, SCALAR|VECN of NUMERIC).into(), + Mf::Abs => regular!(1, SCALAR|VECN of NUMERIC).into(), + Mf::Sign => regular!(1, SCALAR|VECN of SIGNED_NUMERIC).into(), // Component-wise binary numeric operations Mf::Min | Mf::Max => regular!(2, SCALAR|VECN of NUMERIC).into(), diff --git a/naga/src/proc/overloads/scalar_set.rs b/naga/src/proc/overloads/scalar_set.rs index 1e61146e1..ab338ce28 100644 --- a/naga/src/proc/overloads/scalar_set.rs +++ b/naga/src/proc/overloads/scalar_set.rs @@ -120,6 +120,10 @@ impl ScalarSet { .union(Self::U64); pub const NUMERIC: Self = Self::FLOAT.union(Self::INTEGER); + pub const SIGNED_NUMERIC: Self = Self::NUMERIC + .difference(Self::U16) + .difference(Self::U32) + .difference(Self::U64); pub const ABSTRACT: Self = Self::ABSTRACT_INT.union(Self::ABSTRACT_FLOAT); pub const CONCRETE: Self = Self::all().difference(Self::ABSTRACT); pub const CONCRETE_INTEGER: Self = Self::INTEGER.intersection(Self::CONCRETE); diff --git a/naga/tests/naga/validation.rs b/naga/tests/naga/validation.rs index 66e048d60..ba57af0ca 100644 --- a/naga/tests/naga/validation.rs +++ b/naga/tests/naga/validation.rs @@ -414,6 +414,76 @@ fn builtin_cross_product_args() { assert!(variant(VectorSize::Quad, 2).is_err()); } +#[test] +fn builtin_sign_unsigned_arg() { + use naga::{MathFunction, Type, TypeInner, VectorSize}; + + fn variant(scalar: Scalar) -> Result> { + let span = naga::Span::default(); + let mut module = Module::default(); + let ty_vec4 = module.types.insert( + Type { + name: None, + inner: TypeInner::Vector { + size: VectorSize::Quad, + scalar, + }, + }, + span, + ); + + let mut fun = Function { + result: Some(naga::ir::FunctionResult { + ty: ty_vec4, + binding: None, + }), + ..Function::default() + }; + let ex_zero = fun.expressions.append(Expression::ZeroValue(ty_vec4), span); + let ex_sign = fun.expressions.append( + Expression::Math { + fun: MathFunction::Sign, + arg: ex_zero, + arg1: None, + arg2: None, + arg3: None, + }, + span, + ); + + fun.body.push( + naga::Statement::Emit(naga::Range::new_from_bounds(ex_sign, ex_sign)), + span, + ); + fun.body.push( + naga::Statement::Return { + value: Some(ex_sign), + }, + span, + ); + + module.functions.append(fun, span); + + valid::Validator::new(ValidationFlags::default(), Capabilities::all()) + .validate(&module) + .map_err(|err| Box::new(err.into_inner())) + } + + variant(Scalar::I32).expect("module should validate"); + assert!(matches!( + variant(Scalar::U32).map_err(|e| *e), + Err(valid::ValidationError::Function { + source: valid::FunctionError::Expression { + source: valid::ExpressionError::Type( + naga::proc::ResolveError::BuiltinArgumentsInvalid(_) + ), + .. + }, + .. + }) + )); +} + #[test] fn incompatible_interpolation_and_sampling_types() { use dummy_interpolation_shader::DummyInterpolationShader; diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 9dc2f549c..4ebe5dd5b 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -224,6 +224,61 @@ error: wrong type passed as argument #1 to `cross` ); } +#[test] +fn sign_unsigned() { + check( + r#" + @compute @workgroup_size(1) + fn main() { + var input = vec4(1u); + let value = sign(input); + } + "#, + "\ +error: wrong type passed as argument #1 to `sign` + ┌─ wgsl:5:29 + │ +5 │ let value = sign(input); + │ ^^^^ + │ + = note: `sign` accepts the following types for argument #1: + = note: allowed type: {AbstractInt} + = note: allowed type: {AbstractFloat} + = note: allowed type: i32 + = note: allowed type: i16 + = note: allowed type: i64 + = note: allowed type: f32 + = note: allowed type: f16 + = note: allowed type: f64 + = note: allowed type: vec2<{AbstractInt}> + = note: allowed type: vec2<{AbstractFloat}> + = note: allowed type: vec2 + = note: allowed type: vec2 + = note: allowed type: vec2 + = note: allowed type: vec2 + = note: allowed type: vec2 + = note: allowed type: vec2 + = note: allowed type: vec3<{AbstractInt}> + = note: allowed type: vec3<{AbstractFloat}> + = note: allowed type: vec3 + = note: allowed type: vec3 + = note: allowed type: vec3 + = note: allowed type: vec3 + = note: allowed type: vec3 + = note: allowed type: vec3 + = note: allowed type: vec4<{AbstractInt}> + = note: allowed type: vec4<{AbstractFloat}> + = note: allowed type: vec4 + = note: allowed type: vec4 + = note: allowed type: vec4 + = note: allowed type: vec4 + = note: allowed type: vec4 + = note: allowed type: vec4 + +", + ); +} + #[test] fn cross_vec4() { check(