From 3bbd97cf0d10a7d5d462133e6e06e02dc05ef44c Mon Sep 17 00:00:00 2001 From: Jamie Nicol Date: Thu, 20 Mar 2025 19:28:30 +0000 Subject: [PATCH] [naga wgsl-in] Allow explicit cast from AbstractFloat to integer types (#7377) And from either AbstractFloat or AbstractInt to bool. AbstractFloat to integer was presumably not implemented as *automatic* conversion between those types is not allowed. However, explicit conversions (for example `i32(1.0)`) are allowed, and are implemented during const evaluation using the same code path in ConstantEvaluator::cast(). The integer conversion constructors treat AbstractFloats as any other floating point type, meaning we must follow the scalar floating point to integral conversion algorithm [1]. This specifies: To convert a floating point scalar value X to an integer scalar type T: * If X is a NaN, the result is an indeterminate value in T. * If X is exactly representable in the target type T, then the result is that value. * Otherwise, the result is the value in T closest to truncate(X) and also exactly representable in the original floating point type. Fortunately a rust cast satisfies all these conditions apart from the result being exactly representable in the original floating point type. However, as i32::MIN, i32::MAX, u32::MIN, and u32::MAX are all representable by f64 (the underlying type of AbstractFloat), this is not an issue. For i64 and u64 using a rust cast will not meet that requirement, but as these types are not present in the WGSL spec we are free to ignore that. [1] https://www.w3.org/TR/WGSL/#floating-point-conversion --- naga/src/proc/constant_evaluator.rs | 56 ++++++++++++++++++-------- naga/tests/in/wgsl/constructors.wgsl | 22 ++++++++++ naga/tests/out/spv/constructors.spvasm | 15 +++---- 3 files changed, 69 insertions(+), 24 deletions(-) diff --git a/naga/src/proc/constant_evaluator.rs b/naga/src/proc/constant_evaluator.rs index d99f9f82c..9ea63b573 100644 --- a/naga/src/proc/constant_evaluator.rs +++ b/naga/src/proc/constant_evaluator.rs @@ -590,8 +590,6 @@ pub enum ConstantEvaluatorError { value: String, to_type: &'static str, }, - #[error("abstract floating-point values cannot be automatically converted to integers")] - AutomaticConversionFloatToInt { to_type: &'static str }, #[error("Division by zero")] DivisionByZero, #[error("Remainder by zero")] @@ -1688,11 +1686,9 @@ impl<'a> ConstantEvaluator<'a> { Literal::F32(v) => v != 0.0, Literal::F16(v) => v != f16::zero(), Literal::Bool(v) => v, - Literal::F64(_) - | Literal::I64(_) - | Literal::U64(_) - | Literal::AbstractInt(_) - | Literal::AbstractFloat(_) => { + Literal::AbstractInt(v) => v != 0, + Literal::AbstractFloat(v) => v != 0.0, + Literal::F64(_) | Literal::I64(_) | Literal::U64(_) => { return make_error(); } }), @@ -2533,13 +2529,19 @@ trait TryFromAbstract: Sized { /// WGSL, we follow WGSL's conversion rules here: /// /// - WGSL §6.1.2. Conversion Rank says that automatic conversions - /// to integers are either lossless or an error. + /// from `AbstractInt` to an integer type are either lossless or an + /// error. /// - /// - WGSL §14.6.4 Floating Point Conversion says that conversions + /// - WGSL §15.7.6 Floating Point Conversion says that conversions /// to floating point in constant expressions and override /// expressions are errors if the value is out of range for the /// destination type, but rounding is okay. /// + /// - WGSL §17.1.2 i32()/u32() constructors treat AbstractFloat as any + /// other floating point type, following the scalar floating point to + /// integral conversion algorithm (§15.7.6). There is no automatic + /// conversion from AbstractFloat to integer types. + /// /// [`AbstractInt`]: crate::Literal::AbstractInt /// [`Float`]: crate::Literal::Float fn try_from_abstract(value: T) -> Result; @@ -2618,26 +2620,46 @@ impl TryFromAbstract for f64 { } impl TryFromAbstract for i32 { - fn try_from_abstract(_: f64) -> Result { - Err(ConstantEvaluatorError::AutomaticConversionFloatToInt { to_type: "i32" }) + fn try_from_abstract(value: f64) -> Result { + // https://www.w3.org/TR/WGSL/#floating-point-conversion + // To convert a floating point scalar value X to an integer scalar type T: + // * If X is a NaN, the result is an indeterminate value in T. + // * If X is exactly representable in the target type T, then the + // result is that value. + // * Otherwise, the result is the value in T closest to truncate(X) and + // also exactly representable in the original floating point type. + // + // A rust cast satisfies these requirements apart from "the result + // is... exactly representable in the original floating point type". + // However, i32::MIN and i32::MAX are exactly representable by f64, so + // we're all good. + Ok(value as i32) } } impl TryFromAbstract for u32 { - fn try_from_abstract(_: f64) -> Result { - Err(ConstantEvaluatorError::AutomaticConversionFloatToInt { to_type: "u32" }) + fn try_from_abstract(value: f64) -> Result { + // As above, u32::MIN and u32::MAX are exactly representable by f64, + // so a simple rust cast is sufficient. + Ok(value as u32) } } impl TryFromAbstract for i64 { - fn try_from_abstract(_: f64) -> Result { - Err(ConstantEvaluatorError::AutomaticConversionFloatToInt { to_type: "i64" }) + fn try_from_abstract(value: f64) -> Result { + // As above, except i64::MIN and i64::MAX are not exactly representable + // by f64. i64 is not part of the WGSL spec, however, so we're free to + // ignore that requirement. + Ok(value as i64) } } impl TryFromAbstract for u64 { - fn try_from_abstract(_: f64) -> Result { - Err(ConstantEvaluatorError::AutomaticConversionFloatToInt { to_type: "u64" }) + fn try_from_abstract(value: f64) -> Result { + // As above, except u64::MAX is not exactly representable by f64. u64 + // is not part of the WGSL spec, however, so we're free to ignore that + // requirement. + Ok(value as u64) } } diff --git a/naga/tests/in/wgsl/constructors.wgsl b/naga/tests/in/wgsl/constructors.wgsl index d221dc80d..8157ebbf8 100644 --- a/naga/tests/in/wgsl/constructors.wgsl +++ b/naga/tests/in/wgsl/constructors.wgsl @@ -64,4 +64,26 @@ fn main() { let ic5 = mat2x3(mat2x3()); let ic6 = vec2(vec2()); let ic7 = mat2x3(mat2x3()); + + // conversion constructors + let cc00 = i32(1u); + let cc01 = i32(1f); + let cc02 = i32(1); + let cc03 = i32(1.0); + let cc04 = i32(true); + let cc05 = u32(1i); + let cc06 = u32(1f); + let cc07 = u32(1); + let cc08 = u32(1.0); + let cc09 = u32(true); + let cc10 = f32(1i); + let cc11 = f32(1u); + let cc12 = f32(1); + let cc13 = f32(1.0); + let cc14 = f32(true); + let cc15 = bool(1i); + let cc16 = bool(1u); + let cc17 = bool(1f); + let cc18 = bool(1); + let cc19 = bool(1.0); } diff --git a/naga/tests/out/spv/constructors.spvasm b/naga/tests/out/spv/constructors.spvasm index 4aa6a6f66..afa7deded 100644 --- a/naga/tests/out/spv/constructors.spvasm +++ b/naga/tests/out/spv/constructors.spvasm @@ -1,7 +1,7 @@ ; SPIR-V ; Version: 1.1 ; Generator: rspirv -; Bound: 67 +; Bound: 68 OpCapability Shader %1 = OpExtInstImport "GLSL.std.450" OpMemoryModel Logical GLSL450 @@ -71,13 +71,14 @@ OpDecorate %17 ArrayStride 4 %60 = OpConstantComposite %20 %21 %21 %21 %61 = OpConstantComposite %19 %60 %60 %62 = OpConstantNull %19 -%64 = OpTypePointer Function %6 -%65 = OpConstantNull %6 +%63 = OpConstantTrue %12 +%65 = OpTypePointer Function %6 +%66 = OpConstantNull %6 %38 = OpFunction %2 None %39 %37 = OpLabel -%63 = OpVariable %64 Function %65 -OpBranch %66 -%66 = OpLabel -OpStore %63 %42 +%64 = OpVariable %65 Function %66 +OpBranch %67 +%67 = OpLabel +OpStore %64 %42 OpReturn OpFunctionEnd \ No newline at end of file