Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ pub struct FuncEnvironment<'module_environment> {

translation: &'module_environment ModuleTranslation<'module_environment>,

/// In the module startup function, the defined functions whose `VMFuncRef`
/// has already been initialized by an earlier `ref.func`.
func_refs_initialized: std::collections::HashSet<DefinedFuncIndex>,

/// Heaps implementing WebAssembly linear memories.
heaps: PrimaryMap<Heap, HeapData>,

Expand Down Expand Up @@ -318,6 +322,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
offsets,
tunables,
fuel_var: Variable::reserved_value(),
func_refs_initialized: Default::default(),
epoch_deadline_var: Variable::reserved_value(),
epoch_ptr_var: Variable::reserved_value(),

Expand Down Expand Up @@ -3751,6 +3756,19 @@ impl FuncEnvironment<'_> {
let func = &self.module.functions[func_index];
assert!(func.is_escaping());
let func_ref_offset = self.offsets.func_refs().at(func.func_ref);

// In the module startup function, only the first `ref.func` of a
// function initializes its `VMFuncRef`; later ones only need its
// address. This relies on the startup function evaluating constant
// expressions in order, never in conditional code, so that the first
// `ref.func` of a function comes before all the others.
if matches!(self.key, FuncKey::ModuleStartup(..))
&& !self.func_refs_initialized.insert(def_index)
{
let vmctx = self.vmctx_val(pos);
return pos.ins().iadd_imm_s(vmctx, i64::from(func_ref_offset));
}

let sig = func.signature.unwrap_module_type_index();

let wasm_func = self.get_or_create_defined_func_ref(pos.func, def_index);
Expand Down
16 changes: 6 additions & 10 deletions tests/disas/elem-segment.wat
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,25 @@
;; sig0 = (i64 vmctx, i32) -> i64 tail
;; sig1 = (i64 vmctx, i64) tail
;; sig2 = (i64 vmctx, i64, i64, i64) -> i8 system_v
;; sig3 = (i64 vmctx, i64, i64, i64) -> i8 system_v
;; sig4 = (i64 vmctx, i64, i64, i64) -> i8 system_v
;; fn0 = colocated u805306368:4 sig0
;; fn1 = colocated u0:0 sig1
;; fn2 = colocated u268435456:0 sig2
;; fn3 = colocated u268435456:0 sig3
;; fn4 = colocated u268435456:0 sig4
;;
;; block0(v0: i64, v1: i64):
;; v2 = iconst.i32 0
;; v3 = call fn0(v0, v2) ; v2 = 0
;; v8 = iconst.i64 48
;; v9 = iadd v0, v8 ; v8 = 48
;; store notrap aligned little region6 v9, v3
;; store notrap aligned little region6 v9, v3+16
;; v17 = func_addr.i64 fn4
;; store notrap aligned region2 v17, v0+48
;; v5 = func_addr.i64 fn2
;; store notrap aligned region2 v5, v0+48
;; v4 = func_addr.i64 fn1
;; store notrap aligned region3 v4, v0+56
;; store notrap aligned region4 v0, v0+72
;; v6 = load.i64 notrap aligned readonly can_move region0 v0+40
;; v7 = load.i32 notrap aligned readonly can_move region1 v6
;; store notrap aligned region5 v7, v0+64
;; v8 = iconst.i64 48
;; v9 = iadd v0, v8 ; v8 = 48
;; store notrap aligned little region6 v9, v3
;; store notrap aligned little region6 v9, v3+16
;; store notrap aligned little region6 v9, v3+32
;; return
;; }
83 changes: 83 additions & 0 deletions tests/disas/startup-ref-func-once.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
;;! target = 'x86_64'
;;! test = 'optimize'
;;! filter = 'module_start'

;; `$f` is referenced by a global initializer and by a passive element
;; segment. Its `VMFuncRef` is written only once, by the first reference, even
;; though the call that sets up the passive segment separates the two
;; references.

