|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | +use regalloc2::{ |
| 5 | + checker::Checker, serialize::SerializableFunction, Block, Edit, Function, InstOrEdit, Output, |
| 6 | + RegallocOptions, |
| 7 | +}; |
| 8 | + |
| 9 | +#[derive(Parser)] |
| 10 | +/// Tool for testing regalloc2. |
| 11 | +struct Args { |
| 12 | + /// Print the input function and the result of register allocation. |
| 13 | + #[clap(short = 'v')] |
| 14 | + verbose: bool, |
| 15 | + |
| 16 | + /// Input file containing a bincode-encoded SerializedFunction. |
| 17 | + input: PathBuf, |
| 18 | +} |
| 19 | + |
| 20 | +fn main() { |
| 21 | + pretty_env_logger::init(); |
| 22 | + let args = Args::parse(); |
| 23 | + |
| 24 | + let input = std::fs::read(&args.input).expect("could not read input file"); |
| 25 | + let function: SerializableFunction = |
| 26 | + bincode::deserialize(&input).expect("could not deserialize input file"); |
| 27 | + |
| 28 | + if args.verbose { |
| 29 | + println!("Input function: {function:?}"); |
| 30 | + } |
| 31 | + |
| 32 | + let options = RegallocOptions { |
| 33 | + verbose_log: true, |
| 34 | + validate_ssa: true, |
| 35 | + }; |
| 36 | + let output = match regalloc2::run(&function, function.machine_env(), &options) { |
| 37 | + Ok(output) => output, |
| 38 | + Err(e) => { |
| 39 | + panic!("Regsiter allocation failed: {e:#?}"); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + if args.verbose { |
| 44 | + print_output(&function, &output); |
| 45 | + } |
| 46 | + |
| 47 | + let mut checker = Checker::new(&function, function.machine_env()); |
| 48 | + checker.prepare(&output); |
| 49 | + if let Err(e) = checker.run() { |
| 50 | + panic!("Regsiter allocation checker failed: {e:#?}"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fn print_output(func: &SerializableFunction, output: &Output) { |
| 55 | + print!("Register allocation result: {{\n"); |
| 56 | + for i in 0..func.num_blocks() { |
| 57 | + let block = Block::new(i); |
| 58 | + let succs = func |
| 59 | + .block_succs(block) |
| 60 | + .iter() |
| 61 | + .map(|b| b.index()) |
| 62 | + .collect::<Vec<_>>(); |
| 63 | + let preds = func |
| 64 | + .block_preds(block) |
| 65 | + .iter() |
| 66 | + .map(|b| b.index()) |
| 67 | + .collect::<Vec<_>>(); |
| 68 | + print!(" block{}: # succs:{:?} preds:{:?}\n", i, succs, preds); |
| 69 | + for inst_or_edit in output.block_insts_and_edits(func, block) { |
| 70 | + match inst_or_edit { |
| 71 | + InstOrEdit::Inst(inst) => { |
| 72 | + let op = if func.is_ret(inst) { |
| 73 | + "ret" |
| 74 | + } else if func.is_branch(inst) { |
| 75 | + "branch" |
| 76 | + } else { |
| 77 | + "op" |
| 78 | + }; |
| 79 | + let ops: Vec<_> = func |
| 80 | + .inst_operands(inst) |
| 81 | + .iter() |
| 82 | + .zip(output.inst_allocs(inst)) |
| 83 | + .map(|(op, alloc)| format!("{op} => {alloc}")) |
| 84 | + .collect(); |
| 85 | + let ops = ops.join(", "); |
| 86 | + print!(" inst{}: {op} {ops}\n", inst.index(),); |
| 87 | + } |
| 88 | + InstOrEdit::Edit(Edit::Move { from, to }) => { |
| 89 | + print!(" edit: move {to} <- {from}\n"); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + print!("}}\n"); |
| 95 | +} |
0 commit comments