Skip to content

Commit a0dbe5e

Browse files
refactor(msl-out): create a type for bounds check iter items
Co-Authored-By: Erich Gubler <[email protected]>
1 parent a7afb56 commit a0dbe5e

2 files changed

Lines changed: 38 additions & 14 deletions

File tree

naga/src/back/msl/writer.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ use crate::{
1818
arena::{Handle, HandleSet},
1919
back::{self, Baked},
2020
common,
21-
proc::{self, index, NameKey, TypeResolution},
21+
proc::{
22+
self,
23+
index::{self, BoundsCheck},
24+
NameKey, TypeResolution,
25+
},
2226
valid, FastHashMap, FastHashSet,
2327
};
2428

@@ -723,13 +727,7 @@ impl<'a> ExpressionContext<'a> {
723727
fn bounds_check_iter(
724728
&self,
725729
chain: Handle<crate::Expression>,
726-
) -> impl Iterator<
727-
Item = (
728-
Handle<crate::Expression>,
729-
index::GuardedIndex,
730-
index::IndexableLength,
731-
),
732-
> + '_ {
730+
) -> impl Iterator<Item = BoundsCheck> + '_ {
733731
index::bounds_check_iter(chain, self.module, self.function, self.info)
734732
}
735733

@@ -2778,7 +2776,13 @@ impl<W: Write> Writer<W> {
27782776
let mut check_written = false;
27792777

27802778
// Iterate over the access chain, handling each required bounds check.
2781-
for (base, index, length) in context.bounds_check_iter(chain) {
2779+
for item in context.bounds_check_iter(chain) {
2780+
let BoundsCheck {
2781+
base,
2782+
index,
2783+
length,
2784+
} = item;
2785+
27822786
if check_written {
27832787
write!(self.out, " && ")?;
27842788
} else {

naga/src/proc/index.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,27 @@ pub fn access_needs_check(
342342
Some(length)
343343
}
344344

345+
/// Items returned by the [`bounds_check_iter`] iterator.
346+
#[cfg_attr(not(feature = "msl-out"), allow(dead_code))]
347+
pub(crate) struct BoundsCheck {
348+
/// The base of the [`Access`] or [`AccessIndex`] expression.
349+
///
350+
/// [`Access`]: crate::Expression::Access
351+
/// [`AccessIndex`]: crate::Expression::AccessIndex
352+
pub base: Handle<crate::Expression>,
353+
354+
/// The index being accessed.
355+
pub index: GuardedIndex,
356+
357+
/// The length of `base`.
358+
pub length: IndexableLength,
359+
}
360+
345361
/// Returns an iterator of accesses within the chain of `Access` and
346362
/// `AccessIndex` expressions starting from `chain` that may need to be
347363
/// bounds-checked at runtime.
348364
///
349-
/// They're yielded as `(base, index)` pairs, where `base` is the type that the
350-
/// access expression will produce and `index` is the index being used.
365+
/// Items are yielded as [`BoundsCheck`] instances.
351366
///
352367
/// Accesses through a struct are omitted, since you never need a bounds check
353368
/// for accessing a struct field.
@@ -359,7 +374,7 @@ pub(crate) fn bounds_check_iter<'a>(
359374
module: &'a crate::Module,
360375
function: &'a crate::Function,
361376
info: &'a valid::FunctionInfo,
362-
) -> impl Iterator<Item = (Handle<crate::Expression>, GuardedIndex, IndexableLength)> + 'a {
377+
) -> impl Iterator<Item = BoundsCheck> + 'a {
363378
iter::from_fn(move || {
364379
let (next_expr, result) = match function.expressions[chain] {
365380
crate::Expression::Access { base, index } => {
@@ -384,8 +399,13 @@ pub(crate) fn bounds_check_iter<'a>(
384399
})
385400
.flatten()
386401
.filter_map(|(base, index)| {
387-
access_needs_check(base, index, module, &function.expressions, info)
388-
.map(|length| (base, index, length))
402+
access_needs_check(base, index, module, &function.expressions, info).map(|length| {
403+
BoundsCheck {
404+
base,
405+
index,
406+
length,
407+
}
408+
})
389409
})
390410
}
391411

0 commit comments

Comments
 (0)