[naga] Use HandleSet in naga::proc::index.

Change `naga::proc::index::find_checked_indexes` to return a
`HandleSet<Expression>`, rather than an untyped `BitSet`.

Fix uses in the Metal Shading Language backend.
This commit is contained in:
Jim Blandy
2024-06-22 22:39:37 -07:00
committed by Teodor Tanasoaia
parent aac6fc7267
commit 0656fb8ea8
2 changed files with 20 additions and 23 deletions

View File

@@ -1,12 +1,11 @@
use super::{sampler as sm, Error, LocationMode, Options, PipelineOptions, TranslationInfo};
use crate::{
arena::Handle,
arena::{Handle, HandleSet},
back::{self, Baked},
proc::index,
proc::{self, NameKey, TypeResolution},
valid, FastHashMap, FastHashSet,
};
use bit_set::BitSet;
use std::{
fmt::{Display, Error as FmtError, Formatter, Write},
iter,
@@ -584,11 +583,10 @@ struct ExpressionContext<'a> {
lang_version: (u8, u8),
policies: index::BoundsCheckPolicies,
/// A bitset containing the `Expression` handle indexes of expressions used
/// as indices in `ReadZeroSkipWrite`-policy accesses. These may need to be
/// cached in temporary variables. See `index::find_checked_indexes` for
/// details.
guarded_indices: BitSet,
/// The set of expressions used as indices in `ReadZeroSkipWrite`-policy
/// accesses. These may need to be cached in temporary variables. See
/// `index::find_checked_indexes` for details.
guarded_indices: HandleSet<crate::Expression>,
}
impl<'a> ExpressionContext<'a> {
@@ -2873,12 +2871,11 @@ impl<W: Write> Writer<W> {
// If this expression is an index that we're going to first compare
// against a limit, and then actually use as an index, then we may
// want to cache it in a temporary, to avoid evaluating it twice.
let bake =
if context.expression.guarded_indices.contains(handle.index()) {
true
} else {
self.need_bake_expressions.contains(&handle)
};
let bake = if context.expression.guarded_indices.contains(handle) {
true
} else {
self.need_bake_expressions.contains(&handle)
};
if bake {
Some(Baked(handle).to_string())

View File

@@ -2,8 +2,8 @@
Definitions for index bounds checking.
*/
use crate::{valid, Handle, UniqueArena};
use bit_set::BitSet;
use crate::arena::{Handle, HandleSet, UniqueArena};
use crate::valid;
/// How should code generated by Naga do bounds checks?
///
@@ -196,7 +196,7 @@ pub enum GuardedIndex {
/// Build a set of expressions used as indices, to cache in temporary variables when
/// emitted.
///
/// Given the bounds-check policies `policies`, construct a `BitSet` containing the handle
/// Given the bounds-check policies `policies`, construct a `HandleSet` containing the handle
/// indices of all the expressions in `function` that are ever used as guarded indices
/// under the [`ReadZeroSkipWrite`] policy. The `module` argument must be the module to
/// which `function` belongs, and `info` should be that function's analysis results.
@@ -241,10 +241,10 @@ pub fn find_checked_indexes(
function: &crate::Function,
info: &valid::FunctionInfo,
policies: BoundsCheckPolicies,
) -> BitSet {
) -> HandleSet<crate::Expression> {
use crate::Expression as Ex;
let mut guarded_indices = BitSet::new();
let mut guarded_indices = HandleSet::for_arena(&function.expressions);
// Don't bother scanning if we never need `ReadZeroSkipWrite`.
if policies.contains(BoundsCheckPolicy::ReadZeroSkipWrite) {
@@ -264,7 +264,7 @@ pub fn find_checked_indexes(
)
.is_some()
{
guarded_indices.insert(index.index());
guarded_indices.insert(index);
}
}
Ex::ImageLoad {
@@ -275,15 +275,15 @@ pub fn find_checked_indexes(
..
} => {
if policies.image_load == BoundsCheckPolicy::ReadZeroSkipWrite {
guarded_indices.insert(coordinate.index());
guarded_indices.insert(coordinate);
if let Some(array_index) = array_index {
guarded_indices.insert(array_index.index());
guarded_indices.insert(array_index);
}
if let Some(sample) = sample {
guarded_indices.insert(sample.index());
guarded_indices.insert(sample);
}
if let Some(level) = level {
guarded_indices.insert(level.index());
guarded_indices.insert(level);
}
}
}