Skip to content

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

Merged
fitzgen merged 5 commits into
bytecodealliance:mainfrom
fitzgen:cranelift-simplify-block-terminators
May 12, 2026
Merged

Cranelift: Rewrite conditional branches with constant conditions into unconditional jumps#13267
fitzgen merged 5 commits into
bytecodealliance: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.

@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
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 fitzgen requested a review from cfallin May 6, 2026 23:55
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.

Looks great -- thanks for the very detailed proof!

Comment thread cranelift/codegen/src/egraph/mod.rs Outdated
@cfallin
Copy link
Copy Markdown
Member

cfallin commented May 7, 2026

(Merge conflict to resolve before this can merge)

fitzgen added 4 commits May 12, 2026 12:46
… 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.
Lowering expects them
And include a proof of why the new traversal is correct.
@fitzgen fitzgen force-pushed the cranelift-simplify-block-terminators branch from f9f5a48 to 79c2c61 Compare May 12, 2026 19:47
@fitzgen fitzgen enabled auto-merge May 12, 2026 19:51
We were re-processing an instruction we already processed, which led to a
failing assertion due to a missing entry in `value_to_opt_value` for certain
kinds of union nodes which can only be seen by our side-effectful instruction
re-processing. We could ensure that we have `value_to_opt_value` entries for
these values, but we don't have any `simplify_skeleton` rules that benefit from
re-processing, so what is the point of introducing a bunch of new stores during
compilation? Let's just stop re-processing side-effectful instructions instead.
@fitzgen fitzgen added this pull request to the merge queue May 12, 2026
Merged via the queue into bytecodealliance:main with commit ec33f93 May 12, 2026
48 checks passed
@fitzgen fitzgen deleted the cranelift-simplify-block-terminators branch May 12, 2026 22:12
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