| 1 |
// Copyright © 2024 Valve Corporation |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::ir::*; |
| 5 |
use crate::opt_instr_sched_common::*; |
| 6 |
use crate::reg_tracker::RegRefIterable; |
| 7 |
use crate::reg_tracker::RegTracker; |
| 8 |
use std::cmp::max; |
| 9 |
use std::cmp::Reverse; |
| 10 |
use std::collections::BinaryHeap; |
| 11 |
|
| 12 |
struct RegUse<T: Clone> { |
| 13 |
reads: Vec<T>, |
| 14 |
write: Option<T>, |
| 15 |
} |
| 16 |
|
| 17 |
impl<T: Clone> RegUse<T> { |
| 18 |
pub fn new() -> Self { |
| 19 |
RegUse { |
| 20 |
reads: Vec::new(), |
| 21 |
write: None, |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
pub fn add_read(&mut self, dep: T) { |
| 26 |
self.reads.push(dep); |
| 27 |
} |
| 28 |
|
| 29 |
pub fn set_write(&mut self, dep: T) { |
| 30 |
self.write = Some(dep); |
| 31 |
self.reads.clear(); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
fn generate_dep_graph(sm: &ShaderModelInfo, instrs: &[Instr]) -> DepGraph { |
| 36 |
let mut g = DepGraph::new((0..instrs.len()).map(|_| Default::default())); |
| 37 |
|
| 38 |
// Maps registers to RegUse<ip, src_dst_idx>. Predicates are |
| 39 |
// represented by src_idx = usize::MAX. |
| 40 |
let mut uses: Box<RegTracker<RegUse<(usize, usize)>>> = |
| 41 |
Box::new(RegTracker::new_with(&|| RegUse::new())); |
| 42 |
|
| 43 |
let mut last_memory_op = None; |
| 44 |
let mut last_barrier_op = None; |
| 45 |
|
| 46 |
for ip in (0..instrs.len()).rev() { |
| 47 |
let instr = &instrs[ip]; |
| 48 |
|
| 49 |
if let Some(bar_ip) = last_barrier_op { |
| 50 |
g.add_edge(ip, bar_ip, EdgeLabel { latency: 0 }); |
| 51 |
} |
| 52 |
|
| 53 |
match side_effect_type(&instr.op) { |
| 54 |
SideEffect::None => (), |
| 55 |
SideEffect::Barrier => { |
| 56 |
let last_ip = last_barrier_op.unwrap_or(instrs.len()); |
| 57 |
for other_ip in (ip + 1)..last_ip { |
| 58 |
g.add_edge(ip, other_ip, EdgeLabel { latency: 0 }); |
| 59 |
} |
| 60 |
last_barrier_op = Some(ip); |
| 61 |
} |
| 62 |
SideEffect::Memory => { |
| 63 |
if let Some(mem_ip) = last_memory_op { |
| 64 |
g.add_edge(ip, mem_ip, EdgeLabel { latency: 0 }); |
| 65 |
} |
| 66 |
last_memory_op = Some(ip); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
uses.for_each_instr_dst_mut(instr, |i, u| { |
| 71 |
if let Some((w_ip, w_dst_idx)) = u.write { |
| 72 |
let mut latency = sm.waw_latency( |
| 73 |
&instr.op, |
| 74 |
i, |
| 75 |
!instr.pred.pred_ref.is_none(), |
| 76 |
&instrs[w_ip].op, |
| 77 |
w_dst_idx, |
| 78 |
); |
| 79 |
if sm.op_needs_scoreboard(&instr.op) { |
| 80 |
// Barriers take two cycles to become active |
| 81 |
latency = max(latency, 2); |
| 82 |
} |
| 83 |
g.add_edge(ip, w_ip, EdgeLabel { latency }); |
| 84 |
} |
| 85 |
|
| 86 |
for &(r_ip, r_src_idx) in &u.reads { |
| 87 |
let mut latency = if r_src_idx == usize::MAX { |
| 88 |
sm.paw_latency(&instr.op, i) |
| 89 |
} else { |
| 90 |
sm.raw_latency(&instr.op, i, &instrs[r_ip].op, r_src_idx) |
| 91 |
}; |
| 92 |
|
| 93 |
latency = |
| 94 |
max(latency, estimate_variable_latency(sm, &instr.op)); |
| 95 |
|
| 96 |
g.add_edge(ip, r_ip, EdgeLabel { latency }); |
| 97 |
} |
| 98 |
}); |
| 99 |
uses.for_each_instr_src_mut(instr, |i, u| { |
| 100 |
if let Some((w_ip, w_dst_idx)) = u.write { |
| 101 |
let mut latency = |
| 102 |
sm.war_latency(&instr.op, i, &instrs[w_ip].op, w_dst_idx); |
| 103 |
if sm.op_needs_scoreboard(&instr.op) { |
| 104 |
// Barriers take two cycles to become active |
| 105 |
latency = max(latency, 2); |
| 106 |
} |
| 107 |
g.add_edge(ip, w_ip, EdgeLabel { latency }); |
| 108 |
} |
| 109 |
}); |
| 110 |
|
| 111 |
// We're iterating in reverse, so writes are logically first |
| 112 |
uses.for_each_instr_dst_mut(instr, |i, c| { |
| 113 |
c.set_write((ip, i)); |
| 114 |
}); |
| 115 |
uses.for_each_instr_pred_mut(instr, |c| { |
| 116 |
c.add_read((ip, usize::MAX)); |
| 117 |
}); |
| 118 |
uses.for_each_instr_src_mut(instr, |i, c| { |
| 119 |
c.add_read((ip, i)); |
| 120 |
}); |
| 121 |
|
| 122 |
// Initialize this node's distance to the end |
| 123 |
let mut ready_cycle = (0..instr.dsts().len()) |
| 124 |
.map(|i| sm.worst_latency(&instr.op, i)) |
| 125 |
.max() |
| 126 |
.unwrap_or(0); |
| 127 |
|
| 128 |
let var_latency = estimate_variable_latency(sm, &instr.op) |
| 129 |
+ sm.exec_latency(&instrs[instrs.len() - 1].op); |
| 130 |
ready_cycle = max(ready_cycle, var_latency); |
| 131 |
|
| 132 |
let label = &mut g.nodes[ip].label; |
| 133 |
label.exec_latency = sm.exec_latency(&instr.op); |
| 134 |
label.ready_cycle = ready_cycle; |
| 135 |
} |
| 136 |
|
| 137 |
g |
| 138 |
} |
| 139 |
|
| 140 |
fn generate_order( |
| 141 |
g: &mut DepGraph, |
| 142 |
init_ready_list: Vec<usize>, |
| 143 |
) -> (Vec<usize>, u64) { |
| 144 |
let mut ready_instrs: BinaryHeap<ReadyInstr> = BinaryHeap::new(); |
| 145 |
let mut future_ready_instrs: BinaryHeap<FutureReadyInstr> = init_ready_list |
| 146 |
.into_iter() |
| 147 |
.map(|i| FutureReadyInstr::new(g, i)) |
| 148 |
.collect(); |
| 149 |
|
| 150 |
let mut current_cycle = 0u32; |
| 151 |
let mut instr_order = Vec::with_capacity(g.nodes.len()); |
| 152 |
loop { |
| 153 |
// Move ready instructions to the ready list |
| 154 |
loop { |
| 155 |
let Some(fri) = future_ready_instrs.peek() else { |
| 156 |
break; |
| 157 |
}; |
| 158 |
if current_cycle < fri.ready_cycle.0 { |
| 159 |
break; |
| 160 |
} |
| 161 |
ready_instrs.push(ReadyInstr::new(g, fri.index)); |
| 162 |
future_ready_instrs.pop(); |
| 163 |
} |
| 164 |
|
| 165 |
// Pick a ready instruction |
| 166 |
let next_idx = match ready_instrs.pop() { |
| 167 |
None => match future_ready_instrs.peek() { |
| 168 |
None => break, // Both lists are empty. We're done! |
| 169 |
Some(&FutureReadyInstr { |
| 170 |
ready_cycle: Reverse(ready_cycle), |
| 171 |
.. |
| 172 |
}) => { |
| 173 |
// Fast-forward time to when the next instr is ready |
| 174 |
assert!(ready_cycle > current_cycle); |
| 175 |
current_cycle = ready_cycle; |
| 176 |
continue; |
| 177 |
} |
| 178 |
}, |
| 179 |
Some(ReadyInstr { index, .. }) => index, |
| 180 |
}; |
| 181 |
|
| 182 |
// Schedule the instuction |
| 183 |
instr_order.push(next_idx); |
| 184 |
current_cycle += g.nodes[next_idx].label.exec_latency; |
| 185 |
|
| 186 |
let outgoing_edges = |
| 187 |
std::mem::take(&mut g.nodes[next_idx].outgoing_edges); |
| 188 |
for edge in outgoing_edges.into_iter() { |
| 189 |
let dep_instr = &mut g.nodes[edge.head_idx].label; |
| 190 |
dep_instr.ready_cycle = |
| 191 |
max(dep_instr.ready_cycle, current_cycle + edge.label.latency); |
| 192 |
dep_instr.num_uses -= 1; |
| 193 |
if dep_instr.num_uses == 0 { |
| 194 |
future_ready_instrs |
| 195 |
.push(FutureReadyInstr::new(g, edge.head_idx)); |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
(instr_order, u64::from(current_cycle)) |
| 201 |
} |
| 202 |
|
| 203 |
fn sched_buffer( |
| 204 |
sm: &ShaderModelInfo, |
| 205 |
instrs: Vec<Instr>, |
| 206 |
) -> (impl Iterator<Item = Instr> + use<>, u64) { |
| 207 |
let mut g = generate_dep_graph(sm, &instrs); |
| 208 |
let init_ready_list = calc_statistics(&mut g); |
| 209 |
// save_graphviz(&instrs, &g).unwrap(); |
| 210 |
g.reverse(); |
| 211 |
let (new_order, cycle_count) = generate_order(&mut g, init_ready_list); |
| 212 |
|
| 213 |
// Apply the new instruction order |
| 214 |
let mut instrs: Vec<Option<Instr>> = instrs.into_iter().map(Some).collect(); |
| 215 |
let instrs = new_order.into_iter().rev().map(move |i| { |
| 216 |
std::mem::take(&mut instrs[i]).expect("Instruction scheduled twice") |
| 217 |
}); |
| 218 |
(instrs, cycle_count) |
| 219 |
} |
| 220 |
|
| 221 |
impl Function { |
| 222 |
pub fn opt_instr_sched_postpass(&mut self, sm: &ShaderModelInfo) -> u64 { |
| 223 |
let mut num_static_cycles = 0u64; |
| 224 |
for i in 0..self.blocks.len() { |
| 225 |
let block = &mut self.blocks[i]; |
| 226 |
|
| 227 |
let orig_instr_count = block.instrs.len(); |
| 228 |
let instrs = std::mem::take(&mut block.instrs); |
| 229 |
let (instrs, cycle_count) = sched_buffer(sm, instrs); |
| 230 |
block.instrs = instrs.collect(); |
| 231 |
assert_eq!(orig_instr_count, block.instrs.len()); |
| 232 |
|
| 233 |
let block_weight = estimate_block_weight(&self.blocks, i); |
| 234 |
num_static_cycles = cycle_count |
| 235 |
.checked_mul(block_weight) |
| 236 |
.expect("Cycle count estimate overflow") |
| 237 |
.checked_add(num_static_cycles) |
| 238 |
.expect("Cycle count estimate overflow"); |
| 239 |
} |
| 240 |
num_static_cycles |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
impl Shader<'_> { |
| 245 |
/// Post-RA instruction scheduling |
| 246 |
/// |
| 247 |
/// Uses the popular latency-weighted-depth heuristic. |
| 248 |
/// See eg. Cooper & Torczon's "Engineering A Compiler", 3rd ed. |
| 249 |
/// Chapter 12.3 "Local scheduling" |
| 250 |
pub fn opt_instr_sched_postpass(&mut self) { |
| 251 |
self.info.num_static_cycles = 0; |
| 252 |
for f in &mut self.functions { |
| 253 |
self.info.num_static_cycles += f.opt_instr_sched_postpass(self.sm); |
| 254 |
} |
| 255 |
} |
| 256 |
} |