| 1 |
use bitview::{BitMutViewable, BitViewable, SetField}; |
| 2 |
use rustc_hash::FxHashMap; |
| 3 |
|
| 4 |
use crate::ir::{Instr, InstrDeps, Label, Op, OpNop, Shader}; |
| 5 |
|
| 6 |
pub fn instr_latency(_sm: u8, op: &Op, _dst_idx: usize) -> u32 { |
| 7 |
if op.is_fp64() { |
| 8 |
return 20; |
| 9 |
} |
| 10 |
|
| 11 |
match op { |
| 12 |
Op::Ipa(_) => 15, |
| 13 |
Op::Ld(_) => 24, |
| 14 |
Op::ALd(_) => 24, |
| 15 |
Op::IMul(_) => 15, // This does not apply to imad, right? right??? |
| 16 |
Op::ISetP(_) => 13, |
| 17 |
Op::PSetP(_) => 13, |
| 18 |
Op::IAdd2(o) if !o.carry_out.is_none() => 13, |
| 19 |
Op::Tex(_) |
| 20 |
| Op::Tld(_) |
| 21 |
| Op::Tld4(_) |
| 22 |
| Op::Tmml(_) |
| 23 |
| Op::Txd(_) |
| 24 |
| Op::Txq(_) => 17, |
| 25 |
_ => 9, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
pub fn latency_upper_bound() -> u32 { |
| 30 |
24 |
| 31 |
} |
| 32 |
|
| 33 |
pub fn instr_exec_latency(sm: u8, op: &Op) -> u32 { |
| 34 |
let is_kepler_a = sm == 30; |
| 35 |
match op { |
| 36 |
Op::Tex(_) |
| 37 |
| Op::Tld(_) |
| 38 |
| Op::Tld4(_) |
| 39 |
| Op::Tmml(_) |
| 40 |
| Op::Txd(_) |
| 41 |
| Op::Txq(_) => 17, |
| 42 |
Op::MemBar(_) => 16, |
| 43 |
Op::Cont(_) | Op::Brk(_) if is_kepler_a => 5, |
| 44 |
Op::Exit(_) => 15, |
| 45 |
_ => 1, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
fn calc_instr_sched(prev_op: Option<&Op>, op: &Op, deps: &InstrDeps) -> u8 { |
| 50 |
// Kepler is the first generation to lift scoreboarding from the |
| 51 |
// hardware into the compiler. For each instruction we encode |
| 52 |
// the delay but not all the other information necessary for newer |
| 53 |
// architectures. |
| 54 |
// The hardware still checks for data-hazard and, if present, it |
| 55 |
// will delay the instruction by 32 cycles. |
| 56 |
match op { |
| 57 |
Op::TexDepBar(_) => 0xc2, |
| 58 |
Op::Sync(_) => 0x00, // Wait 16 cycles |
| 59 |
_ => { |
| 60 |
// TODO: when we support dual-issue this should check for |
| 61 |
// both previous ops |
| 62 |
let base = match prev_op { |
| 63 |
Some(Op::ASt(_)) => 0x40, |
| 64 |
_ => 0x20, |
| 65 |
}; |
| 66 |
|
| 67 |
let delay = deps.delay; |
| 68 |
debug_assert!(delay >= 1 && delay <= 32); |
| 69 |
base | (delay - 1) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// 0x00: wait for 16 cycles |
| 74 |
// 0x04: dual-issue with next instruction |
| 75 |
// 0xc2: if TEXBAR |
| 76 |
// 0x20 | 0x40: suspend for N+1 cycles (N = bitmask 0x1f) |
| 77 |
// 0x40 only if prev_op is attribute store |
| 78 |
// Unsure: |
| 79 |
// 0x80: global memory bit |
| 80 |
// |
| 81 |
// TODO: |
| 82 |
// - Dual issue (0x04) |
| 83 |
// - Functional Unit tracking |
| 84 |
} |
| 85 |
|
| 86 |
pub trait KeplerInstructionEncoder { |
| 87 |
/// Encode the instruction and push it into the "encoded" vec |
| 88 |
fn encode_instr( |
| 89 |
&self, |
| 90 |
instr: &Instr, |
| 91 |
labels: &FxHashMap<Label, usize>, |
| 92 |
encoded: &mut Vec<u32>, |
| 93 |
); |
| 94 |
|
| 95 |
/// Prepare the scheduling instruction opcode-field and return a |
| 96 |
/// subset where the actual scheduling information will be written |
| 97 |
fn prepare_sched_instr<'a>( |
| 98 |
&self, |
| 99 |
sched_instr: &'a mut [u32; 2], |
| 100 |
) -> impl BitMutViewable + 'a; |
| 101 |
} |
| 102 |
|
| 103 |
/// Helper function that encodes shaders for both KeplerA and KeplerB. |
| 104 |
/// Difference in the encoders are handled by KeplerInstructionEncoder. |
| 105 |
pub fn encode_kepler_shader<E>(encoder: &E, s: &Shader<'_>) -> Vec<u32> |
| 106 |
where |
| 107 |
E: KeplerInstructionEncoder, |
| 108 |
{ |
| 109 |
const INSTR_LEN_BYTES: usize = 8; |
| 110 |
assert!(s.functions.len() == 1); |
| 111 |
let func = &s.functions[0]; |
| 112 |
|
| 113 |
// --- Compute label addresses --- |
| 114 |
// We need a schedule instruction every 7 instructions, these don't |
| 115 |
// define jump boundaries so we can have multible blocks in the same |
| 116 |
// 7-instr group. |
| 117 |
let mut ip = 0_usize; |
| 118 |
let mut labels = FxHashMap::default(); |
| 119 |
for b in &func.blocks { |
| 120 |
let num_sched = (ip / 7) + 1; |
| 121 |
labels.insert(b.label, (ip + num_sched) * INSTR_LEN_BYTES); |
| 122 |
ip += b.instrs.len(); |
| 123 |
} |
| 124 |
|
| 125 |
// --- Real encoding --- |
| 126 |
// Create an instruction iterator and iterate it in chunks of 7. |
| 127 |
// fill the last chunk with a nop (it should never be executed). |
| 128 |
let mut instr_iter = |
| 129 |
func.blocks.iter().flat_map(|b| b.instrs.iter()).peekable(); |
| 130 |
let mut filling_instr = Instr { |
| 131 |
pred: true.into(), |
| 132 |
op: Op::Nop(OpNop { label: None }), |
| 133 |
deps: InstrDeps::new(), |
| 134 |
}; |
| 135 |
filling_instr.deps.set_delay(1); |
| 136 |
let mut sched_chunk_gen = || { |
| 137 |
if instr_iter.peek().is_none() { |
| 138 |
return None; |
| 139 |
} |
| 140 |
Some([0; 7].map(|_| instr_iter.next().unwrap_or(&filling_instr))) |
| 141 |
}; |
| 142 |
|
| 143 |
let mut encoded = Vec::new(); |
| 144 |
let mut prev_op = None; |
| 145 |
while let Some(sched_chunk) = sched_chunk_gen() { |
| 146 |
let sched_i = encoded.len(); |
| 147 |
|
| 148 |
let mut sched_instr = [0u32; 2]; |
| 149 |
encoded.extend(&sched_instr[..]); // Push now, will edit later |
| 150 |
let mut bv = encoder.prepare_sched_instr(&mut sched_instr); |
| 151 |
// There should be 8 bits for each instr in a scheduling block |
| 152 |
debug_assert!(bv.bits() == 8 * 7); |
| 153 |
|
| 154 |
for (i, instr) in sched_chunk.iter().enumerate() { |
| 155 |
encoder.encode_instr(instr, &labels, &mut encoded); |
| 156 |
|
| 157 |
let sched = calc_instr_sched(prev_op, &instr.op, &instr.deps); |
| 158 |
bv.set_field(i * 8..(i + 1) * 8, sched); |
| 159 |
prev_op = Some(&instr.op); |
| 160 |
} |
| 161 |
|
| 162 |
drop(bv); |
| 163 |
encoded[sched_i] = sched_instr[0]; |
| 164 |
encoded[sched_i + 1] = sched_instr[1]; |
| 165 |
} |
| 166 |
|
| 167 |
encoded |
| 168 |
} |