Skip to content

Potential optimization opportunity: Tablify sequences of ifs #2724

Closed as not planned
@dcodeIO

Description

@dcodeIO

So far we have been using sequences of br_ifs to hint to Binaryen that these might be suitable candidates for a br_table if conditions are dense constants potentially. While this works well, there are occasions where it is easier / more idiomatic to emit sequences of normal ifs, either manually or using the Reloopers addBranch, but these don't benefit from RemoveUnusedBrs's tablify. Typical code is:

(local.set $0 (...))
(if
 (i32.or
  (i32.or
   (i32.eq
    (local.get $0)
    (i32.const 1)
   )
   (i32.eq
    (local.get $0)
    (i32.const 2)
   )
  )
  (i32.eq
   (local.get $0)
   (i32.const 3)
  )
 )
 (if
  ...
  (call ...)
 )
 (call ...) ;; or unreachable
)

or

(local.set $0 (...))
(if
 (i32.or
  (i32.or
   (i32.eq
    (local.get $0)
    (i32.const 1)
   )
   (i32.eq
    (local.get $0)
    (i32.const 2)
   )
  )
  (i32.eq
   (local.get $0)
   (i32.const 3)
  )
 )
 (return
  (call ...)
 )
)
(if
 ...
 (return
  (call ...)
 )
)
(call ...) ;; or unreachable

where at some point converting to a br_table seems to become more efficient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions