WebAssembly / WebAssembly/binaryen
SimplifyLocals causes ineffective code
Nobody has claimed this yet.
- Dominant language
- WebAssembly
- Stars
- 8.6k
- Forks
- 885
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 69
Description
Given the following code:
(module
(type (;0;) (func))
(import "External" "external_function" (func $external_function (type 0)))
(func $_start (type 0)
i32.const 9576
i32.load
i32.load
i32.load
drop
i32.const 9576
i32.load
i32.load
i32.load
i32.const 9576
i32.load
i32.load
i32.load
i32.gt_s
if (result i32) ;; label = @1
call $external_function
i32.const 1
else
i32.const 0
end
drop
unreachable)
(memory $0 258 258)
(export "_start" (func $_start)))
For wasm-opt (c91c0520a61), -O3 -sp=simplify-locals can eliminate the unreachable code, while -O3 cannot:
(func $_start
(local $0 i32)
(if
(i32.gt_s
(local.tee $0
(i32.load
(i32.load
(i32.load
(i32.const 9576)
)
)
)
)
(local.get $0)
)
(then
(call $external_function)
)
)
(unreachable)
)
After investigating, it is the Wasm-specific optimization simplify-locals causes the counter-intuitive code. Below is the change made by simplify-locals:
As you can see, the currently wasm-opt cannot optimize the if condition on the right---it can only do that on the left, which I think it is missed optimization (It is not the simplify-locals fault...).
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
Reproduce the example with wasm-opt using -O3 and -O3 -sp=simplify-locals, then inspect the simplify-locals entry point and the subsequent condition optimization. Compare the generated WebAssembly for the two runs. Done means the missed optimization is addressed without incorrectly removing reachable behavior, with a regression test for this input.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- wasm
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100