Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ir/module-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ void renameFunction(Module& wasm, Name oldName, Name newName);

// Convenient iteration over imported/non-imported module elements

inline void iterTypes(const Module& wasm, auto&& visitor) {
for (const auto& [type, _] : wasm.typeIndices) {
Comment thread
stevenfontanella marked this conversation as resolved.
visitor(type);
}
}
Comment thread
stevenfontanella marked this conversation as resolved.
Comment thread
stevenfontanella marked this conversation as resolved.

inline void iterImportedMemories(const Module& wasm, auto&& visitor) {
for (auto& import : wasm.memories) {
if (import->imported()) {
Expand Down
88 changes: 59 additions & 29 deletions src/ir/type-updating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,56 @@

namespace wasm {

namespace {

std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>>

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.

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

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.

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
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);
}
newTypeEffects[newType]->mergeIn(**oldEffects);

}

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
Expand Down Expand Up @@ -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);
Comment thread
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<
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions src/passes/GlobalEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ void mergeMaybeEffects(std::shared_ptr<EffectAnalyzer>& dest,
dest->mergeIn(*src);
}

// Propagate effects from callees to callers transitively
// e.g. if A -> B -> C (A calls B which calls C)
// Then B inherits effects from C and A inherits effects from both B and C.
// Propagate effects from callees to callers transitively and populate direct
// and indirect call effects. e.g. if A -> B -> C (A calls B which calls C),
// then B inherits effects from C and A inherits effects from both B and C.
//
// Generate SCC for the call graph, then traverse it in reverse topological
// order processing each callee before its callers. When traversing:
Expand All @@ -335,7 +335,7 @@ void propagateEffects(
const PassOptions& passOptions,
std::map<Function*, FuncInfo>& funcInfos,
std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>>&
typeEffects,
indirectCallEffects,
const CallGraph& callGraph) {
// We only care about Functions that are roots, not types.
// A type would be a root if a function exists with that type, but no-one
Expand Down Expand Up @@ -435,8 +435,8 @@ void propagateEffects(
// Assign each function's effects to its CC effects.
for (auto node : cc) {
std::visit(overloaded{[&](HeapType type) {
if (ccEffects != UnknownEffects) {
typeEffects[type] = ccEffects;
if (ccEffects) {
indirectCallEffects[type] = ccEffects;
}
},
[&](Function* f) { f->effects = ccEffects; }},
Expand All @@ -455,6 +455,7 @@ struct GenerateGlobalEffects : public Pass {
auto callGraph = buildCallGraph(
*module, funcInfos, referencedFuncs, getPassOptions().worldMode);

module->indirectCallEffects.clear();
propagateEffects(*module,
getPassOptions(),
funcInfos,
Expand All @@ -468,6 +469,7 @@ struct DiscardGlobalEffects : public Pass {
for (auto& func : module->functions) {
func->effects.reset();
}
module->indirectCallEffects.clear();
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 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.
Expand Down
10 changes: 10 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2727,6 +2727,9 @@ class Module {
Name name;

std::unordered_map<HeapType, TypeNames> typeNames;

// The source binary's type indicies. Used in some cases for preserving
Comment thread
stevenfontanella marked this conversation as resolved.
// ordering of types
std::unordered_map<HeapType, Index> typeIndices;

// Potential effects for bodies of indirect calls to this type. Populated by
Expand All @@ -2743,6 +2746,13 @@ class Module {
// This data is only meaningful for indirect calls. If no indirect call
// exists to a function, the data can be out of date (no effort is made to
// clean up the data if e.g. all indirect calls to a function are removed).
//
// A missing key in the map means that the type's effects are explicitly
// unknown. When rewriting types, we distinguish new types from existing
// types by checking whether they existed before the update. If a new type
// is created, it inherits the effects of the old types that map to it. If
// an existing type with unknown effects is mapped, the resulting type will
// also have explicitly unknown effects.
Comment thread
stevenfontanella marked this conversation as resolved.
// TODO: Account for exactness here.
std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>>
indirectCallEffects;
Expand Down
15 changes: 3 additions & 12 deletions src/wasm/wasm-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2764,24 +2764,15 @@ std::unordered_set<HeapType> getIgnorablePublicTypes() {

namespace HeapTypes {

HeapType getMutI8Array() {
static HeapType i8Array = Array(Field(Field::i8, Mutable));
return i8Array;
}
HeapType getMutI8Array() { return Array(Field(Field::i8, Mutable)); }
Comment thread
stevenfontanella marked this conversation as resolved.

HeapType getMutI16Array() {
static HeapType i16Array = Array(Field(Field::i16, Mutable));
return i16Array;
}
HeapType getMutI16Array() { return Array(Field(Field::i16, Mutable)); }

} // namespace HeapTypes

namespace Types {

Type getI64Pair() {
static Type i64Pair({Type::i64, Type::i64});
return i64Pair;
}
Type getI64Pair() { return Type({Type::i64, Type::i64}); }
Comment thread
stevenfontanella marked this conversation as resolved.

} // namespace Types

Expand Down
3 changes: 2 additions & 1 deletion test/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(unittest_SOURCES
suffix_tree.cpp
topological-sort.cpp
type-builder.cpp
type-updating.cpp
wat-lexer.cpp
validator.cpp
source-map.cpp
Expand All @@ -57,5 +58,5 @@ if(BUILD_FUZZTEST)
target_link_libraries(binaryen-unittests PRIVATE gmock)
gtest_discover_tests(binaryen-unittests)
else()
target_link_libraries(binaryen-unittests PRIVATE gtest gtest_main)
target_link_libraries(binaryen-unittests PRIVATE gtest gtest_main gmock)
endif()
49 changes: 49 additions & 0 deletions test/gtest/matchers/effects.h
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; }
Comment thread
stevenfontanella marked this conversation as resolved.

} // namespace wasm

#endif // WASM_TEST_GTEST_MATCHERS_EFFECTS_H
Loading
Loading