WebAssembly / WebAssembly/binaryen

Invalid optimization to select with --ignore-implicit-traps

Open
#3,934 9 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

Optimizing this program with -all -O1 --ignore-implicit-traps:

(module
 (type $box (struct (field (mut i32)) (field (mut i64))))
 (export "f" (func $f))
 (func $f (param $0 (ref null $box)) (result i64)
  (block $label$1
   (block $label$2
    (br_if $label$2
     (ref.is_null
      (local.get $0)
     )
    )
    (br_if $label$1
     (i64.gt_s
      (struct.get $box 1
       (local.get $0)
      )
      (i64.const 8)
     )
    )
   )
   (return
    (i64.const 8)
   )
  )
  (struct.get $box 1
   (local.get $0)
  )
 )
)

yields

(module
 (type $box (struct (field (mut i32)) (field (mut i64))))
 (type $ref?|$box|_=>_i64 (func (param (ref null $box)) (result i64)))
 (export "f" (func $f))
 (func $f (param $0 (ref null $box)) (result i64)
  (if
   (select
    (ref.is_null
     (local.get $0)
    )
    (i32.const 1)
    (i64.gt_s
     (struct.get $box 1
      (local.get $0)
     )
     (i64.const 8)
    )
   )
   (return
    (i64.const 8)
   )
  )
  (struct.get $box 1
   (local.get $0)
  )
 )
)

Even when assuming that no traps are executed, it is invalid in general to change potentially trapping code from being conditionally executed to being unconditionally executed. This happens with the first struct.get here, as it is placed as the second arm of the select.

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 the provided WAT module and -all -O1 --ignore-implicit-traps, then inspect the optimization that turns the conditional struct.get into a select. Done means the optimizer no longer makes potentially trapping code execute unconditionally, while preserving the valid optimized behavior.

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
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.