cranelift-frontend: Fix stack maps and liveness for loops#9071
Merged
fitzgen merged 2 commits intobytecodealliance:mainfrom Aug 2, 2024
Merged
cranelift-frontend: Fix stack maps and liveness for loops#9071fitzgen merged 2 commits intobytecodealliance:mainfrom
cranelift-frontend: Fix stack maps and liveness for loops#9071fitzgen merged 2 commits intobytecodealliance:mainfrom
Conversation
Previously, we were not properly handling back edges. This manifested in values
incorrectly being considered not-live inside loop bodies where they definitely
were live. Consider the following example:
block0:
v0 = needs stack map
block1:
call foo(v0)
call foo(v0)
jump block1
We were previously considering `v0` live only for the first `call foo(v0)` but
not the second, because we mistakenly concluded that `v0` would not be used
again after that second `call`. While it won't be used again in *this* iteration
of the loop, it will be used again in the *next* iteration of the loop.
Trevor and I initially tried implementing a clever trick suggested by Chris
where, if we know the minimum post-order index of all of a block's transitive
predecessors, we can continue to compute liveness in a single pass over the
IR. We believed we could compute the minimum predecessor post-order index via
dynamic programming. It turns out, however, that approach doesn't provide
correct answers out of the box for certain kinds of irreducible control flow,
only nearly correct answers, and would require an additional clever fix-up pass
afterwards. We deemed this cleverness on cleverness unacceptable.
Instead, Trevor and I opted to implement a worklist algorithm where we process
blocks to a fixed-point. This has the advantages of being obviously correct and
producing more-precise results. It has the disadvantage of requiring multiple
passes over the IR in the face of loops and back edges. Because this analysis is
only used when needs-stack-map values are present (i.e. when the function uses
GC values) and is otherwise skipped, this additional compile-time overhead is
tolerable.
Co-Authored-By: Trevor Elliott <telliott@fastly.com>
13e56db to
c145baf
Compare
cfallin
approved these changes
Aug 2, 2024
Member
cfallin
left a comment
There was a problem hiding this comment.
Looks great, thanks! Some minor requests for clarification in comments but otherwise good to go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously, we were not properly handling back edges. This manifested in values incorrectly being considered not-live inside loop bodies where they definitely were live. Consider the following example:
We were previously considering
v0live only for the firstcall foo(v0)but not the second, because we mistakenly concluded thatv0would not be used again after that secondcall. While it won't be used again in this iteration of the loop, it will be used again in the next iteration of the loop.Trevor and I initially tried implementing a clever trick suggested by Chris where, if we know the minimum post-order index of all of a block's transitive predecessors, we can continue to compute liveness in a single pass over the IR. We believed we could compute the minimum predecessor post-order index via dynamic programming. It turns out, however, that approach doesn't provide correct answers out of the box for certain kinds of irreducible control flow, only nearly correct answers, and would require an additional clever fix-up pass afterwards. We deemed this cleverness on cleverness unacceptable.
Instead, Trevor and I opted to implement a worklist algorithm where we process blocks to a fixed-point. This has the advantages of being obviously correct and producing more-precise results. It has the disadvantage of requiring multiple passes over the IR in the face of loops and back edges. Because this analysis is only used when needs-stack-map values are present (i.e. when the function uses GC values) and is otherwise skipped, this additional compile-time overhead is tolerable.