Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Add support for using regalloc2 as a backend register allocation algorithm. #127

Closed
wants to merge 7 commits into from

Conversation

cfallin
Copy link
Member

@cfallin cfallin commented Sep 2, 2021

This PR adds a shim that translates regalloc.rs' view of the input program into a form that regalloc2 can use, and translates the allocations back to regalloc.rs-like results.

This was co-developed and -optimized with regalloc2, as a sort of short-to-medium-term solution to the Cranelift use-case before Cranelift can be migrated to use the regalloc2 API directly. Performance-testing results show that this is a marginal improvement over existing (current default) BTRA, including the shim overhead, so I believe that this is a reasonable thing to use as a stepping-stone rather than wait for the above transition.

It also includes the fuzz target regalloc2 that runs the regalloc.rs checker with the new algorithm; I have fuzzed the combination of the two with this target, in addition to fuzzing just regalloc2 with its own fuzz targets.

regalloc2 is currently pending a relicensing (bytecodealliance/regalloc2#7) and I plan to publish 0.0.1 to crates.io once that is done. Before then, we cannot actually merge this PR, but I am hoping that this will at least allow us to parallelize the review effort :-)

I've squashed the 53-commit development history into one commit for this PR; the original is on the regalloc2-dev-history branch in my fork.

@cfallin cfallin requested a review from bnjbvr September 2, 2021 21:37
…rithm.

This PR adds a shim that translates regalloc.rs' view of the input
program into a form that regalloc2 can use, and translates the
allocations back to regalloc.rs-like results.

This was co-developed and -optimized with regalloc2, as a sort of
short-to-medium-term solution to the Cranelift use-case before Cranelift
can be migrated to use the regalloc2 API directly. Performance-testing
results show that this is a marginal improvement over existing (current
default) BTRA, including the shim overhead, so I believe that this is a
reasonable thing to use as a stepping-stone rather than wait for the
above transition.

It also includes the fuzz target `regalloc2` that runs the regalloc.rs
checker with the new algorithm; I have fuzzed the combination of the two
with this target, in addition to fuzzing just regalloc2 with its own
fuzz targets.
to_reg: Writable<RealReg>,
for_reg: Reg,
},
/// As for DefReg, for for spillslots.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*but for

@bnjbvr
Copy link
Member

bnjbvr commented Sep 10, 2021

Thanks for doing this work, and sorry I couldn't get to review it earlier. I will give it a look, probably in several phases, across next week.

@cfallin
Copy link
Member Author

cfallin commented Sep 10, 2021

No rush, and sorry for throwing such a large PR at you! Thanks in advance for whatever time you're able to spend on it :-)

@cfallin
Copy link
Member Author

cfallin commented Sep 10, 2021

Also the test failures seem to be new since I rebased to the latest regalloc2 -- I'll take a look at those next week and get this into a clean/passing state.

Copy link
Member

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A first batch of comments: I've looked at everything but the main thing in regalloc2.rs. Other changes look great so far!

lib/src/analysis_data_flow.rs Outdated Show resolved Hide resolved
@@ -1,5 +1,7 @@
//! Data structures for the whole crate.

#![allow(dead_code)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit unfortunate to regress on dead code, I am happy to keep it if it's intended to be for the short term and you have thoughts about removing it later, but out of curiosity, why was it needed? (Maybe it is easy to remove)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly enough, I went down a bit of a rabbithole trying to remove this, without success: if I remove the whole function I get a compile error, and if I remove the directive then I see a warning about dead code. Possibly something weird to do with alternative build configurations/features, but I wasn't able to work it out. I added a comment at least to say "it's definitely used" :-)

lib/src/inst_stream.rs Outdated Show resolved Hide resolved
lib/src/inst_stream.rs Show resolved Hide resolved
lib/src/inst_stream.rs Outdated Show resolved Hide resolved
lib/src/checker.rs Outdated Show resolved Hide resolved
lib/src/lib.rs Show resolved Hide resolved
lib/src/lib.rs Outdated Show resolved Hide resolved
@cfallin
Copy link
Member Author

cfallin commented Sep 23, 2021

Thanks @bnjbvr and sorry for the delay in responding/updating here. I think I've addressed everything and I'm happy to address whatever other comments you may have!

@cfallin
Copy link
Member Author

cfallin commented Sep 23, 2021

(And just to state for maximal clarity, the Cargo.toml here still refers to the git repo for regalloc2 as it isn't released onto crates.io yet; once bytecodealliance/regalloc2#7 is addressed, we can release that and update the deps here.)

@bnjbvr bnjbvr self-requested a review October 4, 2021 09:14
@cfallin cfallin requested a review from julian-seward1 October 5, 2021 16:57
@cfallin
Copy link
Member Author

cfallin commented Oct 5, 2021

Adding @julian-seward1 as a reviewer as well after checking with him -- thanks!

@cfallin
Copy link
Member Author

cfallin commented Feb 16, 2022

Closing old PR as the new plan is to use regalloc2 directly in Cranelift and so we won't need this compatibility shim anymore (thanks to all reviewers here though!).

@cfallin cfallin closed this Feb 16, 2022
@bnjbvr
Copy link
Member

bnjbvr commented Feb 16, 2022

Thanks for the heads-up! I've looked in the wasmtime's repository, and both searching for "regalloc2" in issues and PRs doesn't show any relevant discussion. There's no open RFC to talk about it either (although I expect it is the best obvious path and might not even mandate a RFC). I've attended the last few Cranelift meetings too and there was no discussions about this, unless I missed something. Has this plan be discussed in some public forum that I missed?

@bjorn3
Copy link
Contributor

bjorn3 commented Feb 16, 2022

Making the move to regalloc2 has been one of the motivators for introducing ISLE: https://github.com/bytecodealliance/rfcs/pull/13/files#diff-eaec9e420ce24444f724c423e3c78a93525054404ba8c2ac399357f46949315fR172-R186 The transition to ISLE is not yet finished. I believe the plan is to wait on this before switching to regalloc2.

@cfallin
Copy link
Member Author

cfallin commented Feb 16, 2022

Hey @bnjbvr -- sorry for not linking to more detail here! This plan (to use regalloc2 directly) is part of the Cranelift 2022 roadmap (see this section) which merged two weeks ago. And indeed it was part of the motivation for ISLE (more easily allowing cross-cutting changes to be made), as @bjorn3 links above.

The timing for closing this now just had to do with me making another pass at cleaning up my old open PRs, no recent discussions or anything like that :-)

@bnjbvr
Copy link
Member

bnjbvr commented Feb 16, 2022

Ah ok, thanks! Sorry, I mistakenly thought there was something else that I had missed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants