| 1 |
// Copyright © 2023 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::api::{GetDebugFlags, DEBUG}; |
| 5 |
use crate::ir::*; |
| 6 |
|
| 7 |
use compiler::bitset::BitSet; |
| 8 |
use rustc_hash::FxHashMap; |
| 9 |
|
| 10 |
struct PhiMap { |
| 11 |
phi_ssa: FxHashMap<Phi, Vec<SSAValue>>, |
| 12 |
ssa_phi: FxHashMap<SSAValue, Phi>, |
| 13 |
} |
| 14 |
|
| 15 |
impl PhiMap { |
| 16 |
pub fn new() -> PhiMap { |
| 17 |
PhiMap { |
| 18 |
ssa_phi: Default::default(), |
| 19 |
phi_ssa: Default::default(), |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
fn add_phi_srcs(&mut self, op: &OpPhiSrcs) { |
| 24 |
for (phi, src) in op.srcs.iter() { |
| 25 |
if let SrcRef::SSA(ssa) = &src.src_ref { |
| 26 |
assert!(ssa.comps() == 1); |
| 27 |
let phi_srcs = self.phi_ssa.entry(*phi).or_default(); |
| 28 |
phi_srcs.push(ssa[0]); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
fn add_phi_dsts(&mut self, op: &OpPhiDsts) { |
| 34 |
for (phi, dst) in op.dsts.iter() { |
| 35 |
if let Dst::SSA(ssa) = dst { |
| 36 |
assert!(ssa.comps() == 1); |
| 37 |
self.ssa_phi.insert(ssa[0], *phi); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
fn get_phi(&self, ssa: &SSAValue) -> Option<&Phi> { |
| 43 |
self.ssa_phi.get(ssa) |
| 44 |
} |
| 45 |
|
| 46 |
fn phi_srcs(&self, idx: &Phi) -> &[SSAValue] { |
| 47 |
self.phi_ssa.get(idx).unwrap() |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[derive(Default)] |
| 52 |
struct BarPropPass { |
| 53 |
ssa_map: FxHashMap<SSAValue, SSAValue>, |
| 54 |
phi_is_bar: BitSet<Phi>, |
| 55 |
phi_is_not_bar: BitSet<Phi>, |
| 56 |
} |
| 57 |
|
| 58 |
impl BarPropPass { |
| 59 |
pub fn new() -> BarPropPass { |
| 60 |
Default::default() |
| 61 |
} |
| 62 |
|
| 63 |
fn add_copy(&mut self, dst: SSAValue, src: SSAValue) { |
| 64 |
assert!(dst.file() == RegFile::Bar || src.file() == RegFile::Bar); |
| 65 |
self.ssa_map.insert(dst, src); |
| 66 |
} |
| 67 |
|
| 68 |
fn is_bar(&self, ssa: &SSAValue) -> bool { |
| 69 |
ssa.file() == RegFile::Bar || self.ssa_map.contains_key(ssa) |
| 70 |
} |
| 71 |
|
| 72 |
fn map_bar<'a>(&'a self, ssa: &'a SSAValue) -> Option<&'a SSAValue> { |
| 73 |
let mut ssa = ssa; |
| 74 |
let mut last_bar = None; |
| 75 |
loop { |
| 76 |
let Some(mapped) = self.ssa_map.get(ssa) else { |
| 77 |
break; |
| 78 |
}; |
| 79 |
|
| 80 |
if mapped.file() == RegFile::Bar { |
| 81 |
last_bar = Some(mapped); |
| 82 |
} |
| 83 |
ssa = mapped; |
| 84 |
} |
| 85 |
|
| 86 |
last_bar |
| 87 |
} |
| 88 |
|
| 89 |
fn phi_can_be_bar_recur( |
| 90 |
&mut self, |
| 91 |
phi_map: &PhiMap, |
| 92 |
seen: &mut BitSet<Phi>, |
| 93 |
phi: Phi, |
| 94 |
) -> bool { |
| 95 |
if self.phi_is_not_bar.contains(phi) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
if seen.contains(phi) { |
| 100 |
// If we've hit a cycle, that's not a fail |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
seen.insert(phi); |
| 105 |
|
| 106 |
for src_ssa in phi_map.phi_srcs(&phi) { |
| 107 |
if self.is_bar(src_ssa) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
if let Some(src_phi) = phi_map.get_phi(src_ssa) { |
| 112 |
if self.phi_can_be_bar_recur(phi_map, seen, *src_phi) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
self.phi_is_not_bar.insert(phi); |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
true |
| 122 |
} |
| 123 |
|
| 124 |
fn add_phi_recur( |
| 125 |
&mut self, |
| 126 |
ssa_alloc: &mut SSAValueAllocator, |
| 127 |
phi_map: &PhiMap, |
| 128 |
needs_bar: &mut BitSet<Phi>, |
| 129 |
phi: Phi, |
| 130 |
ssa: SSAValue, |
| 131 |
) { |
| 132 |
if !needs_bar.contains(phi) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
let bar = ssa_alloc.alloc(RegFile::Bar); |
| 137 |
self.ssa_map.insert(ssa, bar); |
| 138 |
self.phi_is_bar.insert(phi); |
| 139 |
needs_bar.remove(phi); |
| 140 |
|
| 141 |
for src_ssa in phi_map.phi_srcs(&phi) { |
| 142 |
if let Some(src_phi) = phi_map.get_phi(src_ssa) { |
| 143 |
self.add_phi_recur( |
| 144 |
ssa_alloc, phi_map, needs_bar, *src_phi, *src_ssa, |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
fn try_add_phi( |
| 151 |
&mut self, |
| 152 |
ssa_alloc: &mut SSAValueAllocator, |
| 153 |
phi_map: &PhiMap, |
| 154 |
phi: Phi, |
| 155 |
ssa: SSAValue, |
| 156 |
) { |
| 157 |
if self.phi_is_bar.contains(phi) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
let mut seen = BitSet::new(); |
| 162 |
if self.phi_can_be_bar_recur(phi_map, &mut seen, phi) { |
| 163 |
self.add_phi_recur(ssa_alloc, phi_map, &mut seen, phi, ssa); |
| 164 |
assert!(seen.is_empty()); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
fn run(&mut self, f: &mut Function) { |
| 169 |
let mut phi_map = PhiMap::new(); |
| 170 |
let mut phis_want_bar = Vec::new(); |
| 171 |
for b in &f.blocks { |
| 172 |
for instr in &b.instrs { |
| 173 |
match &instr.op { |
| 174 |
Op::PhiSrcs(op) => { |
| 175 |
phi_map.add_phi_srcs(op); |
| 176 |
} |
| 177 |
Op::PhiDsts(op) => { |
| 178 |
phi_map.add_phi_dsts(op); |
| 179 |
} |
| 180 |
Op::BMov(op) => { |
| 181 |
assert!(!op.clear); |
| 182 |
assert!(op.src.is_unmodified()); |
| 183 |
let dst = op.dst.as_ssa().unwrap(); |
| 184 |
let src = op.src.as_ssa().unwrap(); |
| 185 |
assert!(dst.comps() == 1 && src.comps() == 1); |
| 186 |
|
| 187 |
self.add_copy(dst[0], src[0]); |
| 188 |
|
| 189 |
if let Some(phi) = phi_map.get_phi(&src[0]) { |
| 190 |
phis_want_bar.push((*phi, src[0])); |
| 191 |
} |
| 192 |
} |
| 193 |
_ => (), |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
for (phi, ssa) in phis_want_bar.drain(..) { |
| 199 |
self.try_add_phi(&mut f.ssa_alloc, &phi_map, phi, ssa); |
| 200 |
} |
| 201 |
|
| 202 |
f.map_instrs(|mut instr, _| { |
| 203 |
match &mut instr.op { |
| 204 |
Op::PhiSrcs(op) => { |
| 205 |
for (idx, src) in op.srcs.iter_mut() { |
| 206 |
if self.phi_is_bar.contains(*idx) { |
| 207 |
// Barrier immediates don't exist |
| 208 |
let ssa = src.as_ssa().unwrap(); |
| 209 |
let bar = *self.map_bar(&ssa[0]).unwrap(); |
| 210 |
*src = bar.into(); |
| 211 |
} |
| 212 |
} |
| 213 |
MappedInstrs::One(instr) |
| 214 |
} |
| 215 |
Op::PhiDsts(op) => { |
| 216 |
let mut bmovs = Vec::new(); |
| 217 |
for (idx, dst) in op.dsts.iter_mut() { |
| 218 |
if self.phi_is_bar.contains(*idx) { |
| 219 |
let ssa = dst.as_ssa().unwrap().clone(); |
| 220 |
let bar = *self.ssa_map.get(&ssa[0]).unwrap(); |
| 221 |
*dst = bar.into(); |
| 222 |
|
| 223 |
// On the off chance that someone still wants the |
| 224 |
// GPR version of this barrier, insert an OpBMov to |
| 225 |
// copy into the GPR. DCE will clean it up if it's |
| 226 |
// never used. |
| 227 |
bmovs.push(Instr::new(OpBMov { |
| 228 |
dst: ssa.into(), |
| 229 |
src: bar.into(), |
| 230 |
clear: false, |
| 231 |
})); |
| 232 |
} |
| 233 |
} |
| 234 |
if bmovs.is_empty() { |
| 235 |
MappedInstrs::One(instr) |
| 236 |
} else { |
| 237 |
if DEBUG.annotate() { |
| 238 |
bmovs.insert( |
| 239 |
0, |
| 240 |
Instr::new(OpAnnotate { |
| 241 |
annotation: "generated by opt_bar_prop" |
| 242 |
.into(), |
| 243 |
}), |
| 244 |
); |
| 245 |
} |
| 246 |
bmovs.insert(1, instr); |
| 247 |
MappedInstrs::Many(bmovs) |
| 248 |
} |
| 249 |
} |
| 250 |
_ => { |
| 251 |
let src_types = instr.src_types(); |
| 252 |
for (i, src) in instr.srcs_mut().iter_mut().enumerate() { |
| 253 |
if src_types[i] != SrcType::Bar { |
| 254 |
continue; |
| 255 |
} |
| 256 |
if let SrcRef::SSA(ssa) = &src.src_ref { |
| 257 |
if let Some(bar) = self.map_bar(&ssa[0]) { |
| 258 |
*src = (*bar).into(); |
| 259 |
} |
| 260 |
}; |
| 261 |
} |
| 262 |
MappedInstrs::One(instr) |
| 263 |
} |
| 264 |
} |
| 265 |
}); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
impl Shader<'_> { |
| 270 |
pub fn opt_bar_prop(&mut self) { |
| 271 |
for f in &mut self.functions { |
| 272 |
BarPropPass::new().run(f); |
| 273 |
} |
| 274 |
} |
| 275 |
} |