1use super::{HandleMap, HandleSet, ModuleMap};
2use crate::arena::{Arena, Handle};
3
4pub struct ExpressionTracer<'tracer> {
5 pub constants: &'tracer Arena<crate::Constant>,
6 pub overrides: &'tracer Arena<crate::Override>,
7
8 pub expressions: &'tracer Arena<crate::Expression>,
10
11 pub types_used: &'tracer mut HandleSet<crate::Type>,
13
14 pub global_variables_used: &'tracer mut HandleSet<crate::GlobalVariable>,
16
17 pub constants_used: &'tracer mut HandleSet<crate::Constant>,
19
20 pub overrides_used: &'tracer mut HandleSet<crate::Override>,
22
23 pub expressions_used: &'tracer mut HandleSet<crate::Expression>,
29
30 pub global_expressions_used: Option<&'tracer mut HandleSet<crate::Expression>>,
35}
36
37impl ExpressionTracer<'_> {
38 pub fn trace_expressions(&mut self) {
51 log::trace!(
52 "entering trace_expression of {}",
53 if self.global_expressions_used.is_some() {
54 "function expressions"
55 } else {
56 "const expressions"
57 }
58 );
59
60 for (handle, expr) in self.expressions.iter().rev() {
66 if !self.expressions_used.contains(handle) {
68 continue;
69 }
70
71 log::trace!("tracing new expression {expr:?}");
72 self.trace_expression(expr);
73 }
74 }
75
76 pub fn trace_expression(&mut self, expr: &crate::Expression) {
77 use crate::Expression as Ex;
78 match *expr {
79 Ex::Literal(_)
81 | Ex::FunctionArgument(_)
82 | Ex::LocalVariable(_)
83 | Ex::SubgroupBallotResult
84 | Ex::RayQueryProceedResult => {}
85
86 Ex::Constant(handle) => {
97 self.constants_used.insert(handle);
98 let constant = &self.constants[handle];
99 self.types_used.insert(constant.ty);
100 match self.global_expressions_used {
101 Some(ref mut used) => used.insert(constant.init),
102 None => self.expressions_used.insert(constant.init),
103 };
104 }
105 Ex::Override(handle) => {
106 self.overrides_used.insert(handle);
107 let r#override = &self.overrides[handle];
108 self.types_used.insert(r#override.ty);
109 if let Some(init) = r#override.init {
110 match self.global_expressions_used {
111 Some(ref mut used) => used.insert(init),
112 None => self.expressions_used.insert(init),
113 };
114 }
115 }
116 Ex::ZeroValue(ty) => {
117 self.types_used.insert(ty);
118 }
119 Ex::Compose { ty, ref components } => {
120 self.types_used.insert(ty);
121 self.expressions_used
122 .insert_iter(components.iter().cloned());
123 }
124 Ex::Access { base, index } => self.expressions_used.insert_iter([base, index]),
125 Ex::AccessIndex { base, index: _ } => {
126 self.expressions_used.insert(base);
127 }
128 Ex::Splat { size: _, value } => {
129 self.expressions_used.insert(value);
130 }
131 Ex::Swizzle {
132 size: _,
133 vector,
134 pattern: _,
135 } => {
136 self.expressions_used.insert(vector);
137 }
138 Ex::GlobalVariable(handle) => {
139 self.global_variables_used.insert(handle);
140 }
141 Ex::Load { pointer } => {
142 self.expressions_used.insert(pointer);
143 }
144 Ex::ImageSample {
145 image,
146 sampler,
147 gather: _,
148 coordinate,
149 array_index,
150 offset,
151 ref level,
152 depth_ref,
153 clamp_to_edge: _,
154 } => {
155 self.expressions_used
156 .insert_iter([image, sampler, coordinate]);
157 self.expressions_used.insert_iter(array_index);
158 self.expressions_used.insert_iter(offset);
159 use crate::SampleLevel as Sl;
160 match *level {
161 Sl::Auto | Sl::Zero => {}
162 Sl::Exact(expr) | Sl::Bias(expr) => {
163 self.expressions_used.insert(expr);
164 }
165 Sl::Gradient { x, y } => self.expressions_used.insert_iter([x, y]),
166 }
167 self.expressions_used.insert_iter(depth_ref);
168 }
169 Ex::ImageLoad {
170 image,
171 coordinate,
172 array_index,
173 sample,
174 level,
175 } => {
176 self.expressions_used.insert(image);
177 self.expressions_used.insert(coordinate);
178 self.expressions_used.insert_iter(array_index);
179 self.expressions_used.insert_iter(sample);
180 self.expressions_used.insert_iter(level);
181 }
182 Ex::ImageQuery { image, ref query } => {
183 self.expressions_used.insert(image);
184 use crate::ImageQuery as Iq;
185 match *query {
186 Iq::Size { level } => self.expressions_used.insert_iter(level),
187 Iq::NumLevels | Iq::NumLayers | Iq::NumSamples => {}
188 }
189 }
190 Ex::RayQueryVertexPositions {
191 query,
192 committed: _,
193 } => {
194 self.expressions_used.insert(query);
195 }
196 Ex::Unary { op: _, expr } => {
197 self.expressions_used.insert(expr);
198 }
199 Ex::Binary { op: _, left, right } => {
200 self.expressions_used.insert_iter([left, right]);
201 }
202 Ex::Select {
203 condition,
204 accept,
205 reject,
206 } => self
207 .expressions_used
208 .insert_iter([condition, accept, reject]),
209 Ex::Derivative {
210 axis: _,
211 ctrl: _,
212 expr,
213 } => {
214 self.expressions_used.insert(expr);
215 }
216 Ex::Relational { fun: _, argument } => {
217 self.expressions_used.insert(argument);
218 }
219 Ex::Math {
220 fun: _,
221 arg,
222 arg1,
223 arg2,
224 arg3,
225 } => {
226 self.expressions_used.insert(arg);
227 self.expressions_used.insert_iter(arg1);
228 self.expressions_used.insert_iter(arg2);
229 self.expressions_used.insert_iter(arg3);
230 }
231 Ex::As { expr, target: _ } => {
232 self.expressions_used.insert(expr);
233 }
234 Ex::Bitcast { expr, ty } => {
235 self.types_used.insert(ty);
236 self.expressions_used.insert(expr);
237 }
238 Ex::ArrayLength(expr) => {
239 self.expressions_used.insert(expr);
240 }
241 Ex::CallResult(_) => {}
245 Ex::AtomicResult { ty, comparison: _ }
246 | Ex::WorkGroupUniformLoadResult { ty }
247 | Ex::SubgroupOperationResult { ty } => {
248 self.types_used.insert(ty);
249 }
250 Ex::RayQueryGetIntersection {
251 query,
252 committed: _,
253 } => {
254 self.expressions_used.insert(query);
255 }
256 Ex::HitObjectQuery { hit_object, .. } => {
257 self.expressions_used.insert(hit_object);
258 }
259 Ex::CooperativeLoad { ref data, .. } => {
260 self.expressions_used.insert(data.pointer);
261 self.expressions_used.insert(data.stride);
262 }
263 Ex::CooperativeMultiplyAdd { a, b, c } => {
264 self.expressions_used.insert(a);
265 self.expressions_used.insert(b);
266 self.expressions_used.insert(c);
267 }
268 }
269 }
270}
271
272impl ModuleMap {
273 pub fn adjust_expression(
278 &self,
279 expr: &mut crate::Expression,
280 operand_map: &HandleMap<crate::Expression>,
281 ) {
282 let adjust = |expr: &mut Handle<crate::Expression>| {
283 operand_map.adjust(expr);
284 };
285
286 use crate::Expression as Ex;
287 match *expr {
288 Ex::Literal(_)
290 | Ex::FunctionArgument(_)
291 | Ex::LocalVariable(_)
292 | Ex::SubgroupBallotResult
293 | Ex::RayQueryProceedResult => {}
294
295 Ex::Constant(ref mut constant) => self.constants.adjust(constant),
297 Ex::Override(ref mut r#override) => self.overrides.adjust(r#override),
298 Ex::ZeroValue(ref mut ty) => self.types.adjust(ty),
299 Ex::Compose {
300 ref mut ty,
301 ref mut components,
302 } => {
303 self.types.adjust(ty);
304 for component in components {
305 adjust(component);
306 }
307 }
308 Ex::Access {
309 ref mut base,
310 ref mut index,
311 } => {
312 adjust(base);
313 adjust(index);
314 }
315 Ex::AccessIndex {
316 ref mut base,
317 index: _,
318 } => adjust(base),
319 Ex::Splat {
320 size: _,
321 ref mut value,
322 } => adjust(value),
323 Ex::Swizzle {
324 size: _,
325 ref mut vector,
326 pattern: _,
327 } => adjust(vector),
328 Ex::GlobalVariable(ref mut handle) => self.globals.adjust(handle),
329 Ex::Load { ref mut pointer } => adjust(pointer),
330 Ex::ImageSample {
331 ref mut image,
332 ref mut sampler,
333 gather: _,
334 ref mut coordinate,
335 ref mut array_index,
336 ref mut offset,
337 ref mut level,
338 ref mut depth_ref,
339 clamp_to_edge: _,
340 } => {
341 adjust(image);
342 adjust(sampler);
343 adjust(coordinate);
344 operand_map.adjust_option(array_index);
345 operand_map.adjust_option(offset);
346 self.adjust_sample_level(level, operand_map);
347 operand_map.adjust_option(depth_ref);
348 }
349 Ex::ImageLoad {
350 ref mut image,
351 ref mut coordinate,
352 ref mut array_index,
353 ref mut sample,
354 ref mut level,
355 } => {
356 adjust(image);
357 adjust(coordinate);
358 operand_map.adjust_option(array_index);
359 operand_map.adjust_option(sample);
360 operand_map.adjust_option(level);
361 }
362 Ex::ImageQuery {
363 ref mut image,
364 ref mut query,
365 } => {
366 adjust(image);
367 self.adjust_image_query(query, operand_map);
368 }
369 Ex::Unary {
370 op: _,
371 ref mut expr,
372 } => adjust(expr),
373 Ex::Binary {
374 op: _,
375 ref mut left,
376 ref mut right,
377 } => {
378 adjust(left);
379 adjust(right);
380 }
381 Ex::Select {
382 ref mut condition,
383 ref mut accept,
384 ref mut reject,
385 } => {
386 adjust(condition);
387 adjust(accept);
388 adjust(reject);
389 }
390 Ex::Derivative {
391 axis: _,
392 ctrl: _,
393 ref mut expr,
394 } => adjust(expr),
395 Ex::Relational {
396 fun: _,
397 ref mut argument,
398 } => adjust(argument),
399 Ex::Math {
400 fun: _,
401 ref mut arg,
402 ref mut arg1,
403 ref mut arg2,
404 ref mut arg3,
405 } => {
406 adjust(arg);
407 operand_map.adjust_option(arg1);
408 operand_map.adjust_option(arg2);
409 operand_map.adjust_option(arg3);
410 }
411 Ex::As {
412 ref mut expr,
413 target: _,
414 } => adjust(expr),
415 Ex::Bitcast {
416 ref mut expr,
417 ref mut ty,
418 } => {
419 adjust(expr);
420 self.types.adjust(ty);
421 }
422 Ex::CallResult(ref mut function) => {
423 self.functions.adjust(function);
424 }
425 Ex::AtomicResult {
426 ref mut ty,
427 comparison: _,
428 } => self.types.adjust(ty),
429 Ex::WorkGroupUniformLoadResult { ref mut ty } => self.types.adjust(ty),
430 Ex::SubgroupOperationResult { ref mut ty } => self.types.adjust(ty),
431 Ex::ArrayLength(ref mut expr) => adjust(expr),
432 Ex::RayQueryGetIntersection {
433 ref mut query,
434 committed: _,
435 } => adjust(query),
436 Ex::RayQueryVertexPositions {
437 ref mut query,
438 committed: _,
439 } => adjust(query),
440 Ex::HitObjectQuery {
441 ref mut hit_object,
442 query: _,
443 } => adjust(hit_object),
444 Ex::CooperativeLoad { ref mut data, .. } => {
445 adjust(&mut data.pointer);
446 adjust(&mut data.stride);
447 }
448 Ex::CooperativeMultiplyAdd {
449 ref mut a,
450 ref mut b,
451 ref mut c,
452 } => {
453 adjust(a);
454 adjust(b);
455 adjust(c);
456 }
457 }
458 }
459
460 fn adjust_sample_level(
461 &self,
462 level: &mut crate::SampleLevel,
463 operand_map: &HandleMap<crate::Expression>,
464 ) {
465 let adjust = |expr: &mut Handle<crate::Expression>| operand_map.adjust(expr);
466
467 use crate::SampleLevel as Sl;
468 match *level {
469 Sl::Auto | Sl::Zero => {}
470 Sl::Exact(ref mut expr) => adjust(expr),
471 Sl::Bias(ref mut expr) => adjust(expr),
472 Sl::Gradient {
473 ref mut x,
474 ref mut y,
475 } => {
476 adjust(x);
477 adjust(y);
478 }
479 }
480 }
481
482 fn adjust_image_query(
483 &self,
484 query: &mut crate::ImageQuery,
485 operand_map: &HandleMap<crate::Expression>,
486 ) {
487 use crate::ImageQuery as Iq;
488
489 match *query {
490 Iq::Size { ref mut level } => operand_map.adjust_option(level),
491 Iq::NumLevels | Iq::NumLayers | Iq::NumSamples => {}
492 }
493 }
494}