Skip to content

Commit

Permalink
resolving conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
mlokr committed Sep 24, 2024
2 parents 09d36db + e684ee5 commit 34f4b5d
Show file tree
Hide file tree
Showing 10 changed files with 2,311 additions and 6 deletions.
6 changes: 6 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ path = "fuzz_targets/ion_checker.rs"
test = false
doc = false

[[bin]]
name = "fastalloc_checker"
path = "fuzz_targets/fastalloc_checker.rs"
test = false
doc = false

# Enable debug assertions and overflow checks when fuzzing
[profile.release]
debug = true
Expand Down
45 changes: 45 additions & 0 deletions fuzz/fuzz_targets/fastalloc_checker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Released under the terms of the Apache 2.0 license with LLVM
* exception. See `LICENSE` for details.
*/

#![no_main]
use regalloc2::fuzzing::arbitrary::{Arbitrary, Result, Unstructured};
use regalloc2::fuzzing::checker::Checker;
use regalloc2::fuzzing::func::{Func, Options};
use regalloc2::fuzzing::fuzz_target;

#[derive(Clone, Debug)]
struct TestCase {
func: Func,
}

impl Arbitrary<'_> for TestCase {
fn arbitrary(u: &mut Unstructured) -> Result<TestCase> {
Ok(TestCase {
func: Func::arbitrary_with_options(
u,
&Options {
reused_inputs: true,
fixed_regs: true,
fixed_nonallocatable: true,
clobbers: true,
reftypes: false,
},
)?,
})
}
}

fuzz_target!(|testcase: TestCase| {
let func = testcase.func;
let _ = env_logger::try_init();
log::trace!("func:\n{:?}", func);
let env = regalloc2::fuzzing::func::machine_env();
let out =
regalloc2::fuzzing::fastalloc::run(&func, &env, true, false).expect("regalloc did not succeed");

let mut checker = Checker::new(&func, &env);
checker.prepare(&out);
checker.run().expect("checker failed");
});
23 changes: 21 additions & 2 deletions regalloc2-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ 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 +15,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,6 +50,7 @@ fn main() {
let options = RegallocOptions {
verbose_log: true,
validate_ssa: true,
algorithm: args.algorithm.into(),
};
let output = match regalloc2::run(&function, function.machine_env(), &options) {
Ok(output) => output,
Expand Down
44 changes: 44 additions & 0 deletions src/fastalloc/iter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{Operand, OperandConstraint, OperandKind};

pub struct Operands<'a>(pub &'a [Operand]);

impl<'a> Operands<'a> {
pub fn new(operands: &'a [Operand]) -> Self {
Self(operands)
}

pub fn matches<F: Fn(Operand) -> bool + 'a>(
&self,
predicate: F,
) -> impl Iterator<Item = (usize, Operand)> + 'a {
self.0
.iter()
.cloned()
.enumerate()
.filter(move |(_, op)| predicate(*op))
}

pub fn def_ops(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
self.matches(|op| op.kind() == OperandKind::Def)
}

pub fn use_ops(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
self.matches(|op| op.kind() == OperandKind::Use)
}

pub fn reuse(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
self.matches(|op| matches!(op.constraint(), OperandConstraint::Reuse(_)))
}

pub fn fixed(&self) -> impl Iterator<Item = (usize, Operand)> + 'a {
self.matches(|op| matches!(op.constraint(), OperandConstraint::FixedReg(_)))
}
}

impl<'a> core::ops::Index<usize> for Operands<'a> {
type Output = Operand;

fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
Loading

0 comments on commit 34f4b5d

Please sign in to comment.