GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — OptimizeInstructions: Fold select with non-zero condition and impure ifFalse by ArkadySkv · Pull Request #9298 · WebAssembly/binaryen

Conversation

@ArkadySkv

@ArkadySkv ArkadySkv commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Problem: When a select's condition is a known non-zero constant, the result
is always ifTrue. OptimizeInstructions::optimizeSelect already handled
this when ifFalse is pure by returning ifTrue directly, but it gave up
when ifFalse has side effects, because the obvious fold
(block (drop ifFalse) ifTrue) reorders ifFalse before ifTrue. That
left select instructions in optimized output that could have been folded.

Fix: Extend OptimizeInstructions::optimizeSelect in
src/passes/OptimizeInstructions.cpp to also handle the impure-ifFalse
case when
EffectAnalyzer::canReorder(getPassOptions(), *getModule(), ifFalse, ifTrue)
holds. The fold emits (block (drop ifFalse) ifTrue), which runs
ifFalse's effects, discards its result, then returns ifTrue. Value
semantics match the original select when the condition is non-zero, and
effect semantics are preserved. When canReorder fails, the select is
left unchanged; folding that case would require a temporary local to hold
ifFalse's value across ifTrue.

Verification:

./check.py succeeds. 453 gtest cases pass; 1027 lit cases pass; the
2 unsupported lit cases are host-related. All tool-level suites pass.

timeout 300 ./scripts/fuzz_opt.py --binaryen-bin=./bin ran 963 iterations
in 5 minutes without finding a bug. --optimize-instructions was exercised
directly and transitively via -O1/-O3/-Oz on inputs including
select-containing modules; --fuzz-exec-before, --fuzz-exec-second,
and CompareVMs reported no divergences.

The two affected lit tests (optimize-instructions-gc-tnh.wast,
optimize-instructions-mvp.wast) were regenerated with
scripts/update_lit_checks.py. The new CHECK lines expect
(block (drop <ifFalse>) <ifTrue>) for the folded cases and leave the
select unchanged when canReorder is false.

Follow-up: A more general fold would handle the case where canReorder
fails, by introducing a temporary local to hold ifFalse's value across
ifTrue. That would remove every constant-condition select from optimized
output, at the cost of one local per such site. Left for a separate PR
since it changes local assignment.

Fixes #6930.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gemini-code-assist[bot] reacted with eyes emoji
All reactions
  • 👀 1 reaction
@ArkadySkv
OptimizeInstructions: Fold select with non-zero condition and impure …
fdb20ff
…ifFalse
@ArkadySkv
ArkadySkv requested a review from a team as a code owner October 23, 2026 06:21
@ArkadySkv
ArkadySkv requested review from tlively and removed request for a team October 23, 2026 06:21
gemini-code-assist[bot]
gemini-code-assist Bot reviewed Oct 23, 2026
View reviewed changes

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Code Review

This pull request enhances the OptimizeInstructions pass by generalizing the optimization of select instructions with constant conditions. It now handles cases where either the true or false arm has side effects, preserving those side effects and utilizing EffectAnalyzer::canReorder to ensure safe execution order when necessary. The test cases have been updated to reflect these optimizations. I have no feedback to provide as the changes are correct and well-tested.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tlively
tlively reviewed Oct 23, 2026
View reviewed changes
Comment thread src/passes/OptimizeInstructions.cpp
Comment on lines +3426 to +3427
if (EffectAnalyzer::canReorder(
getPassOptions(), *getModule(), ifFalse, ifTrue)) {

@tlively tlively Oct 23, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

  1. Please use asymmetrical orderedBefore rather than canReorder.
  2. This should honor neverReorder.
  3. It would be worth using ChildLocalizer to add scratch locals to reorder the values if necessary.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tlively tlively tlively left review comments
+1 more reviewer
@gemini-code-assist gemini-code-assist[bot] gemini-code-assist[bot] left review comments
Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

select not optimized when condition is true and ifFalse arm isn't pure

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@ArkadySkv @tlively
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.