WebAssembly / WebAssembly/binaryen

redundant second cast to integer after already casted int value in asm.js?

Open
#2,951 1 comment 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.