-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR has 2 components: - A `SerializableFunction` which wraps a `Function` and `MachineEnv`. This type can be serialized and deserialized with Serde, and is enabled by the "enable-serde" feature. - A `regalloc2-tool` binary which reads a bincode-encoded `SerializableFunction` and then runs the register allocator and checker on it. This is a useful tool for debugging register allocation failures and to investigate cases of poor register allocation.
- Loading branch information
Showing
7 changed files
with
436 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[workspace] | ||
members = ["regalloc2-tool"] | ||
|
||
[package] | ||
name = "regalloc2" | ||
version = "0.9.1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "regalloc2-tool" | ||
authors = [ | ||
"Chris Fallin <chris@cfallin.org>", | ||
"Mozilla SpiderMonkey Developers", | ||
] | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
license = "Apache-2.0 WITH LLVM-exception" | ||
description = "Tool for testing regalloc2" | ||
repository = "https://github.com/bytecodealliance/regalloc2" | ||
|
||
[dependencies] | ||
bincode = "1.3.3" | ||
clap = { version = "4.3.11", features = ["derive"] } | ||
pretty_env_logger = "0.5.0" | ||
regalloc2 = { path = "..", features = ["trace-log", "enable-serde"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::Parser; | ||
use regalloc2::{ | ||
checker::Checker, serialize::SerializableFunction, Block, Edit, Function, InstOrEdit, Output, | ||
RegallocOptions, | ||
}; | ||
|
||
#[derive(Parser)] | ||
/// Tool for testing regalloc2. | ||
struct Args { | ||
/// Print the input function and the result of register allocation. | ||
#[clap(short = 'v')] | ||
verbose: bool, | ||
|
||
/// Input file containing a bincode-encoded SerializedFunction. | ||
input: PathBuf, | ||
} | ||
|
||
fn main() { | ||
pretty_env_logger::init(); | ||
let args = Args::parse(); | ||
|
||
let input = std::fs::read(&args.input).expect("could not read input file"); | ||
let function: SerializableFunction = | ||
bincode::deserialize(&input).expect("could not deserialize input file"); | ||
|
||
if args.verbose { | ||
println!("Input function: {function:?}"); | ||
} | ||
|
||
let options = RegallocOptions { | ||
verbose_log: true, | ||
validate_ssa: true, | ||
}; | ||
let output = match regalloc2::run(&function, function.machine_env(), &options) { | ||
Ok(output) => output, | ||
Err(e) => { | ||
panic!("Register allocation failed: {e:#?}"); | ||
} | ||
}; | ||
|
||
if args.verbose { | ||
print_output(&function, &output); | ||
} | ||
|
||
let mut checker = Checker::new(&function, function.machine_env()); | ||
checker.prepare(&output); | ||
if let Err(e) = checker.run() { | ||
panic!("Regsiter allocation checker failed: {e:#?}"); | ||
} | ||
} | ||
|
||
fn print_output(func: &SerializableFunction, output: &Output) { | ||
print!("Register allocation result: {{\n"); | ||
for i in 0..func.num_blocks() { | ||
let block = Block::new(i); | ||
let succs = func | ||
.block_succs(block) | ||
.iter() | ||
.map(|b| b.index()) | ||
.collect::<Vec<_>>(); | ||
let preds = func | ||
.block_preds(block) | ||
.iter() | ||
.map(|b| b.index()) | ||
.collect::<Vec<_>>(); | ||
print!(" block{}: # succs:{:?} preds:{:?}\n", i, succs, preds); | ||
for inst_or_edit in output.block_insts_and_edits(func, block) { | ||
match inst_or_edit { | ||
InstOrEdit::Inst(inst) => { | ||
let op = if func.is_ret(inst) { | ||
"ret" | ||
} else if func.is_branch(inst) { | ||
"branch" | ||
} else { | ||
"op" | ||
}; | ||
let ops: Vec<_> = func | ||
.inst_operands(inst) | ||
.iter() | ||
.zip(output.inst_allocs(inst)) | ||
.map(|(op, alloc)| format!("{op} => {alloc}")) | ||
.collect(); | ||
let ops = ops.join(", "); | ||
print!(" inst{}: {op} {ops}\n", inst.index(),); | ||
} | ||
InstOrEdit::Edit(Edit::Move { from, to }) => { | ||
print!(" edit: move {to} <- {from}\n"); | ||
} | ||
} | ||
} | ||
} | ||
print!("}}\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.