Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

6.3 KB
Raw Blame
;;! target = "x86_64"
;;! test = "optimize"
;;! flags = "-Wbranch-hinting"

;; Branch hints from the `metadata.code.branch_hint` custom section mark blocks
;; cold during compilation: `\00` = condition likely false, `\01` = likely true.
;;
;; The annotation must immediately precede the `br_if`/`if` keyword: before a
;; folded instruction the encoder attaches the hint to the first nested
;; instruction (the condition) rather than the branch.

(module
  (global $g (mut i32) (i32.const 0))

  ;; condition likely false: branch target is cold.
  (func $br_if_unlikely (param i32) (result i32)
    (block $target
      local.get 0
      (@metadata.code.branch_hint "\00")
      br_if $target
      i32.const 1
      return
    )
    i32.const 2
  )

  ;; condition likely true: fallthrough is cold.
  (func $br_if_likely (param i32) (result i32)
    (block $target
      local.get 0
      (@metadata.code.branch_hint "\01")
      br_if $target
      i32.const 1
      return
    )
    i32.const 2
  )

  ;; condition likely false: then-block is cold.
  (func $if_unlikely (param i32) (result i32)
    local.get 0
    (@metadata.code.branch_hint "\00")
    if (result i32)
      i32.const 1
    else
      i32.const 2
    end
  )

  ;; condition likely true: else-block is cold.
  (func $if_likely (param i32) (result i32)
    local.get 0
    (@metadata.code.branch_hint "\01")
    if (result i32)
      i32.const 1
    else
      i32.const 2
    end
  )

  ;; condition likely true with a lazily allocated else (void if/else): the
  ;; else-block is still cold (the hint is deferred to the `else`).
  (func $if_void_likely (param i32)
    local.get 0
    (@metadata.code.branch_hint "\01")
    if
      i32.const 1
      global.set $g
    else
      i32.const 2
      global.set $g
    end
  )
)
;; function u0:0(i64 vmctx, i64, i32) -> i32 tail {
;;     region0 = 123 ""
;;     region1 = 160 ""
;;     gv0 = vmctx
;;     gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8
;;     gv2 = load.i64 notrap aligned region1 gv1+24
;;     stack_limit = gv2
;;
;;                                 block0(v0: i64, v1: i64, v2: i32):
;; @0063                               brif v2, block2, block3
;;
;;                                 block3:
;; @0065                               v3 = iconst.i32 1
;; @0067                               return v3  ; v3 = 1
;;
;;                                 block2 cold:
;; @006b                               jump block1
;;
;;                                 block1:
;; @0069                               v4 = iconst.i32 2
;; @006b                               return v4  ; v4 = 2
;; }
;;
;; function u0:1(i64 vmctx, i64, i32) -> i32 tail {
;;     region0 = 123 ""
;;     region1 = 160 ""
;;     gv0 = vmctx
;;     gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8
;;     gv2 = load.i64 notrap aligned region1 gv1+24
;;     stack_limit = gv2
;;
;;                                 block0(v0: i64, v1: i64, v2: i32):
;; @0072                               brif v2, block2, block3
;;
;;                                 block3 cold:
;; @0074                               v3 = iconst.i32 1
;; @0076                               return v3  ; v3 = 1
;;
;;                                 block2:
;; @007a                               jump block1
;;
;;                                 block1:
;; @0078                               v4 = iconst.i32 2
;; @007a                               return v4  ; v4 = 2
;; }
;;
;; function u0:2(i64 vmctx, i64, i32) -> i32 tail {
;;     region0 = 123 ""
;;     region1 = 160 ""
;;     gv0 = vmctx
;;     gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8
;;     gv2 = load.i64 notrap aligned region1 gv1+24
;;     stack_limit = gv2
;;
;;                                 block0(v0: i64, v1: i64, v2: i32):
;; @007f                               brif v2, block2, block4
;;
;;                                 block2 cold:
;; @0081                               v3 = iconst.i32 1
;; @0083                               jump block3(v3)  ; v3 = 1
;;
;;                                 block4:
;; @0084                               v4 = iconst.i32 2
;; @0086                               jump block3(v4)  ; v4 = 2
;;
;;                                 block3(v5: i32):
;; @0087                               jump block1
;;
;;                                 block1:
;; @0087                               return v5
;; }
;;
;; function u0:3(i64 vmctx, i64, i32) -> i32 tail {
;;     region0 = 123 ""
;;     region1 = 160 ""
;;     gv0 = vmctx
;;     gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8
;;     gv2 = load.i64 notrap aligned region1 gv1+24
;;     stack_limit = gv2
;;
;;                                 block0(v0: i64, v1: i64, v2: i32):
;; @008c                               brif v2, block2, block4
;;
;;                                 block2:
;; @008e                               v3 = iconst.i32 1
;; @0090                               jump block3(v3)  ; v3 = 1
;;
;;                                 block4 cold:
;; @0091                               v4 = iconst.i32 2
;; @0093                               jump block3(v4)  ; v4 = 2
;;
;;                                 block3(v5: i32):
;; @0094                               jump block1
;;
;;                                 block1:
;; @0094                               return v5
;; }
;;
;; function u0:4(i64 vmctx, i64, i32) tail {
;;     region0 = 123 ""
;;     region1 = 160 ""
;;     region2 = 250 ""
;;     gv0 = vmctx
;;     gv1 = load.i64 notrap aligned readonly can_move region0 gv0+8
;;     gv2 = load.i64 notrap aligned region1 gv1+24
;;     stack_limit = gv2
;;
;;                                 block0(v0: i64, v1: i64, v2: i32):
;; @0099                               brif v2, block2, block4
;;
;;                                 block2:
;; @009b                               v3 = iconst.i32 1
;; @009d                               store notrap aligned region2 v3, v0+48  ; v3 = 1
;; @009f                               jump block3
;;
;;                                 block4 cold:
;; @00a0                               v4 = iconst.i32 2
;; @00a2                               store notrap aligned region2 v4, v0+48  ; v4 = 2
;; @00a4                               jump block3
;;
;;                                 block3:
;; @00a5                               jump block1
;;
;;                                 block1:
;; @00a5                               return
;; }