| 1 |
// Copyright © 2022 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::{ |
| 5 |
api::{GetDebugFlags, DEBUG}, |
| 6 |
ir::*, |
| 7 |
}; |
| 8 |
|
| 9 |
use rustc_hash::FxHashMap; |
| 10 |
|
| 11 |
struct CopyNode { |
| 12 |
num_reads: usize, |
| 13 |
src: isize, |
| 14 |
} |
| 15 |
|
| 16 |
struct CopyGraph { |
| 17 |
nodes: Vec<CopyNode>, |
| 18 |
} |
| 19 |
|
| 20 |
impl CopyGraph { |
| 21 |
pub fn new() -> CopyGraph { |
| 22 |
CopyGraph { nodes: Vec::new() } |
| 23 |
} |
| 24 |
|
| 25 |
fn add_node(&mut self) -> usize { |
| 26 |
let node_idx = self.nodes.len(); |
| 27 |
self.nodes.push(CopyNode { |
| 28 |
num_reads: 0, |
| 29 |
src: -1, |
| 30 |
}); |
| 31 |
node_idx |
| 32 |
} |
| 33 |
|
| 34 |
fn num_reads(&self, node_idx: usize) -> usize { |
| 35 |
self.nodes[node_idx].num_reads |
| 36 |
} |
| 37 |
|
| 38 |
fn src(&self, node_idx: usize) -> Option<usize> { |
| 39 |
if self.nodes[node_idx].src < 0 { |
| 40 |
None |
| 41 |
} else { |
| 42 |
Some(self.nodes[node_idx].src.try_into().unwrap()) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
fn add_edge(&mut self, dst_idx: usize, src_idx: usize) { |
| 47 |
// Disallow self-loops |
| 48 |
assert!(dst_idx != src_idx); |
| 49 |
|
| 50 |
// Each node has in-degree at most 1 |
| 51 |
assert!(self.nodes[dst_idx].src == -1); |
| 52 |
self.nodes[dst_idx].src = src_idx.try_into().unwrap(); |
| 53 |
self.nodes[src_idx].num_reads += 1; |
| 54 |
} |
| 55 |
|
| 56 |
fn del_edge(&mut self, dst_idx: usize, src_idx: usize) -> bool { |
| 57 |
assert!(self.nodes[dst_idx].src >= 0); |
| 58 |
self.nodes[dst_idx].src = -1; |
| 59 |
self.nodes[src_idx].num_reads -= 1; |
| 60 |
self.nodes[src_idx].num_reads == 0 |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
fn copy_needs_tmp(dst: &RegRef, src: &SrcRef) -> bool { |
| 65 |
if let Some(src_reg) = src.as_reg() { |
| 66 |
(dst.file() == RegFile::Mem && src_reg.file() != RegFile::GPR) |
| 67 |
|| (dst.file() == RegFile::Bar && src_reg.file() == RegFile::Bar) |
| 68 |
} else { |
| 69 |
// Non-GPR to Mem copies need a temporary |
| 70 |
dst.file() == RegFile::Mem |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
fn cycle_use_swap(pc: &OpParCopy, file: RegFile) -> bool { |
| 75 |
match file { |
| 76 |
RegFile::GPR => { |
| 77 |
if let Some(tmp) = &pc.tmp { |
| 78 |
debug_assert!(tmp.file() == RegFile::GPR); |
| 79 |
false |
| 80 |
} else { |
| 81 |
true |
| 82 |
} |
| 83 |
} |
| 84 |
RegFile::Bar | RegFile::Mem => { |
| 85 |
let tmp = &pc.tmp.expect("This copy needs a temporary"); |
| 86 |
assert!(tmp.comps() >= 2, "Memory cycles need 2 temporaries"); |
| 87 |
false |
| 88 |
} |
| 89 |
_ => true, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
fn lower_par_copy(pc: OpParCopy, sm: &ShaderModelInfo) -> MappedInstrs { |
| 94 |
let mut graph = CopyGraph::new(); |
| 95 |
let mut vals = Vec::new(); |
| 96 |
let mut reg_to_idx = FxHashMap::default(); |
| 97 |
|
| 98 |
for (i, (dst, _)) in pc.dsts_srcs.iter().enumerate() { |
| 99 |
// Destinations must be pairwise unique |
| 100 |
let reg = dst.as_reg().unwrap(); |
| 101 |
assert!(!reg_to_idx.contains_key(reg)); |
| 102 |
|
| 103 |
// Everything must be scalar |
| 104 |
assert!(reg.comps() == 1); |
| 105 |
|
| 106 |
let node_idx = graph.add_node(); |
| 107 |
assert!(node_idx == i && vals.len() == i); |
| 108 |
vals.push(SrcRef::from(*reg)); |
| 109 |
reg_to_idx.insert(*reg, i); |
| 110 |
} |
| 111 |
|
| 112 |
for (dst_idx, (_, src)) in pc.dsts_srcs.iter().enumerate() { |
| 113 |
assert!(src.is_unmodified()); |
| 114 |
let src = &src.src_ref; |
| 115 |
|
| 116 |
let src_idx = if let SrcRef::Reg(reg) = src { |
| 117 |
// Everything must be scalar |
| 118 |
assert!(reg.comps() == 1); |
| 119 |
|
| 120 |
*reg_to_idx.entry(*reg).or_insert_with(|| { |
| 121 |
let node_idx = graph.add_node(); |
| 122 |
assert!(node_idx == vals.len()); |
| 123 |
vals.push(src.clone()); |
| 124 |
node_idx |
| 125 |
}) |
| 126 |
} else { |
| 127 |
// We can't have bindless CBufs because we can't resolve cycles |
| 128 |
// containing one. |
| 129 |
assert!(src.get_reg().is_none()); |
| 130 |
|
| 131 |
let node_idx = graph.add_node(); |
| 132 |
assert!(node_idx == vals.len()); |
| 133 |
vals.push(src.clone()); |
| 134 |
node_idx |
| 135 |
}; |
| 136 |
|
| 137 |
if dst_idx != src_idx { |
| 138 |
graph.add_edge(dst_idx, src_idx); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
let mut b = InstrBuilder::new(sm); |
| 143 |
|
| 144 |
let mut ready = Vec::new(); |
| 145 |
for i in 0..pc.dsts_srcs.len() { |
| 146 |
if graph.num_reads(i) == 0 { |
| 147 |
ready.push(i); |
| 148 |
} |
| 149 |
} |
| 150 |
while let Some(dst_idx) = ready.pop() { |
| 151 |
if let Some(src_idx) = graph.src(dst_idx) { |
| 152 |
let dst = *vals[dst_idx].as_reg().unwrap(); |
| 153 |
let src = vals[src_idx].clone(); |
| 154 |
if copy_needs_tmp(&dst, &src) { |
| 155 |
let tmp = pc.tmp.expect("This copy needs a temporary").comp(0); |
| 156 |
b.copy_to(tmp.into(), src.into()); |
| 157 |
b.copy_to(dst.into(), tmp.into()); |
| 158 |
} else { |
| 159 |
b.copy_to(dst.into(), src.into()); |
| 160 |
} |
| 161 |
if graph.del_edge(dst_idx, src_idx) { |
| 162 |
ready.push(src_idx); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// At this point, all we are left with in the graph are isolated nodes |
| 168 |
// (no edges) and cycles. |
| 169 |
// |
| 170 |
// Proof: |
| 171 |
// |
| 172 |
// Without loss of generality, we can assume that there are no isolated |
| 173 |
// nodes in the graph. By construction, no node has in-degree more than 1 |
| 174 |
// (that would indicate a duplicate destination). The loop above ensures |
| 175 |
// that there are no nodes with an in-degree of 1 and an out-degree of 0. |
| 176 |
// |
| 177 |
// Suppose that there were a node with an in-degree of 0. Then, because no |
| 178 |
// node has an in-degree greater than 1, the number of edges must be less |
| 179 |
// than the number of nodes. This implies that there is some node N with |
| 180 |
// with out-degree of 0. If N has an in-degree of 0 then it is isolated, |
| 181 |
// which is a contradiction. If N has an in-degree of 1 then it is a node |
| 182 |
// with in-degree of 1 and out-degree of 0 which is also a contradiction. |
| 183 |
// Therefore, there are no nodes with in-degree of 0 and all nodes have an |
| 184 |
// in-degree of 1. |
| 185 |
// |
| 186 |
// Since all nodes have an in-degree of 1, no node has an out-degree of 0 |
| 187 |
// and, because the sum of all in-degrees equals the sum of all out-degrees |
| 188 |
// (they both equal the number of edges), every node must also have an |
| 189 |
// out-degree of 1. Therefore, the graph only contains cycles. |
| 190 |
// |
| 191 |
// QED |
| 192 |
for i in 0..pc.dsts_srcs.len() { |
| 193 |
if graph.src(i).is_none() { |
| 194 |
debug_assert!(graph.num_reads(i) == 0); |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
let file = vals[i].as_reg().unwrap().file(); |
| 199 |
if cycle_use_swap(&pc, file) { |
| 200 |
loop { |
| 201 |
let j = graph.src(i).unwrap(); |
| 202 |
// We're part of a cycle so j also has a source |
| 203 |
let k = graph.src(j).unwrap(); |
| 204 |
let j_reg = vals[j].as_reg().unwrap(); |
| 205 |
let k_reg = vals[k].as_reg().unwrap(); |
| 206 |
b.swap(*j_reg, *k_reg); |
| 207 |
graph.del_edge(i, j); |
| 208 |
graph.del_edge(j, k); |
| 209 |
if i == k { |
| 210 |
// This was our last swap |
| 211 |
break; |
| 212 |
} else { |
| 213 |
graph.add_edge(i, k); |
| 214 |
} |
| 215 |
} |
| 216 |
} else { |
| 217 |
let pc_tmp = pc.tmp.expect("This copy needs a temporary"); |
| 218 |
let tmp = pc_tmp.comp(0); |
| 219 |
|
| 220 |
let mut p = i; |
| 221 |
let mut p_reg = *vals[p].as_reg().unwrap(); |
| 222 |
b.copy_to(tmp.into(), p_reg.into()); |
| 223 |
|
| 224 |
loop { |
| 225 |
let j = graph.src(p).unwrap(); |
| 226 |
if j == i { |
| 227 |
break; |
| 228 |
} |
| 229 |
|
| 230 |
let j_reg = *vals[j].as_reg().unwrap(); |
| 231 |
debug_assert!(j_reg.file() == file); |
| 232 |
|
| 233 |
if file == RegFile::Bar || file == RegFile::Mem { |
| 234 |
let copy_tmp = pc_tmp.comp(1); |
| 235 |
b.copy_to(copy_tmp.into(), j_reg.into()); |
| 236 |
b.copy_to(p_reg.into(), copy_tmp.into()); |
| 237 |
} else { |
| 238 |
b.copy_to(p_reg.into(), j_reg.into()); |
| 239 |
} |
| 240 |
graph.del_edge(p, j); |
| 241 |
|
| 242 |
p = j; |
| 243 |
p_reg = j_reg; |
| 244 |
} |
| 245 |
|
| 246 |
b.copy_to(p_reg.into(), tmp.into()); |
| 247 |
graph.del_edge(p, i); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
b.into_mapped_instrs() |
| 252 |
} |
| 253 |
|
| 254 |
impl Shader<'_> { |
| 255 |
pub fn lower_par_copies(&mut self) { |
| 256 |
let sm = self.sm; |
| 257 |
self.map_instrs(|instr, _| -> MappedInstrs { |
| 258 |
match instr.op { |
| 259 |
Op::ParCopy(pc) => { |
| 260 |
assert!(instr.pred.is_true()); |
| 261 |
let mut instrs = Vec::new(); |
| 262 |
if DEBUG.annotate() { |
| 263 |
instrs.push(Instr::new(OpAnnotate { |
| 264 |
annotation: "par_copy lowered by lower_par_copy" |
| 265 |
.into(), |
| 266 |
})); |
| 267 |
} |
| 268 |
match lower_par_copy(*pc, sm) { |
| 269 |
MappedInstrs::None => { |
| 270 |
if let Some(instr) = instrs.pop() { |
| 271 |
MappedInstrs::One(instr) |
| 272 |
} else { |
| 273 |
MappedInstrs::None |
| 274 |
} |
| 275 |
} |
| 276 |
MappedInstrs::One(i) => { |
| 277 |
instrs.push(i); |
| 278 |
MappedInstrs::Many(instrs) |
| 279 |
} |
| 280 |
MappedInstrs::Many(i) => { |
| 281 |
instrs.extend(i); |
| 282 |
MappedInstrs::Many(instrs) |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
_ => MappedInstrs::One(instr), |
| 287 |
} |
| 288 |
}); |
| 289 |
} |
| 290 |
} |