Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm-split] Run RemoveUnusedElements on secondary modules #6945

Merged
merged 10 commits into from
Sep 17, 2024
11 changes: 11 additions & 0 deletions src/ir/module-splitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ struct ModuleSplitter {
void exportImportCalledPrimaryFunctions();
void setupTablePatching();
void shareImportableItems();
void removeUnusedSecondaryElements();

ModuleSplitter(Module& primary, const Config& config)
: config(config), secondaryPtr(initSecondary(primary)), primary(primary),
Expand All @@ -334,6 +335,7 @@ struct ModuleSplitter {
exportImportCalledPrimaryFunctions();
setupTablePatching();
shareImportableItems();
removeUnusedSecondaryElements();
}
};

Expand Down Expand Up @@ -872,6 +874,15 @@ void ModuleSplitter::shareImportableItems() {
}
}

void ModuleSplitter::removeUnusedSecondaryElements() {
// TODO: It would be better to be more selective about only exporting and
// importing those items that the secondary module needs. This would reduce
// code size in the primary module as well.
PassRunner runner(&secondary);
runner.add("remove-unused-module-elements");
runner.run();
}

} // anonymous namespace

Results splitFunctions(Module& primary, const Config& config) {
Expand Down
11 changes: 6 additions & 5 deletions src/tools/wasm-split/wasm-split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ uint64_t hashFile(const std::string& filename) {
return uint64_t(digest);
}

void adjustTableSize(Module& wasm, int initialSize) {
void adjustTableSize(Module& wasm, int initialSize, bool secondary = false) {
if (initialSize < 0) {
return;
}
if (wasm.tables.empty()) {
if (secondary) {
// It's not a problem if the table is not used in the secondary module.
return;
}
Fatal() << "--initial-table used but there is no table";
}

Expand Down Expand Up @@ -336,7 +340,7 @@ void splitModule(const WasmSplitOptions& options) {
auto& secondary = splitResults.secondary;

adjustTableSize(wasm, options.initialTableSize);
adjustTableSize(*secondary, options.initialTableSize);
adjustTableSize(*secondary, options.initialTableSize, /*secondary=*/true);

if (options.symbolMap) {
writeSymbolMap(wasm, options.primaryOutput + ".symbols");
Expand Down Expand Up @@ -435,9 +439,6 @@ void multiSplitModule(const WasmSplitOptions& options) {
// TODO: symbolMap, placeholderMap, emitModuleNames
// TODO: Support --emit-text and use .wast in that case.
auto moduleName = options.outPrefix + mod + ".wasm";
PassRunner runner(&*splitResults.secondary);
runner.add("remove-unused-module-elements");
runner.run();
writeModule(*splitResults.secondary, moduleName, options);
}
writeModule(wasm, options.output, options);
Expand Down
105 changes: 12 additions & 93 deletions test/example/module-splitting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ After:
)
Secondary:
(module
(type $0 (func (param i32)))
(import "primary" "%memory" (memory $mem 3 42 shared))
(import "primary" "%table" (table $tab 3 42 funcref))
(import "primary" "%global" (global $glob (mut i32)))
(import "primary" "%tag" (tag $e (param i32)))
)


Expand All @@ -64,11 +59,6 @@ After:
)
Secondary:
(module
(type $0 (func (param i32)))
(import "primary" "%memory" (memory $mem 3 42 shared))
(import "primary" "%table" (table $tab 3 42 funcref))
(import "primary" "%global" (global $glob (mut i32)))
(import "primary" "%tag" (tag $e (param i32)))
)


Expand Down Expand Up @@ -99,11 +89,6 @@ After:
)
Secondary:
(module
(type $0 (func (param i32)))
(import "primary" "mem" (memory $mem 3 42 shared))
(import "primary" "tab" (table $tab 3 42 funcref))
(import "primary" "glob" (global $glob (mut i32)))
(import "primary" "e" (tag $e (param i32)))
)


Expand Down Expand Up @@ -171,7 +156,6 @@ After:
)
Secondary:
(module
(import "primary" "%table" (table $table 1 funcref))
)


Expand All @@ -197,7 +181,6 @@ After:
)
Secondary:
(module
(import "primary" "%table" (table $table 2 funcref))
)


Expand Down Expand Up @@ -226,8 +209,6 @@ After:
)
Secondary:
(module
(import "primary" "%table" (table $table 1 funcref))
(import "primary" "%global" (global $base i32))
)


Expand Down Expand Up @@ -256,8 +237,6 @@ After:
)
Secondary:
(module
(import "primary" "%table" (table $table 2 funcref))
(import "primary" "%global" (global $base i32))
)


