-
Notifications
You must be signed in to change notification settings - Fork 837
[wasm-split] Export/Import only necessary elements #8221
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ec039c6
[wasm-split] Export/Import only necessary elements
aheejin 6fdbc5a
Merge branch 'main' into wasm_split_share_needed
aheejin 8642d46
Use switch-case
aheejin 8b7850a
Use walkModuleCode()
aheejin e859242
Add missing memory.is() check
aheejin 5e7b376
Table export comments
aheejin 8f80464
Example test update
aheejin ee78dd7
Merge branch 'main' into wasm_split_share_needed
aheejin 51e3151
Measure time
aheejin 794a530
Revert "Measure time"
aheejin 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 |
|---|---|---|
|
|
@@ -343,7 +343,6 @@ struct ModuleSplitter { | |
| void exportImportCalledPrimaryFunctions(); | ||
| void setupTablePatching(); | ||
| void shareImportableItems(); | ||
| void removeUnusedSecondaryElements(); | ||
|
|
||
| ModuleSplitter(Module& primary, const Config& config) | ||
| : config(config), primary(primary), tableManager(primary), | ||
|
|
@@ -359,7 +358,6 @@ struct ModuleSplitter { | |
| exportImportCalledPrimaryFunctions(); | ||
| setupTablePatching(); | ||
| shareImportableItems(); | ||
| removeUnusedSecondaryElements(); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -911,27 +909,122 @@ void ModuleSplitter::shareImportableItems() { | |
| } | ||
| }; | ||
|
|
||
| // TODO: Be more selective by only sharing global items that are actually used | ||
| // in the secondary module, just like we do for functions. | ||
| struct UsedNames { | ||
| std::unordered_set<Name> globals; | ||
| std::unordered_set<Name> memories; | ||
| std::unordered_set<Name> tables; | ||
| std::unordered_set<Name> tags; | ||
| }; | ||
|
|
||
| struct NameCollector | ||
| : public PostWalker<NameCollector, | ||
| UnifiedExpressionVisitor<NameCollector>> { | ||
| UsedNames& used; | ||
| NameCollector(UsedNames& used) : used(used) {} | ||
|
|
||
| void visitExpression(Expression* curr) { | ||
| #define DELEGATE_ID curr->_id | ||
| #define DELEGATE_START(id) [[maybe_unused]] auto* cast = curr->cast<id>(); | ||
| #define DELEGATE_GET_FIELD(id, field) cast->field | ||
| #define DELEGATE_FIELD_TYPE(id, field) | ||
| #define DELEGATE_FIELD_HEAPTYPE(id, field) | ||
| #define DELEGATE_FIELD_CHILD(id, field) | ||
| #define DELEGATE_FIELD_INT(id, field) | ||
| #define DELEGATE_FIELD_LITERAL(id, field) | ||
| #define DELEGATE_FIELD_NAME(id, field) | ||
| #define DELEGATE_FIELD_SCOPE_NAME_DEF(id, field) | ||
| #define DELEGATE_FIELD_SCOPE_NAME_USE(id, field) | ||
| #define DELEGATE_FIELD_ADDRESS(id, field) | ||
|
|
||
| #define DELEGATE_FIELD_NAME_KIND(id, field, kind) \ | ||
| if (cast->field.is()) { \ | ||
| switch (kind) { \ | ||
| case ModuleItemKind::Table: \ | ||
| used.tables.insert(cast->field); \ | ||
| break; \ | ||
| case ModuleItemKind::Memory: \ | ||
| used.memories.insert(cast->field); \ | ||
| break; \ | ||
| case ModuleItemKind::Global: \ | ||
| used.globals.insert(cast->field); \ | ||
| break; \ | ||
| case ModuleItemKind::Tag: \ | ||
| used.tags.insert(cast->field); \ | ||
| break; \ | ||
| case ModuleItemKind::Function: \ | ||
| case ModuleItemKind::DataSegment: \ | ||
| case ModuleItemKind::ElementSegment: \ | ||
| case ModuleItemKind::Invalid: \ | ||
| break; \ | ||
| } \ | ||
| } | ||
|
|
||
| #include "wasm-delegations-fields.def" | ||
| } | ||
| }; | ||
|
|
||
| for (auto& secondaryPtr : secondaries) { | ||
| Module& secondary = *secondaryPtr; | ||
|
|
||
| // Collect names used in the secondary module | ||
| UsedNames used; | ||
| ModuleUtils::ParallelFunctionAnalysis<UsedNames> nameCollector( | ||
| secondary, [&](Function* func, UsedNames& used) { | ||
| if (!func->imported()) { | ||
| NameCollector(used).walk(func->body); | ||
| } | ||
| }); | ||
|
|
||
| for (auto& [_, funcUsed] : nameCollector.map) { | ||
| used.globals.insert(funcUsed.globals.begin(), funcUsed.globals.end()); | ||
| used.memories.insert(funcUsed.memories.begin(), funcUsed.memories.end()); | ||
| used.tables.insert(funcUsed.tables.begin(), funcUsed.tables.end()); | ||
| used.tags.insert(funcUsed.tags.begin(), funcUsed.tags.end()); | ||
| } | ||
|
|
||
| NameCollector collector(used); | ||
| collector.walkModuleCode(&secondary); | ||
| for (auto& segment : secondary.dataSegments) { | ||
| if (segment->memory.is()) { | ||
| used.memories.insert(segment->memory); | ||
| } | ||
| } | ||
| for (auto& segment : secondary.elementSegments) { | ||
| if (segment->table.is()) { | ||
| used.tables.insert(segment->table); | ||
| } | ||
| } | ||
|
|
||
| // Export module items that are used in the secondary module | ||
| for (auto& memory : primary.memories) { | ||
| if (!used.memories.count(memory->name)) { | ||
| continue; | ||
| } | ||
| auto secondaryMemory = ModuleUtils::copyMemory(memory.get(), secondary); | ||
| makeImportExport( | ||
| *memory, *secondaryMemory, "memory", ExternalKind::Memory); | ||
| } | ||
|
|
||
| for (auto& table : primary.tables) { | ||
| // 1. In case we copied this table to this secondary module in | ||
| // setupTablePatching(), secondary.getTableOrNull(table->name) is not | ||
| // null, and we need to export it. | ||
| // 2. As in the case with other module elements, if the table is used in | ||
| // the secondary module's instructions, we need to export it. | ||
| auto secondaryTable = secondary.getTableOrNull(table->name); | ||
| if (!secondaryTable && !used.tables.count(table->name)) { | ||
|
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. Why is this condition different from the others? Maybe worth a comment.
Member
Author
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. Done: 5e7b376 |
||
| continue; | ||
| } | ||
| if (!secondaryTable) { | ||
| secondaryTable = ModuleUtils::copyTable(table.get(), secondary); | ||
| } | ||
|
|
||
| makeImportExport(*table, *secondaryTable, "table", ExternalKind::Table); | ||
| } | ||
|
|
||
| for (auto& global : primary.globals) { | ||
| if (!used.globals.count(global->name)) { | ||
| continue; | ||
| } | ||
| if (global->mutable_) { | ||
| assert(primary.features.hasMutableGlobals() && | ||
| "TODO: add wrapper functions for disallowed mutable globals"); | ||
|
|
@@ -949,6 +1042,9 @@ void ModuleSplitter::shareImportableItems() { | |
| } | ||
|
|
||
| for (auto& tag : primary.tags) { | ||
| if (!used.tags.count(tag->name)) { | ||
| continue; | ||
| } | ||
| auto secondaryTag = std::make_unique<Tag>(); | ||
| secondaryTag->type = tag->type; | ||
| makeImportExport(*tag, *secondaryTag, "tag", ExternalKind::Tag); | ||
|
|
@@ -957,19 +1053,6 @@ 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. | ||
| for (auto& secondaryPtr : secondaries) { | ||
| PassRunner runner(secondaryPtr.get()); | ||
| // Do not validate here in the middle, as the IR still needs updating later. | ||
| runner.options.validate = false; | ||
| runner.add("remove-unused-module-elements"); | ||
| runner.run(); | ||
| } | ||
| } | ||
|
|
||
| } // anonymous namespace | ||
|
|
||
| Results splitFunctions(Module& primary, const Config& config) { | ||
|
|
||
Oops, something went wrong.
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.
All of this except checking for tables and memories in active segments could be replaced with a call to
collector.walkModuleCode().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.
Done: 8b7850a
And added a missing check e859242