Skip to content

Commit 2423d2c

Browse files
committed
Add support for using regalloc3 as the allocator back-end
1 parent 36f34e3 commit 2423d2c

File tree

9 files changed

+804
-4
lines changed

9 files changed

+804
-4
lines changed

.github/workflows/rust.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v3
17+
- uses: dtolnay/rust-toolchain@stable
1718
- name: Install rustfmt
1819
run: rustup component add rustfmt
1920
- name: Run rustfmt and check there's no difference
@@ -24,6 +25,7 @@ jobs:
2425
runs-on: ubuntu-latest
2526
steps:
2627
- uses: actions/checkout@v3
28+
- uses: dtolnay/rust-toolchain@stable
2729
- name: Build
2830
run: cargo build
2931
- name: Run tests
@@ -34,6 +36,7 @@ jobs:
3436
runs-on: ubuntu-latest
3537
steps:
3638
- uses: actions/checkout@v3
39+
- uses: dtolnay/rust-toolchain@stable
3740
- name: Check with all features
3841
run: cargo check --all-features
3942

@@ -42,6 +45,7 @@ jobs:
4245
runs-on: ubuntu-latest
4346
steps:
4447
- uses: actions/checkout@v3
48+
- uses: dtolnay/rust-toolchain@stable
4549
- name: Install thumbv6m-none-eabi target
4650
run: rustup target add thumbv6m-none-eabi
4751
- name: Check no_std build

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ authors = [
88
"Chris Fallin <[email protected]>",
99
"Mozilla SpiderMonkey Developers",
1010
]
11-
edition = "2018"
11+
edition = "2021"
1212
license = "Apache-2.0 WITH LLVM-exception"
1313
description = "Backtracking register allocator inspired from IonMonkey"
1414
repository = "https://github.com/bytecodealliance/regalloc2"
@@ -18,6 +18,7 @@ log = { version = "0.4.8", default-features = false }
1818
smallvec = { version = "1.6.1", features = ["union"] }
1919
rustc-hash = { version = "2.0.0", default-features = false }
2020
hashbrown = { version = "0.15", default-features = false, features = [] }
21+
regalloc3 = { git = "https://github.com/Amanieu/regalloc3.git" }
2122

