Skip to content

Commit

Permalink
[hlsl-out] clear named_expressions inserted by duplicated blocks
Browse files Browse the repository at this point in the history
changed the type of `named_expressions` from `HashMap` to `IndexMap` so that insertion order is preserved
  • Loading branch information
teoxoy authored and jimblandy committed Jan 31, 2023
1 parent 0074c68 commit a2b39e4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ glsl-out = []
msl-out = []
serialize = ["serde", "indexmap/serde-1"]
deserialize = ["serde", "indexmap/serde-1"]
arbitrary = ["dep:arbitrary", "indexmap/arbitrary"]
spv-in = ["petgraph", "spirv"]
spv-out = ["spirv"]
wgsl-in = ["codespan-reporting", "hexf-parse", "termcolor", "unicode-xid"]
Expand All @@ -52,7 +53,7 @@ termcolor = { version = "1.0.4", optional = true }
# https://github.com/brendanzab/codespan/commit/e99c867339a877731437e7ee6a903a3d03b5439e
codespan-reporting = { version = "0.11.0", optional = true }
rustc-hash = "1.1.0"
indexmap = { version = "1.6", features = ["std"] }
indexmap = { version = "1.9.2", features = ["std"] }
log = "0.4"
num-traits = "0.2"
spirv = { version = "0.2", optional = true }
Expand Down
3 changes: 3 additions & 0 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1908,9 +1908,12 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
let indent_level_3 = indent_level_2.next();
for case in &cases[i..=end_case_idx] {
writeln!(self.out, "{indent_level_2}{{")?;
let prev_len = self.named_expressions.len();
for sta in case.body.iter() {
self.write_stmt(module, sta, func_ctx, indent_level_3)?;
}
// Clear all named expressions that were previously inserted by the statements in the block
self.named_expressions.truncate(prev_len);
writeln!(self.out, "{indent_level_2}}}")?;
}

Expand Down
7 changes: 3 additions & 4 deletions src/front/glsl/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use super::{
};
use crate::{
front::glsl::types::type_power, proc::ensure_block_returns, AddressSpace, Arena, Block,
Constant, ConstantInner, EntryPoint, Expression, FastHashMap, Function, FunctionArgument,
FunctionResult, Handle, LocalVariable, ScalarKind, ScalarValue, Span, Statement, StructMember,
Type, TypeInner,
Constant, ConstantInner, EntryPoint, Expression, Function, FunctionArgument, FunctionResult,
Handle, LocalVariable, ScalarKind, ScalarValue, Span, Statement, StructMember, Type, TypeInner,
};
use std::iter;

Expand Down Expand Up @@ -1064,7 +1063,7 @@ impl Frontend {
result,
local_variables: locals,
expressions,
named_expressions: FastHashMap::default(),
named_expressions: crate::NamedExpressions::default(),
body,
};

Expand Down
4 changes: 2 additions & 2 deletions src/front/spv/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
local_variables: Arena::new(),
expressions: self
.make_expression_storage(&module.global_variables, &module.constants),
named_expressions: crate::FastHashMap::default(),
named_expressions: crate::NamedExpressions::default(),
body: crate::Block::new(),
}
};
Expand Down Expand Up @@ -298,7 +298,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
result: None,
local_variables: Arena::new(),
expressions: Arena::new(),
named_expressions: crate::FastHashMap::default(),
named_expressions: crate::NamedExpressions::default(),
body: crate::Block::new(),
};

Expand Down
2 changes: 1 addition & 1 deletion src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct StatementContext<'source, 'temp, 'out> {
typifier: &'temp mut Typifier,
variables: &'out mut Arena<crate::LocalVariable>,
naga_expressions: &'out mut Arena<crate::Expression>,
named_expressions: &'out mut FastHashMap<Handle<crate::Expression>, String>,
named_expressions: &'out mut crate::NamedExpressions,
arguments: &'out [crate::FunctionArgument],
module: &'out mut crate::Module,
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ pub type FastHashMap<K, T> = rustc_hash::FxHashMap<K, T>;
pub type FastHashSet<K> = rustc_hash::FxHashSet<K>;

/// Map of expressions that have associated variable names
pub(crate) type NamedExpressions = FastHashMap<Handle<Expression>, String>;
pub(crate) type NamedExpressions = indexmap::IndexMap<
Handle<Expression>,
String,
std::hash::BuildHasherDefault<rustc_hash::FxHasher>,
>;

/// Early fragment tests.
///
Expand Down
12 changes: 6 additions & 6 deletions tests/out/ir/access.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2149,17 +2149,17 @@
),
],
named_expressions: {
29: "c",
4: "baz",
17: "b",
1: "vi",
27: "a",
4: "baz",
8: "_matrix",
11: "arr",
34: "data_pointer",
47: "value",
12: "index",
17: "b",
27: "a",
29: "c",
34: "data_pointer",
35: "foo_value",
47: "value",
},
body: [
Store(
Expand Down

0 comments on commit a2b39e4

Please sign in to comment.