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
5 changes: 5 additions & 0 deletions src/wasm-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ class StackIRToBinaryWriter {
};

// Stack IR optimizer
struct LazyLocalGraph;

class StackIROptimizer {
Function* func;
StackIR& insts;
Expand All @@ -683,6 +685,9 @@ class StackIROptimizer {
bool isControlFlow(StackInst* inst);
void removeAt(Index i);
Index getNumConsumedValues(StackInst* inst);
bool isSingleUseSetGetPair(Index setIndex,
Index getIndex,
LazyLocalGraph& localGraph);
bool canRemoveSetGetPair(Index setIndex, Index getIndex);
bool isConsumedBySymmetricOp(Index getIndex);
std::unordered_set<LocalGet*> findStringViewDeferredGets();
Expand Down
152 changes: 119 additions & 33 deletions src/wasm/wasm-stack-opts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,30 @@
// stack at that location.
const Index null = -1;
std::vector<Index> values;
// We also maintain a stack of values vectors for control flow,
// saving the stack as we enter and restoring it when we exit.
std::vector<std::vector<Index>> savedValues;
// We also maintain a stack of active control-flow scopes, saving the outer
// value stack as we enter and restoring it when we exit, while recording any
// LocalSet values threaded across BlockBegin boundaries as block input
// parameters.
struct BlockScope {
// An outer LocalSet value threaded into this block as an input parameter.
struct ParamSet {
// Index into `insts` of the eliminated LocalSet, used to sort parameters
// into the order their values were pushed onto the stack.
Index setIndex;
// Value type of the parameter.
Type type;
};

// Index into `insts` of the control-flow begin instruction, used to test
// whether this scope was entered after a candidate LocalSet and to update
// `insts[beginInstIndex]->params` on exit.
Index beginInstIndex;
// Outer value stack saved on scope entry and restored on scope exit.
std::vector<Index> savedValues;
// Eliminated outer LocalSets threaded across this block boundary.
SmallVector<ParamSet, 2> paramSets;
};
std::vector<BlockScope> scopeStack;
#if STACK_OPT_DEBUG
std::cout << "func: " << func->name << '\n' << insts << '\n';
#endif
Expand Down Expand Up @@ -214,13 +235,45 @@
// After consuming, we can see what to do with this. First, handle
// control flow.
if (isControlFlowBegin(inst)) {
// Save the stack for when we end this control flow.
savedValues.push_back(values); // TODO: optimize copies
values.assign(inst->params.size(), null);
assert(inst->params == Type::none);
std::vector<Index> innerValues;
if (features.hasMultivalue() && inst->op == StackInst::BlockBegin) {

Check warning on line 240 in src/wasm/wasm-stack-opts.cpp

View workflow job for this annotation

GitHub Actions / coverage

Uncovered MC/DC condition

Decision `features.hasMultivalue() && inst->op == StackInst::BlockBegin` -> missing MC/DC independence pair for C1

Check warning on line 240 in src/wasm/wasm-stack-opts.cpp

View workflow job for this annotation

GitHub Actions / coverage

One-sided branch in diff

Branch `features.hasMultivalue()` -> missing FALSE branch (True: 341x, False: 0x)
// Initialize `innerValues` with the contiguous suffix of candidate
// LocalSets at the top of the outer stack so inner LocalGets can thread
// them across `BlockBegin` as block input parameters. Stop at the first
// `null` (which represents an actual unnamed value already on the Wasm
// value stack) because block parameters are popped from the top of the
// stack at `BlockBegin` and cannot reach underneath an unconsumed stack
// value.
size_t start = values.size();
while (start > 0 && values[start - 1] != null) {
start--;
}
innerValues.assign(values.begin() + start, values.end());
}
scopeStack.push_back({instIndex, std::move(values), {}});
values = std::move(innerValues);
} else if (isControlFlowEnd(inst)) {
assert(!savedValues.empty());
values = savedValues.back();
savedValues.pop_back();
assert(!scopeStack.empty());
auto scope = std::move(scopeStack.back());
scopeStack.pop_back();
if (!scope.paramSets.empty()) {
// Inner LocalGets can be optimized in reverse stack order when consumed
// by a symmetric operation, so sort by `setIndex` to match the order
// the parameter values were pushed before `BlockBegin`.
std::sort(scope.paramSets.begin(),
scope.paramSets.end(),
[](const auto& lhs, const auto& rhs) {
return lhs.setIndex < rhs.setIndex;
});
std::vector<Type> paramTypes;
paramTypes.reserve(scope.paramSets.size());
for (const auto& param : scope.paramSets) {
paramTypes.push_back(param.type);
}
insts[scope.beginInstIndex]->params = Type(paramTypes);
}
values = std::move(scope.savedValues);
} else if (isControlFlow(inst)) {
// Otherwise, in the middle of control flow, just clear it
values.clear();
Expand Down Expand Up @@ -262,34 +315,48 @@
continue;
}
auto* set = insts[setIndex]->origin->cast<LocalSet>();
if (set->index == get->index) {
// This might be a proper set-get pair, where the set is
// used by this get and nothing else, check that.
auto& sets = localGraph.getSets(get);
if (sets.size() == 1 && *sets.begin() == set) {
auto& setInfluences = localGraph.getSetInfluences(set);
// If this has the proper value of 1, also do the potentially-
// expensive check of whether we can remove this pair at all.
if (setInfluences.size() == 1 &&
canRemoveSetGetPair(setIndex, getIndex)) {
assert(*setInfluences.begin() == get);
// Do it! The set and the get can go away, the proper
// value is on the stack.
if (set->index == get->index &&
isSingleUseSetGetPair(setIndex, getIndex, localGraph)) {
// Do it! The set and the get can go away, the proper
// value is on the stack.
#if STACK_OPT_DEBUG
std::cout << " stackify the get\n";
std::cout << " stackify the get\n";
#endif
insts[setIndex] = nullptr;
insts[getIndex] = nullptr;
// Continuing on from here, replace this on the stack
// with a null, representing a regular value. We
// keep possible values above us active - they may
// be optimized later, as they would be pushed after
// us, and used before us, so there is no conflict.
values[j] = null;
optimized = true;
break;
insts[setIndex] = nullptr;
insts[getIndex] = nullptr;
// Continuing on from here, replace this on the stack
// with a null, representing a regular value. We
// keep possible values above us active - they may
// be optimized later, as they would be pushed after
// us, and used before us, so there is no conflict.
values[j] = null;
// Record the new block parameter on each crossed block, scanning
// from innermost to outermost since `beginInstIndex` increases
// monotonically along `scopeStack`.
for (auto scopeIt = scopeStack.rbegin();
scopeIt != scopeStack.rend() &&
scopeIt->beginInstIndex > setIndex;
++scopeIt) {
assert(insts[scopeIt->beginInstIndex]->op ==
StackInst::BlockBegin);
scopeIt->paramSets.push_back({setIndex, get->type});
auto savedIt = std::find(scopeIt->savedValues.begin(),
scopeIt->savedValues.end(),
setIndex);
if (savedIt != scopeIt->savedValues.end()) {
// `setIndex` is not necessarily at the back of `savedValues`:
// any candidate sets pushed after `setIndex` sit above this
// value on the outer stack at `BlockBegin` and would be
// consumed by the block if left on the stack, so erase
// `setIndex` and everything above it. (`savedIt` may already
// have been erased if an even deeper outer set was threaded
// earlier in this block.)
scopeIt->savedValues.erase(savedIt,
scopeIt->savedValues.end());
}
}
optimized = true;
break;
}
// We failed here. Can we look some more?
if (j == 0) {
Expand Down Expand Up @@ -493,6 +560,25 @@
return ChildIterator(inst->origin).children.size();
}

// Check that `set` at `setIndex` and `get` at `getIndex` form a single-use
// def-use pair whose removal preserves non-nullable local validation.
bool StackIROptimizer::isSingleUseSetGetPair(Index setIndex,
Index getIndex,
LazyLocalGraph& localGraph) {
auto* set = insts[setIndex]->origin->cast<LocalSet>();
auto* get = insts[getIndex]->origin->cast<LocalGet>();
auto& sets = localGraph.getSets(get);
if (sets.size() != 1 || *sets.begin() != set) {

Check warning on line 571 in src/wasm/wasm-stack-opts.cpp

View workflow job for this annotation

GitHub Actions / coverage

One-sided branch in diff

Branch `*sets.begin() != set` -> missing TRUE branch (True: 0x, False: 190x)
return false;
}
auto& setInfluences = localGraph.getSetInfluences(set);
if (setInfluences.size() != 1) {
return false;
}
assert(*setInfluences.begin() == get);
return canRemoveSetGetPair(setIndex, getIndex);
}

// Given a pair of a local.set and local.get, see if we can remove them
// without breaking validation. Specifically, we must keep sets of non-
// nullable locals that dominate a get until the end of the block, such as
Expand Down
106 changes: 0 additions & 106 deletions test/gtest/printing.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ir/module-utils.h"
#include "pass.h"
#include "print-test.h"
#include "wasm-binary.h"
#include "wasm-stack.h"
#include "wasm.h"

Expand Down Expand Up @@ -154,108 +153,3 @@ TEST_F(PrintingTest, StackIRParams) {
EXPECT_TRUE(indexedTypes.indices.contains(
Signature(Type({Type::i32, Type::f64}), Type::i32)));
}

TEST_F(PrintingTest, StackIRParamBinaryAndOptimizer) {
auto moduleText = R"wasm(
(module
(func $id (param $x i32) (result i32)
(local $tmp i32)
(local.set $tmp (local.get $x))
(block $b (result i32)
(i32.add
(local.get $tmp)
(i32.const 1)
)
)
)
)
)wasm";

Module wasm;
wasm.features = FeatureSet::All;
parseWast(wasm, moduleText);

PassOptions options;
options.optimizeLevel = 3;
ModuleStackIR moduleStackIR(wasm, options);
auto* func = wasm.getFunction("id");
auto* stackIR = moduleStackIR.getStackIROrNull(func);
ASSERT_NE(stackIR, nullptr);

// Build a StackIR sequence where $b takes an i32 param from the stack and
// adds it to a local.get inside the block:
// local.get $x
// local.set $tmp
// i32.const 10 ;; consumed as block param
// block $b (param i32) (result i32)
// local.get $tmp
// i32.add ;; consumes block param + $tmp
// i32.const 0
// br_if $b
// end
Builder irBuilder(wasm);
auto* getX = irBuilder.makeLocalGet(0, Type::i32);
auto* setTmp = irBuilder.makeLocalSet(1, getX);
auto* c10 = irBuilder.makeConst(Literal(int32_t(10)));
auto* getTmp = irBuilder.makeLocalGet(1, Type::i32);
auto* add = irBuilder.makeBinary(AddInt32, c10, getTmp);
auto* c0 = irBuilder.makeConst(Literal(int32_t(0)));
auto* brIf = irBuilder.makeBreak("b", add, c0);
auto* block = irBuilder.makeBlock("b", {brIf}, Type::i32);
func->body = irBuilder.makeSequence(setTmp, block);

auto makeInst = [&](StackInst::Op op, Expression* origin) {
auto* inst = wasm.allocator.alloc<StackInst>();
inst->op = op;
inst->origin = origin;
inst->type = origin->type;
return inst;
};

auto* blockBegin = makeInst(StackInst::BlockBegin, block);
blockBegin->params = Type::i32;
auto* blockEnd = makeInst(StackInst::BlockEnd, block);

*stackIR = {
makeInst(StackInst::Basic, getX),
makeInst(StackInst::Basic, setTmp),
makeInst(StackInst::Basic, c10),
blockBegin,
makeInst(StackInst::Basic, getTmp),
makeInst(StackInst::Basic, add),
makeInst(StackInst::Basic, c0),
makeInst(StackInst::Basic, brIf),
blockEnd,
};

// Run StackIROptimizer: local2Stack must see that blockBegin consumes c10 and
// pushes 1 parameter inside the block, so setTmp/getTmp cannot be eliminated
// across the block boundary, and i32.add consumes both the block param and
// getTmp without stack underflow.
StackIROptimizer optimizer(func, *stackIR, options, wasm.features);
optimizer.run();
EXPECT_NE((*stackIR)[1], nullptr);
EXPECT_NE((*stackIR)[4], nullptr);

// Also test binary emission via StackIRToBinaryWriter: the block header must
// emit the signature index for (param i32) (result i32).
BufferWithRandomAccess moduleBuf;
WasmBinaryWriter binaryWriter(&wasm, moduleBuf, options);
BufferWithRandomAccess funcBuf;
StackIRToBinaryWriter stackWriter(binaryWriter, funcBuf, func, *stackIR);
stackWriter.write();

// Verify the emitted bytes contain BinaryConsts::Block followed by the
// signature index of (param i32) (result i32).
auto sigIndex =
binaryWriter.getSignatureIndex(Signature(Type::i32, Type::i32));
bool foundParameterizedBlock = false;
for (size_t i = 0; i + 1 < funcBuf.size(); ++i) {
if (funcBuf[i] == uint8_t(BinaryConsts::Block) &&
funcBuf[i + 1] == uint8_t(sigIndex)) {
foundParameterizedBlock = true;
break;
}
}
EXPECT_TRUE(foundParameterizedBlock);
}
Loading
Loading