From 6dcccc40a1b7204db29b9657fbd6064e5c9268fd Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Thu, 20 Mar 2025 17:10:38 -0700 Subject: [PATCH] [naga] Move predeclared type name generation to common code. Add a new module, `naga::common::predeclared`, which implements a new `struct_name` method on `naga::ir::PredeclaredType`, to produce the name of the struct type corresponding to that `PredeclaredType` value. Use this new method in `naga::front::type_gen`. --- naga/src/common/mod.rs | 1 + naga/src/common/predeclared.rs | 33 +++++++++++++++++++++++++++++++++ naga/src/front/type_gen.rs | 26 +++++--------------------- 3 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 naga/src/common/predeclared.rs diff --git a/naga/src/common/mod.rs b/naga/src/common/mod.rs index 8ed0396952..79b4e574a9 100644 --- a/naga/src/common/mod.rs +++ b/naga/src/common/mod.rs @@ -1,5 +1,6 @@ //! Code common to the front and backends for specific languages. +pub mod predeclared; pub mod wgsl; /// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize) diff --git a/naga/src/common/predeclared.rs b/naga/src/common/predeclared.rs new file mode 100644 index 0000000000..0c2004f7fd --- /dev/null +++ b/naga/src/common/predeclared.rs @@ -0,0 +1,33 @@ +//! Generating names for predeclared types. + +use crate::ir; + +use alloc::format; +use alloc::string::String; + +impl ir::PredeclaredType { + pub fn struct_name(&self) -> String { + use crate::PredeclaredType as Pt; + match *self { + Pt::AtomicCompareExchangeWeakResult(scalar) => { + format!( + "__atomic_compare_exchange_result<{:?},{}>", + scalar.kind, scalar.width, + ) + } + Pt::ModfResult { size, scalar } => frexp_mod_name("modf", size, scalar), + Pt::FrexpResult { size, scalar } => frexp_mod_name("frexp", size, scalar), + } + } +} + +fn frexp_mod_name(function: &str, size: Option, scalar: ir::Scalar) -> String { + let bits = 8 * scalar.width; + match size { + Some(size) => { + let size = size as u8; + format!("__{function}_result_vec{size}_f{bits}") + } + None => format!("__{function}_result_f{bits}"), + } +} diff --git a/naga/src/front/type_gen.rs b/naga/src/front/type_gen.rs index 687d245b1e..9a01b637d5 100644 --- a/naga/src/front/type_gen.rs +++ b/naga/src/front/type_gen.rs @@ -2,7 +2,7 @@ Type generators. */ -use alloc::{format, string::ToString, vec}; +use alloc::{string::ToString, vec}; use crate::{arena::Handle, span::Span}; @@ -283,12 +283,11 @@ impl crate::Module { &mut self, special_type: crate::PredeclaredType, ) -> Handle { - use core::fmt::Write; - if let Some(value) = self.special_types.predeclared_types.get(&special_type) { return *value; } + let name = special_type.struct_name(); let ty = match special_type { crate::PredeclaredType::AtomicCompareExchangeWeakResult(scalar) => { let bool_ty = self.types.insert( @@ -307,10 +306,7 @@ impl crate::Module { ); crate::Type { - name: Some(format!( - "__atomic_compare_exchange_result<{:?},{}>", - scalar.kind, scalar.width, - )), + name: Some(name), inner: crate::TypeInner::Struct { members: vec![ crate::StructMember { @@ -352,14 +348,8 @@ impl crate::Module { (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{}", scalar.width * 8); - crate::Type { - name: Some(type_name), + name: Some(name), inner: crate::TypeInner::Struct { members: vec![ crate::StructMember { @@ -425,14 +415,8 @@ impl crate::Module { (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{}", scalar.width * 8); - crate::Type { - name: Some(type_name), + name: Some(name), inner: crate::TypeInner::Struct { members: vec![ crate::StructMember {