Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

[spv-out] Use IndexSet instead of HashSet for iterated sets (capabilities/extensions). #2389

Merged
merged 1 commit into from
Jun 21, 2023
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
4 changes: 2 additions & 2 deletions src/back/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,10 @@ pub struct Writer {
///
/// If `capabilities_available` is `Some`, then this is always a subset of
/// that.
capabilities_used: crate::FastHashSet<Capability>,
capabilities_used: crate::FastIndexSet<Capability>,

/// The set of spirv extensions used.
extensions_used: crate::FastHashSet<&'static str>,
extensions_used: crate::FastIndexSet<&'static str>,

debugs: Vec<Instruction>,
annotations: Vec<Instruction>,
Expand Down
7 changes: 7 additions & 0 deletions src/back/spv/recyclable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ impl<K, S: Clone> Recyclable for std::collections::HashSet<K, S> {
}
}

impl<K, S: Clone> Recyclable for indexmap::IndexSet<K, S> {
fn recycle(mut self) -> Self {
self.clear();
self
}
}

impl<K: Ord, V> Recyclable for std::collections::BTreeMap<K, V> {
fn recycle(mut self) -> Self {
self.clear();
Expand Down
6 changes: 3 additions & 3 deletions src/back/spv/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Writer {
}
let raw_version = ((major as u32) << 16) | ((minor as u32) << 8);

let mut capabilities_used = crate::FastHashSet::default();
let mut capabilities_used = crate::FastIndexSet::default();
capabilities_used.insert(spirv::Capability::Shader);

let mut id_gen = IdGenerator::default();
Expand All @@ -60,7 +60,7 @@ impl Writer {
id_gen,
capabilities_available: options.capabilities.clone(),
capabilities_used,
extensions_used: crate::FastHashSet::default(),
extensions_used: crate::FastIndexSet::default(),
debugs: vec![],
annotations: vec![],
flags: options.flags,
Expand Down Expand Up @@ -1936,7 +1936,7 @@ impl Writer {
}

/// Return the set of capabilities the last module written used.
pub const fn get_capabilities_used(&self) -> &crate::FastHashSet<spirv::Capability> {
pub const fn get_capabilities_used(&self) -> &crate::FastIndexSet<spirv::Capability> {
&self.capabilities_used
}

Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ pub type FastHashMap<K, T> = rustc_hash::FxHashMap<K, T>;
/// Hash set that is faster but not resilient to DoS attacks.
pub type FastHashSet<K> = rustc_hash::FxHashSet<K>;

/// Insertion-order-preserving hash set (`IndexSet<K>`), but with the same
/// hasher as `FastHashSet<K>` (faster but not resilient to DoS attacks).
pub type FastIndexSet<K> =
indexmap::IndexSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;

/// Map of expressions that have associated variable names
pub(crate) type NamedExpressions = indexmap::IndexMap<
Handle<Expression>,
Expand Down
4 changes: 2 additions & 2 deletions tests/out/spv/bounds-check-image-restrict.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
; Version: 1.1
; Generator: rspirv
; Bound: 310
OpCapability ImageQuery
OpCapability Image1D
OpCapability Shader
OpCapability Sampled1D
OpCapability Image1D
OpCapability ImageQuery
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %269 "fragment_shader" %267
Expand Down
4 changes: 2 additions & 2 deletions tests/out/spv/bounds-check-image-rzsw.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
; Version: 1.1
; Generator: rspirv
; Bound: 347
OpCapability ImageQuery
OpCapability Image1D
OpCapability Shader
OpCapability Sampled1D
OpCapability Image1D
OpCapability ImageQuery
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %306 "fragment_shader" %304
Expand Down
6 changes: 3 additions & 3 deletions tests/out/spv/image.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
; Version: 1.1
; Generator: rspirv
; Bound: 546
OpCapability SampledCubeArray
OpCapability ImageQuery
OpCapability Image1D
OpCapability Shader
OpCapability Image1D
OpCapability Sampled1D
OpCapability SampledCubeArray
OpCapability ImageQuery
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %82 "main" %79
Expand Down
2 changes: 1 addition & 1 deletion tests/out/spv/ray-query.spvasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
; Version: 1.4
; Generator: rspirv
; Bound: 95
OpCapability RayQueryKHR
OpCapability Shader
OpCapability RayQueryKHR
OpExtension "SPV_KHR_ray_query"
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
Expand Down
6 changes: 3 additions & 3 deletions tests/spirv-capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Test SPIR-V backend capability checks.

use spirv::Capability as Ca;

fn capabilities_used(source: &str) -> naga::FastHashSet<Ca> {
fn capabilities_used(source: &str) -> naga::FastIndexSet<Ca> {
use naga::back::spv;
use naga::valid;

Expand Down Expand Up @@ -36,7 +36,7 @@ fn require_and_forbid(required: &[Ca], forbidden: &[Ca], source: &str) {

let missing_caps: Vec<_> = required
.iter()
.filter(|cap| !caps_used.contains(cap))
.filter(|&cap| !caps_used.contains(cap))
.cloned()
.collect();
if !missing_caps.is_empty() {
Expand All @@ -45,7 +45,7 @@ fn require_and_forbid(required: &[Ca], forbidden: &[Ca], source: &str) {

let forbidden_caps: Vec<_> = forbidden
.iter()
.filter(|cap| caps_used.contains(cap))
.filter(|&cap| caps_used.contains(cap))
.cloned()
.collect();
if !forbidden_caps.is_empty() {
Expand Down