Skip to content

Commit f67f4fa

Browse files
committed
[wasm-split] Split globals' ref.func dependencies
When a global is exclusively used by a secondary module and thus moved to that module, and its initializer has a `(ref.func $func)`, we used to create a trampoline and export it from the primary module in all cases, even in the case that the function is in the same secondary module. This now moves those functions referred to by `ref.func`s to the secondary module, as long as they don't have uses anywhere else. To do this, we now skip scanning global initializers in `indirectReferencesToSecondaryFunctions`, and selectively create trampolines only when needed in `shareImportableItems`. The running time of `wasm-split` hasn't really changed with this PR, compared to the previous PR #8442 (~25s range in acx_gallery). #8441, #8442, and this PR combined reduce the size of the primary module by 46.6%. --- `wasm-objdump -h` result: - Before (#8442) ``` Type start=0x0000000c end=0x00035d44 (size=0x00035d38) count: 11185 Import start=0x00035d48 end=0x00132efc (size=0x000fd1b4) count: 32642 Function start=0x00132f00 end=0x00145dac (size=0x00012eac) count: 62890 Table start=0x00145daf end=0x001498ea (size=0x00003b3b) count: 2921 Tag start=0x001498ec end=0x001498f0 (size=0x00000004) count: 1 Global start=0x001498f4 end=0x00289e60 (size=0x0014056c) count: 47728 Export start=0x00289e65 end=0x004977fe (size=0x0020d999) count: 35861 Start start=0x00497800 end=0x00497802 (size=0x00000002) start: 828 Elem start=0x00497806 end=0x00501649 (size=0x00069e43) count: 12303 DataCount start=0x0050164b end=0x0050164c (size=0x00000001) count: 1 Code start=0x00501651 end=0x00730f22 (size=0x0022f8d1) count: 62890 Data start=0x00730f26 end=0x00750ab3 (size=0x0001fb8d) count: 1 ``` - After (This PR) ``` Type start=0x0000000c end=0x00035d38 (size=0x00035d2c) count: 11185 Import start=0x00035d3c end=0x00132ef0 (size=0x000fd1b4) count: 32642 Function start=0x00132ef4 end=0x001436cc (size=0x000107d8) count: 53001 Table start=0x001436cf end=0x0014720a (size=0x00003b3b) count: 2921 Tag start=0x0014720c end=0x00147210 (size=0x00000004) count: 1 Global start=0x00147214 end=0x00287b75 (size=0x00140961) count: 47728 Export start=0x00287b79 end=0x002e703f (size=0x0005f4c6) count: 25972 Start start=0x002e7041 end=0x002e7043 (size=0x00000002) start: 828 Elem start=0x002e7047 end=0x00349aa7 (size=0x00062a60) count: 12303 DataCount start=0x00349aa9 end=0x00349aaa (size=0x00000001) count: 1 Code start=0x00349aaf end=0x00550a4e (size=0x00206f9f) count: 53001 Data start=0x00550a52 end=0x005705df (size=0x0001fb8d) count: 1 ``` We can see while the size of the function and the code sections have decreased, the big gains come from the decrease of the export section, which can contain long function names.
1 parent 3d7d1f4 commit f67f4fa

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

