[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`.
This commit is contained in:
Jim Blandy
2025-03-20 17:10:38 -07:00
committed by Connor Fitzgerald
parent f11b3b98e3
commit 6dcccc40a1
3 changed files with 39 additions and 21 deletions

View File

@@ -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)

View File

@@ -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<ir::VectorSize>, 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}"),
}
}

View File

@@ -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<crate::Type> {
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 {