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
13 changes: 2 additions & 11 deletions src/ir/ReFinalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#include "ir/eh-utils.h"
#include "ir/utils.h"

namespace wasm {
Expand Down Expand Up @@ -140,10 +139,7 @@ void ReFinalize::visitRethrow(Rethrow* curr) { curr->finalize(); }
void ReFinalize::visitThrowRef(ThrowRef* curr) { curr->finalize(); }
void ReFinalize::visitNop(Nop* curr) { curr->finalize(); }
void ReFinalize::visitUnreachable(Unreachable* curr) { curr->finalize(); }
void ReFinalize::visitPop(Pop* curr) {
curr->finalize();
seenPop = true;
}
void ReFinalize::visitPop(Pop* curr) { curr->finalize(); }
void ReFinalize::visitTupleMake(TupleMake* curr) { curr->finalize(); }
void ReFinalize::visitTupleExtract(TupleExtract* curr) { curr->finalize(); }
void ReFinalize::visitRefI31(RefI31* curr) { curr->finalize(); }
Expand Down Expand Up @@ -224,11 +220,7 @@ void ReFinalize::visitDataSegment(DataSegment* curr) {
WASM_UNREACHABLE("unimp");
}
void ReFinalize::visitTag(Tag* curr) { WASM_UNREACHABLE("unimp"); }
void ReFinalize::visitFunction(Function* curr) {
if (seenPop && addedBlock) {
EHUtils::handleBlockNestedPops(curr, *getModule());
}
}
void ReFinalize::visitFunction(Function* curr) {}
void ReFinalize::visitModule(Module* curr) { WASM_UNREACHABLE("unimp"); }

void ReFinalize::updateBreakValueType(Name name, Type type) {
Expand All @@ -251,7 +243,6 @@ void ReFinalize::replaceUntaken(Expression* value, Expression* otherChild) {
otherChild = builder.makeDrop(otherChild);
}
replaceCurrent(builder.makeBlock({value, otherChild}, Type::unreachable));
addedBlock = true;
} else if (value) {
replaceCurrent(value);
} else {
Expand Down
26 changes: 10 additions & 16 deletions src/ir/eh-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

#include "ir/eh-utils.h"
#include "ir/branch-utils.h"
#include "ir/find_all.h"
#include "ir/iteration.h"
#include "ir/properties.h"
#include "wasm-builder.h"

namespace wasm {

Expand All @@ -37,9 +39,6 @@ getFirstPop(Expression* catchBody, bool& isPopNested, Expression**& popPtr) {
Expression* firstChild = catchBody;
isPopNested = false;
popPtr = nullptr;
// When there are multiple expressions within a catch body, an implicit
// block is created within it for convenience purposes.
auto* implicitBlock = catchBody->dynCast<Block>();

// Go down the line for the first child until we reach a leaf. A pop should be
// in that first-descendant line.
Expand All @@ -64,24 +63,19 @@ getFirstPop(Expression* catchBody, bool& isPopNested, Expression**& popPtr) {
// run more than once
return nullptr;
}
if (firstChild->is<Block>()) {
// If there are no branches that targets the implicit block, it will be
// removed when written back. But if there are branches that target the
// implicit block,
if (auto* block = firstChild->dynCast<Block>()) {
// An unnamed block will be removed when written back. But if the block
// has a name,
// (catch $e
// (block $l0
// (pop i32) ;; within a block!
// (br $l0)
// ...
// )
// This cannot be removed, so this is considered a nested pop (which we
// should fix).
if (firstChild == implicitBlock) {
if (BranchUtils::BranchSeeker::has(implicitBlock,
implicitBlock->name)) {
isPopNested = true;
}
} else {
// )
// It may be emitted in the binary format, so this is considered a
// nested pop (which we should fix).
if (block->name.is()) {
isPopNested = true;
}
} else if (firstChild->is<Try>() || firstChild->is<TryTable>()) {
Expand Down
4 changes: 0 additions & 4 deletions src/ir/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ struct ReFinalize
// Replace an untaken branch/switch with an unreachable value.
// A condition may also exist and may or may not be unreachable.
void replaceUntaken(Expression* value, Expression* condition);

// If we see a pop and also add a block, we need to do EH fixups.
bool seenPop = false;
bool addedBlock = false;
};

// Re-finalize a single node. This is slow, if you want to refinalize
Expand Down
11 changes: 0 additions & 11 deletions src/passes/AbstractTypeRefining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <memory>

#include "ir/drop.h"
#include "ir/eh-utils.h"
#include "ir/localize.h"
#include "ir/module-utils.h"
#include "ir/subtypes.h"
Expand Down Expand Up @@ -320,7 +319,6 @@ struct AbstractTypeRefining : public Pass {

struct Preoptimizer : WalkerPass<PostWalker<Preoptimizer>> {
const TypeMapper::TypeUpdates& mapping;
bool addedBlock = false;

Preoptimizer(const TypeMapper::TypeUpdates& mapping) : mapping(mapping) {}

Expand All @@ -331,7 +329,6 @@ struct AbstractTypeRefining : public Pass {
}

Block* localizeChildren(Expression* curr) {
addedBlock = true;
return ChildLocalizer(
curr, getFunction(), *getModule(), getPassOptions())
.getChildrenReplacement();
Expand Down Expand Up @@ -420,7 +417,6 @@ struct AbstractTypeRefining : public Pass {
// optimize to. In this case it must trap.
replaceCurrent(builder.makeSequence(builder.makeDrop(curr->ref),
builder.makeUnreachable()));
addedBlock = true;
} else {
// The descriptor is abstract, but it has a subtype that is not
// (which we want to optimize to). We know this will only succeed if
Expand Down Expand Up @@ -490,17 +486,10 @@ struct AbstractTypeRefining : public Pass {
if (getFunction()) {
replaceCurrent(getDroppedChildrenAndAppend(
curr, *getModule(), getPassOptions(), builder.makeUnreachable()));
addedBlock = true;
} else {
curr->desc = builder.makeRefNull(HeapType::none);
}
}

void visitFunction(Function* func) {
if (addedBlock) {
EHUtils::handleBlockNestedPops(func, *getModule());
}
}
} preoptimizer(mapping);
preoptimizer.run(getPassRunner(), &module);
preoptimizer.runOnModuleCode(getPassRunner(), &module);
Expand Down
10 changes: 0 additions & 10 deletions src/passes/CodeFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@

#include "ir/branch-utils.h"
#include "ir/effects.h"
#include "ir/eh-utils.h"
#include "ir/find_all.h"
#include "ir/iteration.h"
#include "ir/label-utils.h"
Expand Down Expand Up @@ -119,9 +118,6 @@ struct CodeFolding

// Set when we optimized and believe another pass is warranted.
bool anotherPass;
// Set when we optimized in a manner that requires EH fixups specifically,
// which is generally the case when we wrap things in a block.
bool needEHFixups;

// pass state

Expand Down Expand Up @@ -286,7 +282,6 @@ struct CodeFolding
anotherPass = true;
while (anotherPass) {
anotherPass = false;
needEHFixups = false;
Super::doWalkFunction(func);
optimizeTerminatingTails(unreachableTails);
// optimize returns at the end, so we can benefit from a fallthrough if
Expand All @@ -301,9 +296,6 @@ struct CodeFolding
unoptimizables.clear();
modifieds.clear();
exitingBranchCache.clear();
if (needEHFixups) {
EHUtils::handleBlockNestedPops(func, *getModule());
}
}
}

Expand Down Expand Up @@ -629,7 +621,6 @@ struct CodeFolding
// ensure the replacement has the same type, so the outside is not surprised
block->finalize(oldType);
replaceCurrent(block);
needEHFixups = true;
}

// optimize tails that terminate control flow in this function, so we
Expand Down Expand Up @@ -899,7 +890,6 @@ struct CodeFolding
// ensure the replacement has the same type, so the outside is not surprised
outer->finalize(getFunction()->getResults());
getFunction()->body = outer;
needEHFixups = true;
return true;
}

Expand Down
2 changes: 0 additions & 2 deletions src/passes/ConstraintAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
#include "cfg/wto.h"
#include "ir/constraint.h"
#include "ir/drop.h"
#include "ir/eh-utils.h"
#include "ir/literal-utils.h"
#include "ir/local-graph.h"
#include "ir/properties.h"
Expand Down Expand Up @@ -390,7 +389,6 @@ struct ConstraintAnalysis

if (refinalize) {
ReFinalize().walkFunctionInModule(getFunction(), getModule());
EHUtils::handleBlockNestedPops(getFunction(), *getModule());
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/passes/DeadArgumentElimination2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

#include "analysis/lattices/bool.h"
#include "ir/effects.h"
#include "ir/eh-utils.h"
#include "ir/intrinsics.h"
#include "ir/label-utils.h"
#include "ir/local-graph.h"
Expand Down Expand Up @@ -1113,9 +1112,6 @@ struct Optimizer

assert(!hasRemovedResult || !func->body->type.isConcrete());

// We may have moved pops around. Fix them.
EHUtils::handleBlockNestedPops(func, *wasm);

// We may have introduced a new non-nullable local whose sets do not
// structurally dominate its gets. Fix it.
if (hasNewNonNullableLocal) {
Expand Down
19 changes: 0 additions & 19 deletions src/passes/DeadCodeElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
// have no side effects.
//

#include "ir/eh-utils.h"
#include "ir/iteration.h"
#include "ir/properties.h"
#include "ir/type-updating.h"
Expand Down Expand Up @@ -56,10 +55,6 @@ struct DeadCodeElimination
// as we remove code, we must keep the types of other nodes valid
TypeUpdater typeUpdater;

// Information used to decide whether we need EH fixups at the end
bool hasPop = false; // Do we have a 'pop' in this function?
bool addedBlock = false; // Have we added blocks in this function?

Expression* replaceCurrent(Expression* expression) {
auto* old = getCurrent();
if (old == expression) {
Expand All @@ -77,10 +72,6 @@ struct DeadCodeElimination
}

void visitExpression(Expression* curr) {
if (curr->is<Pop>()) {
hasPop = true;
}

if (!Properties::isControlFlowStructure(curr)) {
// Control flow structures require special handling, but others are
// simple.
Expand Down Expand Up @@ -113,7 +104,6 @@ struct DeadCodeElimination
if (remainingChildren.size() == 1) {
replaceCurrent(remainingChildren[0]);
} else {
addedBlock = true;
replaceCurrent(builder.makeBlock(remainingChildren));
}
}
Expand Down Expand Up @@ -195,15 +185,6 @@ struct DeadCodeElimination
WASM_UNREACHABLE("unimplemented DCE control flow structure");
}
}

void visitFunction(Function* curr) {
// We conservatively run the EH pop fixup if this function has a 'pop' and
// if we have ever added blocks in the optimization, which may have moved
// pops into the blocks.
if (hasPop && addedBlock) {
EHUtils::handleBlockNestedPops(curr, *getModule());
}
}
};

Pass* createDeadCodeEliminationPass() { return new DeadCodeElimination(); }
Expand Down
13 changes: 0 additions & 13 deletions src/passes/Directize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include "call-utils.h"
#include "ir/drop.h"
#include "ir/eh-utils.h"
#include "ir/find_all.h"
#include "ir/table-utils.h"
#include "ir/utils.h"
Expand All @@ -53,8 +52,6 @@ struct FunctionDirectizer : public WalkerPass<PostWalker<FunctionDirectizer>> {

FunctionDirectizer(const TableUtils::TableInfoMap& tables) : tables(tables) {}

bool optimized = false;

void visitCallIndirect(CallIndirect* curr) {
auto& table = tables.at(curr->table);
if (!table.canOptimizeByEntry()) {
Expand All @@ -65,7 +62,6 @@ struct FunctionDirectizer : public WalkerPass<PostWalker<FunctionDirectizer>> {
std::vector<Expression*> operands(curr->operands.begin(),
curr->operands.end());
makeDirectCall(operands, curr->target, table, curr);
optimized = true;
return;
}

Expand All @@ -78,25 +74,16 @@ struct FunctionDirectizer : public WalkerPass<PostWalker<FunctionDirectizer>> {
*getFunction(),
*getModule())) {
replaceCurrent(calls);
optimized = true;
// Note that types may have changed, as the utility here can add locals
// which require fixups if they are non-nullable, for example.
changedTypes = true;
return;
}
}

bool hasTry = false;

void visitTry(Try* curr) { hasTry = true; }

void doWalkFunction(Function* func) {
WalkerPass<PostWalker<FunctionDirectizer>>::doWalkFunction(func);

if (optimized && hasTry) {
EHUtils::handleBlockNestedPops(func, *getModule());
}

if (changedTypes) {
ReFinalize().walkFunctionInModule(func, getModule());
}
Expand Down
5 changes: 0 additions & 5 deletions src/passes/Flatten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

#include <ir/branch-utils.h>
#include <ir/effects.h>
#include <ir/eh-utils.h>
#include <ir/flat.h>
#include <ir/properties.h>
#include <ir/utils.h>
Expand Down Expand Up @@ -376,10 +375,6 @@ struct Flatten
}
// the body may have preludes
curr->body = getPreludesWithExpression(originalBody, curr->body);

// Flatten can generate blocks within 'catch', making pops invalid. Fix them
// up.
EHUtils::handleBlockNestedPops(curr, *getModule());
}

private:
Expand Down
4 changes: 0 additions & 4 deletions src/passes/GUFA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
//

#include "ir/drop.h"
#include "ir/eh-utils.h"
#include "ir/possible-contents.h"
#include "ir/properties.h"
#include "ir/utils.h"
Expand Down Expand Up @@ -318,9 +317,6 @@ struct GUFAOptimizer
return;
}

// We may add blocks around pops, which we must fix up.
EHUtils::handleBlockNestedPops(func, *getModule());

// If we are in "optimizing" mode, we'll also run some more passes on this
// function that we just optimized. If not, leave now.
if (!optimizing) {
Expand Down
Loading
Loading