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
60 changes: 60 additions & 0 deletions src/passes/SpillPointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,70 @@

#include "abi/stack.h"
#include "cfg/liveness-traversal.h"
#include "ir/iteration.h"
#include "ir/type-updating.h"
#include "pass.h"
#include "wasm-builder.h"
#include "wasm.h"

namespace wasm {

// Make values held on the operand stack across calls visible to local liveness
// analysis. Only expressions with a possible pointer before a calling operand
// need to be rewritten, rather than flattening the entire function.
struct LocalizePointerOperands
: PostWalker<LocalizePointerOperands,
UnifiedExpressionVisitor<LocalizePointerOperands>> {
Type pointerType;
std::unordered_set<Expression*> hasCalls;

LocalizePointerOperands(Type pointerType) : pointerType(pointerType) {}

void visitExpression(Expression* curr) {
bool calls = curr->is<Call>() || curr->is<CallIndirect>();
for (auto* child : ChildIterator(curr)) {
calls |= hasCalls.contains(child);
}
if (calls) {
hasCalls.insert(curr);
}

bool pointer = false;
bool needsLocals = false;
ValueChildIterator children(curr);
for (auto* child : children) {
if (!child->type.isConcrete()) {
// The parent is never reached, so earlier operand values are not used.
return;
}
needsLocals |= pointer && hasCalls.contains(child);
pointer |= child->type == pointerType;
}
if (!needsLocals) {
return;
}

Builder builder(*getModule());
auto* block = builder.makeBlock();
for (auto*& child : children) {
// Move all operands in order, including non-pointers, to preserve their
// effects. Use fresh locals even for local.get: a later operand may write
// to its source local while the old value is still on the operand stack.
auto type = child->type;
auto temp = builder.addVar(
getFunction(),
TypeUpdating::getValidLocalType(type, getModule()->features));
block->list.push_back(builder.makeLocalSet(temp, child));
child = TypeUpdating::fixLocalGet(builder.makeLocalGet(temp, type),
*getModule());
}
block->list.push_back(curr);
block->finalize();
hasCalls.insert(block);
replaceCurrent(block);
}
};

struct SpillPointers
: public WalkerPass<LivenessWalker<SpillPointers, Visitor<SpillPointers>>> {
bool isFunctionParallel() override { return true; }
Expand Down Expand Up @@ -69,6 +127,8 @@ struct SpillPointers
// main entry point

void doWalkFunction(Function* func) {
LocalizePointerOperands(getModule()->memories[0]->addressType)
.walkFunctionInModule(func, getModule());
Super::doWalkFunction(func);
spillPointers();
}
Expand Down
304 changes: 304 additions & 0 deletions test/lit/passes/spill-pointers-stack.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.

;; RUN: wasm-opt %s --spill-pointers -S -o - | filecheck %s

(module
;; CHECK: (type $0 (func))

;; CHECK: (type $1 (func (result i32)))

;; CHECK: (type $ii (func (param i32 i32)))
(type $ii (func (param i32 i32)))

;; CHECK: (type $3 (func (param i32)))

;; CHECK: (type $4 (func (result f64)))

;; CHECK: (type $5 (func (param f64 i32)))

;; CHECK: (import "env" "make" (func $make (result i32)))
(import "env" "make" (func $make (result i32)))
;; CHECK: (import "env" "collect" (func $collect (result i32)))
(import "env" "collect" (func $collect (result i32)))
;; CHECK: (import "env" "use" (func $use (param i32 i32)))
(import "env" "use" (func $use (param i32 i32)))
;; CHECK: (import "env" "make-f64" (func $make-f64 (result f64)))
(import "env" "make-f64" (func $make-f64 (result f64)))
;; CHECK: (import "env" "use-f64" (func $use-f64 (param f64 i32)))
(import "env" "use-f64" (func $use-f64 (param f64 i32)))

(memory 1)
(table 1 1 funcref)
;; CHECK: (global $__stack_pointer (mut i32) (i32.const 65536))
(global $__stack_pointer (mut i32) (i32.const 65536))

;; CHECK: (memory $0 1)

;; CHECK: (table $0 1 1 funcref)

;; CHECK: (func $spill-call-operand
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (local.tee $2
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (global.get $__stack_pointer)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (call $make)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $collect)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $use
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $spill-call-operand
;; The result of $make waits on the operand stack while $collect runs, so we
;; must spill it.
(call $use
(call $make)
(call $collect)
)
)

;; CHECK: (func $spill-const-operand
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (local.tee $2
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (global.get $__stack_pointer)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $collect)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $use
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $spill-const-operand
;; The i32 constant might be a pointer, so we must spill it across $collect.
(call $use
(i32.const 42)
(call $collect)
)
)

;; CHECK: (func $spill-overwritten-local-operand (param $p i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (local $3 i32)
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (local.tee $3
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (global.get $__stack_pointer)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (local.get $p)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $2
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (local.set $p
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (i32.store offset=4
;; CHECK-NEXT: (local.get $3)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $collect)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $use
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $3)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $spill-overwritten-local-operand (param $p i32)
;; $p is read onto the operand stack before a later operand overwrites $p
;; and calls $collect, so the original pointer value must be captured and
;; spilled.
(call $use
(local.get $p)
(block (result i32)
(local.set $p (i32.const 0))
(call $collect)
)
)
)

;; CHECK: (func $spill-binary-operand
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (local.tee $2
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (global.get $__stack_pointer)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (call $make)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $collect)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $spill-binary-operand
;; Non-call expressions can also hold a pointer on the operand stack across
;; a call in a later operand.
(drop
(i32.add
(call $make)
(call $collect)
)
)
)

;; CHECK: (func $spill-indirect-target (param $p i32)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local $2 i32)
;; CHECK-NEXT: (local $3 i32)
;; CHECK-NEXT: (local $4 i32)
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (local.tee $4
;; CHECK-NEXT: (i32.sub
;; CHECK-NEXT: (global.get $__stack_pointer)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (block
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (local.get $p)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $2
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $3
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (i32.store offset=4
;; CHECK-NEXT: (local.get $4)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.store offset=8
;; CHECK-NEXT: (local.get $4)
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (call $collect)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (call_indirect (type $ii)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: (local.get $2)
;; CHECK-NEXT: (local.get $3)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (global.set $__stack_pointer
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: (local.get $4)
;; CHECK-NEXT: (i32.const 16)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $spill-indirect-target (param $p i32)
;; call_indirect evaluates its target operand last, so earlier pointer
;; operands must be spilled across a call in the target expression.
(call_indirect (type $ii)
(local.get $p)
(i32.const 0)
(call $collect)
)
)

;; CHECK: (func $no-spill-non-pointer-operand
;; CHECK-NEXT: (call $use-f64
;; CHECK-NEXT: (call $make-f64)
;; CHECK-NEXT: (call $collect)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $no-spill-non-pointer-operand
;; An f64 operand cannot be a pointer, so nothing needs to be spilled.
(call $use-f64
(call $make-f64)
(call $collect)
)
)
)