Skip to content
Prev Previous commit
Next Next commit
work
  • Loading branch information
kripken committed May 6, 2024
commit b2b93b43193563a8826ffb2132e42f459ce4d354
19 changes: 14 additions & 5 deletions src/ir/module-splitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,19 @@ void ModuleSplitter::indirectReferencesToSecondaryFunctions() {
Builder builder(primary);
// Generate the new trampoline function and add it to the module.
for (auto& [name, refFuncs] : gatherer.map) {
// Find the relevant (non-ignored) RefFuncs. If there are none, we can skip
// creating a thunk entirely.
std::vector<RefFunc*> relevantRefFuncs;
for (auto* refFunc : refFuncs) {
assert(refFunc->func == name);
if (!ignore.count(refFunc)) {
relevantRefFuncs.push_back(refFunc);
}
}
if (relevantRefFuncs.empty()) {
continue;
}

auto* oldFunc = secondary.getFunction(name);
auto newName = Names::getValidFunctionName(
primary, std::string("trampoline_") + name.toString());
Expand All @@ -585,11 +598,7 @@ void ModuleSplitter::indirectReferencesToSecondaryFunctions() {
builder.makeFunction(newName, oldFunc->type, {}, call));

// Update RefFuncs to refer to it.
for (auto* refFunc : refFuncs) {
assert(refFunc->func == name);
if (ignore.count(refFunc)) {
continue;
}
for (auto* refFunc : relevantRefFuncs) {
refFunc->func = newName;
}
}
Expand Down
24 changes: 17 additions & 7 deletions test/lit/wasm-split/ref.func.wast
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.

;; RUN: wasm-split %s --split-funcs=second -g -o1 %t.1.wasm -o2 %t.2.wasm -all | filecheck %s
;; RUN: wasm-split %s --split-funcs=second,second-in-table -g -o1 %t.1.wasm -o2 %t.2.wasm -all | filecheck %s
;; RUN: wasm-dis %t.1.wasm | filecheck %s --check-prefix PRIMARY
;; RUN: wasm-dis %t.2.wasm | filecheck %s --check-prefix SECONDARY

Expand All @@ -12,20 +12,22 @@

;; PRIMARY: (import "placeholder" "1" (func $placeholder_1))

;; PRIMARY: (import "placeholder" "2" (func $placeholder_2))

;; PRIMARY: (global $glob1 (ref func) (ref.func $prime))

;; PRIMARY: (global $glob2 (ref func) (ref.func $2))

;; PRIMARY: (table $table 2 2 funcref)
;; PRIMARY: (table $table 3 3 funcref)
(table $table 1 1 funcref)

(global $glob1 (ref func) (ref.func $prime))

(global $glob2 (ref func) (ref.func $second))

(elem (i32.const 0) $in-table)
(elem (i32.const 0) $in-table $second-in-table)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you give this a name, its output line will be attached above it.


;; PRIMARY: (elem $0 (i32.const 0) $in-table $placeholder_1)
;; PRIMARY: (elem $0 (i32.const 0) $in-table $placeholder_1 $placeholder_2)

;; PRIMARY: (export "prime" (func $prime))

Expand Down Expand Up @@ -54,15 +56,15 @@

;; SECONDARY: (type $0 (func))

;; SECONDARY: (import "primary" "table" (table $table 2 2 funcref))
;; SECONDARY: (import "primary" "table" (table $table 3 3 funcref))

;; SECONDARY: (import "primary" "global" (global $glob1 (ref func)))

;; SECONDARY: (import "primary" "global_3" (global $glob2 (ref func)))

;; SECONDARY: (import "primary" "prime" (func $prime))

;; SECONDARY: (elem $0 (i32.const 1) $second)
;; SECONDARY: (elem $0 (i32.const 1) $second-in-table $second)

;; SECONDARY: (func $second
;; SECONDARY-NEXT: (drop
Expand All @@ -89,9 +91,17 @@
;; enough of a reason for us to make a trampoline, even though in our IR the
;; table is a list of ref.funcs.
)

;; SECONDARY: (func $second-in-table
;; SECONDARY-NEXT: (nop)
;; SECONDARY-NEXT: )
(func $second-in-table
;; As above, but in the secondary module. We still don't need a trampoline
;; (but we will get a placeholder, as all split-out functions do).
)
)
;; PRIMARY: (func $2
;; PRIMARY-NEXT: (call_indirect (type $0)
;; PRIMARY-NEXT: (i32.const 1)
;; PRIMARY-NEXT: (i32.const 2)
;; PRIMARY-NEXT: )
;; PRIMARY-NEXT: )