diff --git a/regalloc2-tool/src/main.rs b/regalloc2-tool/src/main.rs index 1763456f..c224834b 100644 --- a/regalloc2-tool/src/main.rs +++ b/regalloc2-tool/src/main.rs @@ -2,8 +2,7 @@ use std::path::PathBuf; use clap::Parser; use regalloc2::{ - checker::Checker, serialize::SerializableFunction, Block, Edit, Function, InstOrEdit, Output, - RegallocOptions, + checker::Checker, serialize::SerializableFunction, Algorithm, Block, Edit, Function, InstOrEdit, Output, RegallocOptions }; #[derive(Parser)] @@ -15,6 +14,24 @@ struct Args { /// Input file containing a bincode-encoded SerializedFunction. input: PathBuf, + + /// Which register allocation algorithm to use. + algorithm: CliAlgorithm, +} + +#[derive(Clone, Copy, Debug, clap::ValueEnum)] +enum CliAlgorithm { + Ion, + Fastalloc, +} + +impl From for Algorithm { + fn from(cli_algo: CliAlgorithm) -> Algorithm { + match cli_algo { + CliAlgorithm::Ion => Algorithm::Ion, + CliAlgorithm::Fastalloc => Algorithm::Fastalloc, + } + } } fn main() { @@ -32,7 +49,7 @@ fn main() { let options = RegallocOptions { verbose_log: true, validate_ssa: true, - use_fastalloc: true, + algorithm: args.algorithm.into() }; let output = match regalloc2::run(&function, function.machine_env(), &options) { Ok(output) => output, diff --git a/src/lib.rs b/src/lib.rs index 3b0c4c72..248d5a83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,7 @@ macro_rules! trace_enabled { }; } -use core::hash::BuildHasherDefault; +use core::{default, hash::BuildHasherDefault}; use std::iter::FromIterator; use rustc_hash::FxHasher; type FxHashMap = hashbrown::HashMap>; @@ -1559,13 +1559,19 @@ pub fn run( env: &MachineEnv, options: &RegallocOptions, ) -> Result { - if options.use_fastalloc { - fastalloc::run(func, env, options.verbose_log, options.validate_ssa) - } else { - ion::run(func, env, options.verbose_log, options.validate_ssa) + match options.algorithm { + Algorithm::Ion => ion::run(func, env, options.verbose_log, options.validate_ssa), + Algorithm::Fastalloc => fastalloc::run(func, env, options.verbose_log, options.validate_ssa) } } +#[derive(Clone, Copy, Debug, Default)] +pub enum Algorithm { + #[default] + Ion, + Fastalloc, +} + /// Options for allocation. #[derive(Clone, Copy, Debug, Default)] pub struct RegallocOptions { @@ -1575,6 +1581,6 @@ pub struct RegallocOptions { /// Run the SSA validator before allocating registers. pub validate_ssa: bool, - /// Run the SSRA algorithm - pub use_fastalloc: bool, + /// The register allocation algorithm to be used. + pub algorithm: Algorithm, }