WebAssembly / WebAssembly/binaryen

Missed optimization: failure to remove unnecessary locals usage with multi-value

Open
#8,009 0 comments 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

Test Case
;; test.wat
(module
  (import "" "get-a" (func $get-a (result i32)))
  (import "" "get-b" (func $get-b (result i64)))
  (import "" "get-both" (func $get-both (result i32 i64)))
  (import "" "take" (func $take (param i32 i64)))

  (func (export "f")
    (local i32 i64)
    call $get-both
    local.set 1
    local.tee 0
    local.get 1
    call $take
  )

  (func (export "g")
    (local i32 i64)
    call $get-a
    call $get-b
    local.set 1
    local.tee 0
    local.get 1
    call $take
  )
)
Steps to Reproduce
$ wasm-tools parse test.wat -o test.wasm # or wasm2wat or whatever...
$ wasm-opt -O4 --enable-multivalue test.wasm -o test.opt.wasm
$ wasm-tools print ~/scratch/missing-opt.opt.wasm 
Expected Results

Neither f nor g should have any locals or local.{get,tee} instructions after optimizations.

Actual Results

The locals are successfully removed from g, which does not use multi-value; however, they are not removed from f, which is equivalent to g except that it uses multi-value:

(module
  (type (;0;) (func))
  (type (;1;) (func (result i32)))
  (type (;2;) (func (result i64)))
  (type (;3;) (func (result i32 i64)))
  (type (;4;) (func (param i32 i64)))
  (import "" "get-a" (func (;0;) (type 1)))
  (import "" "get-b" (func (;1;) (type 2)))
  (import "" "get-both" (func (;2;) (type 3)))
  (import "" "take" (func (;3;) (type 4)))
  (export "f" (func 4))
  (export "g" (func 5))
  (func (;4;) (type 0)
    (local i32 i64)
    call 2
    local.set 1
    local.tee 0
    local.get 1
    call 3
  )
  (func (;5;) (type 0)
    call 0
    call 1
    call 3
  )
)
Additional Information

This wasm-opt was built from commit d0156b447.


cc @tlively

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

Reproduce the issue with test.wat using wasm-tools parse and wasm-opt -O4 --enable-multivalue, then inspect the optimized output with wasm-tools print. Compare the handling of functions f and g and trace the optimization responsible for removing unnecessary locals. Done means both functions retain neither locals nor local.get/local.tee instructions.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.