-
Notifications
You must be signed in to change notification settings - Fork 869
Fix type updating for indirect call effects #8874
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,6 +26,56 @@ | |||||||||||||||
|
|
||||||||||||||||
| namespace wasm { | ||||||||||||||||
|
|
||||||||||||||||
| namespace { | ||||||||||||||||
|
|
||||||||||||||||
| std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>> | ||||||||||||||||
|
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. Please add a comment here. |
||||||||||||||||
| updateIndirectCallEffects(const Module& wasm, | ||||||||||||||||
| const std::vector<HeapType>& oldTypes, | ||||||||||||||||
| const GlobalTypeRewriter::TypeMap& oldToNewTypes) { | ||||||||||||||||
| std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>> | ||||||||||||||||
| newTypeEffects; | ||||||||||||||||
|
|
||||||||||||||||
| std::unordered_set<HeapType> unknownNewTypes; | ||||||||||||||||
|
|
||||||||||||||||
| for (HeapType oldType : oldTypes) { | ||||||||||||||||
| HeapType newType; | ||||||||||||||||
| { | ||||||||||||||||
| auto it = oldToNewTypes.find(oldType); | ||||||||||||||||
| if (it == oldToNewTypes.end()) { | ||||||||||||||||
| newType = oldType; | ||||||||||||||||
| } else { | ||||||||||||||||
| newType = it->second; | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if (unknownNewTypes.count(newType)) { | ||||||||||||||||
| continue; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const std::shared_ptr<const EffectAnalyzer>* oldEffects = | ||||||||||||||||
| find_or_null(wasm.indirectCallEffects, oldType); | ||||||||||||||||
|
|
||||||||||||||||
| if (!oldEffects) { | ||||||||||||||||
| // oldType is an existing type and has no entry, which means its | ||||||||||||||||
| // effects are explicitly UNKNOWN. | ||||||||||||||||
| unknownNewTypes.insert(newType); | ||||||||||||||||
| newTypeEffects.erase(newType); | ||||||||||||||||
| continue; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| auto [it, inserted] = newTypeEffects.emplace(newType, *oldEffects); | ||||||||||||||||
| if (!inserted) { | ||||||||||||||||
| auto merged = std::make_shared<EffectAnalyzer>(*it->second); | ||||||||||||||||
| merged->mergeIn(**oldEffects); | ||||||||||||||||
| it->second = std::move(merged); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+66
to
+71
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 do we have to allocate a new EffectAnalyzer when we already have one for the new type? Can we not just merge the old effects into the existing effects for the new type? Then this could be simplified:
Suggested change
|
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return newTypeEffects; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| } // anonymous namespace | ||||||||||||||||
|
|
||||||||||||||||
| GlobalTypeRewriter::GlobalTypeRewriter(Module& wasm, WorldMode worldMode) | ||||||||||||||||
| : wasm(wasm), publicGroups(wasm.features) { | ||||||||||||||||
| // Find the heap types that are not publicly observable. Even in a closed | ||||||||||||||||
|
|
@@ -212,6 +262,15 @@ GlobalTypeRewriter::rebuildTypes(std::vector<HeapType> types) { | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) { | ||||||||||||||||
| if (!wasm.indirectCallEffects.empty()) { | ||||||||||||||||
| // Update indirect call effects per type. | ||||||||||||||||
| // When A is rewritten to B, B inherits the effects of A and A loses its | ||||||||||||||||
| // effects. | ||||||||||||||||
| std::vector<HeapType> oldTypes = ModuleUtils::collectHeapTypes(wasm); | ||||||||||||||||
|
stevenfontanella marked this conversation as resolved.
|
||||||||||||||||
| wasm.indirectCallEffects = | ||||||||||||||||
| updateIndirectCallEffects(wasm, oldTypes, oldToNewTypes); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Replace all the old types in the module with the new ones. | ||||||||||||||||
| struct CodeUpdater | ||||||||||||||||
| : public WalkerPass< | ||||||||||||||||
|
|
@@ -325,35 +384,6 @@ void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) { | |||||||||||||||
| for (auto& tag : wasm.tags) { | ||||||||||||||||
| tag->type = updater.getNew(tag->type); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // Update indirect call effects per type. | ||||||||||||||||
| // When A is rewritten to B, B inherits the effects of A and A loses its | ||||||||||||||||
| // effects. | ||||||||||||||||
| std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>> | ||||||||||||||||
| newTypeEffects; | ||||||||||||||||
|
|
||||||||||||||||
| for (const auto& [oldType, newType] : oldToNewTypes) { | ||||||||||||||||
| std::shared_ptr<const EffectAnalyzer>* oldEffects = | ||||||||||||||||
| find_or_null(wasm.indirectCallEffects, oldType); | ||||||||||||||||
| std::shared_ptr<const EffectAnalyzer>* targetEffects = | ||||||||||||||||
| find_or_null(wasm.indirectCallEffects, newType); | ||||||||||||||||
|
|
||||||||||||||||
| if (!targetEffects) { | ||||||||||||||||
| // Nothing to update, we already know nothing and assume all effects. | ||||||||||||||||
| continue; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if (!oldEffects) { | ||||||||||||||||
| targetEffects->reset(); | ||||||||||||||||
| continue; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| auto merged = std::make_shared<EffectAnalyzer>(**targetEffects); | ||||||||||||||||
| merged->mergeIn(**oldEffects); | ||||||||||||||||
| *targetEffects = std::move(merged); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| wasm.indirectCallEffects = std::move(newTypeEffects); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| void GlobalTypeRewriter::mapTypeNamesAndIndices(const TypeMap& oldToNewTypes) { | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1082,6 +1082,11 @@ void PassRunner::handleAfterEffects(Pass* pass, Function* func) { | |
| // Binaryen IR is modified, so we may have work here. | ||
|
|
||
| if (!func) { | ||
| if (pass->addsEffects()) { | ||
| // Indirect call effects are now under-approximating. Clear them to avoid | ||
| // incorrect optimizations. | ||
| wasm->indirectCallEffects.clear(); | ||
| } | ||
|
Comment on lines
+1085
to
+1089
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. Can we write a test for which this is necessary? |
||
| // If no function is provided, then this is not a function-parallel pass, | ||
| // and it may have operated on any of the functions in theory, so run on | ||
| // them all. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright 2026 WebAssembly Community Group participants | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #ifndef WASM_TEST_GTEST_MATCHERS_EFFECTS_H | ||
| #define WASM_TEST_GTEST_MATCHERS_EFFECTS_H | ||
|
|
||
| #include "gmock/gmock.h" | ||
|
|
||
| namespace wasm { | ||
|
|
||
| MATCHER(BranchesOut, "") { return arg->branchesOut; } | ||
| MATCHER(Calls, "") { return arg->calls; } | ||
| MATCHER(ReadsMemory, "") { return arg->readsMemory; } | ||
| MATCHER(WritesMemory, "") { return arg->writesMemory; } | ||
| MATCHER(ReadsSharedMemory, "") { return arg->readsSharedMemory; } | ||
| MATCHER(WritesSharedMemory, "") { return arg->writesSharedMemory; } | ||
| MATCHER(ReadsTable, "") { return arg->readsTable; } | ||
| MATCHER(WritesTable, "") { return arg->writesTable; } | ||
| MATCHER(ReadsMutableStruct, "") { return arg->readsMutableStruct; } | ||
| MATCHER(WritesStruct, "") { return arg->writesStruct; } | ||
| MATCHER(ReadsSharedMutableStruct, "") { return arg->readsSharedMutableStruct; } | ||
| MATCHER(WritesSharedStruct, "") { return arg->writesSharedStruct; } | ||
| MATCHER(ReadsMutableArray, "") { return arg->readsMutableArray; } | ||
| MATCHER(WritesArray, "") { return arg->writesArray; } | ||
| MATCHER(ReadsSharedMutableArray, "") { return arg->readsSharedMutableArray; } | ||
| MATCHER(WritesSharedArray, "") { return arg->writesSharedArray; } | ||
| MATCHER(Traps, "") { return arg->trap; } | ||
| MATCHER(ImplicitTraps, "") { return arg->implicitTrap; } | ||
| MATCHER(Throws, "") { return arg->throws_; } | ||
| MATCHER(DanglingPop, "") { return arg->danglingPop; } | ||
| MATCHER(MayNotReturn, "") { return arg->mayNotReturn; } | ||
| MATCHER(HasReturnCallThrow, "") { return arg->hasReturnCallThrow; } | ||
|
stevenfontanella marked this conversation as resolved.
|
||
|
|
||
| } // namespace wasm | ||
|
|
||
| #endif // WASM_TEST_GTEST_MATCHERS_EFFECTS_H | ||
Uh oh!
There was an error while loading. Please reload this page.