Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New graph-coloring-based register allocator #816

Merged
merged 6 commits into from
Feb 23, 2022

Conversation

mohammadfawaz
Copy link
Contributor

@mohammadfawaz mohammadfawaz commented Feb 19, 2022

Closes #155 and #800

This is basically Chaitin's original graph-coloring based algorithm for register allocation. The new file that I that added, called register_allocator.rs, has all the steps in details. I tried to make the steps and the different pieces of the algorithm as clear as possible. I couldn't really find one source where the full algorithm in details is described. However, my comments should contain the union of what I found in the literature and how it applies to the data structures we have in CodeGen.

With this, the bytecode size typically goes down due to the coalescing phase. The table below shows the bytecode size saving percentages. I noticed that the larger the code, the bigger the savings.

Pipeline Min Max Average
Without IR 0% 56% 17%
With IR 0% 58% 16%

Also, this patch fixes the functional issue described in #800. Loops introduce complexities to liveness analysis due to all the backward jumps, so I suspect something is going wrong with the existing allocator.

Now that I have all the building blocks, I can experiment with other algorithms such as the iterartive one described in https://c9x.me/compile/bib/irc.pdf. The steps are basically the same with minor tweeks and run in an iterative outer loop.

Update: E2E tests with the IR pipeline enabled seem to be okay except for three tests that are currently failing on master and the test that I'm adding here (due to #821).

@mohammadfawaz mohammadfawaz self-assigned this Feb 19, 2022
@mohammadfawaz mohammadfawaz added compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request bug Something isn't working labels Feb 19, 2022
@mohammadfawaz
Copy link
Contributor Author

mohammadfawaz commented Feb 19, 2022

Some IR tests are failing because they check for exact the ASM output which is different now. I will update them. Converting to draft until I’m done.

@mohammadfawaz mohammadfawaz marked this pull request as draft February 19, 2022 04:22
@mohammadfawaz mohammadfawaz marked this pull request as ready for review February 20, 2022 03:46
@mohammadfawaz
Copy link
Contributor Author

Ready for review.

@otrho - regarding the IR tests that I had to update: how did you figure out/check the assembly for these tests? Did you do it by inspection?

@otrho
Copy link
Contributor

otrho commented Feb 20, 2022

@otrho - regarding the IR tests that I had to update: how did you figure out/check the assembly for these tests? Did you do it by inspection?

Yeah, when you're adding them progressively it's fairly easy to eyeball them and confirm they're correct. But making a pervasive change to codegen like this means there's a fair bit of work confirming the new ASM is right.

At the same time you're trusting that the E2E regression tests are confirming that the changes haven't broken behaviour so I guess I'd be checking for stuff which looks obviously wrong in the IR tests, rather than making sure they're absolutely doing the right thing.

To run E2E with IR you can use the SWAY_TEST_USE_IR=1 environment variable. Note, at the moment there are 3 tests on master which are failing -- the recent generics and traits stuff you added, which I mentioned to you last week.

@mohammadfawaz
Copy link
Contributor Author

mohammadfawaz commented Feb 20, 2022

@otrho - regarding the IR tests that I had to update: how did you figure out/check the assembly for these tests? Did you do it by inspection?

Yeah, when you're adding them progressively it's fairly easy to eyeball them and confirm they're correct. But making a pervasive change to codegen like this means there's a fair bit of work confirming the new ASM is right.

At the same time you're trusting that the E2E regression tests are confirming that the changes haven't broken behaviour so I guess I'd be checking for stuff which looks obviously wrong in the IR tests, rather than making sure they're absolutely doing the right thing.

To run E2E with IR you can use the SWAY_TEST_USE_IR=1 environment variable. Note, at the moment there are 3 tests on master which are failing -- the recent generics and traits stuff you added, which I mentioned to you last week.

I'll inspect the changes then - it would also be a good exercise to help understand the IR better :)
That being said, the E2E tests with the IR pipeline enabled seem to be okay except for the three tests that you mentiond and the test that I added in this PR (due to #821), so I think we're okay!

otrho
otrho previously approved these changes Feb 21, 2022
Copy link
Contributor

@otrho otrho left a comment

Choose a reason for hiding this comment

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

Looks good. Not sure why the new test would fail for IR though?

@@ -223,21 +223,575 @@ impl VirtualOp {
.collect()
}

Copy link
Contributor

Choose a reason for hiding this comment

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

For all these giant functions I feel like there must be a more data driven compile time computational way to build these, with macros or something. But I don't know specifically the best way.

move $r1 $r2
ji i8
move $r0 $r1
ji i7
ret $r0
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

Copy link
Contributor

@emilyaherbert emilyaherbert left a comment

Choose a reason for hiding this comment

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

Ahh so cool, great job 😄

@emilyaherbert emilyaherbert self-requested a review February 22, 2022 17:15
Copy link
Contributor

@emilyaherbert emilyaherbert left a comment

Choose a reason for hiding this comment

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

Actually one nit, would you be able to include the name of the algorithm (and possibly a link to the wikipedia page) in the docstring associated with the function? That way we won't have to dig for this issue if/when we need to expand on this code in the future 😄

@mohammadfawaz
Copy link
Contributor Author

Actually one nit, would you be able to include the name of the algorithm (and possibly a link to the wikipedia page) in the docstring associated with the function? That way we won't have to dig for this issue if/when we need to expand on this code in the future 😄

Done!

@mohammadfawaz
Copy link
Contributor Author

Looks good. Not sure why the new test would fail for IR though?

It's due to #821 which is an issue on master. I see you're already looking at it though, thanks! :)

Copy link
Contributor

@emilyaherbert emilyaherbert left a comment

Choose a reason for hiding this comment

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

Great job!

@mohammadfawaz mohammadfawaz merged commit 24bb7aa into FuelLabs:master Feb 23, 2022
@mohammadfawaz mohammadfawaz deleted the new_register_allocator branch February 23, 2022 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged enhancement New feature or request
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Graph-coloring register allocator
4 participants