| 1 |
// Copyright © 2024 Valve Corporation |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::ir::*; |
| 5 |
use compiler::cfg::CFG; |
| 6 |
use std::cmp::max; |
| 7 |
use std::cmp::Reverse; |
| 8 |
|
| 9 |
pub mod graph { |
| 10 |
#[derive(Clone)] |
| 11 |
pub struct Edge<EdgeLabel> { |
| 12 |
pub label: EdgeLabel, |
| 13 |
pub head_idx: usize, |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(Clone)] |
| 17 |
pub struct Node<NodeLabel, EdgeLabel> { |
| 18 |
pub label: NodeLabel, |
| 19 |
pub outgoing_edges: Vec<Edge<EdgeLabel>>, |
| 20 |
} |
| 21 |
|
| 22 |
#[derive(Clone)] |
| 23 |
pub struct Graph<NodeLabel, EdgeLabel> { |
| 24 |
pub nodes: Vec<Node<NodeLabel, EdgeLabel>>, |
| 25 |
} |
| 26 |
|
| 27 |
impl<NodeLabel, EdgeLabel> Graph<NodeLabel, EdgeLabel> { |
| 28 |
pub fn new(node_labels: impl Iterator<Item = NodeLabel>) -> Self { |
| 29 |
let nodes = node_labels |
| 30 |
.map(|label| Node { |
| 31 |
label, |
| 32 |
outgoing_edges: Vec::new(), |
| 33 |
}) |
| 34 |
.collect(); |
| 35 |
|
| 36 |
Graph { nodes } |
| 37 |
} |
| 38 |
|
| 39 |
pub fn add_edge( |
| 40 |
&mut self, |
| 41 |
tail_idx: usize, |
| 42 |
head_idx: usize, |
| 43 |
label: EdgeLabel, |
| 44 |
) { |
| 45 |
assert!(head_idx < self.nodes.len()); |
| 46 |
self.nodes[tail_idx] |
| 47 |
.outgoing_edges |
| 48 |
.push(Edge { label, head_idx }); |
| 49 |
} |
| 50 |
|
| 51 |
pub fn reverse(&mut self) { |
| 52 |
let old_edges: Vec<_> = self |
| 53 |
.nodes |
| 54 |
.iter_mut() |
| 55 |
.map(|node| std::mem::take(&mut node.outgoing_edges)) |
| 56 |
.collect(); |
| 57 |
|
| 58 |
for (tail_idx, edges) in old_edges.into_iter().enumerate() { |
| 59 |
for e in edges.into_iter() { |
| 60 |
self.add_edge(e.head_idx, tail_idx, e.label); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[derive(Eq, PartialEq)] |
| 68 |
pub enum SideEffect { |
| 69 |
/// No side effect (ALU-like) |
| 70 |
None, |
| 71 |
|
| 72 |
/// Instruction reads or writes memory |
| 73 |
/// |
| 74 |
/// This will be serialized with respect to other |
| 75 |
/// SideEffect::Memory instructions |
| 76 |
Memory, |
| 77 |
|
| 78 |
/// This instcuction is a full code motion barrier |
| 79 |
/// |
| 80 |
/// No other instruction will be re-ordered with respect to this one |
| 81 |
Barrier, |
| 82 |
} |
| 83 |
|
| 84 |
pub fn side_effect_type(op: &Op) -> SideEffect { |
| 85 |
match op { |
| 86 |
// Float ALU |
| 87 |
Op::F2FP(_) |
| 88 |
| Op::FAdd(_) |
| 89 |
| Op::FFma(_) |
| 90 |
| Op::FMnMx(_) |
| 91 |
| Op::FMul(_) |
| 92 |
| Op::FSet(_) |
| 93 |
| Op::FSetP(_) |
| 94 |
| Op::HAdd2(_) |
| 95 |
| Op::HFma2(_) |
| 96 |
| Op::HMul2(_) |
| 97 |
| Op::HSet2(_) |
| 98 |
| Op::HSetP2(_) |
| 99 |
| Op::HMnMx2(_) |
| 100 |
| Op::FSwz(_) |
| 101 |
| Op::FSwzAdd(_) => SideEffect::None, |
| 102 |
|
| 103 |
// Multi-function unit |
| 104 |
Op::Rro(_) | Op::MuFu(_) => SideEffect::None, |
| 105 |
|
| 106 |
// Double-precision float ALU |
| 107 |
Op::DAdd(_) |
| 108 |
| Op::DFma(_) |
| 109 |
| Op::DMnMx(_) |
| 110 |
| Op::DMul(_) |
| 111 |
| Op::DSetP(_) => SideEffect::None, |
| 112 |
|
| 113 |
// Integer ALU |
| 114 |
Op::BRev(_) |
| 115 |
| Op::Flo(_) |
| 116 |
| Op::PopC(_) |
| 117 |
| Op::IMad(_) |
| 118 |
| Op::IMul(_) |
| 119 |
| Op::BMsk(_) |
| 120 |
| Op::IAbs(_) |
| 121 |
| Op::IAdd2(_) |
| 122 |
| Op::IAdd2X(_) |
| 123 |
| Op::IAdd3(_) |
| 124 |
| Op::IAdd3X(_) |
| 125 |
| Op::IDp4(_) |
| 126 |
| Op::IMad64(_) |
| 127 |
| Op::IMnMx(_) |
| 128 |
| Op::ISetP(_) |
| 129 |
| Op::Lea(_) |
| 130 |
| Op::LeaX(_) |
| 131 |
| Op::Lop2(_) |
| 132 |
| Op::Lop3(_) |
| 133 |
| Op::SuClamp(_) |
| 134 |
| Op::SuBfm(_) |
| 135 |
| Op::SuEau(_) |
| 136 |
| Op::IMadSp(_) |
| 137 |
| Op::Shf(_) |
| 138 |
| Op::Shl(_) |
| 139 |
| Op::Shr(_) |
| 140 |
| Op::Bfe(_) => SideEffect::None, |
| 141 |
|
| 142 |
// Conversions |
| 143 |
Op::F2F(_) | Op::F2I(_) | Op::I2F(_) | Op::I2I(_) | Op::FRnd(_) => { |
| 144 |
SideEffect::None |
| 145 |
} |
| 146 |
|
| 147 |
// Move ops |
| 148 |
Op::Mov(_) | Op::Prmt(_) | Op::Sel(_) | Op::Sgxt(_) => SideEffect::None, |
| 149 |
Op::Shfl(_) => SideEffect::None, |
| 150 |
|
| 151 |
// Predicate ops |
| 152 |
Op::PLop3(_) | Op::PSetP(_) => SideEffect::None, |
| 153 |
|
| 154 |
// Uniform ops |
| 155 |
Op::R2UR(_) | Op::Redux(_) => SideEffect::None, |
| 156 |
|
| 157 |
// Texture ops |
| 158 |
Op::Tex(_) |
| 159 |
| Op::Tld(_) |
| 160 |
| Op::Tld4(_) |
| 161 |
| Op::Tmml(_) |
| 162 |
| Op::Txd(_) |
| 163 |
| Op::Txq(_) => SideEffect::Memory, |
| 164 |
|
| 165 |
// Surface ops |
| 166 |
Op::SuLd(_) |
| 167 |
| Op::SuSt(_) |
| 168 |
| Op::SuAtom(_) |
| 169 |
| Op::SuLdGa(_) |
| 170 |
| Op::SuStGa(_) => SideEffect::Memory, |
| 171 |
|
| 172 |
// Memory ops |
| 173 |
Op::Ipa(_) | Op::Ldc(_) => SideEffect::None, |
| 174 |
Op::Ld(_) |
| 175 |
| Op::Ldsm(_) |
| 176 |
| Op::LdSharedLock(_) |
| 177 |
| Op::St(_) |
| 178 |
| Op::StSCheckUnlock(_) |
| 179 |
| Op::Atom(_) |
| 180 |
| Op::AL2P(_) |
| 181 |
| Op::ALd(_) |
| 182 |
| Op::ASt(_) |
| 183 |
| Op::CCtl(_) |
| 184 |
| Op::LdTram(_) |
| 185 |
| Op::MemBar(_) => SideEffect::Memory, |
| 186 |
|
| 187 |
// Matrix ops |
| 188 |
Op::Imma(_) | Op::Hmma(_) | Op::Movm(_) => SideEffect::None, |
| 189 |
|
| 190 |
// Control-flow ops |
| 191 |
Op::BClear(_) |
| 192 |
| Op::Break(_) |
| 193 |
| Op::BSSy(_) |
| 194 |
| Op::BSync(_) |
| 195 |
| Op::SSy(_) |
| 196 |
| Op::Sync(_) |
| 197 |
| Op::Brk(_) |
| 198 |
| Op::PBk(_) |
| 199 |
| Op::Cont(_) |
| 200 |
| Op::PCnt(_) |
| 201 |
| Op::Bra(_) |
| 202 |
| Op::Exit(_) |
| 203 |
| Op::WarpSync(_) => SideEffect::Barrier, |
| 204 |
|
| 205 |
// We don't model the barrier register yet, so serialize these |
| 206 |
Op::BMov(_) => SideEffect::Memory, |
| 207 |
|
| 208 |
// Geometry ops |
| 209 |
Op::Out(_) | Op::OutFinal(_) => SideEffect::Barrier, |
| 210 |
|
| 211 |
// Miscellaneous ops |
| 212 |
Op::Bar(_) |
| 213 |
| Op::TexDepBar(_) |
| 214 |
| Op::CS2R(_) |
| 215 |
| Op::Isberd(_) |
| 216 |
| Op::ViLd(_) |
| 217 |
| Op::Kill(_) |
| 218 |
| Op::S2R(_) => SideEffect::Barrier, |
| 219 |
Op::PixLd(_) | Op::Vote(_) | Op::Match(_) => SideEffect::None, |
| 220 |
Op::Nop(OpNop { label, .. }) => { |
| 221 |
if label.is_none() { |
| 222 |
SideEffect::None |
| 223 |
} else { |
| 224 |
SideEffect::Barrier |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
// Virtual ops |
| 229 |
Op::Annotate(_) |
| 230 |
| Op::ParCopy(_) |
| 231 |
| Op::Swap(_) |
| 232 |
| Op::Copy(_) |
| 233 |
| Op::Undef(_) => SideEffect::None, |
| 234 |
|
| 235 |
Op::SrcBar(_) |
| 236 |
| Op::Pin(_) |
| 237 |
| Op::Unpin(_) |
| 238 |
| Op::PhiSrcs(_) |
| 239 |
| Op::PhiDsts(_) |
| 240 |
| Op::RegOut(_) => SideEffect::Barrier, |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
pub fn estimate_block_weight(cfg: &CFG<BasicBlock>, block_idx: usize) -> u64 { |
| 245 |
let loop_depth = cfg.loop_depth(block_idx) as f32; |
| 246 |
10_f32.powf((loop_depth + 1.0).log2()) as u64 |
| 247 |
} |
| 248 |
|
| 249 |
/// Try to guess how many cycles a variable latency instruction will take |
| 250 |
/// |
| 251 |
/// These values are based on the cycle estimates from ["Dissecting the NVidia |
| 252 |
/// Turing T4 GPU via Microbenchmarking"](https://arxiv.org/pdf/1903.07486). |
| 253 |
/// Memory instructions were copied from L1 data cache latencies. |
| 254 |
/// For instructions not mentioned in the paper, I made up numbers. |
| 255 |
/// This could probably be improved. |
| 256 |
pub fn estimate_variable_latency(sm: &ShaderModelInfo, op: &Op) -> u32 { |
| 257 |
if !sm.op_needs_scoreboard(op) { |
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
match op { |
| 262 |
// Multi-function unit |
| 263 |
Op::Rro(_) | Op::MuFu(_) => 15, |
| 264 |
|
| 265 |
// Double-precision float ALU |
| 266 |
Op::DFma(_) | Op::DSetP(_) => 54, |
| 267 |
Op::DAdd(_) | Op::DMnMx(_) | Op::DMul(_) => 48, |
| 268 |
|
| 269 |
// Integer ALU |
| 270 |
Op::BRev(_) | Op::Flo(_) | Op::PopC(_) => 15, |
| 271 |
Op::IMad(_) | Op::IMul(_) => { |
| 272 |
assert!(sm.sm() < 70); |
| 273 |
86 |
| 274 |
} |
| 275 |
|
| 276 |
// Conversions |
| 277 |
Op::F2F(_) | Op::F2I(_) | Op::I2F(_) | Op::I2I(_) | Op::FRnd(_) => 15, |
| 278 |
|
| 279 |
// Move ops |
| 280 |
Op::Shfl(_) => 15, |
| 281 |
|
| 282 |
// Uniform ops |
| 283 |
Op::Redux(_) | Op::R2UR(_) => 15, |
| 284 |
|
| 285 |
// Texture ops |
| 286 |
Op::Tex(_) |
| 287 |
| Op::Tld(_) |
| 288 |
| Op::Tld4(_) |
| 289 |
| Op::Tmml(_) |
| 290 |
| Op::Txd(_) |
| 291 |
| Op::Txq(_) => 32, |
| 292 |
|
| 293 |
// Surface ops |
| 294 |
Op::SuLd(_) |
| 295 |
| Op::SuSt(_) |
| 296 |
| Op::SuAtom(_) |
| 297 |
| Op::SuLdGa(_) |
| 298 |
| Op::SuStGa(_) => 32, |
| 299 |
|
| 300 |
// Memory ops |
| 301 |
Op::Ldc(_) => 4, |
| 302 |
|
| 303 |
Op::Ld(_) |
| 304 |
| Op::Ldsm(_) |
| 305 |
| Op::Movm(_) |
| 306 |
| Op::LdSharedLock(_) |
| 307 |
| Op::St(_) |
| 308 |
| Op::StSCheckUnlock(_) |
| 309 |
| Op::Atom(_) |
| 310 |
| Op::AL2P(_) |
| 311 |
| Op::ALd(_) |
| 312 |
| Op::ASt(_) |
| 313 |
| Op::Ipa(_) |
| 314 |
| Op::CCtl(_) |
| 315 |
| Op::LdTram(_) |
| 316 |
| Op::MemBar(_) => 32, |
| 317 |
|
| 318 |
// Control-flow ops |
| 319 |
Op::WarpSync(_) => 16, |
| 320 |
|
| 321 |
// Barrier |
| 322 |
Op::BMov(_) => 16, |
| 323 |
|
| 324 |
// Geometry ops |
| 325 |
Op::Out(_) | Op::OutFinal(_) => 2, |
| 326 |
|
| 327 |
// Miscellaneous ops |
| 328 |
Op::Bar(_) |
| 329 |
| Op::TexDepBar(_) |
| 330 |
| Op::CS2R(_) |
| 331 |
| Op::Isberd(_) |
| 332 |
| Op::ViLd(_) |
| 333 |
| Op::Kill(_) |
| 334 |
| Op::PixLd(_) |
| 335 |
| Op::S2R(_) |
| 336 |
| Op::Match(_) => 16, |
| 337 |
|
| 338 |
Op::Hmma(_) | Op::Imma(_) => 22, |
| 339 |
|
| 340 |
_ => panic!("Unknown variable latency op {op}"), |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
#[derive(Default, Clone)] |
| 345 |
pub struct NodeLabel { |
| 346 |
pub cycles_to_end: u32, |
| 347 |
pub num_uses: u32, |
| 348 |
|
| 349 |
/// The first cycle that the instruction can begin executing |
| 350 |
pub ready_cycle: u32, |
| 351 |
|
| 352 |
pub exec_latency: u32, |
| 353 |
} |
| 354 |
|
| 355 |
pub struct EdgeLabel { |
| 356 |
pub latency: u32, |
| 357 |
} |
| 358 |
|
| 359 |
pub type DepGraph = graph::Graph<NodeLabel, EdgeLabel>; |
| 360 |
|
| 361 |
pub fn calc_statistics(g: &mut DepGraph) -> Vec<usize> { |
| 362 |
let mut initial_ready_list = Vec::new(); |
| 363 |
for i in (0..g.nodes.len()).rev() { |
| 364 |
let node = &g.nodes[i]; |
| 365 |
let mut max_delay = 0; |
| 366 |
for edge in &node.outgoing_edges { |
| 367 |
assert!(edge.head_idx > i); |
| 368 |
max_delay = max( |
| 369 |
max_delay, |
| 370 |
g.nodes[edge.head_idx].label.cycles_to_end + edge.label.latency, |
| 371 |
); |
| 372 |
} |
| 373 |
let node = &mut g.nodes[i]; |
| 374 |
node.label.cycles_to_end = max_delay; |
| 375 |
node.label.num_uses = node.outgoing_edges.len().try_into().unwrap(); |
| 376 |
if node.label.num_uses == 0 { |
| 377 |
initial_ready_list.push(i); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
initial_ready_list |
| 382 |
} |
| 383 |
|
| 384 |
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 385 |
pub struct ReadyInstr { |
| 386 |
cycles_to_end: u32, |
| 387 |
|
| 388 |
// We use the original instruction order as a final tie-breaker, the idea |
| 389 |
// being that the original schedule is often not too bad. Since we're |
| 390 |
// iterating in reverse order, that means scheduling the largest instruciton |
| 391 |
// index first. |
| 392 |
pub index: usize, |
| 393 |
} |
| 394 |
|
| 395 |
impl ReadyInstr { |
| 396 |
pub fn new<E>(g: &graph::Graph<NodeLabel, E>, i: usize) -> Self { |
| 397 |
let label = &g.nodes[i].label; |
| 398 |
// NodeLabel::cycles_to_end is cycles from the beginning of the |
| 399 |
// instruction to the top of the block, but we want from the end of the |
| 400 |
// instruction to the top of the block |
| 401 |
let cycles_to_end = label.cycles_to_end + label.exec_latency; |
| 402 |
ReadyInstr { |
| 403 |
cycles_to_end, |
| 404 |
index: i, |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 410 |
pub struct FutureReadyInstr { |
| 411 |
/// The first cycle that the instruction can end executing |
| 412 |
pub ready_cycle: Reverse<u32>, |
| 413 |
pub index: usize, |
| 414 |
} |
| 415 |
|
| 416 |
impl FutureReadyInstr { |
| 417 |
pub fn new<E>(g: &graph::Graph<NodeLabel, E>, i: usize) -> Self { |
| 418 |
let label = &g.nodes[i].label; |
| 419 |
// NodeLabel::ready_cycle is the earliest beginning cycle for the |
| 420 |
// instruction, but we need the earliest end cycle for the instruction. |
| 421 |
let ready_cycle = label.ready_cycle.saturating_sub(label.exec_latency); |
| 422 |
FutureReadyInstr { |
| 423 |
ready_cycle: Reverse(ready_cycle), |
| 424 |
index: i, |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
#[allow(dead_code)] |
| 430 |
pub fn save_graphviz( |
| 431 |
instrs: &[Box<Instr>], |
| 432 |
g: &DepGraph, |
| 433 |
) -> std::io::Result<()> { |
| 434 |
// dot /tmp/instr_dep_graph.dot -Tsvg > /tmp/instr_dep_graph.svg |
| 435 |
|
| 436 |
use std::fs::File; |
| 437 |
use std::io::{BufWriter, Write}; |
| 438 |
|
| 439 |
let file = File::create("/tmp/instr_dep_graph.dot")?; |
| 440 |
let mut w = BufWriter::new(file); |
| 441 |
|
| 442 |
writeln!(w, "digraph {{")?; |
| 443 |
for (i, instr) in instrs.iter().enumerate() { |
| 444 |
let l = &g.nodes[i].label; |
| 445 |
writeln!( |
| 446 |
w, |
| 447 |
" {i} [label=\"{}\\n{}, {}\"];", |
| 448 |
instr, l.cycles_to_end, l.num_uses |
| 449 |
)?; |
| 450 |
} |
| 451 |
for (i, node) in g.nodes.iter().enumerate() { |
| 452 |
for j in &node.outgoing_edges { |
| 453 |
writeln!( |
| 454 |
w, |
| 455 |
" {i} -> {} [label=\"{}\"];", |
| 456 |
j.head_idx, j.label.latency |
| 457 |
)?; |
| 458 |
} |
| 459 |
} |
| 460 |
writeln!(w, "}}")?; |
| 461 |
w.flush()?; |
| 462 |
Ok(()) |
| 463 |
} |