Skip to content

Commit

Permalink
changed interface to specify which algorithm to run
Browse files Browse the repository at this point in the history
  • Loading branch information
d-sonuga committed Aug 7, 2024
1 parent 2dd493f commit 7099412
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
23 changes: 20 additions & 3 deletions regalloc2-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<CliAlgorithm> for Algorithm {
fn from(cli_algo: CliAlgorithm) -> Algorithm {
match cli_algo {
CliAlgorithm::Ion => Algorithm::Ion,
CliAlgorithm::Fastalloc => Algorithm::Fastalloc,
}
}
}

fn main() {
Expand All @@ -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,
Expand Down
20 changes: 13 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V> = hashbrown::HashMap<K, V, BuildHasherDefault<FxHasher>>;
Expand Down Expand Up @@ -1559,13 +1559,19 @@ pub fn run<F: Function>(
env: &MachineEnv,
options: &RegallocOptions,
) -> Result<Output, RegAllocError> {
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 {
Expand All @@ -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,
}

0 comments on commit 7099412

Please sign in to comment.