Skip to content
Draft
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
105 changes: 71 additions & 34 deletions src/ir/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,10 @@ class EffectAnalyzer {
}

void visitCall(Call* curr) {
if (curr->isReturn) {
parent.branchesOut = true;
}

// call.without.effects has no effects.
if (Intrinsics(parent.module).isCallWithoutEffects(curr)) {
return;
Expand All @@ -730,36 +734,19 @@ class EffectAnalyzer {
if (auto* target = parent.module.getFunctionOrNull(curr->target)) {
targetEffects = target->effects.get();
}
if (targetEffects) {
populateEffectsFromGlobalEffects(*targetEffects, curr);
return;
}

if (curr->isReturn) {
parent.branchesOut = true;
// When EH is enabled, any call can throw.
if (parent.features.hasExceptionHandling() &&
(!targetEffects || targetEffects->throws())) {
if (parent.features.hasExceptionHandling()) {
parent.hasReturnCallThrow = true;
}
}

if (targetEffects) {
// We have effect information for this call target, and can just use
// that. The one change we may want to make is to remove throws_, if the
// target function throws and we know that will be caught anyhow, the
// same as the code below for the general path. We can always filter out
// throws for return calls because they are already more precisely
// captured by `branchesOut`, which models the return, and
// `hasReturnCallThrow`, which models the throw that will happen after
// the return.
if (targetEffects->throws_ && (parent.tryDepth > 0 || curr->isReturn)) {
auto filteredEffects = *targetEffects;
filteredEffects.throws_ = false;
parent.mergeIn(filteredEffects);
} else {
// Just merge in all the effects.
parent.mergeIn(*targetEffects);
}
return;
}

parent.calls = true;
// When EH is enabled, any call can throw. Skip this for return calls
// because the throw is already more precisely captured by the combination
Expand All @@ -770,16 +757,34 @@ class EffectAnalyzer {
}
}
void visitCallIndirect(CallIndirect* curr) {
parent.calls = true;
auto* table = parent.module.getTable(curr->table);
if (!Type::isSubType(Type(curr->heapType, Nullability::Nullable), table->type)) {
parent.trap = true;
return;
}
if (table->type.isNullable()) {
parent.implicitTrap = true;
}
if (curr->isReturn) {
parent.branchesOut = true;
if (parent.features.hasExceptionHandling()) {
}

if (auto it = parent.module.typeEffects.find(curr->heapType);
it != parent.module.typeEffects.end() && it->second) {
populateEffectsFromGlobalEffects(*it->second, curr);
return;
}

parent.calls = true;
// If EH is enabled and we don't have global effects information,
// assume that the call body may throw.
if (parent.features.hasExceptionHandling()) {
if (curr->isReturn) {
parent.hasReturnCallThrow = true;
}
}
if (parent.features.hasExceptionHandling() &&
(parent.tryDepth == 0 && !curr->isReturn)) {
parent.throws_ = true;
if (parent.tryDepth == 0 && !curr->isReturn) {
parent.throws_ = true;
}
}
}
void visitLocalGet(LocalGet* curr) {
Expand Down Expand Up @@ -1042,16 +1047,29 @@ class EffectAnalyzer {
if (trapOnNull(curr->target)) {
return;
}

if (curr->isReturn) {
parent.branchesOut = true;
if (parent.features.hasExceptionHandling()) {
parent.hasReturnCallThrow = true;
}
}

if (auto it =
parent.module.typeEffects.find(curr->target->type.getHeapType());
it != parent.module.typeEffects.end() && it->second) {
populateEffectsFromGlobalEffects(*it->second, curr);
return;
}
parent.calls = true;
if (parent.features.hasExceptionHandling() &&
(parent.tryDepth == 0 && !curr->isReturn)) {
parent.throws_ = true;

// If EH is enabled and we don't have global effects information,
// assume that the call body may throw.
if (parent.features.hasExceptionHandling()) {
if (curr->isReturn) {
parent.hasReturnCallThrow = true;
}

if (parent.tryDepth == 0 && !curr->isReturn) {
parent.throws_ = true;
}
}
}
void visitRefTest(RefTest* curr) {}
Expand Down Expand Up @@ -1335,6 +1353,25 @@ class EffectAnalyzer {
parent.throws_ = true;
}
}

private:
template <typename CallType>
void populateEffectsFromGlobalEffects(const EffectAnalyzer& effects, const CallType* curr) {
if (curr->isReturn) {
if (effects.throws()) {
parent.hasReturnCallThrow = true;
}
}

if (effects.throws_ && (parent.tryDepth > 0 || curr->isReturn)) {
auto filteredEffects = effects;
filteredEffects.throws_ = false;
parent.mergeIn(filteredEffects);
} else {
// Just merge in all the effects.
parent.mergeIn(effects);
}
}
};

public:
Expand Down
19 changes: 19 additions & 0 deletions src/ir/type-updating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,25 @@ void GlobalTypeRewriter::mapTypes(const TypeMap& oldToNewTypes) {
for (auto& tag : wasm.tags) {
tag->type = updater.getNew(tag->type);
}

// Update type effects.
std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>>
newTypeEffects;
for (auto& [oldType, effects] : wasm.typeEffects) {
if (!effects) {
continue;
}
auto newType = updater.getNew(oldType);
auto& targetEffects = newTypeEffects[newType];
if (!targetEffects) {
targetEffects = effects;
} else {
auto merged = std::make_shared<EffectAnalyzer>(*targetEffects);
merged->mergeIn(*effects);
targetEffects = merged;
}
}
wasm.typeEffects = std::move(newTypeEffects);
}

void GlobalTypeRewriter::mapTypeNamesAndIndices(const TypeMap& oldToNewTypes) {
Expand Down
36 changes: 25 additions & 11 deletions src/passes/GlobalEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "pass.h"
#include "support/graph_traversal.h"
#include "support/strongly_connected_components.h"
#include "support/utilities.h"
#include "wasm.h"

namespace wasm {
Expand Down Expand Up @@ -225,10 +226,13 @@ void mergeMaybeEffects(std::optional<EffectAnalyzer>& dest,
// - Merge all of the effects of functions within the CC
// - Also merge the (already computed) effects of each callee CC
// - Add trap effects for potentially recursive call chains
void propagateEffects(const Module& module,
const PassOptions& passOptions,
std::map<Function*, FuncInfo>& funcInfos,
const CallGraph& callGraph) {
void propagateEffects(
const Module& module,
const PassOptions& passOptions,
std::map<Function*, FuncInfo>& funcInfos,
std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>>&
typeEffects,
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
// indirect calls the type.
Expand Down Expand Up @@ -317,12 +321,21 @@ void propagateEffects(const Module& module,
}

// Assign each function's effects to its CC effects.
for (Function* f : ccFuncs) {
if (!ccEffects) {
funcInfos.at(f).effects = UnknownEffects;
} else {
funcInfos.at(f).effects.emplace(*ccEffects);
}
for (auto node : cc) {
std::visit(overloaded{[&](HeapType type) {
if (ccEffects != UnknownEffects) {
typeEffects[type] =
std::make_shared<EffectAnalyzer>(*ccEffects);
}
},
[&](Function* f) {
if (!ccEffects) {
funcInfos.at(f).effects = UnknownEffects;
} else {
funcInfos.at(f).effects.emplace(*ccEffects);
}
}},
node);
}
}
}
Expand All @@ -346,7 +359,8 @@ struct GenerateGlobalEffects : public Pass {
auto callGraph =
buildCallGraph(*module, funcInfos, getPassOptions().closedWorld);

propagateEffects(*module, getPassOptions(), funcInfos, callGraph);
propagateEffects(
*module, getPassOptions(), funcInfos, module->typeEffects, callGraph);

copyEffectsToFunctions(funcInfos);
}
Expand Down
11 changes: 11 additions & 0 deletions src/support/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ class Fatal {
#define WASM_UNREACHABLE(msg) wasm::handle_unreachable()
#endif

// Helper to create an invocable with an overloaded operator(), for use with
// std::visit e.g.
// std::visit(
// overloaded{
// [](const A& a) { ... },
// [](const B& b) { ... }},
// variant)
template<class... Ts> struct overloaded : Ts... {
using Ts::operator()...;
};

} // namespace wasm

#endif // wasm_support_utilities_h
6 changes: 6 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,12 @@ class Module {
std::unordered_map<HeapType, TypeNames> typeNames;
std::unordered_map<HeapType, Index> typeIndices;

// Potential effects for bodies of indirect calls to this type.
// TODO: Use Type instead of HeapType to account for nullability and
// exactness.
std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>>
typeEffects;

MixedArena allocator;

private:
Expand Down
15 changes: 1 addition & 14 deletions test/lit/passes/global-effects-closed-world-tnh.wast
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,9 @@
)

;; CHECK: (func $calls-nop-via-nullable-ref (type $1) (param $ref (ref null $nopType))
;; CHECK-NEXT: (call_ref $nopType
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: (local.get $ref)
;; CHECK-NEXT: )
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $calls-nop-via-nullable-ref (param $ref (ref null $nopType))
(call_ref $nopType (i32.const 1) (local.get $ref))
)

;; CHECK: (func $f (type $1) (param $ref (ref null $nopType))
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $f (param $ref (ref null $nopType))
;; The only possible implementation of $nopType has no effects.
;; $calls-nop-via-nullable-ref may trap from a null reference, but
;; --traps-never-happen is enabled, so we're free to optimize this out.
(call $calls-nop-via-nullable-ref (local.get $ref))
)
)
Loading
Loading