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

[lsra]: Fix nested live ranges due to finishLoop #313

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

k-sareen
Copy link
Contributor

In certain edge cases, finishLoop would not insert LsraEnds for active variables resulting in nested live ranges which manifested as an infinite loop in the register allocator (see discussion here 1).

This commit fixes this bug by keeping track of whether a variable will be made active in the future or not (i.e. if it will have a live range). If a variable will have a live range in the future, then finishLoop will make sure to insert LsraEnds for them to avoid the above bug.

In certain edge cases, `finishLoop` would not insert `LsraEnd`s for
active variables resulting in nested live ranges which manifested as an
infinite loop in the register allocator (see discussion here [1]).

This commit fixes this bug by keeping track of whether a variable will
be made active in the future or not (i.e. if it will have a live range).
If a variable will have a live range in the future, then `finishLoop`
will make sure to insert `LsraEnd`s for them to avoid the above bug.

[1]: titzer#308
@k-sareen
Copy link
Contributor Author

k-sareen commented Dec 13, 2024

@titzer Feel free to edit this PR directly if you think it's a good baseline. The hardest part for me was to reason about activeInFuture when removing the interior Lsra{Start,End}s in finishLoop: https://github.com/titzer/virgil/pull/313/files#diff-733fa4b1a9c66210d8c6e3b0b156b605203d2a9b0feb6aa1fec7a96141274fbcR555-R566

I'm not entirely sure what I have is correct because surely this is not the only test/program which has variables whose live range ends inside a loop and then are used in the future as well. I worry this change has artificially increased the live range for certain cases, but I don't know how I would have checked that. What I mean is when we remove an LsraEnd, we set activeInFuture = true, but this variable may never actually be used in the future. So setting activeInFuture = true is a lie but it seems to work for all test cases, not just the broken one (the broken one definitely needs this though). But my other assertions in #312 (which are not in this PR) don't seem to trigger for any test case, so I'm inclined to believe what we have here is at least more correct than before.

@titzer
Copy link
Owner

titzer commented Dec 13, 2024

I noticed that splitting critical edges before codegen fixes all of these issues. I am planning on doing a stable rev of the compiler and have basically been whacking bugs and making things more stable. Thus a simpler and less invasive change is attractive here.

@k-sareen
Copy link
Contributor Author

k-sareen commented Dec 13, 2024

Okay sure. I think LSRA still needs debug assertions to prevent this from slipping through. Feel free to close this PR since you've merged your fix.

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

Successfully merging this pull request may close these issues.

2 participants