Skip to content
1 change: 1 addition & 0 deletions scripts/test/fuzzing.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'duplicate-function-elimination_annotations.wast',
'once-reduction_idempotent.wast',
'local-cse_idempotent.wast',
'local-cse-global-effects-idempotent.wast',
'optimize-instructions-global-effects-idempotent.wast',
'noreturn.wast',
'optimize-noreturn.wast',
Expand Down
160 changes: 102 additions & 58 deletions src/passes/LocalCSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@

#include <algorithm>
#include <memory>
#include <optional>

#include <ir/cost.h>
#include <ir/effects.h>
Expand Down Expand Up @@ -449,18 +450,76 @@ struct Checker
// the block.
Index requestsLeft;

// The effects in the expression.
EffectAnalyzer effects;
// The effects when re-evaluating the expression as a copy (excluding the
// shallow effects of any idempotent calls inside it - that is, it is ok for
// the original to have effects which, due to idempotency, the copy does not
// have, so we ignore such effects here).
EffectAnalyzer copyEffects;
};

// The currently relevant original expressions, that is, the ones that may be
// optimized in the current basic block.
std::unordered_map<Expression*, ActiveOriginalInfo> activeOriginals;

// Collect the effects of an expression when re-evaluated as a copy. Any
// idempotent calls inside it have no effects on subsequent executions (as
// long as their operands evaluate to the same values), so we split their
// shallow effects into |idempotentCallEffects| and put all other shallow
// effects (operands and enclosing expressions) into |copyEffects|.
void collectEffects(Expression* expr,
EffectAnalyzer& copyEffects,
std::optional<EffectAnalyzer>& idempotentCallEffects) {
SmallVector<Expression*, 16> worklist{expr};
while (!worklist.empty()) {

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.

It would be really nice if we had an iterator over expression trees so we could avoid implementing a tree search or defining a visitor every time we had to do this. Such an iterator would be much nicer if defined in terms of C++ coroutines. I bumped the discussion on #8657 to see where we are with that.

auto* curr = worklist.back();
worklist.pop_back();
if (isIdempotent(curr, *getModule())) {
if (!idempotentCallEffects) {
idempotentCallEffects.emplace(options, *getModule());
}
Comment on lines +477 to +479

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.

Instead of passing an optional idempotentCallEffects that may need to be initialized, can we just pass a non-optional but empty idempotentCallEffects?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, though an EffectAnalyzer is a large data structure and this is rarely needed, and it just takes a trivial if to avoid creating it here?

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.

Sad that C++ doesn't have a good pattern for "initialize this optional if necessary" as part of a one-liner :( Rust gets this right with the get_or_insert family of methods on std::Option<T>.

idempotentCallEffects->visit(curr);
} else {
copyEffects.visit(curr);
}
for (auto* child : ChildIterator(curr)) {
worklist.push_back(child);
}
}
}

