Skip to content

Commit 2a456f5

Browse files
authored
[naga] Move methods on TypeInner and friends into their own module. (#7018)
1 parent 65d499f commit 2a456f5

File tree

2 files changed

+263
-255
lines changed

2 files changed

+263
-255
lines changed

naga/src/proc/mod.rs

Lines changed: 1 addition & 255 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod index;
88
mod layouter;
99
mod namer;
1010
mod terminator;
11+
mod type_methods;
1112
mod typifier;
1213

1314
pub use constant_evaluator::{
@@ -74,91 +75,6 @@ impl From<super::StorageFormat> for super::Scalar {
7475
}
7576
}
7677

77-
impl super::ScalarKind {
78-
pub const fn is_numeric(self) -> bool {
79-
match self {
80-
crate::ScalarKind::Sint
81-
| crate::ScalarKind::Uint
82-
| crate::ScalarKind::Float
83-
| crate::ScalarKind::AbstractInt
84-
| crate::ScalarKind::AbstractFloat => true,
85-
crate::ScalarKind::Bool => false,
86-
}
87-
}
88-
}
89-
90-
impl super::Scalar {
91-
pub const I32: Self = Self {
92-
kind: crate::ScalarKind::Sint,
93-
width: 4,
94-
};
95-
pub const U32: Self = Self {
96-
kind: crate::ScalarKind::Uint,
97-
width: 4,
98-
};
99-
pub const F32: Self = Self {
100-
kind: crate::ScalarKind::Float,
101-
width: 4,
102-
};
103-
pub const F64: Self = Self {
104-
kind: crate::ScalarKind::Float,
105-
width: 8,
106-
};
107-
pub const I64: Self = Self {
108-
kind: crate::ScalarKind::Sint,
109-
width: 8,
110-
};
111-
pub const U64: Self = Self {
112-
kind: crate::ScalarKind::Uint,
113-
width: 8,
114-
};
115-
pub const BOOL: Self = Self {
116-
kind: crate::ScalarKind::Bool,
117-
width: crate::BOOL_WIDTH,
118-
};
119-
pub const ABSTRACT_INT: Self = Self {
120-
kind: crate::ScalarKind::AbstractInt,
121-
width: crate::ABSTRACT_WIDTH,
122-
};
123-
pub const ABSTRACT_FLOAT: Self = Self {
124-
kind: crate::ScalarKind::AbstractFloat,
125-
width: crate::ABSTRACT_WIDTH,
126-
};
127-
128-
pub const fn is_abstract(self) -> bool {
129-
match self.kind {
130-
crate::ScalarKind::AbstractInt | crate::ScalarKind::AbstractFloat => true,
131-
crate::ScalarKind::Sint
132-
| crate::ScalarKind::Uint
133-
| crate::ScalarKind::Float
134-
| crate::ScalarKind::Bool => false,
135-
}
136-
}
137-
138-
/// Construct a float `Scalar` with the given width.
139-
///
140-
/// This is especially common when dealing with
141-
/// `TypeInner::Matrix`, where the scalar kind is implicit.
142-
pub const fn float(width: crate::Bytes) -> Self {
143-
Self {
144-
kind: crate::ScalarKind::Float,
145-
width,
146-
}
147-
}
148-
149-
pub const fn to_inner_scalar(self) -> crate::TypeInner {
150-
crate::TypeInner::Scalar(self)
151-
}
152-
153-
pub const fn to_inner_vector(self, size: crate::VectorSize) -> crate::TypeInner {
154-
crate::TypeInner::Vector { size, scalar: self }
155-
}
156-
157-
pub const fn to_inner_atomic(self) -> crate::TypeInner {
158-
crate::TypeInner::Atomic(self)
159-
}
160-
}
161-
16278
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16379
pub enum HashableLiteral {
16480
F64(u64),
@@ -240,176 +156,6 @@ impl crate::Literal {
240156
}
241157
}
242158

243-
pub const POINTER_SPAN: u32 = 4;
244-
245-
impl super::TypeInner {
246-
/// Return the scalar type of `self`.
247-
///
248-
/// If `inner` is a scalar, vector, or matrix type, return
249-
/// its scalar type. Otherwise, return `None`.
250-
pub const fn scalar(&self) -> Option<super::Scalar> {
251-
use crate::TypeInner as Ti;
252-
match *self {
253-
Ti::Scalar(scalar) | Ti::Vector { scalar, .. } => Some(scalar),
254-
Ti::Matrix { scalar, .. } => Some(scalar),
255-
_ => None,
256-
}
257-
}
258-
259-
pub fn scalar_kind(&self) -> Option<super::ScalarKind> {
260-
self.scalar().map(|scalar| scalar.kind)
261-
}
262-
263-
/// Returns the scalar width in bytes
264-
pub fn scalar_width(&self) -> Option<u8> {
265-
self.scalar().map(|scalar| scalar.width)
266-
}
267-
268-
pub const fn pointer_space(&self) -> Option<crate::AddressSpace> {
269-
match *self {
270-
Self::Pointer { space, .. } => Some(space),
271-
Self::ValuePointer { space, .. } => Some(space),
272-
_ => None,
273-
}
274-
}
275-
276-
pub fn is_atomic_pointer(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
277-
match *self {
278-
crate::TypeInner::Pointer { base, .. } => match types[base].inner {
279-
crate::TypeInner::Atomic { .. } => true,
280-
_ => false,
281-
},
282-
_ => false,
283-
}
284-
}
285-
286-
/// Get the size of this type.
287-
pub fn size(&self, _gctx: GlobalCtx) -> u32 {
288-
match *self {
289-
Self::Scalar(scalar) | Self::Atomic(scalar) => scalar.width as u32,
290-
Self::Vector { size, scalar } => size as u32 * scalar.width as u32,
291-
// matrices are treated as arrays of aligned columns
292-
Self::Matrix {
293-
columns,
294-
rows,
295-
scalar,
296-
} => Alignment::from(rows) * scalar.width as u32 * columns as u32,
297-
Self::Pointer { .. } | Self::ValuePointer { .. } => POINTER_SPAN,
298-
Self::Array {
299-
base: _,
300-
size,
301-
stride,
302-
} => {
303-
let count = match size {
304-
super::ArraySize::Constant(count) => count.get(),
305-
// any struct member or array element needing a size at pipeline-creation time
306-
// must have a creation-fixed footprint
307-
super::ArraySize::Pending(_) => 0,
308-
// A dynamically-sized array has to have at least one element
309-
super::ArraySize::Dynamic => 1,
310-
};
311-
count * stride
312-
}
313-
Self::Struct { span, .. } => span,
314-
Self::Image { .. }
315-
| Self::Sampler { .. }
316-
| Self::AccelerationStructure
317-
| Self::RayQuery
318-
| Self::BindingArray { .. } => 0,
319-
}
320-
}
321-
322-
/// Return the canonical form of `self`, or `None` if it's already in
323-
/// canonical form.
324-
///
325-
/// Certain types have multiple representations in `TypeInner`. This
326-
/// function converts all forms of equivalent types to a single
327-
/// representative of their class, so that simply applying `Eq` to the
328-
/// result indicates whether the types are equivalent, as far as Naga IR is
329-
/// concerned.
330-
pub fn canonical_form(
331-
&self,
332-
types: &crate::UniqueArena<crate::Type>,
333-
) -> Option<crate::TypeInner> {
334-
use crate::TypeInner as Ti;
335-
match *self {
336-
Ti::Pointer { base, space } => match types[base].inner {
337-
Ti::Scalar(scalar) => Some(Ti::ValuePointer {
338-
size: None,
339-
scalar,
340-
space,
341-
}),
342-
Ti::Vector { size, scalar } => Some(Ti::ValuePointer {
343-
size: Some(size),
344-
scalar,
345-
space,
346-
}),
347-
_ => None,
348-
},
349-
_ => None,
350-
}
351-
}
352-
353-
/// Compare `self` and `rhs` as types.
354-
///
355-
/// This is mostly the same as `<TypeInner as Eq>::eq`, but it treats
356-
/// `ValuePointer` and `Pointer` types as equivalent.
357-
///
358-
/// When you know that one side of the comparison is never a pointer, it's
359-
/// fine to not bother with canonicalization, and just compare `TypeInner`
360-
/// values with `==`.
361-
pub fn equivalent(
362-
&self,
363-
rhs: &crate::TypeInner,
364-
types: &crate::UniqueArena<crate::Type>,
365-
) -> bool {
366-
let left = self.canonical_form(types);
367-
let right = rhs.canonical_form(types);
368-
left.as_ref().unwrap_or(self) == right.as_ref().unwrap_or(rhs)
369-
}
370-
371-
pub fn is_dynamically_sized(&self, types: &crate::UniqueArena<crate::Type>) -> bool {
372-
use crate::TypeInner as Ti;
373-
match *self {
374-
Ti::Array { size, .. } => size == crate::ArraySize::Dynamic,
375-
Ti::Struct { ref members, .. } => members
376-
.last()
377-
.map(|last| types[last.ty].inner.is_dynamically_sized(types))
378-
.unwrap_or(false),
379-
_ => false,
380-
}
381-
}
382-
383-
pub fn components(&self) -> Option<u32> {
384-
Some(match *self {
385-
Self::Vector { size, .. } => size as u32,
386-
Self::Matrix { columns, .. } => columns as u32,
387-
Self::Array {
388-
size: crate::ArraySize::Constant(len),
389-
..
390-
} => len.get(),
391-
Self::Struct { ref members, .. } => members.len() as u32,
392-
_ => return None,
393-
})
394-
}
395-
396-
pub fn component_type(&self, index: usize) -> Option<TypeResolution> {
397-
Some(match *self {
398-
Self::Vector { scalar, .. } => TypeResolution::Value(crate::TypeInner::Scalar(scalar)),
399-
Self::Matrix { rows, scalar, .. } => {
400-
TypeResolution::Value(crate::TypeInner::Vector { size: rows, scalar })
401-
}
402-
Self::Array {
403-
base,
404-
size: crate::ArraySize::Constant(_),
405-
..
406-
} => TypeResolution::Handle(base),
407-
Self::Struct { ref members, .. } => TypeResolution::Handle(members[index].ty),
408-
_ => return None,
409-
})
410-
}
411-
}
412-
413159
impl super::AddressSpace {
414160
pub fn access(self) -> crate::StorageAccess {
415161
use crate::StorageAccess as Sa;

0 commit comments

Comments
 (0)