|
@@ -1240,13 +1240,28 @@ fn liveness(blocks: &[Block], order: &[usize]) -> (Vec<RegSet>, Vec<RegSet>) {
|
|
|
/// Phis are placed for every live register at every join; those whose
|
|
/// Phis are placed for every live register at every join; those whose
|
|
|
/// arguments are all one value (or the phi itself) are replaced by it.
|
|
/// arguments are all one value (or the phi itself) are replaced by it.
|
|
|
fn remove_trivial_phis(blocks: &mut [Block], values: u32) {
|
|
fn remove_trivial_phis(blocks: &mut [Block], values: u32) {
|
|
|
|
|
+ remove_trivial_phis_steps(blocks, values);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn remove_trivial_phis_steps(blocks: &mut [Block], values: u32) -> usize {
|
|
|
|
|
+ fn find(replaced: &mut [Value], v: Value, steps: &mut usize) -> Value {
|
|
|
|
|
+ let mut root = v;
|
|
|
|
|
+ while replaced[root.0 as usize] != root {
|
|
|
|
|
+ *steps += 1;
|
|
|
|
|
+ root = replaced[root.0 as usize];
|
|
|
|
|
+ }
|
|
|
|
|
+ let mut cur = v;
|
|
|
|
|
+ while replaced[cur.0 as usize] != root {
|
|
|
|
|
+ *steps += 1;
|
|
|
|
|
+ let next = replaced[cur.0 as usize];
|
|
|
|
|
+ replaced[cur.0 as usize] = root;
|
|
|
|
|
+ cur = next;
|
|
|
|
|
+ }
|
|
|
|
|
+ root
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
let mut replaced: Vec<Value> = (0..values).map(Value).collect();
|
|
let mut replaced: Vec<Value> = (0..values).map(Value).collect();
|
|
|
- let find = |replaced: &[Value], mut v: Value| {
|
|
|
|
|
- while replaced[v.0 as usize] != v {
|
|
|
|
|
- v = replaced[v.0 as usize];
|
|
|
|
|
- }
|
|
|
|
|
- v
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ let mut steps = 0usize;
|
|
|
let mut changed = true;
|
|
let mut changed = true;
|
|
|
while changed {
|
|
while changed {
|
|
|
changed = false;
|
|
changed = false;
|
|
@@ -1254,7 +1269,7 @@ fn remove_trivial_phis(blocks: &mut [Block], values: u32) {
|
|
|
block.phis.retain(|phi| {
|
|
block.phis.retain(|phi| {
|
|
|
let mut only = None;
|
|
let mut only = None;
|
|
|
for &a in &phi.args {
|
|
for &a in &phi.args {
|
|
|
- let a = find(&replaced, a);
|
|
|
|
|
|
|
+ let a = find(&mut replaced, a, &mut steps);
|
|
|
if a == phi.value || Some(a) == only {
|
|
if a == phi.value || Some(a) == only {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
@@ -1273,15 +1288,16 @@ fn remove_trivial_phis(blocks: &mut [Block], values: u32) {
|
|
|
for block in blocks {
|
|
for block in blocks {
|
|
|
for phi in &mut block.phis {
|
|
for phi in &mut block.phis {
|
|
|
for a in &mut phi.args {
|
|
for a in &mut phi.args {
|
|
|
- *a = find(&replaced, *a);
|
|
|
|
|
|
|
+ *a = find(&mut replaced, *a, &mut steps);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
for inst in &mut block.insts {
|
|
for inst in &mut block.insts {
|
|
|
for (_, v) in &mut inst.reads {
|
|
for (_, v) in &mut inst.reads {
|
|
|
- *v = find(&replaced, *v);
|
|
|
|
|
|
|
+ *v = find(&mut replaced, *v, &mut steps);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ steps
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Scheduling hints, performance counters and tracing: no effect on results.
|
|
/// Scheduling hints, performance counters and tracing: no effect on results.
|
|
@@ -1934,4 +1950,204 @@ mod tests {
|
|
|
assert_eq!(movrels.read(Reg::V(4)), movreld.write(Reg::V(4)));
|
|
assert_eq!(movrels.read(Reg::V(4)), movreld.write(Reg::V(4)));
|
|
|
assert!(build(&code, false, 4).is_err(), "v4 is past 4 VGPRs");
|
|
assert!(build(&code, false, 4).is_err(), "v4 is past 4 VGPRs");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ const fn s_branch(simm16: i16) -> u32 {
|
|
|
|
|
+ 0xBF82_0000 | simm16 as u16 as u32
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const fn s_cbranch_scc1(simm16: i16) -> u32 {
|
|
|
|
|
+ 0xBF85_0000 | simm16 as u16 as u32
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn reverse_order_diamond_chain_eliminates_trivial_phis() {
|
|
|
|
|
+ const LAYERS: usize = 4096;
|
|
|
|
|
+ let mut reverse = vec![
|
|
|
|
|
+ 0xBE81_0381, // s_mov_b32 s1, 1
|
|
|
|
|
+ 0xBE82_0382, // s_mov_b32 s2, 2
|
|
|
|
|
+ s_branch(((LAYERS - 1) * 4) as i16),
|
|
|
|
|
+ ];
|
|
|
|
|
+ for k in 0..LAYERS {
|
|
|
|
|
+ reverse.extend([
|
|
|
|
|
+ 0xBF06_0201, // s_cmp_eq_u32 s1, s2
|
|
|
|
|
+ s_cbranch_scc1(1),
|
|
|
|
|
+ 0xBF80_0000, // s_nop 0
|
|
|
|
|
+ if k == 0 {
|
|
|
|
|
+ s_branch((LAYERS * 4 - 4) as i16)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ s_branch(-8)
|
|
|
|
|
+ },
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ reverse.extend([0x8000_0201, S_ENDPGM]); // s_add_u32 s0, s1, s2
|
|
|
|
|
+
|
|
|
|
|
+ let mut forward = vec![
|
|
|
|
|
+ 0xBE81_0381, // s_mov_b32 s1, 1
|
|
|
|
|
+ 0xBE82_0382, // s_mov_b32 s2, 2
|
|
|
|
|
+ s_branch(0),
|
|
|
|
|
+ ];
|
|
|
|
|
+ for _ in 0..LAYERS {
|
|
|
|
|
+ forward.extend([
|
|
|
|
|
+ 0xBF06_0201, // s_cmp_eq_u32 s1, s2
|
|
|
|
|
+ s_cbranch_scc1(1),
|
|
|
|
|
+ 0xBF80_0000, // s_nop 0
|
|
|
|
|
+ s_branch(0),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ forward.extend([0x8000_0201, S_ENDPGM]); // s_add_u32 s0, s1, s2
|
|
|
|
|
+
|
|
|
|
|
+ let rev = build_words(&reverse, false);
|
|
|
|
|
+ let fwd = build_words(&forward, false);
|
|
|
|
|
+ assert!(rev.inputs.is_empty(), "{:?}", rev.inputs);
|
|
|
|
|
+ assert!(fwd.inputs.is_empty(), "{:?}", fwd.inputs);
|
|
|
|
|
+ assert!(rev.blocks.iter().all(|b| b.phis.is_empty()));
|
|
|
|
|
+ assert!(fwd.blocks.iter().all(|b| b.phis.is_empty()));
|
|
|
|
|
+ assert_eq!(rev.blocks.len(), fwd.blocks.len());
|
|
|
|
|
+
|
|
|
|
|
+ let rev_s1 = rev.blocks[0].insts[0].write(Reg::S(1)).unwrap();
|
|
|
|
|
+ let rev_s2 = rev.blocks[0].insts[1].write(Reg::S(2)).unwrap();
|
|
|
|
|
+ let rev_exit = rev.blocks.last().unwrap();
|
|
|
|
|
+ assert_eq!(rev_exit.insts[0].name(), "s_add_u32");
|
|
|
|
|
+ assert_eq!(rev_exit.insts[0].read(Reg::S(1)), Some(rev_s1));
|
|
|
|
|
+ assert_eq!(rev_exit.insts[0].read(Reg::S(2)), Some(rev_s2));
|
|
|
|
|
+
|
|
|
|
|
+ let fwd_s1 = fwd.blocks[0].insts[0].write(Reg::S(1)).unwrap();
|
|
|
|
|
+ let fwd_s2 = fwd.blocks[0].insts[1].write(Reg::S(2)).unwrap();
|
|
|
|
|
+ let fwd_exit = fwd.blocks.last().unwrap();
|
|
|
|
|
+ assert_eq!(fwd_exit.insts[0].name(), "s_add_u32");
|
|
|
|
|
+ assert_eq!(fwd_exit.insts[0].read(Reg::S(1)), Some(fwd_s1));
|
|
|
|
|
+ assert_eq!(fwd_exit.insts[0].read(Reg::S(2)), Some(fwd_s2));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn remove_trivial_phis_compresses_reverse_chain_in_bounded_steps() {
|
|
|
|
|
+ const CHAIN: usize = 4096;
|
|
|
|
|
+ let mut template = build_words(&[0xBE80_0380, S_ENDPGM], false).blocks[0].insts[0].clone();
|
|
|
|
|
+ template.writes.clear();
|
|
|
|
|
+ let mut blocks = Vec::with_capacity(CHAIN);
|
|
|
|
|
+ for k in 0..CHAIN {
|
|
|
|
|
+ let v1 = Value(2 + 2 * k as u32);
|
|
|
|
|
+ let v2 = Value(3 + 2 * k as u32);
|
|
|
|
|
+ let (n1, n2) = if k + 1 < CHAIN {
|
|
|
|
|
+ (Value(2 + 2 * (k + 1) as u32), Value(3 + 2 * (k + 1) as u32))
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (Value(0), Value(1))
|
|
|
|
|
+ };
|
|
|
|
|
+ let mut inst = template.clone();
|
|
|
|
|
+ inst.reads = vec![(Reg::S(1), v1), (Reg::S(2), v2)];
|
|
|
|
|
+ blocks.push(Block {
|
|
|
|
|
+ offset: k * 4,
|
|
|
|
|
+ preds: vec![],
|
|
|
|
|
+ phis: vec![
|
|
|
|
|
+ Phi {
|
|
|
|
|
+ reg: Reg::S(1),
|
|
|
|
|
+ value: v1,
|
|
|
|
|
+ args: vec![n1, n1],
|
|
|
|
|
+ },
|
|
|
|
|
+ Phi {
|
|
|
|
|
+ reg: Reg::S(2),
|
|
|
|
|
+ value: v2,
|
|
|
|
|
+ args: vec![n2, n2],
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ insts: vec![inst],
|
|
|
|
|
+ exit: Exit::End,
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ let steps = remove_trivial_phis_steps(&mut blocks, 2 + 2 * CHAIN as u32);
|
|
|
|
|
+ assert!(blocks.iter().all(|b| b.phis.is_empty()));
|
|
|
|
|
+ assert!(
|
|
|
|
|
+ blocks
|
|
|
|
|
+ .iter()
|
|
|
|
|
+ .all(|b| b.insts[0].reads == [(Reg::S(1), Value(0)), (Reg::S(2), Value(1))])
|
|
|
|
|
+ );
|
|
|
|
|
+ assert!(steps < 100_000, "steps = {steps}");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn non_trivial_phis_preserve_compressed_representatives() {
|
|
|
|
|
+ const LAYERS: usize = 4;
|
|
|
|
|
+ let mut code = vec![
|
|
|
|
|
+ 0xBE81_0381, // s_mov_b32 s1, 1
|
|
|
|
|
+ 0xBE82_0382, // s_mov_b32 s2, 2
|
|
|
|
|
+ 0xBF06_8000, // s_cmp_eq_u32 s0, 0
|
|
|
|
|
+ s_cbranch_scc1((2 + (LAYERS - 1) * 4) as i16),
|
|
|
|
|
+ 0xBE81_0383, // s_mov_b32 s1, 3
|
|
|
|
|
+ s_branch((LAYERS * 4) as i16),
|
|
|
|
|
+ ];
|
|
|
|
|
+ for k in 0..LAYERS {
|
|
|
|
|
+ code.extend([
|
|
|
|
|
+ 0xBF06_0201, // s_cmp_eq_u32 s1, s2
|
|
|
|
|
+ s_cbranch_scc1(1),
|
|
|
|
|
+ 0xBF80_0000, // s_nop 0
|
|
|
|
|
+ if k == 0 {
|
|
|
|
|
+ s_branch((LAYERS * 4 - 4) as i16)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ s_branch(-8)
|
|
|
|
|
+ },
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ code.extend([0x8000_0201, S_ENDPGM]); // s_add_u32 s0, s1, s2
|
|
|
|
|
+
|
|
|
|
|
+ let p = build_words(&code, false);
|
|
|
|
|
+ let entry_s1 = p.blocks[0].insts[0].write(Reg::S(1)).unwrap();
|
|
|
|
|
+ let entry_s2 = p.blocks[0].insts[1].write(Reg::S(2)).unwrap();
|
|
|
|
|
+ let arm_s1 = p.blocks[1].insts[0].write(Reg::S(1)).unwrap();
|
|
|
|
|
+ let merge = p.blocks.last().unwrap();
|
|
|
|
|
+ for b in &p.blocks[..p.blocks.len() - 1] {
|
|
|
|
|
+ assert!(b.phis.is_empty(), "{:?}", b.phis);
|
|
|
|
|
+ }
|
|
|
|
|
+ let [phi] = &merge.phis[..] else {
|
|
|
|
|
+ panic!("{:?}", merge.phis)
|
|
|
|
|
+ };
|
|
|
|
|
+ assert_eq!(phi.reg, Reg::S(1));
|
|
|
|
|
+ assert_eq!(phi.args, [arm_s1, entry_s1]);
|
|
|
|
|
+ assert_eq!(merge.insts[0].read(Reg::S(1)), Some(phi.value));
|
|
|
|
|
+ assert_eq!(merge.insts[0].read(Reg::S(2)), Some(entry_s2));
|
|
|
|
|
+
|
|
|
|
|
+ let mut loop_code = vec![
|
|
|
|
|
+ 0xBE81_0381, // s_mov_b32 s1, 1
|
|
|
|
|
+ 0xBE82_0382, // s_mov_b32 s2, 2
|
|
|
|
|
+ s_branch(((LAYERS - 1) * 4) as i16),
|
|
|
|
|
+ ];
|
|
|
|
|
+ for k in 0..LAYERS {
|
|
|
|
|
+ loop_code.extend([
|
|
|
|
|
+ 0xBF06_0201, // s_cmp_eq_u32 s1, s2
|
|
|
|
|
+ s_cbranch_scc1(1),
|
|
|
|
|
+ 0xBF80_0000, // s_nop 0
|
|
|
|
|
+ if k == 0 {
|
|
|
|
|
+ s_branch((LAYERS * 4 - 4) as i16)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ s_branch(-8)
|
|
|
|
|
+ },
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ loop_code.extend([
|
|
|
|
|
+ 0x8001_0201, // s_add_u32 s1, s1, s2
|
|
|
|
|
+ 0xBF07_8401, // s_cmp_lg_u32 s1, 4
|
|
|
|
|
+ s_cbranch_scc1(-3),
|
|
|
|
|
+ 0x8000_0201, // s_add_u32 s0, s1, s2
|
|
|
|
|
+ S_ENDPGM,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ let lp = build_words(&loop_code, false);
|
|
|
|
|
+ let lp_s1 = lp.blocks[0].insts[0].write(Reg::S(1)).unwrap();
|
|
|
|
|
+ let lp_s2 = lp.blocks[0].insts[1].write(Reg::S(2)).unwrap();
|
|
|
|
|
+ let header = &lp.blocks[lp.blocks.len() - 2];
|
|
|
|
|
+ let exit = lp.blocks.last().unwrap();
|
|
|
|
|
+ for b in &lp.blocks[..lp.blocks.len() - 2] {
|
|
|
|
|
+ assert!(b.phis.is_empty(), "{:?}", b.phis);
|
|
|
|
|
+ }
|
|
|
|
|
+ assert!(exit.phis.is_empty(), "{:?}", exit.phis);
|
|
|
|
|
+ let [loop_phi] = &header.phis[..] else {
|
|
|
|
|
+ panic!("{:?}", header.phis)
|
|
|
|
|
+ };
|
|
|
|
|
+ let next_s1 = header.insts[0].write(Reg::S(1)).unwrap();
|
|
|
|
|
+ assert_eq!(loop_phi.reg, Reg::S(1));
|
|
|
|
|
+ assert_eq!(loop_phi.args, [lp_s1, next_s1]);
|
|
|
|
|
+ assert_eq!(header.insts[0].read(Reg::S(1)), Some(loop_phi.value));
|
|
|
|
|
+ assert_eq!(header.insts[0].read(Reg::S(2)), Some(lp_s2));
|
|
|
|
|
+ assert_eq!(exit.insts[0].read(Reg::S(1)), Some(next_s1));
|
|
|
|
|
+ assert_eq!(exit.insts[0].read(Reg::S(2)), Some(lp_s2));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|