WebAssembly / WebAssembly/binaryen

Improve selectify

Open
#4,166 2 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

I think I already mentioned a similar problem, but it was a long time ago.

If we have an "if" branch without a false arm, you selectify stops working:

function selectify_early_return(x: i32): i32 {
  if (x) return x;
  return 1;
}

Just add this test into lit/remove-unused-brs.wast as:

  ;; CHECK:      (func $selectify-early-return (param $x i32) (result i32)
  ;; CHECK-NEXT:  (if
  ;; CHECK-NEXT:   (local.get $x)
  ;; CHECK-NEXT:   (return
  ;; CHECK-NEXT:    (local.get $x)
  ;; CHECK-NEXT:   )
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (i32.const 1)
  ;; CHECK-NEXT: )
  (func $selectify-early-return (param $x i32) (result i32)
    (if
      (local.get $x)
      (return (local.get $x))
    )
    (i32.const 1)
  )

But obversely we expect something like this insted:

(func $selectify-early-return (param $x i32) (result i32)
  (select
    (local.get $0)
    (i32.const 1)
    (local.get $0)
  )
)

The main problem here: https://github.com/WebAssembly/binaryen/blob/main/src/passes/RemoveUnusedBrs.cpp#L1077
selectify just skip optimisation if ifFalse is not exists. So it is necessary to canonicalze such cases before applying selectify. Any idea how best to do this?

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 selectify logic near src/passes/RemoveUnusedBrs.cpp#L1077 and the existing cases in lit/remove-unused-brs.wast. Add the supplied early-return regression case and verify that the test expects the if without a false arm to be canonicalized so selectify can produce the shown select form.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, wasm
Domain
compilers, testing-qa
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.