2223
# Optional serde support, enabled by feature below.
2324
serde = { version = "1.0.136", features = [
@@ -46,7 +47,7 @@ std = []
4647
checker = []
4748

4849
# Enables detailed logging which can be somewhat expensive.
49-
trace-log = []
50+
trace-log = ["regalloc3/trace-log"]
5051

5152
# Exposes the internal API for fuzzing.
5253
fuzzing = ["libfuzzer-sys", "checker", "trace-log"]

fuzz/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ path = "fuzz_targets/fastalloc_checker.rs"
5555
test = false
5656
doc = false
5757

58+
[[bin]]
59+
name = "ra3_checker"
60+
path = "fuzz_targets/ra3_checker.rs"
61+
test = false
62+
doc = false
63+
5864
# Enable debug assertions and overflow checks when fuzzing
5965
[profile.release]
6066
debug = true

fuzz/fuzz_targets/ra3_checker.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Released under the terms of the Apache 2.0 license with LLVM
3+
* exception. See `LICENSE` for details.
4+
*/
5+
6+
#![no_main]
7+
use regalloc2::fuzzing::arbitrary::{Arbitrary, Result, Unstructured};
8+
use regalloc2::fuzzing::checker::Checker;
9+
use regalloc2::fuzzing::func::{Func, Options};
10+
use regalloc2::fuzzing::fuzz_target;
11+
12+
#[derive(Clone, Debug)]
13+
struct TestCase {
14+
func: Func,
15+
}
16+
17+
impl Arbitrary<'_> for TestCase {
18+
fn arbitrary(u: &mut Unstructured) -> Result<TestCase> {
19+
Ok(TestCase {
20+
func: Func::arbitrary_with_options(
21+
u,
22+
&Options {
23+
reused_inputs: true,
24+
fixed_regs: true,
25+
fixed_nonallocatable: true,
26+
clobbers: true,
27+
reftypes: true,
28+
},
29+
)?,
30+
})
31+
}
32+
}
33+
34+
fuzz_target!(|testcase: TestCase| {
35+
let func = testcase.func;
36+
let _ = env_logger::try_init();
37+
log::trace!("func:\n{:?}", func);
38+
let env = regalloc2::fuzzing::func::machine_env();
39+
40+
thread_local! {
41+
// We test that ctx is cleared properly between runs.
42+
static CTX: std::cell::RefCell<regalloc2::fuzzing::ion::Ctx> = std::cell::RefCell::default();
43+
}
44+
45+
CTX.with(|ctx| {
46+
let options = regalloc2::RegallocOptions {
47+
verbose_log: true,
48+
validate_ssa: true,
49+
algorithm: regalloc2::Algorithm::Regalloc3,
50+
};
51+
let mut ctx = ctx.borrow_mut();
52+
let out = regalloc2::run_with_ctx(&func, &env, &options, &mut *ctx)
53+
.expect("regalloc did not succeed");
54+
55+
let mut checker = Checker::new(&func, &env);
56+
checker.prepare(&out);
57+
checker.run().expect("checker failed");
58+
});
59+
});

src/fuzzing/func.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,10 @@ impl Func {
313313
let mut out_blocks = vec![];
314314
let mut in_blocks = vec![];
315315
while from < num_blocks {
316-
in_blocks.push(from);
316+
// regalloc3 doesn't allow the entry block to have predecessors.
317+
if from != 0 {
318+
in_blocks.push(from);
319+
}
317320
if num_blocks > 3 && from < num_blocks - 3 && bool::arbitrary(u)? {
318321
// To avoid critical edges, we use from+1 as an edge
319322
// block, and advance `from` an extra block; `from+2`

src/fuzzing/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub mod ion {
2727
pub mod fastalloc {
2828
pub use crate::fastalloc::*;
2929
}
30+
pub mod regalloc3 {
31+
pub use crate::regalloc3::*;
32+
}
3033
pub mod checker {
3134
pub use crate::checker::*;
3235
}

src/ion/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::liveranges::SpillWeight;
1616
use crate::cfg::{CFGInfo, CFGInfoCtx};
1717
use crate::index::ContainerComparator;
1818
use crate::indexset::IndexSet;
19+
use crate::regalloc3::Regalloc3Ctx;
1920
use crate::Vec2;
2021
use crate::{
2122
define_index, Allocation, Block, Bump, Edit, Function, FxHashMap, FxHashSet, MachineEnv,
@@ -489,6 +490,8 @@ pub struct Ctx {
489490
pub(crate) scratch_workqueue_set: FxHashSet<Block>,
490491

491492
pub(crate) scratch_bump: Bump,
493+
494+
pub(crate) ra3_ctx: Regalloc3Ctx,
492495
}
493496

494497
impl Ctx {

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub mod indexset;
5050
pub(crate) mod ion;
5151
pub(crate) mod moves;
5252
pub(crate) mod postorder;
53+
pub(crate) mod regalloc3;
5354
pub mod ssa;
5455

5556
#[macro_use]
@@ -1598,7 +1599,7 @@ pub fn run<F: Function>(
15981599
options: &RegallocOptions,
15991600
) -> Result<Output, RegAllocError> {
16001601
match options.algorithm {
1601-
Algorithm::Ion => {
1602+
Algorithm::Ion | Algorithm::Regalloc3 => {
16021603
let mut ctx = Ctx::default();
16031604
run_with_ctx(func, env, options, &mut ctx)?;
16041605
Ok(ctx.output)
@@ -1623,6 +1624,13 @@ pub fn run_with_ctx<'a, F: Function>(
16231624
Algorithm::Fastalloc => {
16241625
ctx.output = fastalloc::run(func, env, options.verbose_log, options.validate_ssa)?
16251626
}
1627+
Algorithm::Regalloc3 => ctx.ra3_ctx.run(
1628+
func,
1629+
env,
1630+
&mut ctx.cfginfo,
1631+
&mut ctx.cfginfo_ctx,
1632+
&mut ctx.output,
1633+
)?,
16261634
}
16271635
Ok(&ctx.output)
16281636
}
@@ -1632,6 +1640,7 @@ pub enum Algorithm {
16321640
#[default]
16331641
Ion,
16341642
Fastalloc,
1643+
Regalloc3,
16351644
}
16361645

16371646
/// Options for allocation.

0 commit comments

Comments
 (0)