Skip to content

[wasm-split] Remove dead globals#8505

Merged
aheejin merged 3 commits into
WebAssembly:mainfrom
aheejin:wasm_split_dead_global_fix
Mar 21, 2026
Merged

[wasm-split] Remove dead globals#8505
aheejin merged 3 commits into
WebAssembly:mainfrom
aheejin:wasm_split_dead_global_fix

Conversation

@aheejin

@aheejin aheejin commented Mar 21, 2026

Copy link
Copy Markdown
Member

Currently dead module items are not split and just end up remaining in the primary module. Usually a user runs DCE before or after the splitting, and the goal of wasm-split is not DCE, so from the optimization perspective it shouldn't be a problem.

But after #8441, this can be a problem because a dead global's initializer can refer to another global that is moved to a secondary module:

;; Primary
(global.get $dead i32 (global.get $a))

;; Secondary
(global $a i32 (...))

This PR just removes those dead globals. We leave it and do some post processing to make it work but that's more complicated, or we can move it to the same secondary module but this requires scanning of the reverse mapping. Removing it seems the simplest. We can remove dead items for other module items (memories, tables, and tags) but it is not necessary for wasm-split to run, and they can be handled later in DCE.

Fixes #8442 (comment).

Currently dead module items are not split and just end up remaining in
the primary module. Usually a user runs DCE before or after the
splitting, and the goal of wasm-split is not DCE, so from the
optimization perspective it shouldn't be a problem.

But after WebAssembly#8441, this can be a problem because a dead global's
initializer can refer to another global that is moved to a secondary
module:
```wast
;; Primary
(global.get $dead i32 (global.get $a))

;; Secondary
(global $a i32 (...))
```

This PR just removes those dead globals. We leave it and do some post
processing to make it work but that's more complicated, or we can move
it to the same secondary module but this requires scanning of the
reverse mapping. Removing it seems the simplest. We can remove those
dead items for other module items (memories, tables, and tags) but they
are not necessary for wasm-split to run, and they can be handled later
in DCE.

Fixes
WebAssembly#8442 (comment).
@aheejin aheejin requested a review from tlively March 21, 2026 00:29
@aheejin aheejin requested a review from a team as a code owner March 21, 2026 00:29
@aheejin aheejin removed the request for review from a team March 21, 2026 00:30
Comment on lines +62 to +67
(drop
(global.get $glob1)
)
(drop
(global.get $glob2)
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After this PR, glob1 and glob2 are dead and the test doesn't do the intended testing anymore, so we use them here in the primary function

Comment thread src/ir/module-splitting.cpp Outdated
aheejin and others added 2 commits March 20, 2026 17:38
Co-authored-by: Thomas Lively <tlively123@gmail.com>
@aheejin aheejin merged commit 132f7e8 into WebAssembly:main Mar 21, 2026
16 checks passed
@aheejin aheejin deleted the wasm_split_dead_global_fix branch March 21, 2026 03:10
aheejin added a commit that referenced this pull request Jun 8, 2026
We currently just leave unused module items in the primary module,
except for globals (#8505). This removes other unused module items too,
because it is simpler for upcoming PRs.

We currently pin all segments in the primary module, but future PRs can
change that, in which case segments' usage can be dependent on tables,
in which case unused tables can become tricky as in the case of globals.
This PR just removes all unused module items for simplicity. This
doesn't mean this runs a comprehensive fixed-point analysis like
RemoveUnusedModuleElements pass; it just drops elements that are never
referenced anywhere in any modules when exporting them.

Update some tests so that tables are "used" in not droped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants