Skip to content

[wasm-split] Simplify OwnershipTracker (NFC) - #9374

Merged
aheejin merged 11 commits into
mainfrom
wasm_split_tracker_cleanup
Nov 12, 2026
Merged

aheejin merged 11 commits into
mainfrom
wasm_split_tracker_cleanup

Conversation

@aheejin

@aheejin aheejin commented Nov 9, 2026

Copy link
Copy Markdown
Member

This removes UsedNames and directly uses Modules 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).

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)`.
@aheejin
aheejin requested a review from tlively November 9, 2026 09:59
@aheejin
aheejin requested a review from a team as a code owner November 9, 2026 09:59

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment thread src/ir/module-splitting.cpp Resolved
Comment on lines +1081 to 1090
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);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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);
        }
      }
    }

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.

We don't have a method called tracker.getItemInfo. Also creating one doesn't seem to simplify things anyway.

@aheejin
aheejin removed the request for review from a team November 9, 2026 10:01

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice simplification!

Comment thread src/ir/module-splitting.cpp Outdated
} else if constexpr (std::is_same_v<T, ElementSegment>) {
return elementSegments;
} else {
static_assert(!sizeof(T*), "Unsupported module item type");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this just be static_assert(false, ...)?

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.

@aheejin

aheejin commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

Oh and this needs to land after #9064. (Sorry for the long silence there. I addressed the comments there)

Base automatically changed from wasm_split_immutable_globals3 to main November 12, 2026 02:31
@aheejin
aheejin merged commit a9fd4c7 into main Nov 12, 2026
18 checks passed
@aheejin
aheejin deleted the wasm_split_tracker_cleanup branch November 12, 2026 06:22
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