From 5ff49fda04fa716e7cb0b76395a49853e232d248 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Mon, 23 Dec 2024 18:45:59 -0800 Subject: [PATCH] [naga] Allow abstract scalars in `modf` and `frexp` results. Allow `PredeclaredType::ModfResult` and `PredeclaredType::FrexpResult` to hold any sort of scalar, not just a floating-point scalar. This prepares Naga for implementing the `AbstractFloat` overloads for the `modf` and `frexp` builtin functions. --- naga/src/back/glsl/mod.rs | 13 +++++++----- naga/src/back/hlsl/help.rs | 8 ++++---- naga/src/back/msl/writer.rs | 8 ++++---- naga/src/front/type_gen.rs | 34 +++++++++++++------------------- naga/src/front/wgsl/lower/mod.rs | 16 ++++++--------- naga/src/lib.rs | 4 ++-- naga/src/proc/typifier.rs | 5 +++-- 7 files changed, 41 insertions(+), 47 deletions(-) diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index e34127b3a1..83aeeebdd3 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -747,14 +747,17 @@ impl<'a, W: Write> Writer<'a, W> { // Write functions to create special types. for (type_key, struct_ty) in self.module.special_types.predeclared_types.iter() { match type_key { - &crate::PredeclaredType::ModfResult { size, width } - | &crate::PredeclaredType::FrexpResult { size, width } => { + &crate::PredeclaredType::ModfResult { size, scalar } + | &crate::PredeclaredType::FrexpResult { size, scalar } => { let arg_type_name_owner; let arg_type_name = if let Some(size) = size { - arg_type_name_owner = - format!("{}vec{}", if width == 8 { "d" } else { "" }, size as u8); + arg_type_name_owner = format!( + "{}vec{}", + if scalar.width == 8 { "d" } else { "" }, + size as u8 + ); &arg_type_name_owner - } else if width == 8 { + } else if scalar.width == 8 { "double" } else { "float" diff --git a/naga/src/back/hlsl/help.rs b/naga/src/back/hlsl/help.rs index e060529dcf..347addd67e 100644 --- a/naga/src/back/hlsl/help.rs +++ b/naga/src/back/hlsl/help.rs @@ -796,17 +796,17 @@ impl super::Writer<'_, W> { pub(super) fn write_special_functions(&mut self, module: &crate::Module) -> BackendResult { for (type_key, struct_ty) in module.special_types.predeclared_types.iter() { match type_key { - &crate::PredeclaredType::ModfResult { size, width } - | &crate::PredeclaredType::FrexpResult { size, width } => { + &crate::PredeclaredType::ModfResult { size, scalar } + | &crate::PredeclaredType::FrexpResult { size, scalar } => { let arg_type_name_owner; let arg_type_name = if let Some(size) = size { arg_type_name_owner = format!( "{}{}", - if width == 8 { "double" } else { "float" }, + if scalar.width == 8 { "double" } else { "float" }, size as u8 ); &arg_type_name_owner - } else if width == 8 { + } else if scalar.width == 8 { "double" } else { "float" diff --git a/naga/src/back/msl/writer.rs b/naga/src/back/msl/writer.rs index dc444bddb5..a8b34d4574 100644 --- a/naga/src/back/msl/writer.rs +++ b/naga/src/back/msl/writer.rs @@ -3830,17 +3830,17 @@ impl Writer { // Write functions to create special types. for (type_key, struct_ty) in module.special_types.predeclared_types.iter() { match type_key { - &crate::PredeclaredType::ModfResult { size, width } - | &crate::PredeclaredType::FrexpResult { size, width } => { + &crate::PredeclaredType::ModfResult { size, scalar } + | &crate::PredeclaredType::FrexpResult { size, scalar } => { let arg_type_name_owner; let arg_type_name = if let Some(size) = size { arg_type_name_owner = format!( "{NAMESPACE}::{}{}", - if width == 8 { "double" } else { "float" }, + if scalar.width == 8 { "double" } else { "float" }, size as u8 ); &arg_type_name_owner - } else if width == 8 { + } else if scalar.width == 8 { "double" } else { "float" diff --git a/naga/src/front/type_gen.rs b/naga/src/front/type_gen.rs index 1cd9f7f378..737c456bbd 100644 --- a/naga/src/front/type_gen.rs +++ b/naga/src/front/type_gen.rs @@ -298,11 +298,11 @@ impl crate::Module { }, } } - crate::PredeclaredType::ModfResult { size, width } => { + crate::PredeclaredType::ModfResult { size, scalar } => { let float_ty = self.types.insert( crate::Type { name: None, - inner: crate::TypeInner::Scalar(crate::Scalar::float(width)), + inner: crate::TypeInner::Scalar(scalar), }, Span::UNDEFINED, ); @@ -311,23 +311,20 @@ impl crate::Module { let vec_ty = self.types.insert( crate::Type { name: None, - inner: crate::TypeInner::Vector { - size, - scalar: crate::Scalar::float(width), - }, + inner: crate::TypeInner::Vector { size, scalar }, }, Span::UNDEFINED, ); - (vec_ty, size as u32 * width as u32) + (vec_ty, size as u32 * scalar.width as u32) } else { - (float_ty, width as u32) + (float_ty, scalar.width as u32) }; let mut type_name = "__modf_result_".to_string(); if let Some(size) = size { let _ = write!(type_name, "vec{}_", size as u8); } - let _ = write!(type_name, "f{}", width * 8); + let _ = write!(type_name, "f{}", scalar.width * 8); crate::Type { name: Some(type_name), @@ -350,11 +347,11 @@ impl crate::Module { }, } } - crate::PredeclaredType::FrexpResult { size, width } => { + crate::PredeclaredType::FrexpResult { size, scalar } => { let float_ty = self.types.insert( crate::Type { name: None, - inner: crate::TypeInner::Scalar(crate::Scalar::float(width)), + inner: crate::TypeInner::Scalar(scalar), }, Span::UNDEFINED, ); @@ -364,7 +361,7 @@ impl crate::Module { name: None, inner: crate::TypeInner::Scalar(crate::Scalar { kind: crate::ScalarKind::Sint, - width, + width: scalar.width, }), }, Span::UNDEFINED, @@ -374,10 +371,7 @@ impl crate::Module { let vec_float_ty = self.types.insert( crate::Type { name: None, - inner: crate::TypeInner::Vector { - size, - scalar: crate::Scalar::float(width), - }, + inner: crate::TypeInner::Vector { size, scalar }, }, Span::UNDEFINED, ); @@ -388,22 +382,22 @@ impl crate::Module { size, scalar: crate::Scalar { kind: crate::ScalarKind::Sint, - width, + width: scalar.width, }, }, }, Span::UNDEFINED, ); - (vec_float_ty, vec_int_ty, size as u32 * width as u32) + (vec_float_ty, vec_int_ty, size as u32 * scalar.width as u32) } else { - (float_ty, int_ty, width as u32) + (float_ty, int_ty, scalar.width as u32) }; let mut type_name = "__frexp_result_".to_string(); if let Some(size) = size { let _ = write!(type_name, "vec{}_", size as u8); } - let _ = write!(type_name, "f{}", width * 8); + let _ = write!(type_name, "f{}", scalar.width * 8); crate::Type { name: Some(type_name), diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index fc31e43ecf..6bfcdefe55 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -2287,22 +2287,18 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { args.finish()?; if fun == crate::MathFunction::Modf || fun == crate::MathFunction::Frexp { - if let Some((size, width)) = match *resolve_inner!(ctx, arg) { - crate::TypeInner::Scalar(crate::Scalar { width, .. }) => { - Some((None, width)) + if let Some((size, scalar)) = match *resolve_inner!(ctx, arg) { + crate::TypeInner::Scalar(scalar) => Some((None, scalar)), + crate::TypeInner::Vector { size, scalar, .. } => { + Some((Some(size), scalar)) } - crate::TypeInner::Vector { - size, - scalar: crate::Scalar { width, .. }, - .. - } => Some((Some(size), width)), _ => None, } { ctx.module.generate_predeclared_type( if fun == crate::MathFunction::Modf { - crate::PredeclaredType::ModfResult { size, width } + crate::PredeclaredType::ModfResult { size, scalar } } else { - crate::PredeclaredType::FrexpResult { size, width } + crate::PredeclaredType::FrexpResult { size, scalar } }, ); } diff --git a/naga/src/lib.rs b/naga/src/lib.rs index 687dc5b441..8db5b676d6 100644 --- a/naga/src/lib.rs +++ b/naga/src/lib.rs @@ -2217,11 +2217,11 @@ pub enum PredeclaredType { AtomicCompareExchangeWeakResult(Scalar), ModfResult { size: Option, - width: Bytes, + scalar: Scalar, }, FrexpResult { size: Option, - width: Bytes, + scalar: Scalar, }, } diff --git a/naga/src/proc/typifier.rs b/naga/src/proc/typifier.rs index 1e1a4c93a4..898cf35d17 100644 --- a/naga/src/proc/typifier.rs +++ b/naga/src/proc/typifier.rs @@ -687,13 +687,14 @@ impl<'a> ResolveContext<'a> { ))) } }; + let scalar = crate::Scalar::float(width); let result = self .special_types .predeclared_types .get(&if fun == Mf::Modf { - crate::PredeclaredType::ModfResult { size, width } + crate::PredeclaredType::ModfResult { size, scalar } } else { - crate::PredeclaredType::FrexpResult { size, width } + crate::PredeclaredType::FrexpResult { size, scalar } }) .ok_or(ResolveError::MissingSpecialType)?; TypeResolution::Handle(*result)