Debian Codegraph @

third_party/rust/naga/src/compact/expressions.rs

raw
1 use super::{HandleMap, HandleSet, ModuleMap};
2 use crate::arena::{Arena, Handle};
3
4 pub struct ExpressionTracer<'tracer> {
5 pub constants: &'tracer Arena<crate::Constant>,
6 pub overrides: &'tracer Arena<crate::Override>,
7
8 /// The arena in which we are currently tracing expressions.
9 pub expressions: &'tracer Arena<crate::Expression>,
10
11 /// The used map for `types`.
12 pub types_used: &'tracer mut HandleSet<crate::Type>,
13
14 /// The used map for `constants`.
15 pub constants_used: &'tracer mut HandleSet<crate::Constant>,
16
17 /// The used map for `overrides`.
18 pub overrides_used: &'tracer mut HandleSet<crate::Override>,
19
20 /// The used set for `arena`.
21 ///
22 /// This points to whatever arena holds the expressions we are
23 /// currently tracing: either a function's expression arena, or
24 /// the module's constant expression arena.
25 pub expressions_used: &'tracer mut HandleSet<crate::Expression>,
26
27 /// The used set for the module's `global_expressions` arena.
28 ///
29 /// If `None`, we are already tracing the constant expressions,
30 /// and `expressions_used` already refers to their handle set.
31 pub global_expressions_used: Option<&'tracer mut HandleSet<crate::Expression>>,
32 }
33
34 impl ExpressionTracer<'_> {
35 /// Propagate usage through `self.expressions`, starting with `self.expressions_used`.
36 ///
37 /// Treat `self.expressions_used` as the initial set of "known
38 /// live" expressions, and follow through to identify all
39 /// transitively used expressions.
40 ///
41 /// Mark types, constants, and constant expressions used directly
42 /// by `self.expressions` as used. Items used indirectly are not
43 /// marked.
44 ///
45 /// [fe]: crate::Function::expressions
46 /// [ce]: crate::Module::global_expressions
47 pub fn trace_expressions(&mut self) {
48 log::trace!(
49 "entering trace_expression of {}",
50 if self.global_expressions_used.is_some() {
51 "function expressions"
52 } else {
53 "const expressions"
54 }
55 );
56
57 // We don't need recursion or a work list. Because an
58 // expression may only refer to other expressions that precede
59 // it in the arena, it suffices to make a single pass over the
60 // arena from back to front, marking the referents of used
61 // expressions as used themselves.
62 for (handle, expr) in self.expressions.iter().rev() {
63 // If this expression isn't used, it doesn't matter what it uses.
64 if !self.expressions_used.contains(handle) {
65 continue;
66 }
67
68 log::trace!("tracing new expression {:?}", expr);
69 self.trace_expression(expr);
70 }
71 }
72
73 pub fn trace_expression(&mut self, expr: &crate::Expression) {
74 use crate::Expression as Ex;
75 match *expr {
76 // Expressions that do not contain handles that need to be traced.
77 Ex::Literal(_)
78 | Ex::FunctionArgument(_)
79 | Ex::GlobalVariable(_)
80 | Ex::LocalVariable(_)
81 | Ex::CallResult(_)
82 | Ex::SubgroupBallotResult
83 | Ex::RayQueryProceedResult => {}
84
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.
95 Ex::Constant(handle) => {
96 self.constants_used.insert(handle);
97 let constant = &self.constants[handle];
98 self.types_used.insert(constant.ty);
99 match self.global_expressions_used {
100 Some(ref mut used) => used.insert(constant.init),
101 None => self.expressions_used.insert(constant.init),
102 };
103 }
104 Ex::Override(handle) => {
105 self.overrides_used.insert(handle);
106 let r#override = &self.overrides[handle];
107 self.types_used.insert(r#override.ty);
108 if let Some(init) = r#override.init {
109 match self.global_expressions_used {
110 Some(ref mut used) => used.insert(init),
111 None => self.expressions_used.insert(init),
112 };
113 }
114 }
115 Ex::ZeroValue(ty) => {
116 self.types_used.insert(ty);
117 }
118 Ex::Compose { ty, ref components } => {
119 self.types_used.insert(ty);
120 self.expressions_used
121 .insert_iter(components.iter().cloned());
122 }
123 Ex::Access { base, index } => self.expressions_used.insert_iter([base, index]),
124 Ex::AccessIndex { base, index: _ } => {
125 self.expressions_used.insert(base);
126 }
127 Ex::Splat { size: _, value } => {
128 self.expressions_used.insert(value);
129 }
130 Ex::Swizzle {
131 size: _,
132 vector,
133 pattern: _,
134 } => {
135 self.expressions_used.insert(vector);
136 }
137 Ex::Load { pointer } => {
138 self.expressions_used.insert(pointer);
139 }
140 Ex::ImageSample {
141 image,
142 sampler,
143 gather: _,
144 coordinate,
145 array_index,
146 offset,
147 ref level,
148 depth_ref,
149 } => {
150 self.expressions_used
151 .insert_iter([image, sampler, coordinate]);
152 self.expressions_used.insert_iter(array_index);
153 self.expressions_used.insert_iter(offset);
154 use crate::SampleLevel as Sl;
155 match *level {
156 Sl::Auto | Sl::Zero => {}
157 Sl::Exact(expr) | Sl::Bias(expr) => {
158 self.expressions_used.insert(expr);
159 }
160 Sl::Gradient { x, y } => self.expressions_used.insert_iter([x, y]),
161 }
162 self.expressions_used.insert_iter(depth_ref);
163 }
164 Ex::ImageLoad {
165 image,
166 coordinate,
167 array_index,
168 sample,
169 level,
170 } => {
171 self.expressions_used.insert(image);
172 self.expressions_used.insert(coordinate);
173 self.expressions_used.insert_iter(array_index);
174 self.expressions_used.insert_iter(sample);
175 self.expressions_used.insert_iter(level);
176 }
177 Ex::ImageQuery { image, ref query } => {
178 self.expressions_used.insert(image);
179 use crate::ImageQuery as Iq;
180 match *query {
181 Iq::Size { level } => self.expressions_used.insert_iter(level),
182 Iq::NumLevels | Iq::NumLayers | Iq::NumSamples => {}
183 }
184 }
185 Ex::RayQueryVertexPositions {
186 query,
187 committed: _,
188 } => {
189 self.expressions_used.insert(query);
190 }
191 Ex::Unary { op: _, expr } => {
192 self.expressions_used.insert(expr);
193 }
194 Ex::Binary { op: _, left, right } => {
195 self.expressions_used.insert_iter([left, right]);
196 }
197 Ex::Select {
198 condition,
199 accept,
200 reject,
201 } => self
202 .expressions_used
203 .insert_iter([condition, accept, reject]),
204 Ex::Derivative {
205 axis: _,
206 ctrl: _,
207 expr,
208 } => {
209 self.expressions_used.insert(expr);
210 }
211 Ex::Relational { fun: _, argument } => {
212 self.expressions_used.insert(argument);
213 }
214 Ex::Math {
215 fun: _,
216 arg,
217 arg1,
218 arg2,
219 arg3,
220 } => {
221 self.expressions_used.insert(arg);
222 self.expressions_used.insert_iter(arg1);
223 self.expressions_used.insert_iter(arg2);
224 self.expressions_used.insert_iter(arg3);
225 }
226 Ex::As {
227 expr,
228 kind: _,
229 convert: _,
230 } => {
231 self.expressions_used.insert(expr);
232 }
233 Ex::ArrayLength(expr) => {
234 self.expressions_used.insert(expr);
235 }
236 Ex::AtomicResult { ty, comparison: _ }
237 | Ex::WorkGroupUniformLoadResult { ty }
238 | Ex::SubgroupOperationResult { ty } => {
239 self.types_used.insert(ty);
240 }
241 Ex::RayQueryGetIntersection {
242 query,
243 committed: _,
244 } => {
245 self.expressions_used.insert(query);
246 }
247 }
248 }
249 }
250
251 impl ModuleMap {
252 /// Fix up all handles in `expr`.
253 ///
254 /// Use the expression handle remappings in `operand_map`, and all
255 /// other mappings from `self`.
256 pub fn adjust_expression(
257 &self,
258 expr: &mut crate::Expression,
259 operand_map: &HandleMap<crate::Expression>,
260 ) {
261 let adjust = |expr: &mut Handle<crate::Expression>| {
262 operand_map.adjust(expr);
263 };
264
265 use crate::Expression as Ex;
266 match *expr {
267 // Expressions that do not contain handles that need to be adjusted.
268 Ex::Literal(_)
269 | Ex::FunctionArgument(_)
270 | Ex::GlobalVariable(_)
271 | Ex::LocalVariable(_)
272 | Ex::CallResult(_)
273 | Ex::SubgroupBallotResult
274 | Ex::RayQueryProceedResult => {}
275
276 // Expressions that contain handles that need to be adjusted.
277 Ex::Constant(ref mut constant) => self.constants.adjust(constant),
278 Ex::Override(ref mut r#override) => self.overrides.adjust(r#override),
279 Ex::ZeroValue(ref mut ty) => self.types.adjust(ty),
280 Ex::Compose {
281 ref mut ty,
282 ref mut components,
283 } => {
284 self.types.adjust(ty);
285 for component in components {
286 adjust(component);
287 }
288 }
289 Ex::Access {
290 ref mut base,
291 ref mut index,
292 } => {
293 adjust(base);
294 adjust(index);
295 }
296 Ex::AccessIndex {
297 ref mut base,
298 index: _,
299 } => adjust(base),
300 Ex::Splat {
301 size: _,
302 ref mut value,
303 } => adjust(value),
304 Ex::Swizzle {
305 size: _,
306 ref mut vector,
307 pattern: _,
308 } => adjust(vector),
309 Ex::Load { ref mut pointer } => adjust(pointer),
310 Ex::ImageSample {
311 ref mut image,
312 ref mut sampler,
313 gather: _,
314 ref mut coordinate,
315 ref mut array_index,
316 ref mut offset,
317 ref mut level,
318 ref mut depth_ref,
319 } => {
320 adjust(image);
321 adjust(sampler);
322 adjust(coordinate);
323 operand_map.adjust_option(array_index);
324 operand_map.adjust_option(offset);
325 self.adjust_sample_level(level, operand_map);
326 operand_map.adjust_option(depth_ref);
327 }
328 Ex::ImageLoad {
329 ref mut image,
330 ref mut coordinate,
331 ref mut array_index,
332 ref mut sample,
333 ref mut level,
334 } => {
335 adjust(image);
336 adjust(coordinate);
337 operand_map.adjust_option(array_index);
338 operand_map.adjust_option(sample);
339 operand_map.adjust_option(level);
340 }
341 Ex::ImageQuery {
342 ref mut image,
343 ref mut query,
344 } => {
345 adjust(image);
346 self.adjust_image_query(query, operand_map);
347 }
348 Ex::Unary {
349 op: _,
350 ref mut expr,
351 } => adjust(expr),
352 Ex::Binary {
353 op: _,
354 ref mut left,
355 ref mut right,
356 } => {
357 adjust(left);
358 adjust(right);
359 }
360 Ex::Select {
361 ref mut condition,
362 ref mut accept,
363 ref mut reject,
364 } => {
365 adjust(condition);
366 adjust(accept);
367 adjust(reject);
368 }
369 Ex::Derivative {
370 axis: _,
371 ctrl: _,
372 ref mut expr,
373 } => adjust(expr),
374 Ex::Relational {
375 fun: _,
376 ref mut argument,
377 } => adjust(argument),
378 Ex::Math {
379 fun: _,
380 ref mut arg,
381 ref mut arg1,
382 ref mut arg2,
383 ref mut arg3,
384 } => {
385 adjust(arg);
386 operand_map.adjust_option(arg1);
387 operand_map.adjust_option(arg2);
388 operand_map.adjust_option(arg3);
389 }
390 Ex::As {
391 ref mut expr,
392 kind: _,
393 convert: _,
394 } => adjust(expr),
395 Ex::AtomicResult {
396 ref mut ty,
397 comparison: _,
398 } => self.types.adjust(ty),
399 Ex::WorkGroupUniformLoadResult { ref mut ty } => self.types.adjust(ty),
400 Ex::SubgroupOperationResult { ref mut ty } => self.types.adjust(ty),
401 Ex::ArrayLength(ref mut expr) => adjust(expr),
402 Ex::RayQueryGetIntersection {
403 ref mut query,
404 committed: _,
405 } => adjust(query),
406 Ex::RayQueryVertexPositions {
407 ref mut query,
408 committed: _,
409 } => adjust(query),
410 }
411 }
412
413 fn adjust_sample_level(
414 &self,
415 level: &mut crate::SampleLevel,
416 operand_map: &HandleMap<crate::Expression>,
417 ) {
418 let adjust = |expr: &mut Handle<crate::Expression>| operand_map.adjust(expr);
419
420 use crate::SampleLevel as Sl;
421 match *level {
422 Sl::Auto | Sl::Zero => {}
423 Sl::Exact(ref mut expr) => adjust(expr),
424 Sl::Bias(ref mut expr) => adjust(expr),
425 Sl::Gradient {
426 ref mut x,
427 ref mut y,
428 } => {
429 adjust(x);
430 adjust(y);
431 }
432 }
433 }
434
435 fn adjust_image_query(
436 &self,
437 query: &mut crate::ImageQuery,
438 operand_map: &HandleMap<crate::Expression>,
439 ) {
440 use crate::ImageQuery as Iq;
441
442 match *query {
443 Iq::Size { ref mut level } => operand_map.adjust_option(level),
444 Iq::NumLevels | Iq::NumLayers | Iq::NumSamples => {}
445 }
446 }
447 }