Debian Codegraph @

src/nouveau/compiler/nak/sm120_instr_latencies.rs

raw
1 // Copyright © 2025 Red Hat.
2 // SPDX-License-Identifier: MIT
3 #![allow(non_camel_case_types)]
4
5 use crate::ir::*;
6
7 use nak_latencies::sm100::*;
8
9 // This contains the register scheduling information provided by NVIDIA. This
10 // file is for Blackwell only.
11 //
12 // These latencies come from B100 (SM100) and not the consumer RTX chips
13 // (SM120). We have to add some padding to get everything passing on the RTX
14 // chips so that's done in this file while using the sm100 CSVs.
15
16 // Coupled instructions are ones with fixed latencies, they need delays but not
17 // scoreboards. Decoupled instructions are ones with variable latencies, need
18 // scoreboards but not delays. There are also redirected instructions which
19 // depending on the SM, can be coupled or Decoupled so both delays and
20 // scoreboards needs to be provided.
21
22 fn op_reg_latency(op: &Op, reader: bool, op_reg_idx: usize) -> RegLatencySM100 {
23 use RegLatencySM100::*;
24 match op {
25 // this will need updating if imad grows support for input predicates
26 Op::IMad(_) | Op::IMul(_) => Fma,
27 Op::IMad64(_) => {
28 if reader {
29 match op_reg_idx {
30 0 | 1 => ImadWideReadAb,
31 2 => ImadWideReadCl, // vs upper C operand - work it out
32 _ => {
33 panic!("Illegal field in imadwide")
34 }
35 }
36 } else {
37 ImadWideWriteDh // as above this needs more work
38 }
39 }
40
41 Op::PopC(_) => Decoupled,
42 Op::IAdd3(_) | Op::IAdd3X(_) => Alu,
43
44 Op::BMsk(_) => Alu,
45 Op::Sgxt(_) => Alu,
46 Op::Lop3(_) => Alu,
47 Op::Flo(_) => Decoupled,
48 Op::ISetP(_) => Dualalu,
49 Op::IAbs(_) => Alu,
50 Op::Lea(_) => Alu,
51 Op::LeaX(_) => Alu,
52 Op::IMnMx(_) => Dualalu,
53 Op::I2I(_) => Alu,
54 // I2IP => alu
55 Op::Shf(_) => Alu,
56
57 Op::F2FP(_) => Alu,
58 Op::FFma(_) => Fma,
59 Op::FAdd(_) => Fma,
60 Op::FMul(_) => Fma,
61 Op::FMnMx(_) => Dualalu,
62 Op::FSwzAdd(_) => Fma,
63 Op::FSet(_) => Dualalu,
64 // FSel => Alu,
65 Op::FSetP(_) => Dualalu,
66 // FChk => Decoupled,
67 Op::DAdd(_) | Op::DFma(_) | Op::DMul(_) | Op::DSetP(_) => {
68 RedirectedFp64
69 }
70
71 Op::DMnMx(_) => RedirectedFp64, // not in docs
72
73 Op::HAdd2(hadd2) => {
74 if hadd2.f32 {
75 Fp16F32
76 } else {
77 Fp16
78 }
79 }
80 Op::HFma2(_) | Op::HMul2(_) => Fp16,
81
82 Op::HSet2(_) | Op::HSetP2(_) | Op::HMnMx2(_) => Fp16Alu,
83 Op::Hmma(_) => Hmma,
84 Op::Ipa(_) => DecoupledAgu,
85 Op::MuFu(_) => Decoupled,
86
87 // Conversion functions all Decoupled
88 Op::F2F(_) => Decoupled,
89 Op::F2I(_) => Decoupled,
90 Op::I2F(_) => Decoupled,
91 Op::FRnd(_) => Decoupled,
92 Op::AL2P(_) => Decoupled,
93
94 Op::Mov(_) => Dualalu,
95 Op::Movm(_) => DecoupledAgu,
96 Op::Sel(_) => Dualalu,
97 Op::BRev(_) => Decoupled,
98 // P2R => Alu,
99 // R2P => Alu,
100 Op::PLop3(_) => Alu,
101 Op::Prmt(_) => Alu,
102 Op::Nop(_) => Disp64,
103 Op::Vote(_) => Dualalu,
104 Op::Match(_) => Decoupled,
105 Op::S2R(_) => DecoupledAgu,
106 Op::R2UR(_) => Alu,
107 Op::Redux(_) => {
108 if reader {
109 Decoupled
110 } else {
111 panic!("Illegal R2UR");
112 }
113 }
114 Op::CS2R(cs2r) => {
115 if cs2r.dst.comps() == 2 {
116 Disp64
117 } else {
118 Dualalu
119 }
120 }
121 // B2R => DecoupledAgu,
122 // LEPC => Disp64
123 Op::BMov(bmov) => match bmov.dst {
124 Dst::Reg(_) => Branch,
125 _ => Branch,
126 },
127 // RPCMOV.32 => Alu,
128 // RPCMOV.64 => Disp64
129 // PMTRIG => Disp64
130 // CSMTEST => Alu,
131 Op::Bar(_) => DecoupledAgu,
132 Op::Imma(_) => Imma,
133 Op::IDp4(_) => Fma,
134 Op::BClear(_) => Decoupled,
135 Op::Bra(_) => Decoupled,
136 Op::BSSy(_) => Decoupled,
137 Op::Kill(_) => Decoupled,
138 Op::Exit(_) => Decoupled,
139 Op::BSync(_) => Decoupled,
140 Op::Tex(_) => Decoupled,
141 Op::Tld(_) => Decoupled,
142 Op::Tld4(_) => Decoupled,
143 Op::Tmml(_) => Decoupled,
144 Op::Txd(_) => Decoupled,
145 Op::Txq(_) => Decoupled,
146 Op::Ldc(_) => Decoupled,
147 Op::ALd(_) => DecoupledAgu,
148 Op::ASt(_) => DecoupledAgu,
149 Op::Out(_) => DecoupledAgu,
150 Op::OutFinal(_) => DecoupledAgu,
151 Op::Ld(_) => DecoupledAgu,
152 Op::St(_) => DecoupledAgu,
153 Op::Atom(_) => DecoupledAgu,
154 //CCtl.i,c are coupled
155 Op::CCtl(_) => DecoupledAgu,
156 Op::MemBar(_) => Decoupled,
157 Op::SuLd(_) => Decoupled,
158 Op::SuSt(_) => Decoupled,
159 Op::SuAtom(_) => Decoupled,
160 Op::PixLd(_) => DecoupledAgu,
161 Op::Isberd(_) => DecoupledAgu,
162 Op::LdTram(_) => DecoupledAgu,
163 Op::Shfl(_) => DecoupledAgu,
164 Op::Ldsm(_) => DecoupledAgu,
165 x => {
166 panic!("Illegal instuction in reg category {}", x);
167 }
168 }
169 }
170
171 fn op_pred_latency(op: &Op) -> PredLatencySM100 {
172 use PredLatencySM100::*;
173 match op {
174 Op::Atom(_) => Decoupled,
175 Op::Bra(_) => Decoupled,
176 Op::DSetP(_) => RedirectedFp64,
177 Op::FMnMx(_) | Op::FSetP(_) => Dualalu,
178 Op::HFma2(_) => Fp16,
179 Op::HMnMx2(_) => Fp16,
180 Op::HSetP2(_) => Fp16,
181 Op::IAdd3(_) => Coupled,
182 Op::IAdd3X(_) => Coupled,
183 Op::IMad(_) => Fma,
184 Op::IMad64(_) => Fma,
185 Op::IMnMx(_) => Dualalu,
186 Op::IMul(_) => Fma,
187 Op::Ipa(_) => Decoupled,
188 Op::ISetP(_) => Dualalu,
189
190 Op::Ld(_) => Decoupled,
191
192 Op::Lea(_) | Op::LeaX(_) => Coupled,
193 Op::PixLd(_) => Decoupled,
194 Op::PLop3(_) => Coupled,
195 Op::PSetP(_) => Coupled,
196 Op::R2UR(_) => R2Ur,
197 Op::Sel(_) => Dualalu,
198 Op::Shfl(_) => Decoupled,
199 Op::SuLd(_) => Decoupled,
200 Op::SuSt(_) => Decoupled,
201 Op::Tex(_) => Decoupled,
202 Op::Tld(_) => Decoupled,
203 Op::Tld4(_) => Decoupled,
204 Op::Tmml(_) => Decoupled,
205 Op::Txd(_) => Decoupled,
206 Op::Txq(_) => Decoupled,
207
208 Op::Vote(_) => DispDualAlu,
209 Op::Match(_) => Decoupled,
210 _ => {
211 panic!("Illegal op in sm120 pred latency {}", op);
212 }
213 }
214 }
215
216 fn op_ureg_latency(
217 op: &Op,
218 reader: bool,
219 op_reg_idx: usize,
220 ) -> UregLatencySM100 {
221 use UregLatencySM100::*;
222 // this decides between the category types for readers.
223 let bindless = reader && op.srcs_as_slice()[op_reg_idx].is_bindless_cbuf();
224
225 let coupled = if bindless { CoupledBindless } else { Coupled };
226 let decoupled = if bindless {
227 DecoupledBindless
228 } else {
229 Decoupled
230 };
231
232 // if this is a reader from a ureg, it could be a U* instruction or a
233 // regular instruction.
234 let uniform_op = op.is_uniform();
235
236 let coupled = if uniform_op { Udp } else { coupled };
237 let decoupled = if uniform_op { Udp } else { decoupled };
238
239 match op {
240 Op::BMsk(_) => coupled,
241 Op::BRev(_) => decoupled,
242 // uclea?
243 Op::Flo(_) => decoupled,
244 Op::IAdd3(_) | Op::IAdd3X(_) => coupled,
245 Op::IAbs(_) => coupled,
246 Op::IDp4(_) => coupled,
247 Op::IMnMx(_) => coupled,
248 Op::IMad(_) => coupled,
249
250 Op::IMad64(_) => coupled,
251 Op::ISetP(_) => coupled,
252 Op::Ldc(_) => {
253 if uniform_op {
254 ToUr
255 } else {
256 decoupled
257 }
258 }
259 Op::Lea(_) => coupled,
260 Op::LeaX(_) => coupled,
261 Op::Lop2(_) | Op::Lop3(_) => coupled,
262
263 Op::MuFu(_) => decoupled,
264 Op::Mov(_) => {
265 if uniform_op {
266 Umov
267 } else {
268 coupled
269 }
270 }
271
272 // mov32i => uldc
273 // p2ur => udp,
274 Op::PLop3(_) => coupled,
275 Op::PopC(_) => {
276 if uniform_op {
277 coupled
278 } else {
279 decoupled
280 }
281 }
282 Op::Prmt(_) => coupled,
283 Op::PSetP(_) => coupled,
284 // UR2UP
285 Op::Sel(_) => coupled,
286 Op::Sgxt(_) => coupled,
287 Op::Shf(_) => coupled,
288 Op::Shfl(_) => decoupled,
289
290 Op::I2F(_) => decoupled,
291 Op::F2I(_) => decoupled,
292 Op::F2F(_) => decoupled,
293 Op::R2UR(_) => {
294 if !reader {
295 R2Ur
296 } else {
297 panic!("Illegal R2UR in ureg");
298 }
299 }
300 Op::Redux(_) => {
301 if !reader {
302 ToUr
303 } else {
304 panic!("Illegal R2UR in ureg");
305 }
306 }
307 Op::Vote(_) => Voteu,
308 Op::S2R(_) => ToUr,
309
310 Op::Tex(_) | Op::Tld(_) | Op::Tld4(_) | Op::Txq(_) => Tex,
311 Op::FRnd(_) => decoupled,
312 Op::F2FP(_)
313 | Op::FAdd(_)
314 | Op::FMul(_)
315 | Op::FFma(_)
316 | Op::FSet(_)
317 | Op::FSetP(_)
318 | Op::FMnMx(_)
319 | Op::HAdd2(_)
320 | Op::HMul2(_)
321 | Op::HSet2(_)
322 | Op::HFma2(_)
323 | Op::HMnMx2(_)
324 | Op::HSetP2(_) => coupled,
325 Op::DMul(_) | Op::DFma(_) | Op::DAdd(_) | Op::DSetP(_) => decoupled,
326 _ => {
327 panic!("Illegal instuction in ureg category {}", op);
328 }
329 }
330 }
331
332 fn op_upred_latency(op: &Op) -> UpredLatencySM100 {
333 use UpredLatencySM100::*;
334 let uniform_op = op.is_uniform();
335 match op {
336 Op::BMsk(_)
337 | Op::BRev(_)
338 | Op::Flo(_)
339 | Op::IAdd3(_)
340 | Op::IAdd3X(_)
341 | Op::IMad(_)
342 | Op::ISetP(_)
343 | Op::Lea(_)
344 | Op::LeaX(_)
345 | Op::Lop3(_)
346 | Op::Mov(_) => Udp,
347 Op::Bra(_) => BraJmp,
348 Op::Ldc(_) => UldcMma,
349 Op::PLop3(_) => {
350 if uniform_op {
351 Udp
352 } else {
353 Coupled
354 }
355 }
356 Op::PSetP(_) => {
357 if uniform_op {
358 Udp
359 } else {
360 Coupled
361 }
362 }
363 Op::Sel(_) => {
364 if uniform_op {
365 Udp
366 } else {
367 Coupled
368 }
369 }
370 Op::Vote(_) => {
371 if uniform_op {
372 Voteu
373 } else {
374 panic!("Illegal Vote in upred");
375 }
376 }
377 _ => {
378 panic!("Illegal instuction in upred category {}", op);
379 }
380 }
381 }
382
383 pub struct SM120Latency {}
384
385 impl SM120Latency {
386 pub fn needs_scoreboards(op: &Op) -> bool {
387 if op.is_uniform() {
388 matches!(
389 op_ureg_latency(op, false, 0),
390 UregLatencySM100::Uldc
391 | UregLatencySM100::ToUr
392 | UregLatencySM100::Tex
393 )
394 } else {
395 matches!(
396 op_reg_latency(op, false, 0),
397 RegLatencySM100::Dmma
398 | RegLatencySM100::Hmma
399 | RegLatencySM100::RedirectedFp64
400 | RegLatencySM100::Branch
401 | RegLatencySM100::Decoupled
402 | RegLatencySM100::DecoupledAgu
403 )
404 }
405 }
406
407 pub fn raw(
408 write: &Op,
409 dst_idx: usize,
410 read: Option<&Op>,
411 src_idx: usize,
412 ) -> u32 {
413 let Some(dst_file) = write.dsts_as_slice()[dst_idx].file() else {
414 return 0;
415 };
416
417 match dst_file {
418 RegFile::GPR => {
419 let write_latency = op_reg_latency(write, false, dst_idx);
420 let read_latency = match read {
421 Some(op) => op_reg_latency(op, true, src_idx),
422 None => RegLatencySM100::RedirectedFp64,
423 };
424 // The latencies are for SM100 docs, but some chips need large
425 // one just override here.
426 if write_latency == RegLatencySM100::Hmma
427 || read_latency == RegLatencySM100::Hmma
428 {
429 RegLatencySM100::raw(write_latency, read_latency, false) + 9
430 } else if write_latency == RegLatencySM100::Imma
431 || read_latency == RegLatencySM100::Imma
432 {
433 RegLatencySM100::raw(write_latency, read_latency, false) + 5
434 } else {
435 RegLatencySM100::raw(write_latency, read_latency, false) + 1
436 }
437 }
438 RegFile::UGPR => {
439 let write_latency = op_ureg_latency(write, false, dst_idx);
440 let read_latency = match read {
441 Some(op) => op_ureg_latency(op, true, src_idx),
442 None => UregLatencySM100::Uldc,
443 };
444 UregLatencySM100::raw(write_latency, read_latency, false) + 1
445 }
446 RegFile::Pred => {
447 let write_latency = op_pred_latency(write);
448 let read_latency = match read {
449 Some(op) => op_pred_latency(op),
450 None => PredLatencySM100::RedirectedFp64,
451 };
452 PredLatencySM100::raw(write_latency, read_latency, false) + 1
453 }
454 RegFile::UPred => {
455 let write_latency = op_upred_latency(write);
456 let read_latency = match read {
457 Some(op) => op_upred_latency(op),
458 None => UpredLatencySM100::UGuard,
459 };
460 UpredLatencySM100::raw(write_latency, read_latency, false) + 1
461 }
462 RegFile::Bar => 0, // Barriers have a HW scoreboard
463 _ => panic!("Not a register"),
464 }
465 }
466
467 pub fn war(read: &Op, src_idx: usize, write: &Op, dst_idx: usize) -> u32 {
468 let Some(dst_file) = write.dsts_as_slice()[dst_idx].file() else {
469 return 0;
470 };
471
472 match dst_file {
473 RegFile::GPR => {
474 let write_latency = op_reg_latency(write, false, dst_idx);
475 let read_latency = op_reg_latency(read, true, src_idx);
476
477 if write_latency == RegLatencySM100::Hmma
478 || read_latency == RegLatencySM100::Hmma
479 {
480 RegLatencySM100::war(read_latency, write_latency, false) + 7
481 } else {
482 RegLatencySM100::war(read_latency, write_latency, false)
483 }
484 }
485 RegFile::UGPR => {
486 let write_latency = op_ureg_latency(write, false, dst_idx);
487 let read_latency = op_ureg_latency(read, true, src_idx);
488 UregLatencySM100::war(read_latency, write_latency, false)
489 }
490 RegFile::Pred => {
491 let write_latency = op_pred_latency(write);
492 let read_latency = op_pred_latency(read);
493 PredLatencySM100::war(read_latency, write_latency, false)
494 }
495 RegFile::UPred => {
496 let write_latency = op_upred_latency(write);
497 let read_latency = op_upred_latency(read);
498 UpredLatencySM100::war(read_latency, write_latency, false)
499 }
500 _ => panic!("Not a register"),
501 }
502 }
503
504 pub fn waw(
505 a: &Op,
506 a_dst_idx: usize,
507 b: &Op,
508 b_dst_idx: usize,
509 a_op_pred: bool,
510 ) -> u32 {
511 let Some(dst_file) = a.dsts_as_slice()[a_dst_idx].file() else {
512 return 0;
513 };
514
515 match dst_file {
516 RegFile::GPR => {
517 let write1_latency = op_reg_latency(a, false, a_dst_idx);
518 let write2_latency = op_reg_latency(b, false, b_dst_idx);
519 if write1_latency == RegLatencySM100::Hmma
520 || write2_latency == RegLatencySM100::Hmma
521 {
522 RegLatencySM100::waw(
523 write1_latency,
524 write2_latency,
525 a_op_pred,
526 ) + 7
527 } else {
528 RegLatencySM100::waw(
529 write1_latency,
530 write2_latency,
531 a_op_pred,
532 )
533 }
534 }
535 RegFile::UGPR => {
536 let write1_latency = op_ureg_latency(a, false, a_dst_idx);
537 let write2_latency = op_ureg_latency(b, false, b_dst_idx);
538 UregLatencySM100::waw(write1_latency, write2_latency, a_op_pred)
539 }
540 RegFile::Pred => {
541 let write1_latency = op_pred_latency(a);
542 let write2_latency = op_pred_latency(b);
543 PredLatencySM100::waw(write1_latency, write2_latency, a_op_pred)
544 }
545 RegFile::UPred => {
546 let write1_latency = op_upred_latency(a);
547 let write2_latency = op_upred_latency(b);
548 UpredLatencySM100::waw(write1_latency, write2_latency, false)
549 }
550 _ => panic!("Not a register"),
551 }
552 }
553 }