WebAssembly / WebAssembly/binaryen
`select` not optimized when condition is true and ifFalse arm isn't pure
Nobody has claimed this yet.
- Dominant language
- WebAssembly
- Stars
- 8.6k
- Forks
- 885
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 69
Description
Consider the following expression:
(select
(i32.div_u (i32.const 3) (i32.const 0))
(i32.const 2)
(i32.const 0)
)
Binaryen will try to eliminate the select statement when its condition is constant.
The div_u might cause a trap though, so under safe optimizations (without -tnh or -iit) it needs to be preserved.
Binaryen outputs both arms, and drops the value we don't need:
(drop (i32.div_u (i32.const 3) (i32.const 0)))
(i32.const 2)
Now let's look at the opposite case:
(select
(i32.const 2)
(i32.div_u (i32.const 3) (i32.const 0))
(i32.const 1)
)
Surely it will use the same trick, right? Output both arms, drop the one you don't need:
(i32.const 2)
(drop (i32.div_u (i32.const 3) (i32.const 0)) )
Except it doesn't! For some reason it completely gives up here, and keeps the select and its condition as-is.
The code mentions something about needing to "reverse the order using a temp local" but that makes no sense to me.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the select-optimization logic in src/passes/OptimizeInstructions.cpp at lines 2685-2704, especially the comment about reversing evaluation order with a temporary local. Compare the two constant-condition cases in the issue and verify that safe optimizations preserve a potentially trapping arm while still eliminating the unnecessary select when possible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, wasm
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100