[naga] Move some helper functions to type_methods

This commit is contained in:
Andy Leiserson 2025-11-18 14:47:26 -08:00
parent 0a99be6f05
commit e620027f95
5 changed files with 43 additions and 37 deletions

View File

@ -8,11 +8,5 @@ pub mod wgsl;
pub use diagnostic_debug::{DiagnosticDebug, ForDebug, ForDebugWithTypes}; pub use diagnostic_debug::{DiagnosticDebug, ForDebug, ForDebugWithTypes};
pub use diagnostic_display::DiagnosticDisplay; pub use diagnostic_display::DiagnosticDisplay;
/// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize) // Re-exported here for backwards compatibility
pub const fn vector_size_str(size: crate::VectorSize) -> &'static str { pub use super::proc::vector_size_str;
match size {
crate::VectorSize::Bi => "2",
crate::VectorSize::Tri => "3",
crate::VectorSize::Quad => "4",
}
}

View File

@ -24,7 +24,9 @@ pub use namer::{EntryPointIndex, ExternalTextureNameKey, NameKey, Namer};
pub use overloads::{Conclusion, MissingSpecialType, OverloadSet, Rule}; pub use overloads::{Conclusion, MissingSpecialType, OverloadSet, Rule};
pub use terminator::ensure_block_returns; pub use terminator::ensure_block_returns;
use thiserror::Error; use thiserror::Error;
pub use type_methods::min_max_float_representable_by; pub use type_methods::{
concrete_int_scalars, min_max_float_representable_by, vector_size_str, vector_sizes,
};
pub use typifier::{compare_types, ResolveContext, ResolveError, TypeResolution}; pub use typifier::{compare_types, ResolveContext, ResolveError, TypeResolution};
use crate::non_max_u32::NonMaxU32; use crate::non_max_u32::NonMaxU32;

View File

@ -4,10 +4,10 @@ use crate::proc::overloads::any_overload_set::AnyOverloadSet;
use crate::proc::overloads::list::List; use crate::proc::overloads::list::List;
use crate::proc::overloads::regular::regular; use crate::proc::overloads::regular::regular;
use crate::proc::overloads::utils::{ use crate::proc::overloads::utils::{
concrete_int_scalars, float_scalars, float_scalars_unimplemented_abstract, list, pairs, rule, float_scalars, float_scalars_unimplemented_abstract, list, pairs, rule, scalar_or_vecn, triples,
scalar_or_vecn, triples, vector_sizes,
}; };
use crate::proc::overloads::OverloadSet; use crate::proc::overloads::OverloadSet;
use crate::proc::type_methods::{concrete_int_scalars, vector_sizes};
use crate::ir; use crate::ir;

View File

@ -9,17 +9,6 @@ use crate::proc::TypeResolution;
use alloc::vec::Vec; use alloc::vec::Vec;
/// Produce all vector sizes.
pub fn vector_sizes() -> impl Iterator<Item = ir::VectorSize> + Clone {
static SIZES: [ir::VectorSize; 3] = [
ir::VectorSize::Bi,
ir::VectorSize::Tri,
ir::VectorSize::Quad,
];
SIZES.iter().cloned()
}
/// Produce all the floating-point [`ir::Scalar`]s. /// Produce all the floating-point [`ir::Scalar`]s.
/// ///
/// Note that `F32` must appear before other sizes; this is how we /// Note that `F32` must appear before other sizes; this is how we
@ -40,20 +29,6 @@ pub fn float_scalars_unimplemented_abstract() -> impl Iterator<Item = ir::Scalar
[ir::Scalar::F32, ir::Scalar::F16, ir::Scalar::F64].into_iter() [ir::Scalar::F32, ir::Scalar::F16, ir::Scalar::F64].into_iter()
} }
/// Produce all concrete integer [`ir::Scalar`]s.
///
/// Note that `I32` and `U32` must come first; this is how we
/// represent conversion rank.
pub fn concrete_int_scalars() -> impl Iterator<Item = ir::Scalar> {
[
ir::Scalar::I32,
ir::Scalar::U32,
ir::Scalar::I64,
ir::Scalar::U64,
]
.into_iter()
}
/// Produce the scalar and vector [`ir::TypeInner`]s that have `s` as /// Produce the scalar and vector [`ir::TypeInner`]s that have `s` as
/// their scalar. /// their scalar.
pub fn scalar_or_vecn(scalar: ir::Scalar) -> impl Iterator<Item = ir::TypeInner> { pub fn scalar_or_vecn(scalar: ir::Scalar) -> impl Iterator<Item = ir::TypeInner> {

View File

@ -1,8 +1,9 @@
//! Methods on [`TypeInner`], [`Scalar`], and [`ScalarKind`]. //! Methods on or related to [`TypeInner`], [`Scalar`], [`ScalarKind`], and [`VectorSize`].
//! //!
//! [`TypeInner`]: crate::TypeInner //! [`TypeInner`]: crate::TypeInner
//! [`Scalar`]: crate::Scalar //! [`Scalar`]: crate::Scalar
//! [`ScalarKind`]: crate::ScalarKind //! [`ScalarKind`]: crate::ScalarKind
//! [`VectorSize`]: crate::VectorSize
use crate::{ir, valid::MAX_TYPE_SIZE}; use crate::{ir, valid::MAX_TYPE_SIZE};
@ -97,6 +98,31 @@ impl crate::Scalar {
} }
} }
/// Produce all concrete integer [`ir::Scalar`]s.
///
/// Note that `I32` and `U32` must come first; this represents conversion rank
/// in overload resolution.
pub fn concrete_int_scalars() -> impl Iterator<Item = ir::Scalar> {
[
ir::Scalar::I32,
ir::Scalar::U32,
ir::Scalar::I64,
ir::Scalar::U64,
]
.into_iter()
}
/// Produce all vector sizes.
pub fn vector_sizes() -> impl Iterator<Item = ir::VectorSize> + Clone {
static SIZES: [ir::VectorSize; 3] = [
ir::VectorSize::Bi,
ir::VectorSize::Tri,
ir::VectorSize::Quad,
];
SIZES.iter().cloned()
}
const POINTER_SPAN: u32 = 4; const POINTER_SPAN: u32 = 4;
impl crate::TypeInner { impl crate::TypeInner {
@ -612,3 +638,12 @@ pub fn min_max_float_representable_by(
_ => unreachable!(), _ => unreachable!(),
} }
} }
/// Helper function that returns the string corresponding to the [`VectorSize`](crate::VectorSize)
pub const fn vector_size_str(size: crate::VectorSize) -> &'static str {
match size {
crate::VectorSize::Bi => "2",
crate::VectorSize::Tri => "3",
crate::VectorSize::Quad => "4",
}
}