Expand Down Expand Up @@ -297,7 +276,6 @@ After:
)
Secondary:
(module
(import "primary" "%table" (table $table 1000 funcref))
)


Expand All @@ -324,8 +302,6 @@ After:
)
Secondary:
(module
(import "primary" "%table" (table $table 1000 funcref))
(import "primary" "%global" (global $base i32))
)


Expand All @@ -342,10 +318,6 @@ After:
)
Secondary:
(module
(type $0 (func (param i32) (result i32)))
(func $foo (type $0) (param $0 i32) (result i32)
(local.get $0)
)
)


Expand Down Expand Up @@ -415,8 +387,7 @@ Secondary:
(module
(type $0 (func (param i32) (result i32)))
(import "primary" "%table_1" (table $0 1 funcref))
(import "primary" "%table" (table $table 1 funcref))
(elem $0 (table $0) (i32.const 0) func $foo)
(elem $0 (i32.const 0) $foo)
(func $foo (type $0) (param $0 i32) (result i32)
(local.get $0)
)
Expand Down Expand Up @@ -454,8 +425,7 @@ Secondary:
(module
(type $0 (func (param i32) (result i32)))
(import "primary" "%table_1" (table $0 1 funcref))
(import "primary" "%table" (table $table 2 funcref))
(elem $0 (table $0) (i32.const 0) func $foo)
(elem $0 (i32.const 0) $foo)
(func $foo (type $0) (param $0 i32) (result i32)
(local.get $0)
)
Expand Down Expand Up @@ -501,8 +471,7 @@ Secondary:
(module
(type $0 (func (param i32) (result i32)))
(import "primary" "%table_2" (table $0 1 funcref))
(import "primary" "%table" (table $table 1000 funcref))
(elem $0 (table $0) (i32.const 0) func $foo)
(elem $0 (i32.const 0) $foo)
(func $foo (type $0) (param $0 i32) (result i32)
(local.get $0)
)
Expand Down Expand Up @@ -551,9 +520,7 @@ Secondary:
(module
(type $0 (func (param i32) (result i32)))
(import "primary" "%table_2" (table $0 1 funcref))
(import "primary" "%table" (table $table 1000 funcref))
(import "primary" "%global" (global $base i32))
(elem $0 (table $0) (i32.const 0) func $foo)
(elem $0 (i32.const 0) $foo)
(func $foo (type $0) (param $0 i32) (result i32)
(local.get $0)
)
Expand Down Expand Up @@ -602,9 +569,7 @@ Secondary:
(module
(type $0 (func (param i32) (result i32)))
(import "primary" "%table_2" (table $0 1 funcref))
(import "primary" "%table" (table $table 1000 funcref))
(import "primary" "%global" (global $base i32))
(elem $0 (table $0) (i32.const 0) func $foo)
(elem $0 (i32.const 0) $foo)
(func $foo (type $0) (param $0 i32) (result i32)
(local.get $0)
)
Expand Down Expand Up @@ -661,9 +626,7 @@ Secondary:
(module
(type $0 (func (param i32) (result i32)))
(import "primary" "%table_2" (table $0 1 funcref))
(import "primary" "%table" (table $table 1000 funcref))
(import "primary" "%global" (global $base i32))
(elem $0 (table $0) (i32.const 0) func $foo)
(elem $0 (i32.const 0) $foo)
(func $foo (type $0) (param $0 i32) (result i32)
(local.get $0)
)
Expand Down Expand Up @@ -717,11 +680,6 @@ After:
)
Secondary:
(module
(type $0 (func))
(import "primary" "%bar" (func $bar (type $0)))
(func $foo (type $0)
(call $bar)
)
)


Expand Down Expand Up @@ -776,13 +734,6 @@ After:
)
Secondary:
(module
(type $0 (func))
(func $bar (type $0)
(nop)
)
(func $foo (type $0)
(call $bar)
)
)


Expand Down Expand Up @@ -879,8 +830,7 @@ Secondary:
(module
(type $0 (func))
(import "primary" "%table_1" (table $0 2 funcref))
(import "primary" "%table" (table $table 4 funcref))
(elem $0 (table $0) (i32.const 0) func $foo $baz)
(elem $0 (i32.const 0) $foo $baz)
(func $baz (type $0)
(nop)
)
Expand Down Expand Up @@ -944,9 +894,7 @@ Secondary:
(module
(type $0 (func))
(import "primary" "%table_1" (table $0 2 funcref))
(import "primary" "%table" (table $table 4 funcref))
(import "primary" "%global" (global $base i32))
(elem $0 (table $0) (i32.const 0) func $foo $baz)
(elem $0 (i32.const 0) $foo $baz)
(func $baz (type $0)
(nop)
)
Expand Down Expand Up @@ -1010,8 +958,7 @@ Secondary:
(module
(type $0 (func))
(import "primary" "%table_1" (table $0 3 funcref))
(import "primary" "%table" (table $table 4 funcref))
(elem $0 (table $0) (i32.const 0) func $foo $bar $quux)
(elem $0 (i32.const 0) $foo $bar $quux)
(func $bar (type $0)
(nop)
)
Expand Down Expand Up @@ -1081,9 +1028,7 @@ Secondary:
(module
(type $0 (func))
(import "primary" "%table_1" (table $0 3 funcref))
(import "primary" "%table" (table $table 4 funcref))
(import "primary" "%global" (global $base i32))
(elem $0 (table $0) (i32.const 0) func $foo $bar $quux)
(elem $0 (i32.const 0) $foo $bar $quux)
(func $bar (type $0)
(nop)
)
Expand Down Expand Up @@ -1136,10 +1081,8 @@ Secondary:
(module
(type $0 (func))
(import "primary" "%table_2" (table $0 1 funcref))
(import "primary" "%table" (table $table 2 funcref))
(import "primary" "%global" (global $base i32))
(import "primary" "%foo" (func $foo (type $0)))
(elem $0 (table $0) (i32.const 0) func $bar)
(elem $0 (i32.const 0) $bar)
(func $bar (type $0)
(call $foo)
)
Expand Down Expand Up @@ -1185,9 +1128,8 @@ Secondary:
(module
(type $0 (func (param i32) (result i32)))
(import "primary" "%table_2" (table $0 1 funcref))
(import "primary" "%table" (table $table 1 1 funcref))
(import "primary" "%foo" (func $foo (type $0) (param i32) (result i32)))
(elem $0 (table $0) (i32.const 0) func $bar)
(elem $0 (i32.const 0) $bar)
(func $bar (type $0) (param $0 i32) (result i32)
(call $foo
(i32.const 1)
Expand Down Expand Up @@ -1279,28 +1221,5 @@ Minimized names primary:

Minimized names secondary:
(module
(type $0 (func))
(import "primary" "%a" (func $0 (type $0)))
(import "primary" "%c" (func $1 (type $0)))
(import "primary" "%d" (func $2 (type $0)))
(import "primary" "already_exported" (func $3 (type $0)))
(import "primary" "%e" (func $4 (type $0)))
(import "primary" "%f" (func $5 (type $0)))
(import "primary" "%g" (func $6 (type $0)))
(import "primary" "%b" (func $7 (type $0)))
(import "primary" "%h" (func $8 (type $0)))
(import "primary" "%i" (func $9 (type $0)))
(func $call (type $0)
(call $0)
(call $1)
(call $2)
(call $3)
(call $4)
(call $5)
(call $6)
(call $7)
(call $8)
(call $9)
)
)

1 change: 0 additions & 1 deletion test/lit/wasm-split/basic.wast
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@
;; KEEP-BOTH-PRIMARY-NEXT: )

;; KEEP-BOTH-SECONDARY: (module
;; KEEP-BOTH-SECONDARY-NEXT: (import "primary" "%table" (table $table 1 1 funcref))
;; KEEP-BOTH-SECONDARY-NEXT: )

;; SPLIT-BAR-SUPERSEDE: warning: function bar was to be kept in primary module. However it will now be split out into secondary module.
Expand Down
22 changes: 22 additions & 0 deletions test/lit/wasm-split/initial-table-used.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;; Test that the --initial-table flag works as expected when the secondary
;; module uses the table.

;; RUN: wasm-split %s --instrument --initial-table=1234 -S | filecheck %s

;; RUN: wasm-split %s -g -o1 %t.1.wasm -o2 %t.2.wasm --split-funcs=use-table --initial-table=1234
;; RUN: wasm-dis %t.1.wasm | filecheck %s
;; RUN: wasm-dis %t.2.wasm | filecheck %s

;; CHECK: (table $table 1234 funcref)

(module
(table $table 3 funcref)
(func $use-table
(call_indirect
(i32.const 0)
)
)
(func $use-use-table
(call $use-table)
)
)
3 changes: 1 addition & 2 deletions test/lit/wasm-split/initial-table.wast
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
;; Test that the --initial-table flag works as expected
;; Test that the --initial-table flag works as expected.

;; RUN: wasm-split %s --instrument --initial-table=1234 -S | filecheck %s

;; RUN: wasm-split %s -g -o1 %t.1.wasm -o2 %t.2.wasm --initial-table=1234
;; RUN: wasm-dis %t.1.wasm | filecheck %s
;; RUN: wasm-dis %t.2.wasm | filecheck %s

;; CHECK: (table $table 1234 funcref)

Expand Down
Loading
Loading