Repository navigation
[wasm-split] Simplify OwnershipTracker (NFC) - #9374
Conversation
Previously, we treated all modules items in the same way: if a module
item was used in the primary module or multiple secondary modules, we
put the item in the primary module and exported it from there, and
secondary modules using the item imported it. So
1. When an item was used in both the primary and secondary modules, it
was placed in the primary module and exported from there, and
imported from the secondary modules
2. When an item was not used in the primary module but used in multiple
secondary modules, it was still placed in the primary module and
exported from there, and imported from the secondary modules
But in case of immutable globals, we can have as many copies of the same
global as possible, and we can just copy them to the secondary modules.
So whether it is used in the primary module or multiple secondary
modules, each module that uses it has its own copy. This reduces the
primary module size because, in case of 1, we don't need to put the
global in the primary module in the first place. Also in case of 2, we
don't need to add an export of a global to the primary module.
This is a trade-off and may increase the secondary module sizes instead.
But given that the main goal of using wasm-split is mostly to reduce the
loading time of the primary module, I think this trade-off is worth it.
In case of merge-opt-split workflow, after interprocedural
optimizations, many globals that used to be only used in a single
secondary module gets used in multiple secondary modules, and thus are
placed in the primary module after splitting, increasing the primary
module size. This undoes this effect.
Note that this also copy immutable global imports to secondary modules
whenever it is possible, because we didn't exclude them in
`shareElement`.
---
Results
- acx_gallery
- Input
- Total: 23648k
- Primary: 1337k
- Current
- Total: 22240k
- Primary: 1579k
- This PR
- Total: 22884k
- vs. Current: +2.8%
- vs. Input: -3.2%
- Primary: 1348k
- vs. Current: -14.6%
- vs. Input: +0.8%
- essentials
- Input
- Total: 83384k
- Primary: 5600k
- Current
- Total: 77060k
- Primary: 6396k
- This PR
- Total: 79624k
- vs. Current: +3.3%
- vs. Input: -4.5%
- Primary: 5715k
- vs. Current: -10.6%
- vs. Input: +2.0%
Compared to the current code (without this patch), we can see this
reduces the primary module size significantly (-14.6% and -10.6% each)
at the expense of slight increase of the total combined module size
(+3-4%).
But compared to the original input, this still does not reduce the
primary module size (+1-2%), while it decreases the combined
module size slightly (-3-4%).
The measurements were done with with 07/2026 version of Dart apps, and
after removing all (internal) exports that were not marked as
`@binaryen.js.called`. (I have a pass that does this in my local
machine, but I haven't uploaded it yet)
This removes `UsedNames` and directly uses `Module`s in place of that. `UsedNames` was first added in #8221, and after #8986, it becamse largely unnecessary and made code more complicated. Also this removes `owner` field from `ItemInfo`, which can be computed. This also templatize member functions in `OwnershipTracker` so they can be called with a simpler syntax of `tracker.func<T>(name)`.
There was a problem hiding this comment.
Code Review
This pull request refactors OwnershipTracker and ModuleSplitter in src/ir/module-splitting.cpp to track module item ownership directly using Module* pointers instead of the intermediate UsedNames struct. It introduces template-based helper methods to handle different module item types uniformly, significantly simplifying the code and removing redundant macros. The review feedback suggests two optimizations: simplifying the conditional logic in getOwner by using an assertion for the single secondary module case, and reducing map lookups in the global sharing loop by introducing a single lookup helper.
| if (tracker.isUsedByPrimary<Global>(global->name)) { | ||
| for (auto* get : gets) { | ||
| tracker.insert<Global>(get->name, &primaryUsed); | ||
| tracker.insert<Global>(get->name, &primary); | ||
| } | ||
| } | ||
| auto& usingSecs = | ||
| tracker.getUsingSecondaries(global->name, tracker.globals); | ||
| for (size_t i = 0; i < secondaries.size(); ++i) { | ||
| Module* sec = secondaries[i].get(); | ||
| if (std::find(usingSecs.begin(), usingSecs.end(), sec) != | ||
| usingSecs.end()) { | ||
| for (auto* get : gets) { | ||
| tracker.insert<Global>(get->name, &secondaryUsed[i]); | ||
| } | ||
| for (Module* sec : tracker.getUsingSecondaries<Global>(global->name)) { | ||
| for (auto* get : gets) { | ||
| tracker.insert<Global>(get->name, sec); | ||
| } | ||
| } |
There was a problem hiding this comment.
We can also optimize this block by using tracker.getItemInfo<Global>(global->name) to perform a single map lookup instead of separate lookups for isUsedByPrimary and getUsingSecondaries.
if (const auto* info = tracker.getItemInfo<Global>(global->name)) {
if (info->usedByPrimary) {
for (auto* get : gets) {
tracker.insert<Global>(get->name, &primary);
}
}
for (Module* sec : info->usingSecondaries) {
for (auto* get : gets) {
tracker.insert<Global>(get->name, sec);
}
}
}There was a problem hiding this comment.
We don't have a method called tracker.getItemInfo. Also creating one doesn't seem to simplify things anyway.
| } else if constexpr (std::is_same_v<T, ElementSegment>) { | ||
| return elementSegments; | ||
| } else { | ||
| static_assert(!sizeof(T*), "Unsupported module item type"); |
There was a problem hiding this comment.
Can this just be static_assert(false, ...)?
There was a problem hiding this comment.
That doesn't work for older compilers: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2593r1.html
We have a similar trick in our codebase too: https://github.com/WebAssembly/binaryen/blame/c35bddec04ca366d8f307ca5fcec98c358239db0/src/tools/fuzzing/random.h#L120-L127
Anyway, changed the code to avoid using the trick: 0785c49
|
Oh and this needs to land after #9064. (Sorry for the long silence there. I addressed the comments there) |
This removes
UsedNamesand directly usesModules in place of that.UsedNameswas first added in #8221, and after #8986, it becamse largely unnecessary and made code more complicated. Also this removesownerfield fromItemInfo, which can be computed.This also templatize member functions in
OwnershipTrackerso they can be called with a simpler syntax oftracker.func<T>(name).