src/ir/module-splitting.cpp

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
// instantiation.
4646
//
4747
// 8. Export globals, tags, tables, and memories from the primary module and
48-
// import them in the secondary modules.
48+
// import them in the secondary modules. If possible, move those module
49+
// items instead to the secondary modules.
4950
//
5051
// Functions can be used or referenced three ways in a WebAssembly module: they
5152
// can be exported, called, or referenced with ref.func. The above procedure
@@ -630,7 +631,25 @@ void ModuleSplitter::indirectReferencesToSecondaryFunctions() {
630631
}
631632
}
632633
} gatherer(*this);
633-
gatherer.walkModule(&primary);
634+
// We shouldn't use collector.walkModuleCode here, because we don't want to
635+
// walk on global initializers. At this point, all globals are still in the
636+
// primary module, so if we walk on global initializers here, it will create
637+
// unnecessary trampolines.
638+
//
639+
// For example, we have (global $a funcref (ref.func $foo)), and $foo was
640+
// split into a secondary module. Because $a is at this point still in the
641+
// primary module, $foo will be considered to exist in a different module, so
642+
// this will create a trampoline for $foo. But it is possible that later we
643+
// find out $a is exclusively used by that secondary module and move $a there.
644+
// In that case, $a can just reference $foo locally, but if we scan global
645+
// initializers here, we would have created an unnecessary trampoline for
646+
// $foo.
647+
walkSegments(gatherer, &primary);
648+
for (auto& curr : primary.functions) {
649+
if (!curr->imported()) {
650+
gatherer.walkFunction(curr.get());
651+
}
652+
}
634653
for (auto& secondaryPtr : secondaries) {
635654
gatherer.walkModule(secondaryPtr.get());
636655
}
@@ -1157,19 +1176,42 @@ void ModuleSplitter::shareImportableItems() {
11571176
bool inPrimary = primaryUsed.globals.count(global->name);
11581177
if (!inPrimary && usingSecondaries.size() == 1) {
11591178
auto* secondary = usingSecondaries[0];
1160-
ModuleUtils::copyGlobal(global.get(), *secondary);
1179+
auto* secondaryGlobal = ModuleUtils::copyGlobal(global.get(), *secondary);
11611180
globalsToRemove.push_back(global->name);
1162-
// Import global initializer's ref.func dependences
1181+
1182+
if (secondaryGlobal->init) {
1183+
// When a global's initializer contains ref.func
1184+
for (auto* ref : FindAll<RefFunc>(secondaryGlobal->init).list) {
1185+
// If we are moving this global and its dependent function is in a
1186+
// different secondary module, we create a trampoline here.
1187+
if (allSecondaryFuncs.count(ref->func)) {
1188+
Index targetIndex = funcToSecondaryIndex.at(ref->func);
1189+
if (secondaries[targetIndex].get() != secondary) {
1190+
ref->func = getTrampoline(ref->func);
1191+
}
1192+
}
1193+
// If we are moving this global and its dependent function is in the
1194+
// primary module, we export it from there.
1195+
if (primary.getFunctionOrNull(ref->func)) {
1196+
exportImportFunction(ref->func, {secondary});
1197+
}
1198+
// If we are moving this global and its dependent function is in the
1199+
// same secondary module, we don't need to do anything. The ref.func
1200+
// can directly reference the function.
1201+
}
1202+
}
1203+
} else { // We export / import the global
11631204
if (global->init) {
11641205
for (auto* ref : FindAll<RefFunc>(global->init).list) {
1165-
// Here, ref->func is either a function the primary module, or a
1166-
// trampoline created in indirectReferencesToSecondaryFunctions in
1167-
// case the original function is in one of the secondaries.
1168-
assert(primary.getFunctionOrNull(ref->func));
1169-
exportImportFunction(ref->func, {secondary});
1206+
// If we are exporting this global from the primary module, we should
1207+
// create a trampoline here, because we skipped doing it for global
1208+
// initializers in indirectReferencesToSecondaryFunctions.
1209+
if (allSecondaryFuncs.count(ref->func)) {
1210+
ref->func = getTrampoline(ref->func);
1211+
}
11701212
}
11711213
}
1172-
} else {
1214+
11731215
for (auto* secondary : usingSecondaries) {
11741216
auto* secondaryGlobal =
11751217
ModuleUtils::copyGlobal(global.get(), *secondary);

test/lit/wasm-split/global-funcref.wast

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@
88
;; TODO Use $split in the secondary module directly in the split global
99

1010
(module
11-
;; PRIMARY: (export "trampoline_split" (func $trampoline_split))
12-
1311
;; PRIMARY: (func $keep
1412
;; PRIMARY-NEXT: )
1513
(func $keep)
1614

17-
;; PRIMARY: (func $trampoline_split
18-
;; PRIMARY-NEXT: (call_indirect (type $0)
19-
;; PRIMARY-NEXT: (i32.const 0)
20-
;; PRIMARY-NEXT: )
21-
;; PRIMARY-NEXT: )
22-
23-
24-
;; SECONDARY: (import "primary" "trampoline_split" (func $trampoline_split (exact)))
25-
;; SECONDARY: (global $a funcref (ref.func $trampoline_split))
15+
;; SECONDARY: (global $a funcref (ref.func $split))
2616
(global $a funcref (ref.func $split))
2717

2818
;; SECONDARY: (func $split

test/lit/wasm-split/ref.func.wast

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
;; SECONDARY: (import "primary" "prime" (func $prime (exact (type $0))))
6363

64-
;; SECONDARY: (elem $0 (i32.const 0) $second $second-in-table)
64+
;; SECONDARY: (elem $0 (i32.const 0) $second-in-table $second)
6565

6666
;; SECONDARY: (elem declare func $prime)
6767

@@ -97,13 +97,13 @@
9797
;; (but we will get a placeholder, as all split-out functions do).
9898
)
9999
)
100-
;; PRIMARY: (func $trampoline_second (type $0)
100+
;; PRIMARY: (func $trampoline_second-in-table (type $0)
101101
;; PRIMARY-NEXT: (call_indirect $1 (type $0)
102102
;; PRIMARY-NEXT: (i32.const 0)
103103
;; PRIMARY-NEXT: )
104104
;; PRIMARY-NEXT: )
105105

106-
;; PRIMARY: (func $trampoline_second-in-table (type $0)
106+
;; PRIMARY: (func $trampoline_second (type $0)
107107
;; PRIMARY-NEXT: (call_indirect $1 (type $0)
108108
;; PRIMARY-NEXT: (i32.const 1)
109109
;; PRIMARY-NEXT: )

0 commit comments

Comments
 (0)