Debian Codegraph @

src/nouveau/compiler/nak/const_tracker.rs

raw
1 // Copyright © 2025 Collabora, Ltd.
2 // SPDX-License-Identifier: MIT
3
4 use crate::ir::*;
5
6 use rustc_hash::FxHashMap;
7
8 pub struct ConstTracker {
9 map: FxHashMap<SSAValue, SrcRef>,
10 }
11
12 /// A tracker struct for finding re-materializable constants
13 ///
14 /// Anything which is an immediate, Zero, or a bound cbuf can trivially be
15 /// re-materialized anywhere in the shader and it's probably cheaper to do so
16 /// than to try and keep them around in GPRs forever. This is just a helper
17 /// struct for implementing this logic in compiler passes.
18 impl ConstTracker {
19 pub fn new() -> Self {
20 ConstTracker {
21 map: Default::default(),
22 }
23 }
24
25 /// Registers a copy instruction
26 ///
27 /// If the source of the copy is a constant, the destination SSA value and
28 /// the constant value get stored as a key/value pair.
29 pub fn add_copy(&mut self, op: &OpCopy) {
30 let Some(dst) = op.dst.as_ssa() else {
31 return;
32 };
33 debug_assert!(dst.comps() == 1);
34 let dst = dst[0];
35
36 debug_assert!(op.src.is_unmodified());
37 let is_const = match &op.src.src_ref {
38 SrcRef::Zero | SrcRef::True | SrcRef::False | SrcRef::Imm32(_) => {
39 true
40 }
41 SrcRef::CBuf(cb) => matches!(cb.buf, CBuf::Binding(_)),
42 _ => false,
43 };
44
45 if is_const {
46 self.map.insert(dst, op.src.src_ref.clone());
47 }
48 }
49
50 /// Tests if the ConstTracker contains the given SSAValue
51 pub fn contains(&self, ssa: &SSAValue) -> bool {
52 self.map.contains_key(ssa)
53 }
54
55 /// Returns the SrcRef associated with this SSAValue, if any
56 pub fn get(&self, ssa: &SSAValue) -> Option<&SrcRef> {
57 self.map.get(ssa)
58 }
59 }