Skip to main content

naga/compact/
statements.rs

1use alloc::{vec, vec::Vec};
2
3use super::functions::FunctionTracer;
4use super::FunctionMap;
5use crate::arena::Handle;
6use crate::compact::handle_set_map::HandleMap;
7
8impl FunctionTracer<'_> {
9    pub fn trace_block(&mut self, block: &[crate::Statement]) {
10        self.trace_block_impl(block, false)
11    }
12
13    /// Like [`Self::trace_block`], but also mark every [`Emit`]ted expression
14    /// as used, whether or not anything needs its value.
15    ///
16    /// Compaction only keeps values something actually needs, so
17    /// `trace_block` ignores `Emit` statements. A consumer that has to account
18    /// for every expression the generated code *mentions*, rather than every
19    /// expression it needs, wants this instead: the MSL backend does no dead
20    /// code elimination, so it writes out a definition for each expression it
21    /// bakes whether or not anything reads it, and that definition refers to
22    /// the expression's operands.
23    ///
24    /// [`Emit`]: crate::Statement::Emit
25    #[cfg(msl_out)]
26    pub fn trace_block_including_emits(&mut self, block: &[crate::Statement]) {
27        self.trace_block_impl(block, true)
28    }
29
30    fn trace_block_impl(&mut self, block: &[crate::Statement], emits_are_used: bool) {
31        let mut worklist: Vec<&[crate::Statement]> = vec![block];
32        while let Some(last) = worklist.pop() {
33            for stmt in last {
34                use crate::Statement as St;
35                match *stmt {
36                    St::Emit(ref range) => {
37                        // If we come across a statement that actually uses an
38                        // expression in this range, it'll get traced from
39                        // there. But since evaluating expressions has no
40                        // effect, we don't need to assume that everything
41                        // emitted is live - unless our caller says otherwise.
42                        if emits_are_used {
43                            self.expressions_used.insert_iter(range.clone());
44                        }
45                    }
46                    St::Block(ref block) => worklist.push(block),
47                    St::If {
48                        condition,
49                        ref accept,
50                        ref reject,
51                    } => {
52                        self.expressions_used.insert(condition);
53                        worklist.push(accept);
54                        worklist.push(reject);
55                    }
56                    St::Switch {
57                        selector,
58                        ref cases,
59                    } => {
60                        self.expressions_used.insert(selector);
61                        for case in cases {
62                            worklist.push(&case.body);
63                        }
64                    }
65                    St::Loop {
66                        ref body,
67                        ref continuing,
68                        break_if,
69                    } => {
70                        if let Some(break_if) = break_if {
71                            self.expressions_used.insert(break_if);
72                        }
73                        worklist.push(body);
74                        worklist.push(continuing);
75                    }
76                    St::Return { value: Some(value) } => {
77                        self.expressions_used.insert(value);
78                    }
79                    St::Store { pointer, value } => {
80                        self.expressions_used.insert(pointer);
81                        self.expressions_used.insert(value);
82                    }
83                    St::ImageStore {
84                        image,
85                        coordinate,
86                        array_index,
87                        value,
88                    } => {
89                        self.expressions_used.insert(image);
90                        self.expressions_used.insert(coordinate);
91                        if let Some(array_index) = array_index {
92                            self.expressions_used.insert(array_index);
93                        }
94                        self.expressions_used.insert(value);
95                    }
96                    St::Atomic {
97                        pointer,
98                        ref fun,
99                        value,
100                        result,
101                    } => {
102                        self.expressions_used.insert(pointer);
103                        self.trace_atomic_function(fun);
104                        self.expressions_used.insert(value);
105                        if let Some(result) = result {
106                            self.expressions_used.insert(result);
107                        }
108                    }
109                    St::ImageAtomic {
110                        image,
111                        coordinate,
112                        array_index,
113                        fun: _,
114                        value,
115                    } => {
116                        self.expressions_used.insert(image);
117                        self.expressions_used.insert(coordinate);
118                        if let Some(array_index) = array_index {
119                            self.expressions_used.insert(array_index);
120                        }
121                        self.expressions_used.insert(value);
122                    }
123                    St::WorkGroupUniformLoad { pointer, result } => {
124                        self.expressions_used.insert(pointer);
125                        self.expressions_used.insert(result);
126                    }
127                    St::Call {
128                        function,
129                        ref arguments,
130                        result,
131                    } => {
132                        self.trace_call(function);
133                        for expr in arguments {
134                            self.expressions_used.insert(*expr);
135                        }
136                        if let Some(result) = result {
137                            self.expressions_used.insert(result);
138                        }
139                    }
140                    St::RayQuery { query, ref fun } => {
141                        self.expressions_used.insert(query);
142                        self.trace_ray_query_function(fun);
143                    }
144                    St::SubgroupBallot { result, predicate } => {
145                        if let Some(predicate) = predicate {
146                            self.expressions_used.insert(predicate);
147                        }
148                        self.expressions_used.insert(result);
149                    }
150                    St::SubgroupCollectiveOperation {
151                        op: _,
152                        collective_op: _,
153                        argument,
154                        result,
155                    } => {
156                        self.expressions_used.insert(argument);
157                        self.expressions_used.insert(result);
158                    }
159                    St::SubgroupGather {
160                        mode,
161                        argument,
162                        result,
163                    } => {
164                        match mode {
165                            crate::GatherMode::BroadcastFirst => {}
166                            crate::GatherMode::Broadcast(index)
167                            | crate::GatherMode::Shuffle(index)
168                            | crate::GatherMode::ShuffleDown(index)
169                            | crate::GatherMode::ShuffleUp(index)
170                            | crate::GatherMode::ShuffleXor(index)
171                            | crate::GatherMode::QuadBroadcast(index) => {
172                                self.expressions_used.insert(index);
173                            }
174                            crate::GatherMode::QuadSwap(_) => {}
175                        }
176                        self.expressions_used.insert(argument);
177                        self.expressions_used.insert(result);
178                    }
179                    St::SubgroupBallotFindBit {
180                        order: _,
181                        argument,
182                        result,
183                    } => {
184                        self.expressions_used.insert(argument);
185                        self.expressions_used.insert(result);
186                    }
187                    St::CooperativeStore { target, ref data } => {
188                        self.expressions_used.insert(target);
189                        self.expressions_used.insert(data.pointer);
190                        self.expressions_used.insert(data.stride);
191                    }
192                    St::RayPipelineFunction(func) => match func {
193                        crate::RayPipelineFunction::TraceRay {
194                            acceleration_structure,
195                            descriptor,
196                            payload,
197                        } => {
198                            self.expressions_used.insert(acceleration_structure);
199                            self.expressions_used.insert(descriptor);
200                            self.expressions_used.insert(payload);
201                        }
202                        crate::RayPipelineFunction::ReorderThread { hint, bits } => {
203                            self.expressions_used.insert(hint);
204                            self.expressions_used.insert(bits);
205                        }
206                    },
207                    St::HitObject {
208                        hit_object,
209                        ref fun,
210                    } => {
211                        self.expressions_used.insert(hit_object);
212                        self.trace_hit_object_function(fun);
213                    }
214                    St::DebugPrintf {
215                        format: _,
216                        ref arguments,
217                    } => {
218                        for &expr in arguments {
219                            self.expressions_used.insert(expr);
220                        }
221                    }
222
223                    // Trivial statements.
224                    St::Break
225                    | St::Continue
226                    | St::Kill
227                    | St::ControlBarrier(_)
228                    | St::MemoryBarrier(_)
229                    | St::Return { value: None } => {}
230                }
231            }
232        }
233    }
234
235    fn trace_atomic_function(&mut self, fun: &crate::AtomicFunction) {
236        use crate::AtomicFunction as Af;
237        match *fun {
238            Af::Exchange {
239                compare: Some(expr),
240            } => {
241                self.expressions_used.insert(expr);
242            }
243            Af::Exchange { compare: None }
244            | Af::Add
245            | Af::Subtract
246            | Af::And
247            | Af::ExclusiveOr
248            | Af::InclusiveOr
249            | Af::Min
250            | Af::Max => {}
251        }
252    }
253
254    fn trace_hit_object_function(&mut self, fun: &crate::HitObjectFunction) {
255        use crate::HitObjectFunction as Hf;
256        match *fun {
257            Hf::TraceRay {
258                acceleration_structure,
259                descriptor,
260                payload,
261            } => {
262                self.expressions_used.insert(acceleration_structure);
263                self.expressions_used.insert(descriptor);
264                self.expressions_used.insert(payload);
265            }
266            Hf::RecordMiss { descriptor } => {
267                self.expressions_used.insert(descriptor);
268            }
269            Hf::RecordFromQuery { query } => {
270                self.expressions_used.insert(query);
271            }
272            Hf::RecordEmpty => {}
273            Hf::ExecuteShader { payload } => {
274                self.expressions_used.insert(payload);
275            }
276            Hf::Reorder { hint } => {
277                self.trace_reorder_hint(hint);
278            }
279        }
280    }
281
282    fn trace_reorder_hint(&mut self, hint: Option<crate::ReorderHint>) {
283        if let Some(crate::ReorderHint { hint, bits }) = hint {
284            self.expressions_used.insert(hint);
285            self.expressions_used.insert(bits);
286        }
287    }
288
289    fn trace_ray_query_function(&mut self, fun: &crate::RayQueryFunction) {
290        use crate::RayQueryFunction as Qf;
291        match *fun {
292            Qf::Initialize {
293                acceleration_structure,
294                descriptor,
295            } => {
296                self.expressions_used.insert(acceleration_structure);
297                self.expressions_used.insert(descriptor);
298            }
299            Qf::Proceed { result } => {
300                self.expressions_used.insert(result);
301            }
302            Qf::GenerateIntersection { hit_t } => {
303                self.expressions_used.insert(hit_t);
304            }
305            Qf::ConfirmIntersection => {}
306            Qf::Terminate => {}
307            Qf::Begin => {}
308        }
309    }
310}
311
312impl FunctionMap {
313    /// Adjust statements in the body of `function`.
314    ///
315    /// Adjusts expressions using `self.expressions`, and adjusts calls to other
316    /// functions using `function_map`.
317    pub fn adjust_body(
318        &self,
319        function: &mut crate::Function,
320        function_map: &HandleMap<crate::Function>,
321    ) {
322        let block = &mut function.body;
323        let mut worklist: Vec<&mut [crate::Statement]> = vec![block];
324        let adjust = |handle: &mut Handle<crate::Expression>| {
325            self.expressions.adjust(handle);
326        };
327        while let Some(last) = worklist.pop() {
328            for stmt in last {
329                use crate::Statement as St;
330                match *stmt {
331                    St::Emit(ref mut range) => {
332                        self.expressions.adjust_range(range, &function.expressions);
333                    }
334                    St::Block(ref mut block) => worklist.push(block),
335                    St::If {
336                        ref mut condition,
337                        ref mut accept,
338                        ref mut reject,
339                    } => {
340                        adjust(condition);
341                        worklist.push(accept);
342                        worklist.push(reject);
343                    }
344                    St::Switch {
345                        ref mut selector,
346                        ref mut cases,
347                    } => {
348                        adjust(selector);
349                        for case in cases {
350                            worklist.push(&mut case.body);
351                        }
352                    }
353                    St::Loop {
354                        ref mut body,
355                        ref mut continuing,
356                        ref mut break_if,
357                    } => {
358                        if let Some(ref mut break_if) = *break_if {
359                            adjust(break_if);
360                        }
361                        worklist.push(body);
362                        worklist.push(continuing);
363                    }
364                    St::Return {
365                        value: Some(ref mut value),
366                    } => adjust(value),
367                    St::Store {
368                        ref mut pointer,
369                        ref mut value,
370                    } => {
371                        adjust(pointer);
372                        adjust(value);
373                    }
374                    St::ImageStore {
375                        ref mut image,
376                        ref mut coordinate,
377                        ref mut array_index,
378                        ref mut value,
379                    } => {
380                        adjust(image);
381                        adjust(coordinate);
382                        if let Some(ref mut array_index) = *array_index {
383                            adjust(array_index);
384                        }
385                        adjust(value);
386                    }
387                    St::Atomic {
388                        ref mut pointer,
389                        ref mut fun,
390                        ref mut value,
391                        ref mut result,
392                    } => {
393                        adjust(pointer);
394                        self.adjust_atomic_function(fun);
395                        adjust(value);
396                        if let Some(ref mut result) = *result {
397                            adjust(result);
398                        }
399                    }
400                    St::ImageAtomic {
401                        ref mut image,
402                        ref mut coordinate,
403                        ref mut array_index,
404                        fun: _,
405                        ref mut value,
406                    } => {
407                        adjust(image);
408                        adjust(coordinate);
409                        if let Some(ref mut array_index) = *array_index {
410                            adjust(array_index);
411                        }
412                        adjust(value);
413                    }
414                    St::WorkGroupUniformLoad {
415                        ref mut pointer,
416                        ref mut result,
417                    } => {
418                        adjust(pointer);
419                        adjust(result);
420                    }
421                    St::Call {
422                        ref mut function,
423                        ref mut arguments,
424                        ref mut result,
425                    } => {
426                        function_map.adjust(function);
427                        for expr in arguments {
428                            adjust(expr);
429                        }
430                        if let Some(ref mut result) = *result {
431                            adjust(result);
432                        }
433                    }
434                    St::RayQuery {
435                        ref mut query,
436                        ref mut fun,
437                    } => {
438                        adjust(query);
439                        self.adjust_ray_query_function(fun);
440                    }
441                    St::SubgroupBallot {
442                        ref mut result,
443                        ref mut predicate,
444                    } => {
445                        if let Some(ref mut predicate) = *predicate {
446                            adjust(predicate);
447                        }
448                        adjust(result);
449                    }
450                    St::SubgroupCollectiveOperation {
451                        op: _,
452                        collective_op: _,
453                        ref mut argument,
454                        ref mut result,
455                    } => {
456                        adjust(argument);
457                        adjust(result);
458                    }
459                    St::SubgroupGather {
460                        ref mut mode,
461                        ref mut argument,
462                        ref mut result,
463                    } => {
464                        match *mode {
465                            crate::GatherMode::BroadcastFirst => {}
466                            crate::GatherMode::Broadcast(ref mut index)
467                            | crate::GatherMode::Shuffle(ref mut index)
468                            | crate::GatherMode::ShuffleDown(ref mut index)
469                            | crate::GatherMode::ShuffleUp(ref mut index)
470                            | crate::GatherMode::ShuffleXor(ref mut index)
471                            | crate::GatherMode::QuadBroadcast(ref mut index) => adjust(index),
472                            crate::GatherMode::QuadSwap(_) => {}
473                        }
474                        adjust(argument);
475                        adjust(result);
476                    }
477                    St::SubgroupBallotFindBit {
478                        order: _,
479                        ref mut argument,
480                        ref mut result,
481                    } => {
482                        adjust(argument);
483                        adjust(result);
484                    }
485                    St::CooperativeStore {
486                        ref mut target,
487                        ref mut data,
488                    } => {
489                        adjust(target);
490                        adjust(&mut data.pointer);
491                        adjust(&mut data.stride);
492                    }
493                    St::RayPipelineFunction(ref mut func) => match *func {
494                        crate::RayPipelineFunction::TraceRay {
495                            ref mut acceleration_structure,
496                            ref mut descriptor,
497                            ref mut payload,
498                        } => {
499                            adjust(acceleration_structure);
500                            adjust(descriptor);
501                            adjust(payload);
502                        }
503                        crate::RayPipelineFunction::ReorderThread {
504                            ref mut hint,
505                            ref mut bits,
506                        } => {
507                            adjust(hint);
508                            adjust(bits);
509                        }
510                    },
511                    St::HitObject {
512                        ref mut hit_object,
513                        ref mut fun,
514                    } => {
515                        adjust(hit_object);
516                        self.adjust_hit_object_function(fun);
517                    }
518                    St::DebugPrintf {
519                        format: _,
520                        ref mut arguments,
521                    } => {
522                        for expr in arguments {
523                            adjust(expr);
524                        }
525                    }
526
527                    // Trivial statements.
528                    St::Break
529                    | St::Continue
530                    | St::Kill
531                    | St::ControlBarrier(_)
532                    | St::MemoryBarrier(_)
533                    | St::Return { value: None } => {}
534                }
535            }
536        }
537    }
538
539    fn adjust_atomic_function(&self, fun: &mut crate::AtomicFunction) {
540        use crate::AtomicFunction as Af;
541        match *fun {
542            Af::Exchange {
543                compare: Some(ref mut expr),
544            } => {
545                self.expressions.adjust(expr);
546            }
547            Af::Exchange { compare: None }
548            | Af::Add
549            | Af::Subtract
550            | Af::And
551            | Af::ExclusiveOr
552            | Af::InclusiveOr
553            | Af::Min
554            | Af::Max => {}
555        }
556    }
557
558    fn adjust_hit_object_function(&self, fun: &mut crate::HitObjectFunction) {
559        use crate::HitObjectFunction as Hf;
560        match *fun {
561            Hf::TraceRay {
562                ref mut acceleration_structure,
563                ref mut descriptor,
564                ref mut payload,
565            } => {
566                self.expressions.adjust(acceleration_structure);
567                self.expressions.adjust(descriptor);
568                self.expressions.adjust(payload);
569            }
570            Hf::RecordMiss { ref mut descriptor } => {
571                self.expressions.adjust(descriptor);
572            }
573            Hf::RecordFromQuery { ref mut query } => {
574                self.expressions.adjust(query);
575            }
576            Hf::RecordEmpty => {}
577            Hf::ExecuteShader { ref mut payload } => {
578                self.expressions.adjust(payload);
579            }
580            Hf::Reorder { ref mut hint } => {
581                self.adjust_reorder_hint(hint);
582            }
583        }
584    }
585
586    fn adjust_reorder_hint(&self, hint: &mut Option<crate::ReorderHint>) {
587        if let Some(crate::ReorderHint {
588            ref mut hint,
589            ref mut bits,
590        }) = *hint
591        {
592            self.expressions.adjust(hint);
593            self.expressions.adjust(bits);
594        }
595    }
596
597    fn adjust_ray_query_function(&self, fun: &mut crate::RayQueryFunction) {
598        use crate::RayQueryFunction as Qf;
599        match *fun {
600            Qf::Initialize {
601                ref mut acceleration_structure,
602                ref mut descriptor,
603            } => {
604                self.expressions.adjust(acceleration_structure);
605                self.expressions.adjust(descriptor);
606            }
607            Qf::Proceed { ref mut result } => {
608                self.expressions.adjust(result);
609            }
610            Qf::GenerateIntersection { ref mut hit_t } => {
611                self.expressions.adjust(hit_t);
612            }
613            Qf::ConfirmIntersection => {}
614            Qf::Terminate => {}
615            Qf::Begin => {}
616        }
617    }
618}