-
Notifications
You must be signed in to change notification settings - Fork 882
[wasm-split] Split immutable globals #9064
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
Open
aheejin
wants to merge
2
commits into
main
Choose a base branch
from
wasm_split_immutable_globals3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -336,6 +336,7 @@ struct OwnershipTracker { | |
| struct ItemInfo { | ||
| UsedNames* owner = nullptr; | ||
| SmallVector<Module*, 2> usingSecondaries; | ||
| bool usedByPrimary = false; | ||
| }; | ||
|
|
||
| std::unordered_map<Name, ItemInfo> tables; | ||
|
|
@@ -376,6 +377,9 @@ struct OwnershipTracker { | |
| // used by the primary module or multiple secondary modules, the primary | ||
| // module is the owner. | ||
| auto [it, inserted] = (this->*mapField).insert({name, ItemInfo{owner, {}}}); | ||
| if (owner == &primaryUsed) { | ||
| it->second.usedByPrimary = true; | ||
| } | ||
| Module* secondary = nullptr; | ||
| if (owner != &primaryUsed) { | ||
| size_t index = owner - secondaryUsed.data(); | ||
|
|
@@ -420,13 +424,30 @@ struct OwnershipTracker { | |
| } | ||
|
|
||
| bool isUnused(Name name, const std::unordered_map<Name, ItemInfo>& map) { | ||
| return getOwner(name, map) == nullptr; | ||
| auto it = map.find(name); | ||
| if (it != map.end()) { | ||
| return !it->second.usedByPrimary && it->second.usingSecondaries.empty(); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool isUsedByPrimary(Name name, | ||
| const std::unordered_map<Name, ItemInfo>& map) { | ||
| auto it = map.find(name); | ||
| if (it != map.end()) { | ||
| return it->second.usedByPrimary; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| bool usedBySingleSecondary(Name name, | ||
| const std::unordered_map<Name, ItemInfo>& map) { | ||
| auto* owner = getOwner(name, map); | ||
| return owner != nullptr && owner != &primaryUsed; | ||
| auto it = map.find(name); | ||
| if (it != map.end()) { | ||
| return !it->second.usedByPrimary && | ||
| it->second.usingSecondaries.size() == 1; | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -1047,9 +1068,39 @@ void ModuleSplitter::computeUsedNames() { | |
| if (!global->init) { | ||
| continue; | ||
| } | ||
| if (UsedNames* owner = tracker.getOwner(global->name, tracker.globals)) { | ||
| for (auto* get : FindAll<GlobalGet>(global->init).list) { | ||
| tracker.insert<Global>(get->name, owner); | ||
| auto gets = FindAll<GlobalGet>(global->init).list; | ||
| if (gets.empty()) { | ||
| continue; | ||
| } | ||
|
|
||
| // In case of mutable globals, we cannot have multiple copies. Compute the | ||
| // 'owner' of the global and insert its dependent globals there. | ||
| if (global->mutable_) { | ||
| if (UsedNames* owner = tracker.getOwner(global->name, tracker.globals)) { | ||
| for (auto* get : gets) { | ||
| tracker.insert<Global>(get->name, owner); | ||
| } | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| // In case of immutable globals, we can have multiple copies of it. To | ||
| // reduce the primary module size, we just copy the globals to all secondary | ||
| // modules using them. So here we insert the global in all using modules. | ||
| if (tracker.isUsedByPrimary(global->name, tracker.globals)) { | ||
| for (auto* get : gets) { | ||
| tracker.insert<Global>(get->name, &primaryUsed); | ||
| } | ||
| } | ||
| 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()) { | ||
|
Comment on lines
+1097
to
+1100
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it's doing "for each secondary module |
||
| for (auto* get : gets) { | ||
| tracker.insert<Global>(get->name, &secondaryUsed[i]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -1063,8 +1114,8 @@ void ModuleSplitter::shareImportableItems() { | |
| // 2. If an item is used by only a single secondary module, move the item to | ||
| // that secondary module. If an item is used by multiple modules (including | ||
| // the primary and secondary modules), export the item from the primary and | ||
| // import it from the using secondary modules. | ||
|
|
||
| // import it from the using secondary modules. (except for immutable | ||
| // globals, which we just copy to using secondary modules) | ||
| auto shareElements = [&](auto& elements, | ||
| auto& trackerElements, | ||
| auto copyElement, | ||
|
|
@@ -1073,18 +1124,43 @@ void ModuleSplitter::shareImportableItems() { | |
| ExternalKind kind = ExternalKind::Invalid) { | ||
| std::unordered_set<Name> elementsToRemove; | ||
| for (auto& element : elements) { | ||
| using T = std::remove_pointer_t<decltype(element.get())>; | ||
| bool isImmutableGlobal = false; | ||
| if constexpr (std::is_same_v<T, Global>) { | ||
| if (!element->mutable_) { | ||
| isImmutableGlobal = true; | ||
| } | ||
| } | ||
|
|
||
| if (tracker.isUnused(element->name, trackerElements)) { | ||
| // If this element is not used anywhere, just remove it | ||
| elementsToRemove.insert(element->name); | ||
| } else if (tracker.usedBySingleSecondary(element->name, | ||
| trackerElements)) { | ||
| // If this element is used in a single secondary module, move it to the | ||
| // secondary module | ||
| auto* secondary = | ||
| tracker.getUsingSecondaries(element->name, trackerElements)[0]; | ||
| copyElement(element.get(), *secondary); | ||
| elementsToRemove.insert(element->name); | ||
| } else if (isImmutableGlobal) { | ||
| // If this element is an immutable global, copy it to all using | ||
| // secondary modules to reduce the primary module size, because we can | ||
| // have multiple copies. If it is not used in the primary module, remove | ||
| // it from there. | ||
| if (!tracker.isUsedByPrimary(element->name, trackerElements)) { | ||
| elementsToRemove.insert(element->name); | ||
| } | ||
| for (auto* secondary : | ||
| tracker.getUsingSecondaries(element->name, trackerElements)) { | ||
| copyElement(element.get(), *secondary); | ||
| } | ||
| } else { | ||
| // If this element is used by multiple modules (primary + multiple | ||
| // secondaries or just multiple secondaries), export them from the | ||
| // primary module and import them from using secondary modules. | ||
| // We only import and export Importables, i.e., we don't do this for | ||
| // segments. | ||
| using T = std::remove_pointer_t<decltype(element.get())>; | ||
| if constexpr (std::is_base_of_v<Importable, T>) { | ||
| for (auto* secondary : | ||
| tracker.getUsingSecondaries(element->name, trackerElements)) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we could have
tracker.getOwner<Global>(global->name). This might be possible using struct specialization to create a type-level mapping between module item types (Global,Memory, etc) andOwnershipTrackermember pointers. (This mechanism could also potentially replace the macros inOwnershipTracker::insert.)