Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document proc::layouter. #1693

Merged
merged 1 commit into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions src/proc/layouter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{num::NonZeroU32, ops};

pub type Alignment = NonZeroU32;

/// Alignment information for a type.
/// Size and alignment information for a type.
#[derive(Clone, Copy, Debug, Hash, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
Expand All @@ -13,12 +13,19 @@ pub struct TypeLayout {
}

/// Helper processor that derives the sizes of all types.
/// It uses the default layout algorithm/table, described in
/// <https://github.com/gpuweb/gpuweb/issues/1393>
///
/// `Layouter` uses the default layout algorithm/table, described in
/// [WGSL §4.3.7, "Memory Layout"]
///
/// A `Layouter` may be indexed by `Handle<Type>` values: `layouter[handle]` is the
/// layout of the type whose handle is `handle`.
///
/// [WGSL §4.3.7, "Memory Layout"](https://gpuweb.github.io/gpuweb/wgsl/#memory-layouts)
#[derive(Debug, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct Layouter {
/// Layouts for types in an arena, indexed by `Handle` index.
layouts: Vec<TypeLayout>,
}

Expand All @@ -30,7 +37,7 @@ impl ops::Index<Handle<crate::Type>> for Layouter {
}

#[derive(Clone, Copy, Debug, PartialEq, thiserror::Error)]
pub enum TypeLayoutError {
pub enum LayoutErrorInner {
#[error("Array element type {0:?} doesn't exist")]
InvalidArrayElementType(Handle<crate::Type>),
#[error("Struct member[{0}] type {1:?} doesn't exist")]
Expand All @@ -45,27 +52,38 @@ pub enum TypeLayoutError {
#[error("Error laying out type {ty:?}: {inner}")]
pub struct LayoutError {
pub ty: Handle<crate::Type>,
pub inner: TypeLayoutError,
pub inner: LayoutErrorInner,
}

impl TypeLayoutError {
impl LayoutErrorInner {
fn with(self, ty: Handle<crate::Type>) -> LayoutError {
LayoutError { ty, inner: self }
}
}

impl Layouter {
/// Remove all entries from this `Layouter`, retaining storage.
pub fn clear(&mut self) {
self.layouts.clear();
}

/// Round `offset` up to the nearest `alignment` boundary.
pub fn round_up(alignment: Alignment, offset: u32) -> u32 {
match offset & (alignment.get() - 1) {
0 => offset,
other => offset + alignment.get() - other,
}
}

/// Return the offset and span of a struct member.
///
/// The member must fall at or after `offset`. The member's alignment and
/// size are `align` and `size` if given, defaulting to the values this
/// `Layouter` has previously determined for `ty`.
///
/// The return value is the range of offsets within the containing struct to
/// reserve for this member, along with the alignment used. The containing
/// struct must have sufficient space and alignment to accommodate these.
pub fn member_placement(
&self,
offset: u32,
Expand All @@ -83,6 +101,19 @@ impl Layouter {
(start..start + span, alignment)
}

/// Extend this `Layouter` with layouts for any new entries in `types`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sweet, this function needed some text on it for sure!

///
/// Ensure that every type in `types` has a corresponding [TypeLayout] in
/// [`self.layouts`].
///
/// Some front ends need to be able to compute layouts for existing types
/// while module construction is still in progress and new types are still
/// being added. This function assumes that the `TypeLayout` values already
/// present in `self.layouts` cover their corresponding entries in `types`,
/// and extends `self.layouts` as needed to cover the rest. Thus, a front
/// end can call this function at any time, passing its current type and
/// constant arenas, and then assume that layouts are available for all
/// types.
#[allow(clippy::or_fun_call)]
pub fn update(
&mut self,
Expand All @@ -95,12 +126,12 @@ impl Layouter {
let size = ty
.inner
.try_size(constants)
.map_err(|error| TypeLayoutError::BadHandle(error).with(ty_handle))?;
.map_err(|error| LayoutErrorInner::BadHandle(error).with(ty_handle))?;
let layout = match ty.inner {
Ti::Scalar { width, .. } | Ti::Atomic { width, .. } => TypeLayout {
size,
alignment: Alignment::new(width as u32)
.ok_or(TypeLayoutError::ZeroWidth.with(ty_handle))?,
.ok_or(LayoutErrorInner::ZeroWidth.with(ty_handle))?,
},
Ti::Vector {
size: vec_size,
Expand All @@ -115,7 +146,7 @@ impl Layouter {
2
};
Alignment::new(count * width as u32)
.ok_or(TypeLayoutError::ZeroWidth.with(ty_handle))?
.ok_or(LayoutErrorInner::ZeroWidth.with(ty_handle))?
},
},
Ti::Matrix {
Expand All @@ -127,7 +158,7 @@ impl Layouter {
alignment: {
let count = if rows >= crate::VectorSize::Tri { 4 } else { 2 };
Alignment::new(count * width as u32)
.ok_or(TypeLayoutError::ZeroWidth.with(ty_handle))?
.ok_or(LayoutErrorInner::ZeroWidth.with(ty_handle))?
},
},
Ti::Pointer { .. } | Ti::ValuePointer { .. } => TypeLayout {
Expand All @@ -143,7 +174,7 @@ impl Layouter {
alignment: if base < ty_handle {
self[base].alignment
} else {
return Err(TypeLayoutError::InvalidArrayElementType(base).with(ty_handle));
return Err(LayoutErrorInner::InvalidArrayElementType(base).with(ty_handle));
},
},
Ti::Struct { span, ref members } => {
Expand All @@ -152,7 +183,7 @@ impl Layouter {
alignment = if member.ty < ty_handle {
alignment.max(self[member.ty].alignment)
} else {
return Err(TypeLayoutError::InvalidStructMemberType(
return Err(LayoutErrorInner::InvalidStructMemberType(
index as u32,
member.ty,
)
Expand Down
2 changes: 1 addition & 1 deletion src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod typifier;
use std::cmp::PartialEq;

pub use index::{BoundsCheckPolicies, BoundsCheckPolicy, IndexableLength, IndexableLengthError};
pub use layouter::{Alignment, LayoutError, Layouter, TypeLayout, TypeLayoutError};
pub use layouter::{Alignment, LayoutError, LayoutErrorInner, Layouter, TypeLayout};
pub use namer::{EntryPointIndex, NameKey, Namer};
pub use terminator::ensure_block_returns;
pub use typifier::{ResolveContext, ResolveError, TypeResolution};
Expand Down