gfx-rs/wgpu · error

Unexpected abs({:?})

Error message

Unexpected abs({:?})

What it means

The SPIR-V backend's math-instruction lowering for MathFunction::Abs only supports float and signed/unsigned integer scalar kinds (via SPV arithmetic or GlslStd450 SAbs); any other argument scalar kind (None/kindless, e.g. bool or composite) hits the explicit `unimplemented!("Unexpected abs({:?})", other)` panic. Raised in cache_expression_value from write_block.

Source

Thrown at naga/src/back/spv/block.rs:1399

                let id = self.gen_id();
                let math_op = match fun {
                    // comparison
                    Mf::Abs => {
                        match arg_scalar_kind {
                            Some(crate::ScalarKind::Float) => {
                                MathOp::Ext(spirv::GlslStd450Op::FAbs)
                            }
                            Some(crate::ScalarKind::Sint) => MathOp::Ext(spirv::GlslStd450Op::SAbs),
                            Some(crate::ScalarKind::Uint) => {
                                MathOp::Custom(Instruction::unary(
                                    spirv::Op::CopyObject, // do nothing
                                    result_type_id,
                                    id,
                                    arg0_id,
                                ))
                            }
                            other => unimplemented!("Unexpected abs({:?})", other),
                        }
                    }
                    Mf::Min => MathOp::Ext(match arg_scalar_kind {
                        Some(crate::ScalarKind::Float) => spirv::GlslStd450Op::FMin,
                        Some(crate::ScalarKind::Sint) => spirv::GlslStd450Op::SMin,
                        Some(crate::ScalarKind::Uint) => spirv::GlslStd450Op::UMin,
                        other => unimplemented!("Unexpected min({:?})", other),
                    }),
                    Mf::Max => MathOp::Ext(match arg_scalar_kind {
                        Some(crate::ScalarKind::Float) => spirv::GlslStd450Op::FMax,
                        Some(crate::ScalarKind::Sint) => spirv::GlslStd450Op::SMax,
                        Some(crate::ScalarKind::Uint) => spirv::GlslStd450Op::UMax,
                        other => unimplemented!("Unexpected max({:?})", other),
                    }),
                    Mf::Clamp => match arg_scalar_kind {
                        // Clamp is undefined if min > max. In practice this means it can use a median-of-three
                        // instruction to determine the value. This is fine according to the WGSL spec for float
                        // clamp, but integer clamp _must_ use min-max. As such we write out min/max.

View on GitHub (pinned to 3e11ff59bf)

Solutions

  1. Call abs only on numeric (i32/u32/f32 or vectors thereof) values in the shader
  2. Validate the module before SPIR-V emission so bad IR is diagnosed
  3. Dump the IR to find the malformed abs call
  4. File a naga bug with the reproducer if the shader is valid WGSL

Example fix

// before
let x = abs(true); // invalid
// after
let x = abs(-3i); // i32
Defensive patterns

Strategy: validation

Validate before calling

assert!(matches!(arg_scalar_kind, Some(crate::ScalarKind::Float | crate::ScalarKind::Sint | crate::ScalarKind::Uint)), "abs() requires numeric argument");

Type guard

fn abs_supported(kind: Option<crate::ScalarKind>) -> bool {
    matches!(kind, Some(crate::ScalarKind::Float | crate::ScalarKind::Sint | crate::ScalarKind::Uint))
}

Prevention

When it happens

Trigger: Translating an `abs(...)` built-in call whose argument type has no recognized scalar kind (bool, struct, pointer, or unexpected kind) during SPIR-V emission.

Common situations: Fuzzed IR applying abs to a bool/vector-of-bool; validation bypass by calling the backend directly; a naga upgrade changing how argument scalar kinds are derived (arg_scalar_kind).

Related errors


AI-assisted analysis of gfx-rs/wgpu@3e11ff59bf (2026-09-03). Data as JSON: /api/errors/6d14b4caeadccc36. Report an issue: GitHub.