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
130 changes: 78 additions & 52 deletions src/ir/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,70 +716,64 @@ 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;
}

// Get the target's effects, if they exist. Note that we must handle the
// case of the function not yet existing (we may be executed in the middle
// of a pass, which may have built up calls but not the targets of those
// calls; in such a case, we do not find the targets and therefore assume
// we know nothing about the effects, which is safe).
const EffectAnalyzer* targetEffects = nullptr;
if (auto* target = parent.module.getFunctionOrNull(curr->target)) {
targetEffects = target->effects.get();
if (auto* target = parent.module.getFunctionOrNull(curr->target);
target && target->effects) {
populateEffectsFromGlobalEffects(*target->effects, curr);
return;
}

if (curr->isReturn) {
parent.branchesOut = true;
// When EH is enabled, any call can throw.
if (parent.features.hasExceptionHandling() &&
(!targetEffects || targetEffects->throws())) {
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 (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);
if (parent.tryDepth == 0 && !curr->isReturn) {
parent.throws_ = true;
}
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
// of `hasReturnCallThrow` and `branchesOut`.
if (parent.features.hasExceptionHandling() && parent.tryDepth == 0 &&
!curr->isReturn) {
parent.throws_ = true;
}
}
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 +1036,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 +1342,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 {
parent.mergeIn(effects);
}
}
};

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

// Update indirect call effects per type.
std::unordered_map<HeapType, std::shared_ptr<const EffectAnalyzer>>
newTypeEffects;
for (auto& [oldType, effects] : wasm.typeEffects) {
if (!effects) {
continue;
}

auto newType = updater.getNew(oldType);
std::shared_ptr<const EffectAnalyzer>& 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
32 changes: 22 additions & 10 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,34 @@
)

;; 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))
;; We would trap if $ref is null, but otherwise this has no effects.
(call_ref $nopType (i32.const 1) (local.get $ref))
)
)

(module
;; CHECK: (type $nopType (func (param i32)))
(type $nopType (func (param i32)))

;; (table 1 1 (ref $nopType))
(table 1 1 funcref)

;; CHECK: (func $nop (type $nopType) (param $0 i32)
;; CHECK-NEXT: (nop)
;; CHECK-NEXT: )
(func $nop (export "nop") (type $nopType)
(nop)
)

;; CHECK: (func $f (type $1) (param $ref (ref null $nopType))
;; CHECK: (func $calls-nop-via-ref (type $1)
;; 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))
(func $calls-nop-via-ref
;; We may trap due to index out of bounds or the function type not matching
;; the table, but otherwise this has no possible effects.
(call_indirect (type $nopType) (i32.const 1) (i32.const 0))
)
)
Loading
Loading