| 1 |
// Copyright © 2023 Mel Henning |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
use crate::ir::*; |
| 5 |
|
| 6 |
use compiler::cfg::CFGBuilder; |
| 7 |
use rustc_hash::{FxBuildHasher, FxHashMap}; |
| 8 |
|
| 9 |
fn clone_branch(op: &Op) -> Op { |
| 10 |
match op { |
| 11 |
Op::Bra(b) => { |
| 12 |
assert!(b.cond.is_true()); |
| 13 |
Op::Bra(b.clone()) |
| 14 |
} |
| 15 |
Op::Exit(e) => Op::Exit(e.clone()), |
| 16 |
_ => unreachable!(), |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
fn jump_thread(func: &mut Function) -> bool { |
| 21 |
// Let's call a basic block "trivial" if its only instruction is an |
| 22 |
// unconditional branch. If a block is trivial, we can update all of its |
| 23 |
// predecessors to jump to its sucessor. |
| 24 |
// |
| 25 |
// A single reverse pass over the basic blocks is enough to update all of |
| 26 |
// the edges we're interested in. Roughly, if we assume that all loops in |
| 27 |
// the shader can terminate, then loop heads are never trivial and we |
| 28 |
// never replace a backward edge. Therefore, in each step we only need to |
| 29 |
// make sure that later control flow has been replaced in order to update |
| 30 |
// the current block as much as possible. |
| 31 |
// |
| 32 |
// We additionally try to update a branch-to-empty-block to point to the |
| 33 |
// block's successor, which along with block dce/reordering can sometimes |
| 34 |
// enable a later optimization that converts branches to fallthrough. |
| 35 |
let mut progress = false; |
| 36 |
|
| 37 |
// A branch to label can be replaced with Op |
| 38 |
let mut replacements: FxHashMap<Label, Op> = Default::default(); |
| 39 |
|
| 40 |
// Invariant 1: At the end of each loop iteration, |
| 41 |
// every trivial block with an index in [i, blocks.len()) |
| 42 |
// is represented in replacements.keys() |
| 43 |
// Invariant 2: replacements.values() never contains |
| 44 |
// a branch to a trivial block |
| 45 |
for i in (0..func.blocks.len()).rev() { |
| 46 |
// Replace the branch if possible |
| 47 |
if let Some(instr) = func.blocks[i].instrs.last_mut() { |
| 48 |
if let Op::Bra(bra) = &mut instr.op { |
| 49 |
if let Some(replacement) = replacements.get(&bra.target) { |
| 50 |
if bra.cond.is_true() { |
| 51 |
// Unconditional branches can just be replaced |
| 52 |
instr.op = clone_branch(replacement); |
| 53 |
progress = true; |
| 54 |
} else { |
| 55 |
// OpExit has a form that takes an input predicate but it |
| 56 |
// doesn't support upred so there's nothing we can do here. |
| 57 |
// TODO: however we could do something for the case OpBra has a |
| 58 |
// non uniform input predicate. |
| 59 |
if let Op::Bra(replacement) = replacement { |
| 60 |
bra.target = replacement.target; |
| 61 |
progress = true; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
// If the branch target was previously a trivial block then the |
| 66 |
// branch was previously a forward edge (see above) and by |
| 67 |
// invariants 1 and 2 we just updated the branch to target |
| 68 |
// a nontrivial block |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Is this block trivial? |
| 73 |
let block_label = func.blocks[i].label; |
| 74 |
match &func.blocks[i].instrs[..] { |
| 75 |
[instr] => { |
| 76 |
if instr.is_branch_always_taken() { |
| 77 |
// Upholds invariant 2 because we updated the branch above |
| 78 |
replacements.insert(block_label, clone_branch(&instr.op)); |
| 79 |
} |
| 80 |
} |
| 81 |
[] => { |
| 82 |
// Empty block - falls through |
| 83 |
// Our successor might be trivial, so we need to |
| 84 |
// apply the rewrite map to uphold invariant 2 |
| 85 |
let target_label = func.blocks[i + 1].label; |
| 86 |
let replacement = replacements |
| 87 |
.get(&target_label) |
| 88 |
.map(clone_branch) |
| 89 |
.unwrap_or_else(|| { |
| 90 |
Op::Bra( |
| 91 |
OpBra { |
| 92 |
target: target_label, |
| 93 |
cond: true.into(), |
| 94 |
} |
| 95 |
.into(), |
| 96 |
) |
| 97 |
}); |
| 98 |
replacements.insert(block_label, replacement); |
| 99 |
} |
| 100 |
_ => (), |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if progress { |
| 105 |
// We don't update the CFG above, so rewrite it if we made progress |
| 106 |
rewrite_cfg(func); |
| 107 |
} |
| 108 |
|
| 109 |
progress |
| 110 |
} |
| 111 |
|
| 112 |
fn rewrite_cfg(func: &mut Function) { |
| 113 |
// CFGBuilder takes care of removing dead blocks for us |
| 114 |
// We use the basic block's label to identify it |
| 115 |
let mut builder = CFGBuilder::<_, _, FxBuildHasher>::new(); |
| 116 |
|
| 117 |
for i in 0..func.blocks.len() { |
| 118 |
let block = &func.blocks[i]; |
| 119 |
// Note: fall-though must be first edge |
| 120 |
if block.falls_through() { |
| 121 |
let next_block = &func.blocks[i + 1]; |
| 122 |
builder.add_edge(block.label, next_block.label); |
| 123 |
} |
| 124 |
if let Some(control_flow) = block.branch() { |
| 125 |
match &control_flow.op { |
| 126 |
Op::Bra(bra) => { |
| 127 |
builder.add_edge(block.label, bra.target); |
| 128 |
} |
| 129 |
Op::Exit(_) => (), |
| 130 |
_ => unreachable!(), |
| 131 |
}; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
for block in func.blocks.drain() { |
| 136 |
builder.add_node(block.label, block); |
| 137 |
} |
| 138 |
let _ = std::mem::replace(&mut func.blocks, builder.as_cfg()); |
| 139 |
} |
| 140 |
|
| 141 |
/// Replace jumps to the following block with fall-through |
| 142 |
fn opt_fall_through(func: &mut Function) { |
| 143 |
for i in 0..func.blocks.len() - 1 { |
| 144 |
let remove_last_instr = match func.blocks[i].branch() { |
| 145 |
Some(b) => match &b.op { |
| 146 |
Op::Bra(bra) => bra.target == func.blocks[i + 1].label, |
| 147 |
_ => false, |
| 148 |
}, |
| 149 |
None => false, |
| 150 |
}; |
| 151 |
|
| 152 |
if remove_last_instr { |
| 153 |
func.blocks[i].instrs.pop(); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
impl Function { |
| 159 |
pub fn opt_jump_thread(&mut self) { |
| 160 |
if jump_thread(self) { |
| 161 |
opt_fall_through(self); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
impl Shader<'_> { |
| 167 |
/// A simple jump threading pass |
| 168 |
/// |
| 169 |
/// Note that this can introduce critical edges, so it cannot be run before RA |
| 170 |
pub fn opt_jump_thread(&mut self) { |
| 171 |
for f in &mut self.functions { |
| 172 |
f.opt_jump_thread(); |
| 173 |
} |
| 174 |
} |
| 175 |
} |