void visitExpression(Expression* curr) {
// This is the first time we encounter this expression.
assert(!activeOriginals.contains(curr));

auto iter = requestInfos.find(curr);
if (iter != requestInfos.end()) {
auto& info = iter->second;
info.validate();
if (info.original) {
auto originalIter = activeOriginals.find(info.original);
if (originalIter != activeOriginals.end()) {
// This expression is a valid copy that will be replaced with a
// local.get of its original, so it will not actually execute (and in
// particular, if it is an idempotent call, it has no effects here and
// cannot invalidate other active originals). All we need to do is
// remove one request (since we finished processing this expression,
// which was one of those requests), and if none are left, remove the
// original from being active.
auto& originalInfo = originalIter->second;
--originalInfo.requestsLeft;
if (!originalInfo.requestsLeft) {
activeOriginals.erase(originalIter);
}
return;
}
// The original was already invalidated (or rejected). Remove our info,
// and fall through to check if |curr| invalidates other active
// originals (since |curr| will still execute).
requestInfos.erase(iter);
iter = requestInfos.end();
}
}

// Given the current expression, see what it invalidates of the currently-
// hashed expressions, if there are any.
if (!activeOriginals.empty()) {
Expand All @@ -486,86 +545,71 @@ struct Checker
// effects.
effects.trap = false;

auto idempotent = isIdempotent(curr, *getModule());

std::vector<Expression*> invalidated;
for (auto& kv : activeOriginals) {
auto* original = kv.first;
if (idempotent && ExpressionAnalyzer::shallowEqual(curr, original)) {
// |curr| is idempotent, so it does not invalidate later appearances
// of itself.
continue;
}
auto& originalInfo = kv.second;
// Check whether curr must remain before COPY. We use ORIGINAL's effects
// in the check because we know they are the same as COPY's effects.
if (effects.orderedBefore(originalInfo.effects)) {
for (auto& [original, originalInfo] : activeOriginals) {
// Check whether curr must remain before COPY. We use ORIGINAL's copy
// effects in the check because we know they are the same as COPY's
// effects.
if (effects.orderedBefore(originalInfo.copyEffects)) {
invalidated.push_back(original);
}
}

for (auto* original : invalidated) {
assert(original != curr);
// Remove all requests after this expression, as we cannot optimize to
// them.
requestInfos[original].requests -=
activeOriginals.at(original).requestsLeft;
auto& originalReqInfo = requestInfos.at(original);
originalReqInfo.requests -= activeOriginals.at(original).requestsLeft;

// If no requests remain at all (that is, there were no requests we
// could provide before we ran into this invalidation) then we do not
// need this original at all.
if (requestInfos[original].requests == 0) {
if (originalReqInfo.requests == 0) {
requestInfos.erase(original);
}

activeOriginals.erase(original);
}
}

auto iter = requestInfos.find(curr);
if (iter == requestInfos.end()) {
return;
}
auto& info = iter->second;
info.validate();

if (info.requests > 0) {
// This is an original. Compute its side effects, as we cannot optimize
// away repeated appearances if it has any.
EffectAnalyzer effects(options, *getModule(), curr);

// We can ignore traps here, as we replace a repeating expression with a
// single appearance of it, a store to a local, and gets in the other
// locations, and so if the expression traps then the first appearance -
// that we keep around - would trap, and the others are never reached
// anyhow. (The other checks we perform here, including invalidation and
// determinism, will ensure that either all of the appearances trap, or
// none of them.)
effects.trap = false;

// Note that we've already checked above that this has no side effects or
// generativity: if we got here, then it is good to go from the
// perspective of this expression itself (but may be invalidated by other
// code in between, see above).
activeOriginals.emplace(
curr, ActiveOriginalInfo{info.requests, std::move(effects)});
} else if (info.original) {
// The original may have already been invalidated. If so, remove our info
// as well.
auto originalIter = activeOriginals.find(info.original);
if (originalIter == activeOriginals.end()) {
requestInfos.erase(iter);
return;
}

// After visiting this expression, we have one less request for its
// original, and perhaps none are left.
auto& originalInfo = originalIter->second;
if (originalInfo.requestsLeft == 1) {
activeOriginals.erase(info.original);
} else {
originalInfo.requestsLeft--;
}
auto& info = iter->second;
assert(info.requests > 0);
// This is an original. Compute its effects when re-evaluated as a copy,
// noting idempotent effect separately.
EffectAnalyzer copyEffects(options, *getModule());
std::optional<EffectAnalyzer> idempotentCallEffects;
collectEffects(curr, copyEffects, idempotentCallEffects);

// We can ignore traps here, as we replace a repeating expression with a
// single appearance of it, a store to a local, and gets in the other
// locations, and so if the expression traps then the first appearance -
// that we keep around - would trap, and the others are never reached
// anyhow. (The other checks we perform here, including invalidation and
// determinism, will ensure that either all of the appearances trap, or
// none of them.)
copyEffects.trap = false;

if (idempotentCallEffects &&
idempotentCallEffects->orderedBefore(copyEffects)) {
// An idempotent call inside ORIGINAL may have side effects on its first
// call that interfere with re-evaluating the rest of the expression later
// (e.g., (call $idempotent (global.get $mutable))), so we cannot
// deduplicate it.
requestInfos.erase(iter);
return;
}

// Note that we've already checked above that this has no side effects or
// generativity: if we got here, then it is good to go from the
// perspective of this expression itself (but may be invalidated by other
// code in between, see above).
activeOriginals.emplace(
curr, ActiveOriginalInfo{info.requests, std::move(copyEffects)});
}

static void doNoteNonLinear(Checker* self, Expression** currp) {
Expand Down
Loading
Loading