Reject unsigned inputs to sign (#10451)

Co-authored-by: Ferdon <notferdon@users.noreply.github.com>
This commit is contained in:
FerdonandFerdon authored and GitHub committed 2026-09-28 05:18:12 +00:00
1 parent 2c3e503258
commit 5a7601baf4
5 files changed
+132 -1

No files matched your search

+1
View File
@@ -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<immediate>` 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).
+2 -1
View File
@@ -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(),
+4
View File
@@ -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);
+70
View File
@@ -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<ModuleInfo, Box<valid::ValidationError>> {
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;
+55
View File
@@ -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<u32>(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<i32>
= note: allowed type: vec2<i16>
= note: allowed type: vec2<i64>
= note: allowed type: vec2<f32>
= note: allowed type: vec2<f16>
= note: allowed type: vec2<f64>
= note: allowed type: vec3<{AbstractInt}>
= note: allowed type: vec3<{AbstractFloat}>
= note: allowed type: vec3<i32>
= note: allowed type: vec3<i16>
= note: allowed type: vec3<i64>
= note: allowed type: vec3<f32>
= note: allowed type: vec3<f16>
= note: allowed type: vec3<f64>
= note: allowed type: vec4<{AbstractInt}>
= note: allowed type: vec4<{AbstractFloat}>
= note: allowed type: vec4<i32>
= note: allowed type: vec4<i16>
= note: allowed type: vec4<i64>
= note: allowed type: vec4<f32>
= note: allowed type: vec4<f16>
= note: allowed type: vec4<f64>
",
);
}
#[test]
fn cross_vec4() {
check(