Skip to content

Commit 410ab29

Browse files
committed
[naga] Compact out unused anonymous overrides.
In compaction, remove unused anonymous overrides. Since overrides are no longer used by definition, include override initialization expressions in the tandem traversal of types and global expressions. To simplify overload processing, we plan to make all override-sized arrays refer to their lengths via actual `Override`s. Arrays with non-identifier override expressions as their lengths would refer to anonymous `Override`s with interesting `init` expressions. But in order to avoid re-introducing #6788, we need compaction to remove anonymous overrides.
1 parent 936d882 commit 410ab29

File tree

4 files changed

+451
-59
lines changed

4 files changed

+451
-59
lines changed

naga/src/compact/expressions.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::arena::{Arena, Handle};
33

44
pub struct ExpressionTracer<'tracer> {
55
pub constants: &'tracer Arena<crate::Constant>,
6+
pub overrides: &'tracer Arena<crate::Override>,
67

78
/// The arena in which we are currently tracing expressions.
89
pub expressions: &'tracer Arena<crate::Expression>,
@@ -13,6 +14,9 @@ pub struct ExpressionTracer<'tracer> {
1314
/// The used map for `constants`.
1415
pub constants_used: &'tracer mut HandleSet<crate::Constant>,
1516

17+
/// The used map for `overrides`.
18+
pub overrides_used: &'tracer mut HandleSet<crate::Override>,
19+
1620
/// The used set for `arena`.
1721
///
1822
/// This points to whatever arena holds the expressions we are
@@ -78,25 +82,32 @@ impl ExpressionTracer<'_> {
7882
| Ex::SubgroupBallotResult
7983
| Ex::RayQueryProceedResult => {}
8084

85+
// Expressions can refer to constants and overrides, which can refer
86+
// in turn to expressions, which complicates our nice one-pass
87+
// algorithm. But since constants and overrides don't refer to each
88+
// other directly, only via expressions, we can get around this by
89+
// looking *through* each constant/override and marking its
90+
// initializer expression as used immediately. Since `expr` refers
91+
// to the constant/override, which then refers to the initializer,
92+
// the initializer must precede `expr` in the arena, so we know we
93+
// have yet to visit the initializer, so it's not too late to mark
94+
// it.
8195
Ex::Constant(handle) => {
8296
self.constants_used.insert(handle);
83-
// Constants and expressions are mutually recursive, which
84-
// complicates our nice one-pass algorithm. However, since
85-
// constants don't refer to each other, we can get around
86-
// this by looking *through* each constant and marking its
87-
// initializer as used. Since `expr` refers to the constant,
88-
// and the constant refers to the initializer, it must
89-
// precede `expr` in the arena.
9097
let init = self.constants[handle].init;
9198
match self.global_expressions_used {
9299
Some(ref mut used) => used.insert(init),
93100
None => self.expressions_used.insert(init),
94101
};
95102
}
96-
Ex::Override(_) => {
97-
// All overrides are considered used by definition. We mark
98-
// their types and initialization expressions as used in
99-
// `compact::compact`, so we have no more work to do here.
103+
Ex::Override(handle) => {
104+
self.overrides_used.insert(handle);
105+
if let Some(init) = self.overrides[handle].init {
106+
match self.global_expressions_used {
107+
Some(ref mut used) => used.insert(init),
108+
None => self.expressions_used.insert(init),
109+
};
110+
}
100111
}
101112
Ex::ZeroValue(ty) => {
102113
self.types_used.insert(ty);
@@ -256,11 +267,9 @@ impl ModuleMap {
256267
| Ex::SubgroupBallotResult
257268
| Ex::RayQueryProceedResult => {}
258269

259-
// All overrides are retained, so their handles never change.
260-
Ex::Override(_) => {}
261-
262270
// Expressions that contain handles that need to be adjusted.
263271
Ex::Constant(ref mut constant) => self.constants.adjust(constant),
272+
Ex::Override(ref mut r#override) => self.overrides.adjust(r#override),
264273
Ex::ZeroValue(ref mut ty) => self.types.adjust(ty),
265274
Ex::Compose {
266275
ref mut ty,

naga/src/compact/functions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use super::{FunctionMap, ModuleMap};
44
pub struct FunctionTracer<'a> {
55
pub function: &'a crate::Function,
66
pub constants: &'a crate::Arena<crate::Constant>,
7+
pub overrides: &'a crate::Arena<crate::Override>,
78

89
pub types_used: &'a mut HandleSet<crate::Type>,
910
pub constants_used: &'a mut HandleSet<crate::Constant>,
11+
pub overrides_used: &'a mut HandleSet<crate::Override>,
1012
pub global_expressions_used: &'a mut HandleSet<crate::Expression>,
1113

1214
/// Function-local expressions used.
@@ -47,10 +49,12 @@ impl FunctionTracer<'_> {
4749
fn as_expression(&mut self) -> super::expressions::ExpressionTracer {
4850
super::expressions::ExpressionTracer {
4951
constants: self.constants,
52+
overrides: self.overrides,
5053
expressions: &self.function.expressions,
5154

5255
types_used: self.types_used,
5356
constants_used: self.constants_used,
57+
overrides_used: self.overrides_used,
5458
expressions_used: &mut self.expressions_used,
5559
global_expressions_used: Some(&mut self.global_expressions_used),
5660
}

0 commit comments

Comments
 (0)