nak: Add a new ConstTracker struct

Reviewed-by: Mel Henning <mhenning@darkrefraction.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33785>
This commit is contained in:
Faith Ekstrand
2025-02-27 13:26:05 -06:00
committed by Marge Bot
parent 7180d56118
commit 0f4c7e0c0d
2 changed files with 60 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
// Copyright © 2025 Collabora, Ltd.
// SPDX-License-Identifier: MIT
use crate::ir::*;
use std::collections::HashMap;
pub struct ConstTracker {
map: HashMap<SSAValue, SrcRef>,
}
/// A tracker struct for finding re-materializable constants
///
/// Anything which is an immediate, Zero, or a bound cbuf can trivially be
/// re-materialized anywhere in the shader and it's probably cheaper to do so
/// than to try and keep them around in GPRs forever. This is just a helper
/// struct for implementing this logic in compiler passes.
impl ConstTracker {
pub fn new() -> Self {
ConstTracker {
map: HashMap::new(),
}
}
/// Registers a copy instruction
///
/// If the source of the copy is a constant, the destination SSA value and
/// the constant value get stored as a key/value pair.
pub fn add_copy(&mut self, op: &OpCopy) {
let Some(dst) = op.dst.as_ssa() else {
return;
};
debug_assert!(dst.comps() == 1);
let dst = dst[0];
debug_assert!(op.src.src_mod.is_none());
let is_const = match &op.src.src_ref {
SrcRef::Zero | SrcRef::True | SrcRef::False | SrcRef::Imm32(_) => {
true
}
SrcRef::CBuf(cb) => matches!(cb.buf, CBuf::Binding(_)),
_ => false,
};
if is_const {
self.map.insert(dst, op.src.src_ref);
}
}
/// Tests if the ConstTracker contains the given SSAValue
pub fn contains(&self, ssa: &SSAValue) -> bool {
self.map.contains_key(ssa)
}
/// Returns the SrcRef associated with this SSAValue, if any
pub fn get(&self, ssa: &SSAValue) -> Option<&SrcRef> {
self.map.get(ssa)
}
}
+1
View File
@@ -5,6 +5,7 @@ mod api;
mod assign_regs;
mod builder;
mod calc_instr_deps;
mod const_tracker;
mod from_nir;
mod ir;
mod legalize;