| 1 |
// Copyright © 2022 Collabora, Ltd. |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::ir::*; |
| 5 |
use crate::legalize::LegalizeBuilder; |
| 6 |
use crate::sm120_instr_latencies::SM120Latency; |
| 7 |
use crate::sm70_encode::*; |
| 8 |
use crate::sm75_instr_latencies::SM75Latency; |
| 9 |
use crate::sm80_instr_latencies::SM80Latency; |
| 10 |
pub struct ShaderModel70 { |
| 11 |
sm: u8, |
| 12 |
} |
| 13 |
|
| 14 |
impl ShaderModel70 { |
| 15 |
pub fn new(sm: u8) -> Self { |
| 16 |
assert!(sm >= 70); |
| 17 |
Self { sm } |
| 18 |
} |
| 19 |
|
| 20 |
fn has_uniform_alu(&self) -> bool { |
| 21 |
self.sm >= 73 |
| 22 |
} |
| 23 |
|
| 24 |
fn instr_latency(&self, op: &Op, dst_idx: usize) -> u32 { |
| 25 |
let file = match &op.dsts_as_slice()[dst_idx] { |
| 26 |
Dst::None => return 0, |
| 27 |
Dst::SSA(vec) => vec.file(), |
| 28 |
Dst::Reg(reg) => reg.file(), |
| 29 |
}; |
| 30 |
|
| 31 |
let (gpr_latency, pred_latency) = if self.sm < 80 { |
| 32 |
match op { |
| 33 |
// Double-precision float ALU |
| 34 |
Op::DAdd(_) |
| 35 |
| Op::DFma(_) |
| 36 |
| Op::DMnMx(_) |
| 37 |
| Op::DMul(_) |
| 38 |
| Op::DSetP(_) |
| 39 |
// Half-precision float ALU |
| 40 |
| Op::HAdd2(_) |
| 41 |
| Op::HFma2(_) |
| 42 |
| Op::HMul2(_) |
| 43 |
| Op::HSet2(_) |
| 44 |
| Op::HSetP2(_) |
| 45 |
| Op::HMnMx2(_) => if self.is_volta() { |
| 46 |
// Volta is even slower |
| 47 |
(13, 15) |
| 48 |
} else { |
| 49 |
(13, 14) |
| 50 |
} |
| 51 |
_ => (6, 13) |
| 52 |
} |
| 53 |
} else { |
| 54 |
(6, 13) |
| 55 |
}; |
| 56 |
|
| 57 |
// This is BS and we know it |
| 58 |
match file { |
| 59 |
RegFile::GPR => gpr_latency, |
| 60 |
RegFile::UGPR => 12, |
| 61 |
RegFile::Pred => pred_latency, |
| 62 |
RegFile::UPred => 11, |
| 63 |
RegFile::Bar => 0, // Barriers have a HW scoreboard |
| 64 |
RegFile::Carry => 6, |
| 65 |
RegFile::Mem => panic!("Not a register"), |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl ShaderModel for ShaderModel70 { |
| 71 |
fn sm(&self) -> u8 { |
| 72 |
self.sm |
| 73 |
} |
| 74 |
|
| 75 |
fn num_regs(&self, file: RegFile) -> u32 { |
| 76 |
match file { |
| 77 |
RegFile::GPR => 255 - self.hw_reserved_gprs(), |
| 78 |
RegFile::UGPR => { |
| 79 |
if !self.has_uniform_alu() { |
| 80 |
0 |
| 81 |
} else if self.sm >= 100 { |
| 82 |
80 |
| 83 |
} else { |
| 84 |
63 |
| 85 |
} |
| 86 |
} |
| 87 |
RegFile::Pred => 7, |
| 88 |
RegFile::UPred => { |
| 89 |
if self.has_uniform_alu() { |
| 90 |
7 |
| 91 |
} else { |
| 92 |
0 |
| 93 |
} |
| 94 |
} |
| 95 |
RegFile::Carry => 0, |
| 96 |
RegFile::Bar => 16, |
| 97 |
RegFile::Mem => RegRef::MAX_IDX + 1, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
fn hw_reserved_gprs(&self) -> u32 { |
| 102 |
// On Volta+, 2 GPRs get burned for the program counter - see the |
| 103 |
// footnote on table 2 of the volta whitepaper |
| 104 |
// https://images.nvidia.com/content/volta-architecture/pdf/volta-architecture-whitepaper.pdf |
| 105 |
2 |
| 106 |
} |
| 107 |
|
| 108 |
fn crs_size(&self, max_crs_depth: u32) -> u32 { |
| 109 |
assert!(max_crs_depth == 0); |
| 110 |
0 |
| 111 |
} |
| 112 |
|
| 113 |
fn op_can_be_uniform(&self, op: &Op) -> bool { |
| 114 |
if !self.has_uniform_alu() { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
match op { |
| 119 |
Op::R2UR(_) |
| 120 |
| Op::Redux(_) |
| 121 |
| Op::S2R(_) |
| 122 |
| Op::BMsk(_) |
| 123 |
| Op::BRev(_) |
| 124 |
| Op::Flo(_) |
| 125 |
| Op::IAdd3(_) |
| 126 |
| Op::IAdd3X(_) |
| 127 |
| Op::IMad(_) |
| 128 |
| Op::IMad64(_) |
| 129 |
| Op::ISetP(_) |
| 130 |
| Op::Lea(_) |
| 131 |
| Op::LeaX(_) |
| 132 |
| Op::Lop3(_) |
| 133 |
| Op::Mov(_) |
| 134 |
| Op::PLop3(_) |
| 135 |
| Op::PopC(_) |
| 136 |
| Op::Prmt(_) |
| 137 |
| Op::PSetP(_) |
| 138 |
| Op::Sel(_) |
| 139 |
| Op::Sgxt(_) |
| 140 |
| Op::Shf(_) |
| 141 |
| Op::Shl(_) |
| 142 |
| Op::Shr(_) |
| 143 |
| Op::Vote(_) |
| 144 |
| Op::Copy(_) |
| 145 |
| Op::Pin(_) |
| 146 |
| Op::Unpin(_) => true, |
| 147 |
Op::Ldc(op) => op.offset.is_zero(), |
| 148 |
// UCLEA USHL USHR |
| 149 |
_ => false, |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
fn op_needs_scoreboard(&self, op: &Op) -> bool { |
| 154 |
if op.no_scoreboard() { |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
if self.is_blackwell() { |
| 159 |
SM120Latency::needs_scoreboards(op) |
| 160 |
} else if self.is_ampere() || self.is_ada() { |
| 161 |
SM80Latency::needs_scoreboards(op) |
| 162 |
} else if self.is_turing() { |
| 163 |
SM75Latency::needs_scoreboards(op) |
| 164 |
} else { |
| 165 |
!op.has_fixed_latency(self.sm()) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
fn exec_latency(&self, op: &Op) -> u32 { |
| 170 |
match op { |
| 171 |
Op::Bar(_) | Op::MemBar(_) => { |
| 172 |
if self.sm >= 80 { |
| 173 |
6 |
| 174 |
} else { |
| 175 |
5 |
| 176 |
} |
| 177 |
} |
| 178 |
Op::CCtl(_op) => { |
| 179 |
// CCTL.C needs 8, CCTL.I needs 11 |
| 180 |
11 |
| 181 |
} |
| 182 |
// Op::DepBar(_) => 4, |
| 183 |
_ => 1, // TODO: co-issue |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
fn raw_latency( |
| 188 |
&self, |
| 189 |
write: &Op, |
| 190 |
dst_idx: usize, |
| 191 |
read: &Op, |
| 192 |
src_idx: usize, |
| 193 |
) -> u32 { |
| 194 |
if self.is_blackwell() { |
| 195 |
SM120Latency::raw(write, dst_idx, Some(read), src_idx) |
| 196 |
} else if self.is_ampere() || self.is_ada() { |
| 197 |
SM80Latency::raw(write, dst_idx, Some(read), src_idx) |
| 198 |
} else if self.is_turing() { |
| 199 |
SM75Latency::raw(write, dst_idx, Some(read), src_idx) |
| 200 |
} else { |
| 201 |
self.instr_latency(write, dst_idx) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
fn war_latency( |
| 206 |
&self, |
| 207 |
read: &Op, |
| 208 |
src_idx: usize, |
| 209 |
write: &Op, |
| 210 |
dst_idx: usize, |
| 211 |
) -> u32 { |
| 212 |
if self.is_blackwell() { |
| 213 |
SM120Latency::war(read, src_idx, write, dst_idx) |
| 214 |
} else if self.is_ampere() || self.is_ada() { |
| 215 |
SM80Latency::war(read, src_idx, write, dst_idx) |
| 216 |
} else if self.is_turing() { |
| 217 |
SM75Latency::war(read, src_idx, write, dst_idx) |
| 218 |
} else { |
| 219 |
// We assume the source gets read in the first 4 cycles. We don't |
| 220 |
// know how quickly the write will happen. This is all a guess. |
| 221 |
4 |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
fn waw_latency( |
| 226 |
&self, |
| 227 |
a: &Op, |
| 228 |
a_dst_idx: usize, |
| 229 |
a_has_pred: bool, |
| 230 |
b: &Op, |
| 231 |
b_dst_idx: usize, |
| 232 |
) -> u32 { |
| 233 |
if self.is_blackwell() { |
| 234 |
SM120Latency::waw(a, a_dst_idx, b, b_dst_idx, a_has_pred) |
| 235 |
} else if self.is_ampere() || self.is_ada() { |
| 236 |
SM80Latency::waw(a, a_dst_idx, b, b_dst_idx, a_has_pred) |
| 237 |
} else if self.is_turing() { |
| 238 |
SM75Latency::waw(a, a_dst_idx, b, b_dst_idx, a_has_pred) |
| 239 |
} else { |
| 240 |
// We know our latencies are wrong so assume the wrote could happen |
| 241 |
// anywhere between 0 and instr_latency(a) cycles |
| 242 |
self.instr_latency(a, a_dst_idx) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
fn paw_latency(&self, write: &Op, dst_idx: usize) -> u32 { |
| 247 |
if self.is_blackwell() { |
| 248 |
SM120Latency::raw(write, dst_idx, None, 0) |
| 249 |
} else if self.is_ampere() || self.is_ada() { |
| 250 |
SM80Latency::raw(write, dst_idx, None, 0) |
| 251 |
} else if self.is_turing() { |
| 252 |
SM75Latency::raw(write, dst_idx, None, 0) |
| 253 |
} else if self.is_volta() { |
| 254 |
match write { |
| 255 |
Op::DSetP(_) | Op::HSetP2(_) => 15, |
| 256 |
_ => 13, |
| 257 |
} |
| 258 |
} else { |
| 259 |
13 |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
fn latency_upper_bound(&self) -> u32 { |
| 264 |
if self.is_blackwell() || self.is_ampere() || self.is_ada() { |
| 265 |
30 |
| 266 |
} else if self.is_turing() { |
| 267 |
25 |
| 268 |
} else { |
| 269 |
15 |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
fn worst_latency(&self, write: &Op, dst_idx: usize) -> u32 { |
| 274 |
if self.is_blackwell() { |
| 275 |
SM120Latency::raw(write, dst_idx, None, 0) |
| 276 |
} else if self.is_ampere() || self.is_ada() { |
| 277 |
SM80Latency::raw(write, dst_idx, None, 0) |
| 278 |
} else if self.is_turing() { |
| 279 |
SM75Latency::raw(write, dst_idx, None, 0) |
| 280 |
} else { |
| 281 |
self.instr_latency(write, dst_idx) |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
fn max_instr_delay(&self) -> u8 { |
| 286 |
15 |
| 287 |
} |
| 288 |
|
| 289 |
fn legalize_op(&self, b: &mut LegalizeBuilder, op: &mut Op) { |
| 290 |
legalize_sm70_op(self, b, op); |
| 291 |
} |
| 292 |
|
| 293 |
fn encode_shader(&self, s: &Shader<'_>) -> Vec<u32> { |
| 294 |
encode_sm70_shader(self, s) |
| 295 |
} |
| 296 |
} |