Skip to content

Cranelift: A few perf improvements for the dead-store elimination pass - #13947

Merged
fitzgen merged 4 commits into
bytecodealliance:mainfrom
fitzgen:dead-store-elimination-perf
Jul 23, 2026
Merged

fitzgen merged 4 commits into
bytecodealliance:mainfrom
fitzgen:dead-store-elimination-perf

Conversation

@fitzgen

@fitzgen fitzgen commented Jul 23, 2026

Copy link
Copy Markdown
Member

Follow up to #13806 with a few perf improvements; see each commit. This, combined with my other perf PRs that have landed recently, recovers the hit to compilation times from introducing a dead-store elimination pass.

fitzgen added 4 commits July 23, 2026 10:43
Never mixing information sourced from both the CFG and the func, which can get
out of sync if the func is mutated after the CFG's construction.
The post-dominator tree is only consulted by dead-store elimination, and only
for candidates whose two stores live in different blocks (because we can easily
test post-domination without building a post-dominator tree when the two
instructions are in the same block). The majority of functions have no such
dead-store candidates, so building it eagerly for every function is a waste.
This commit replaces the parallel `observed_regions: EntitySet<AliasRegion>`
with an `observed` boolean stored inline in each region's state. This removes a
second heap allocation from every non-empty `LastStores` clone, which happens
$O(blocks)$ times per function during the block-input fixpoint, and collapses
`meet_from`'s per-region work into one pass.
The DSE alias-analysis `update` already has the instruction's opcode; pass it
into `alias_regions_observed` instead of re-reading it from the DFG, since LLVM
doesn't seem to dedupe it, and this code runs for every non-memory instruction
on both the block-input fixpoint and the transform walk.
@fitzgen
fitzgen requested a review from a team as a code owner July 23, 2026 18:00
@fitzgen
fitzgen requested review from cfallin and removed request for a team July 23, 2026 18:00

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM -- thanks!

I was idly wondering whether we could be "even more lazy" wrt postdom: for example a backward walk from the maybe-overwriting store to see if every path meets the maybe-overwritten store before the entrypoint (avoiding revisits with a visit-set, etc). More or less like the Braun SSA construction algorithm, marching backward to find defs. That might be marginally faster when we just have a few queries (i.e. when we only really need a sparse subset of postdom info) but

  • that's a whole new algorithm, and
  • more importantly, I suspect your change here cuts out the majority of the cost anyway, since the computation is only at the end of a series of earlier gates

so no need to do that now. Just thought I'd mention in case it spawns any ideas later.

@fitzgen
fitzgen added this pull request to the merge queue Jul 23, 2026
Merged via the queue into bytecodealliance:main with commit d61adfe Jul 23, 2026
53 checks passed
@fitzgen
fitzgen deleted the dead-store-elimination-perf branch July 23, 2026 18:35
yagehu referenced this pull request in yagehu/wasmtime-x Jul 26, 2026
…s (#13947)

* Make the post-dominator tree based only on the CFG

Never mixing information sourced from both the CFG and the func, which can get
out of sync if the func is mutated after the CFG's construction.

* Cranelift: compute post-dominator tree lazily

The post-dominator tree is only consulted by dead-store elimination, and only
for candidates whose two stores live in different blocks (because we can easily
test post-domination without building a post-dominator tree when the two
instructions are in the same block). The majority of functions have no such
dead-store candidates, so building it eagerly for every function is a waste.

* Cranelift: fold DSE's observed-regions bitset into the regions map

This commit replaces the parallel `observed_regions: EntitySet<AliasRegion>`
with an `observed` boolean stored inline in each region's state. This removes a
second heap allocation from every non-empty `LastStores` clone, which happens
$O(blocks)$ times per function during the block-input fixpoint, and collapses
`meet_from`'s per-region work into one pass.

* Cranelift: avoid re-fetching opcode in alias_regions_observed

The DSE alias-analysis `update` already has the instruction's opcode; pass it
into `alias_regions_observed` instead of re-reading it from the DFG, since LLVM
doesn't seem to dedupe it, and this code runs for every non-memory instruction
on both the block-input fixpoint and the transform walk.
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