Skip to content

Cranelift: Rewrite conditional branches with constant conditions into unconditional jumps#13267

Open
fitzgen wants to merge 4 commits intobytecodealliance:mainfrom
fitzgen:cranelift-simplify-block-terminators
Open

Cranelift: Rewrite conditional branches with constant conditions into unconditional jumps#13267
fitzgen wants to merge 4 commits intobytecodealliance:mainfrom
fitzgen:cranelift-simplify-block-terminators

Conversation

@fitzgen
Copy link
Copy Markdown
Member

@fitzgen fitzgen commented May 4, 2026

This requires two tweaks to the egraph pass's machinery:

  1. We maintain a set of reachable blocks. This is initialized to the entry block. After optimizing a block's intructions, we look at the terminator and mark any other block it branches to as reachable. When visiting blocks to optimize their instructions, we skip unreachable blocks and remove them and their instructions from the function's layout.

  2. We always visit blocks in reverse-post order, rather than dominator-tree order (which is related but slightly weaker). This ensures that we always visit predecessors before successors, and therefore that if a block is not marked reachable, then it really is unreachable and is safe to remove. See code comments for the details of when dominator-tree order breaks down.

… unconditional jumps

This requires two tweaks to the egraph pass's machinery:

1. We maintain a set of reachable blocks. This is initialized to the entry
block. After optimizing a block's intructions, we look at the terminator and
mark any other block it branches to as reachable. When visiting blocks to
optimize their instructions, we skip unreachable blocks and remove them and
their instructions from the function's layout.

2. We always visit blocks in reverse-post order, rather than dominator-tree
order (which is related but slightly weaker). This ensures that we always visit
predecessors before successors, and therefore that if a block is not marked
reachable, then it really is unreachable and is safe to remove. See code
comments for the details of when dominator-tree order breaks down.
@fitzgen fitzgen requested review from a team as code owners May 4, 2026 20:28
@fitzgen fitzgen requested review from cfallin and removed request for a team May 4, 2026 20:28
Lowering expects them
Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

Nice -- I'm happy to see how this is actually pretty straightforward given all of the primitives that we have! The RPO traversal change makes sense; just a comment below on how we describe/justify it. And a small request for testing. Otherwise LGTM!

Comment thread cranelift/codegen/src/egraph/mod.rs Outdated
Comment thread cranelift/filetests/filetests/egraph/skeleton.clif
Comment thread cranelift/codegen/src/egraph/mod.rs Outdated
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language labels May 5, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

fitzgen added 2 commits May 6, 2026 16:52
And include a proof of why the new traversal is correct.
@fitzgen fitzgen requested a review from cfallin May 6, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants