Skip to content

Improve optimization of select between same values#8806

Merged
tlively merged 1 commit into
mainfrom
optimize-instructions-order-3396
Jun 6, 2026
Merged

Improve optimization of select between same values#8806
tlively merged 1 commit into
mainfrom
optimize-instructions-order-3396

Conversation

@tlively

@tlively tlively commented Jun 5, 2026

Copy link
Copy Markdown
Member

We previously optimized selects between the same value only when the ifTrue and ifFalse values had identical structure and no side effects and the condition could be dropped or reordered with the value.

Make this more robust by using the areConsecutiveInputs... utilities to check whether the arms have equal values and whether they are foldable. The old code avoided introducing blocks to keep code size down, but the blocks it would introduce are not named and will be elided in the output, so introducing blocks is not a problem. The old code also avoided introducing a scratch local, but if a scratch local is necessary, it is because the arms have some side effects that would prevent reordering. In that case it is more likely to be beneficial to execute only one arm, so go ahead and use scratch locals in the case where the second arm can be folded away.

We previously optimized selects between the same value only when the ifTrue and ifFalse values had identical structure and no side effects and the condition could be dropped or reordered with the value.

Make this more robust by using the `areConsecutiveInputs...` utilities to check whether the arms have equal values and whether they are foldable. The old code avoided introducing blocks to keep code size down, but the blocks it would introduce are not named and will be elided in the output, so introducing blocks is not a problem. The old code also avoided introducing a scratch local, but if a scratch local is necessary, it is because the arms have some side effects that would prevent reordering. In that case it is more likely to be beneficial to execute only one arm, so go ahead and use scratch locals in the case where the second arm can be folded away.
@tlively tlively requested a review from a team as a code owner June 5, 2026 20:53
@tlively tlively requested review from stevenfontanella and removed request for a team June 5, 2026 20:53
@tlively tlively merged commit c800b9d into main Jun 6, 2026
16 checks passed
@tlively tlively deleted the optimize-instructions-order-3396 branch June 6, 2026 00:15
@kripken

kripken commented Jun 9, 2026

Copy link
Copy Markdown
Member

Fuzz bug:

$ bin/wasm-opt w.wasm -all -tnh  --optimize-instructions
wasm-opt: src/wasm-builder.h:1537: static wasm::Index wasm::Builder::addVar(wasm::Function*, wasm::Name, wasm::Type): Assertion `type.isConcrete()' failed.
(module
 (rec
  (type $9 (func))
  (type $14 (func (result f32)))
  (type $29 (func (param i64) (result v128)))
  (type $26 (func (param v128) (result v128)))
  (type $11 (func (result f64)))
 )
 (func $1 (type $11) (result f64)
  (unreachable)
 )
 (func $2 (type $26) (param $0 v128) (result v128)
  (unreachable)
 )
 (func $5 (type $29) (param $0 i64) (result v128)
  (unreachable)
 )
 (func $8 (type $14) (result f32)
  (unreachable)
 )
 (func $30 (type $9)
  (nop)
 )
 (func $80 (type $9)
  (unreachable)
  (unreachable)
  (block
   (drop
    (select (result (ref none))
     (ref.as_non_null
      (ref.null none)
     )
     (ref.as_non_null
      (ref.null none)
     )
     (i32.wrap_i64
      (i64.trunc_sat_f64_u
       (call $1)
      )
     )
    )
   )
   (unreachable)
   (unreachable)
  )
 )
)

@tlively

tlively commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

Oops, I fixed that this morning but apparently never uploaded a PR. Here it is now: #8817

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants