WebAssembly / WebAssembly/binaryen
redundant second cast to integer after already casted int value in asm.js?
Nobody has claimed this yet.
- Dominant language
- WebAssembly
- Stars
- 8.6k
- Forks
- 885
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 69
Description
https://webassembly.studio/?f=oeg02m22gmq
export function add_div(x: i32): i32 {
return u32(x) / 100 + x / -100;
}
output:
function main_add_div($0) {
$0 = $0 | 0;
return (($0 | 0) / (-100 | 0) | 0) + (($0 >>> 0) / (100 >>> 0) | 0) | 0 | 0;
}
but I'm expecting single | 0 at the end:
function main_add_div($0) {
$0 = $0 | 0;
return (($0 | 0) / (-100 | 0) | 0) + (($0 >>> 0) / (100 >>> 0) | 0) | 0;
}
similar here
but after extra optimizations it normalized to expected. But I guess it could be done earlier without extra opts?
also since asm2js is more like js code and less than asm.js could we simplify some conversion? Like this one:
function i32_trunc_u_f32($0) {
$0 = Math_fround($0);
return ~~$0 >>> 0 | 0;
}
could be just:
function i32_trunc_u_f32($0) {
$0 = Math_fround($0);
return $0 | 0;
}
And Closure Compiler can't do such optimizations btw
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 linked wasm2js example and compare test/wasm2js/add_div.2asm.js with its .opt counterpart, including the referenced lines. Reproduce the generated output from the WebAssembly Studio example, then trace the asm2js optimization path that emits the conversions. Done means the redundant cast is removed at the appropriate stage and the truncation example is handled consistently without regressing the existing test output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, wasm
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100