(module
(func $f)
(global funcref (ref.func $f))
(elem func $f)
)
;; function u2415919104:1(i64 vmctx, i64, i64, i64) -> i8 system_v {
;; region0 = 123 ""
;; region1 = 243 ""
;; region2 = 72 ""
;; region3 = 231 ""
;; region4 = 112 ""
;; region5 = 34 ""
;; sig0 = (i64 vmctx, i64) tail
;; fn0 = colocated u2415919104:0 sig0
;;
;; block0(v0: i64, v1: i64, v2: i64, v3: i64):
;; jump block1
;;
;; block1:
;; v7 = get_frame_pointer.i64
;; v6 = load.i64 notrap aligned readonly can_move region0 v0+8
;; store notrap aligned region1 v7, v6+64
;; v8 = get_stack_pointer.i64
;; store notrap aligned region2 v8, v6+56
;; v9 = get_exception_handler_address.i64 block1, 0
;; store notrap aligned region3 v9, v6+72
;; try_call fn0(v0, v1), sig0, block2, [ default: block3 ]
;;
;; block2:
;; v14 = iconst.i8 1
;; return v14 ; v14 = 1
;;
;; block3:
;; v11 = iconst.i64 1
;; store notrap aligned region4 v11, v6+128 ; v11 = 1
;; v4 = iconst.i64 0
;; store notrap aligned region5 v4, v6+144 ; v4 = 0
;; v13 = iconst.i8 0
;; return v13 ; v13 = 0
;; }
;;
;; function u2415919104:0(i64 vmctx, i64) tail {
;; region0 = 130 ""
;; region1 = 134 ""
;; region2 = 44 ""
;; region3 = 120 ""
;; region4 = 100 ""
;; region5 = 160 ""
;; region6 = 250 ""
;; region7 = 116 ""
;; sig0 = (i64 vmctx, i64) tail
;; sig1 = (i64 vmctx, i64, i64, i64) -> i8 system_v
;; sig2 = (i64 vmctx, i32) -> i64 tail
;; fn0 = colocated u0:0 sig0
;; fn1 = colocated u268435456:0 sig1
;; fn2 = colocated u805306368:4 sig2
;;
;; block0(v0: i64, v1: i64):
;; v3 = func_addr.i64 fn1
;; store notrap aligned region2 v3, v0+64
;; v2 = func_addr.i64 fn0
;; store notrap aligned region3 v2, v0+72
;; store notrap aligned region4 v0, v0+88
;; v4 = load.i64 notrap aligned readonly can_move region0 v0+40
;; v5 = load.i32 notrap aligned readonly can_move region1 v4
;; store notrap aligned region5 v5, v0+80
;; v6 = iconst.i64 64
;; v7 = iadd v0, v6 ; v6 = 64
;; store notrap aligned region6 v7, v0+48
;; v8 = iconst.i32 0
;; v9 = call fn2(v0, v8) ; v8 = 0
;; store notrap aligned little region7 v7, v9
;; return
;; }
41 changes: 41 additions & 0 deletions tests/misc_testsuite/startup-ref-func-shared.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
;;! reference_types = true
;;! bulk_memory = true

;; Functions referenced several times from global initializers and element
;; segments can be called through each of these references.

(module
(type $t (func (result i32)))
(func $f (type $t) (i32.const 1))
(func $g (type $t) (i32.const 2))

(global $a funcref (ref.func $f))
(global $b funcref (ref.func $f))
(global $c funcref (ref.func $g))

(table $tab 6 funcref)
(elem (table $tab) (i32.const 0) func $f $g $f)
(elem $passive funcref (ref.func $g) (ref.func $f))

(func $call-at-5 (param funcref) (result i32)
(table.set $tab (i32.const 5) (local.get 0))
(call_indirect $tab (type $t) (i32.const 5)))
(func (export "call-a") (result i32) (call $call-at-5 (global.get $a)))
(func (export "call-b") (result i32) (call $call-at-5 (global.get $b)))
(func (export "call-c") (result i32) (call $call-at-5 (global.get $c)))

(func (export "call-table") (param i32) (result i32)
(call_indirect $tab (type $t) (local.get 0)))
(func (export "init-from-passive")
(table.init $tab $passive (i32.const 3) (i32.const 0) (i32.const 2)))
)

(assert_return (invoke "call-a") (i32.const 1))
(assert_return (invoke "call-b") (i32.const 1))
(assert_return (invoke "call-c") (i32.const 2))
(assert_return (invoke "call-table" (i32.const 0)) (i32.const 1))
(assert_return (invoke "call-table" (i32.const 1)) (i32.const 2))
(assert_return (invoke "call-table" (i32.const 2)) (i32.const 1))
(invoke "init-from-passive")
(assert_return (invoke "call-table" (i32.const 3)) (i32.const 2))
(assert_return (invoke "call-table" (i32.const 4)) (i32.const 1))
Loading