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

Commit f915d39

Browse files
eddybjimblandy
authored andcommitted
[spv-out] Use IndexSet instead of HashSet for iterated sets (capabilities/extensions).
1 parent f31093f commit f915d39

9 files changed

Lines changed: 28 additions & 16 deletions

File tree

src/back/spv/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,10 @@ pub struct Writer {
595595
///
596596
/// If `capabilities_available` is `Some`, then this is always a subset of
597597
/// that.
598-
capabilities_used: crate::FastHashSet<Capability>,
598+
capabilities_used: crate::FastIndexSet<Capability>,
599599

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

603603
debugs: Vec<Instruction>,
604604
annotations: Vec<Instruction>,

src/back/spv/recyclable.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ impl<K, S: Clone> Recyclable for std::collections::HashSet<K, S> {
5252
}
5353
}
5454

55+
impl<K, S: Clone> Recyclable for indexmap::IndexSet<K, S> {
56+
fn recycle(mut self) -> Self {
57+
self.clear();
58+
self
59+
}
60+
}
61+
5562
impl<K: Ord, V> Recyclable for std::collections::BTreeMap<K, V> {
5663
fn recycle(mut self) -> Self {
5764
self.clear();

src/back/spv/writer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Writer {
4747
}
4848
let raw_version = ((major as u32) << 16) | ((minor as u32) << 8);
4949

50-
let mut capabilities_used = crate::FastHashSet::default();
50+
let mut capabilities_used = crate::FastIndexSet::default();
5151
capabilities_used.insert(spirv::Capability::Shader);
5252

5353
let mut id_gen = IdGenerator::default();
@@ -60,7 +60,7 @@ impl Writer {
6060
id_gen,
6161
capabilities_available: options.capabilities.clone(),
6262
capabilities_used,
63-
extensions_used: crate::FastHashSet::default(),
63+
extensions_used: crate::FastIndexSet::default(),
6464
debugs: vec![],
6565
annotations: vec![],
6666
flags: options.flags,
@@ -1936,7 +1936,7 @@ impl Writer {
19361936
}
19371937

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

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ pub type FastHashMap<K, T> = rustc_hash::FxHashMap<K, T>;
254254
/// Hash set that is faster but not resilient to DoS attacks.
255255
pub type FastHashSet<K> = rustc_hash::FxHashSet<K>;
256256

257+
/// Insertion-order-preserving hash set (`IndexSet<K>`), but with the same
258+
/// hasher as `FastHashSet<K>` (faster but not resilient to DoS attacks).
259+
pub type FastIndexSet<K> =
260+
indexmap::IndexSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
261+
257262
/// Map of expressions that have associated variable names
258263
pub(crate) type NamedExpressions = indexmap::IndexMap<
259264
Handle<Expression>,

tests/out/spv/bounds-check-image-restrict.spvasm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
; Version: 1.1
33
; Generator: rspirv
44
; Bound: 310
5-
OpCapability ImageQuery
6-
OpCapability Image1D
75
OpCapability Shader
86
OpCapability Sampled1D
7+
OpCapability Image1D
8+
OpCapability ImageQuery
99
%1 = OpExtInstImport "GLSL.std.450"
1010
OpMemoryModel Logical GLSL450
1111
OpEntryPoint Fragment %269 "fragment_shader" %267

tests/out/spv/bounds-check-image-rzsw.spvasm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
; Version: 1.1
33
; Generator: rspirv
44
; Bound: 347
5-
OpCapability ImageQuery
6-
OpCapability Image1D
75
OpCapability Shader
86
OpCapability Sampled1D
7+
OpCapability Image1D
8+
OpCapability ImageQuery
99
%1 = OpExtInstImport "GLSL.std.450"
1010
OpMemoryModel Logical GLSL450
1111
OpEntryPoint Fragment %306 "fragment_shader" %304

tests/out/spv/image.spvasm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
; Version: 1.1
33
; Generator: rspirv
44
; Bound: 546
5-
OpCapability SampledCubeArray
6-
OpCapability ImageQuery
7-
OpCapability Image1D
85
OpCapability Shader
6+
OpCapability Image1D
97
OpCapability Sampled1D
8+
OpCapability SampledCubeArray
9+
OpCapability ImageQuery
1010
%1 = OpExtInstImport "GLSL.std.450"
1111
OpMemoryModel Logical GLSL450
1212
OpEntryPoint GLCompute %82 "main" %79

tests/out/spv/ray-query.spvasm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
; Version: 1.4
33
; Generator: rspirv
44
; Bound: 95
5-
OpCapability RayQueryKHR
65
OpCapability Shader
6+
OpCapability RayQueryKHR
77
OpExtension "SPV_KHR_ray_query"
88
%1 = OpExtInstImport "GLSL.std.450"
99
OpMemoryModel Logical GLSL450

tests/spirv-capabilities.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Test SPIR-V backend capability checks.
66

77
use spirv::Capability as Ca;
88

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

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

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

4646
let forbidden_caps: Vec<_> = forbidden
4747
.iter()
48-
.filter(|cap| caps_used.contains(cap))
48+
.filter(|&cap| caps_used.contains(cap))
4949
.cloned()
5050
.collect();
5151
if !forbidden_caps.is_empty() {

0 commit comments

Comments
 (0)