Skip to content

relooper: Avoid recomputing strict_reachable_from in simple case#1827

Open
randomPoison wants to merge 2 commits into
masterfrom
legare/relooper-fix-quadratic
Open

relooper: Avoid recomputing strict_reachable_from in simple case#1827
randomPoison wants to merge 2 commits into
masterfrom
legare/relooper-fix-quadratic

Conversation

@randomPoison

Copy link
Copy Markdown
Contributor

Fixes #1821 by avoiding computing strict_reachable_from in the case of linear control flow. The performance issue there was that we were recomputing strict_reachable_from in every call to relooper, which means we recompute it after processing each block in the CFG. For very long CFGs like the one in #1821 that results in a lot of redundant work, causing translation to take a very long time.

The fix for the specific case in #1821 is simple, but I suspect there are other edge cases that could cause similar quadratic behavior. I think we could be a bit smarter about when we recompute reachability info, since I think reachability only meaningfully changes when we strip back edges while processing a loop. I don't have time to experiment with that now, though, so I've added a TODO comment in there as a note to look into that in the future.

@fw-immunant fw-immunant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One question and one comment suggestion inline.

Also, is it possible/feasible to add a test case here?

Comment on lines +254 to +257
let has_back_edge = local_successors
.values()
.any(|successors| successors.contains(entry));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

has_back_edge here seems to match the original condition here--"if the entry is in the transitive closure of any of its successors, there is a back edge". But the new condition doesn't search transitively. What justifies only looking at immediate successors? Is this connected to the comment on strict_reachable_from that says we only consider the local set of blocks? How does this relate to the comment that says "we don't want to consider back edges"--are we possibly "inside a loop" here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't need the full transitive reachability here, we just need to know if there's a back edge to our entry in our current set of blocks since that determines if we can create a Simple or if we need to make a Loop. We can determine that from local successors alone, i.e. if one of our current blocks has the entry as its immediate successor. I used the transitive reachability info originally because it seemed convenient and we needed to compute transitive reachability anyway, but I was not taking performance into account >__>

Is this connected to the comment on strict_reachable_from that says we only consider the local set of blocks? How does this relate to the comment that says "we don't want to consider back edges"--are we possibly "inside a loop" here?

So that comment is calling out why we can't just compute reachability for the entire graph up front and then reuse that info for the entire relooping. The problem with that is that when we process a loop we strip back edges, which then changes reachability info (which is important for correctly processing the sequence of blocks within the loop body). The new comment is calling out that the reachability info only meaningfully changes when we strip back edges, so in theory we only need to recompute reachability after processing a loop body. Avoiding that recomputation might help performance when dealing with very large CFGs, but would also require some more careful thought about if stale reachability would cause problems later in relooper, so doesn't seem important to do right now.

Comment thread c2rust-transpile/src/cfg/relooper.rs Outdated
Co-authored-by: Frances Wingerter <91758128+fw-immunant@users.noreply.github.com>
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.

Reworked relooper regressed on some large CFGs

2 participants