WebAssembly / WebAssembly/binaryen

SIMD add not being optimized away

Open
#5,633 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

good first bug
Dominant language
WebAssembly
Stars
8.6k
Forks
885
Avg merge
1d 19h
Merged PRs (30d)
69

Description

The i64x2.add in the following WAT isn't being optimized at all (build/bin/wasm-opt --enable-simd -O3 -S -o - test.wat):

(module
    (export "v128-add" (func $v128-add))
    (export "i64-add" (func $i64-add))
    (func $v128-add (param v128) (result v128)
        (i64x2.add (local.get 0) (v128.const i64x2 0 0))
    )
    (func $i64-add (param i64) (result i64)
        (i64.add (local.get 0) (i64.const 0))
    )
)

It outputs:

(module
 (type $v128_=>_v128 (func (param v128) (result v128)))
 (type $i64_=>_i64 (func (param i64) (result i64)))
 (export "v128-add" (func $v128-add))
 (export "i64-add" (func $i64-add))
 (func $v128-add (; has Stack IR ;) (param $0 v128) (result v128)
  (i64x2.add
   (local.get $0)
   (v128.const i32x4 0x00000000 0x00000000 0x00000000 0x00000000)
  )
 )
 (func $i64-add (; has Stack IR ;) (param $0 i64) (result i64)
  (local.get $0)
 )
)

I'd have expected that add to be optimized away into (local.get $0). How do I get that to work?

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 by running the supplied WAT reproducer with build/bin/wasm-opt --enable-simd -O3 -S and compare the i64x2.add result with the scalar i64.add result. Trace the SIMD optimization path responsible for constant-zero additions; done means the v128-add function is reduced to local.get $0 while the existing scalar behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.