From 465664d8585f4d530234c0fa8a1727f5eb6eaa64 Mon Sep 17 00:00:00 2001 From: Asuka <148060537+akasakariko@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:11:06 +0800 Subject: [PATCH 1/2] Spill operand stack pointers across calls --- src/abi/stack.h | 45 ++++- src/passes/SpillPointers.cpp | 60 ++++++ test/passes/spill-pointers.passes | 1 + test/passes/spill-pointers.txt | 293 ++++++++++++++++++++++++++---- test/passes/spill-pointers.wast | 40 ++++ 5 files changed, 397 insertions(+), 42 deletions(-) create mode 100644 test/passes/spill-pointers.passes diff --git a/src/abi/stack.h b/src/abi/stack.h index ae488e8c2a2..2196a176b33 100644 --- a/src/abi/stack.h +++ b/src/abi/stack.h @@ -76,7 +76,11 @@ getStackSpace(Index local, Function* func, Index size, Module& wasm) { FindAllPointers finder(func->body); for (auto** ptr : finder.list) { auto* ret = (*ptr)->cast(); - if (ret->value && ret->value->type != Type::unreachable) { + if (ret->value && ret->value->type == Type::unreachable) { + // The return is not reached. Any exits in its value restore the stack. + continue; + } + if (ret->value) { // handle the returned value auto* block = builder.makeBlock(); auto temp = builder.addVar(func, ret->value->type); @@ -91,13 +95,50 @@ getStackSpace(Index local, Function* func, Index size, Module& wasm) { *ptr = builder.makeSequence(makeStackRestore(), ret); } } + // Tail calls leave the function after evaluating their operands. Keep the + // spill area visible until the last operand has finished, then restore it + // before transferring control to the callee. + auto restoreBeforeTailCall = [&](Expression** ptr, + bool isReturn, + Expression** lastOperand) { + if (!isReturn) { + return; + } + if (!lastOperand) { + *ptr = builder.makeSequence(makeStackRestore(), *ptr); + return; + } + auto type = (*lastOperand)->type; + if (type == Type::unreachable) { + return; + } + auto temp = builder.addVar(func, type); + *lastOperand = builder.makeBlock({builder.makeLocalSet(temp, *lastOperand), + makeStackRestore(), + builder.makeLocalGet(temp, type)}); + }; + for (auto** ptr : FindAllPointers(func->body).list) { + auto* call = (*ptr)->cast(); + restoreBeforeTailCall(ptr, + call->isReturn, + call->operands.empty() ? nullptr + : &call->operands.back()); + } + for (auto** ptr : FindAllPointers(func->body).list) { + auto* call = (*ptr)->cast(); + restoreBeforeTailCall(ptr, call->isReturn, &call->target); + } + for (auto** ptr : FindAllPointers(func->body).list) { + auto* call = (*ptr)->cast(); + restoreBeforeTailCall(ptr, call->isReturn, &call->target); + } // add stack restores to the body if (func->body->type == Type::none) { block->list.push_back(func->body); block->list.push_back(makeStackRestore()); } else if (func->body->type == Type::unreachable) { block->list.push_back(func->body); - // no need to restore the old stack value, we're gone anyhow + // There is no fallthrough. Returns and tail calls restore the stack above. } else { // save the return value auto temp = builder.addVar(func, func->getResults()); diff --git a/src/passes/SpillPointers.cpp b/src/passes/SpillPointers.cpp index 50229f7e707..003df90b3c0 100644 --- a/src/passes/SpillPointers.cpp +++ b/src/passes/SpillPointers.cpp @@ -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> { + Type pointerType; + std::unordered_set hasCalls; + + LocalizePointerOperands(Type pointerType) : pointerType(pointerType) {} + + void visitExpression(Expression* curr) { + bool calls = curr->is() || curr->is(); + 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 && !child->is(); + } + 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>> { bool isFunctionParallel() override { return true; } @@ -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(); } diff --git a/test/passes/spill-pointers.passes b/test/passes/spill-pointers.passes new file mode 100644 index 00000000000..f74f9d20acd --- /dev/null +++ b/test/passes/spill-pointers.passes @@ -0,0 +1 @@ +spill-pointers_enable-tail-call diff --git a/test/passes/spill-pointers.txt b/test/passes/spill-pointers.txt index 98650041a3a..7c2870b54d5 100644 --- a/test/passes/spill-pointers.txt +++ b/test/passes/spill-pointers.txt @@ -1,9 +1,9 @@ (module (type $0 (func)) (type $1 (func (param i32) (result i32))) - (type $2 (func (result i32))) + (type $2 (func (param i32))) (type $ii (func (param i32 i32))) - (type $4 (func (param i32))) + (type $4 (func (result i32))) (type $5 (func (param f64))) (import "env" "STACKTOP" (global $STACKTOP$asm2wasm$import i32)) (import "env" "segfault" (func $segfault (param i32))) @@ -525,28 +525,20 @@ (local.get $x) ) (drop - (block - (global.set $__stack_pointer - (i32.add - (local.get $1) - (i32.const 16) + (return + (block + (local.set $2 + (i32.const 1) ) - ) - (return - (block - (local.set $2 - (i32.const 1) - ) - (global.set $__stack_pointer - (i32.add - (local.get $1) - (i32.const 16) - ) - ) - (return - (local.get $2) + (global.set $__stack_pointer + (i32.add + (local.get $1) + (i32.const 16) ) ) + (return + (local.get $2) + ) ) ) ) @@ -679,6 +671,235 @@ ) ) ) + (func $spill-stack-operand + (local $0 i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (global.set $__stack_pointer + (local.tee $2 + (i32.sub + (global.get $__stack_pointer) + (i32.const 16) + ) + ) + ) + (block + (local.set $0 + (call $spill-call-call0 + (i32.const 1) + ) + ) + (local.set $1 + (block (result i32) + (local.set $3 + (i32.const 2) + ) + (i32.store + (local.get $2) + (local.get $0) + ) + (call $spill-call-call0 + (local.get $3) + ) + ) + ) + (call $spill-args + (local.get $0) + (local.get $1) + ) + ) + (global.set $__stack_pointer + (i32.add + (local.get $2) + (i32.const 16) + ) + ) + ) + (func $spill-local-operand (param $p i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (global.set $__stack_pointer + (local.tee $3 + (i32.sub + (global.get $__stack_pointer) + (i32.const 16) + ) + ) + ) + (block + (local.set $1 + (local.get $p) + ) + (local.set $2 + (block (result i32) + (local.set $p + (i32.const 0) + ) + (block (result i32) + (local.set $4 + (i32.const 2) + ) + (i32.store offset=4 + (local.get $3) + (local.get $1) + ) + (call $spill-call-call0 + (local.get $4) + ) + ) + ) + ) + (call $spill-args + (local.get $1) + (local.get $2) + ) + ) + (global.set $__stack_pointer + (i32.add + (local.get $3) + (i32.const 16) + ) + ) + ) + (func $spill-stack-binary + (local $0 i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (global.set $__stack_pointer + (local.tee $2 + (i32.sub + (global.get $__stack_pointer) + (i32.const 16) + ) + ) + ) + (drop + (block (result i32) + (local.set $0 + (call $spill-call-call0 + (i32.const 1) + ) + ) + (local.set $1 + (block (result i32) + (local.set $3 + (i32.const 2) + ) + (i32.store + (local.get $2) + (local.get $0) + ) + (call $spill-call-call0 + (local.get $3) + ) + ) + ) + (i32.add + (local.get $0) + (local.get $1) + ) + ) + ) + (global.set $__stack_pointer + (i32.add + (local.get $2) + (i32.const 16) + ) + ) + ) + (func $spill-stack-indirect-target (param $p i32) + (local $1 i32) + (local $2 i32) + (local $3 i32) + (local $4 i32) + (local $5 i32) + (global.set $__stack_pointer + (local.tee $4 + (i32.sub + (global.get $__stack_pointer) + (i32.const 16) + ) + ) + ) + (block + (local.set $1 + (local.get $p) + ) + (local.set $2 + (i32.const 0) + ) + (local.set $3 + (block (result i32) + (local.set $5 + (i32.const 2) + ) + (i32.store offset=4 + (local.get $4) + (local.get $1) + ) + (i32.store offset=8 + (local.get $4) + (local.get $2) + ) + (call $spill-call-call0 + (local.get $5) + ) + ) + ) + (call_indirect (type $ii) + (local.get $1) + (local.get $2) + (local.get $3) + ) + ) + (global.set $__stack_pointer + (i32.add + (local.get $4) + (i32.const 16) + ) + ) + ) + (func $spill-tail-operands (param $x i32) (result i32) + (local $1 i32) + (local $2 i32) + (global.set $__stack_pointer + (local.tee $1 + (i32.sub + (global.get $__stack_pointer) + (i32.const 16) + ) + ) + ) + (return + (return_call $spill-call-call0 + (block (result i32) + (local.set $2 + (block (result i32) + (block + (i32.store + (local.get $1) + (local.get $x) + ) + (call $nothing) + ) + (local.get $x) + ) + ) + (global.set $__stack_pointer + (i32.add + (local.get $1) + (i32.const 16) + ) + ) + (local.get $2) + ) + ) + ) + ) ) (module (type $0 (func)) @@ -1210,28 +1431,20 @@ (local.get $x) ) (drop - (block - (global.set $__stack_pointer - (i32.add - (local.get $1) - (i32.const 16) + (return + (block + (local.set $2 + (i32.const 1) ) - ) - (return - (block - (local.set $2 - (i32.const 1) - ) - (global.set $__stack_pointer - (i32.add - (local.get $1) - (i32.const 16) - ) - ) - (return - (local.get $2) + (global.set $__stack_pointer + (i32.add + (local.get $1) + (i32.const 16) ) ) + (return + (local.get $2) + ) ) ) ) diff --git a/test/passes/spill-pointers.wast b/test/passes/spill-pointers.wast index de15ffb001a..42cfbbff1a7 100644 --- a/test/passes/spill-pointers.wast +++ b/test/passes/spill-pointers.wast @@ -168,6 +168,46 @@ ) (drop (local.get $x)) ) + (func $spill-stack-operand + (call $spill-args + (call $spill-call-call0 (i32.const 1)) + (call $spill-call-call0 (i32.const 2)) + ) + ) + (func $spill-local-operand (param $p i32) + (call $spill-args + (local.get $p) + (block (result i32) + (local.set $p (i32.const 0)) + (call $spill-call-call0 (i32.const 2)) + ) + ) + ) + (func $spill-stack-binary + (drop + (i32.add + (call $spill-call-call0 (i32.const 1)) + (call $spill-call-call0 (i32.const 2)) + ) + ) + ) + (func $spill-stack-indirect-target (param $p i32) + (call_indirect (type $ii) + (local.get $p) + (i32.const 0) + (call $spill-call-call0 (i32.const 2)) + ) + ) + (func $spill-tail-operands (param $x i32) (result i32) + (return + (return_call $spill-call-call0 + (block (result i32) + (call $nothing) + (local.get $x) + ) + ) + ) + ) ) (module From 8c2bb37fddb3e8bab6ca64470b40fa1f7a81c209 Mon Sep 17 00:00:00 2001 From: Asuka <148060537+akasakariko@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:01:13 +0800 Subject: [PATCH 2/2] Address review feedback on SpillPointers operand spilling --- src/abi/stack.h | 45 +--- src/passes/SpillPointers.cpp | 2 +- test/lit/passes/spill-pointers-stack.wast | 304 ++++++++++++++++++++++ test/passes/spill-pointers.passes | 1 - test/passes/spill-pointers.txt | 293 +++------------------ test/passes/spill-pointers.wast | 40 --- 6 files changed, 347 insertions(+), 338 deletions(-) create mode 100644 test/lit/passes/spill-pointers-stack.wast delete mode 100644 test/passes/spill-pointers.passes diff --git a/src/abi/stack.h b/src/abi/stack.h index 2196a176b33..ae488e8c2a2 100644 --- a/src/abi/stack.h +++ b/src/abi/stack.h @@ -76,11 +76,7 @@ getStackSpace(Index local, Function* func, Index size, Module& wasm) { FindAllPointers finder(func->body); for (auto** ptr : finder.list) { auto* ret = (*ptr)->cast(); - if (ret->value && ret->value->type == Type::unreachable) { - // The return is not reached. Any exits in its value restore the stack. - continue; - } - if (ret->value) { + if (ret->value && ret->value->type != Type::unreachable) { // handle the returned value auto* block = builder.makeBlock(); auto temp = builder.addVar(func, ret->value->type); @@ -95,50 +91,13 @@ getStackSpace(Index local, Function* func, Index size, Module& wasm) { *ptr = builder.makeSequence(makeStackRestore(), ret); } } - // Tail calls leave the function after evaluating their operands. Keep the - // spill area visible until the last operand has finished, then restore it - // before transferring control to the callee. - auto restoreBeforeTailCall = [&](Expression** ptr, - bool isReturn, - Expression** lastOperand) { - if (!isReturn) { - return; - } - if (!lastOperand) { - *ptr = builder.makeSequence(makeStackRestore(), *ptr); - return; - } - auto type = (*lastOperand)->type; - if (type == Type::unreachable) { - return; - } - auto temp = builder.addVar(func, type); - *lastOperand = builder.makeBlock({builder.makeLocalSet(temp, *lastOperand), - makeStackRestore(), - builder.makeLocalGet(temp, type)}); - }; - for (auto** ptr : FindAllPointers(func->body).list) { - auto* call = (*ptr)->cast(); - restoreBeforeTailCall(ptr, - call->isReturn, - call->operands.empty() ? nullptr - : &call->operands.back()); - } - for (auto** ptr : FindAllPointers(func->body).list) { - auto* call = (*ptr)->cast(); - restoreBeforeTailCall(ptr, call->isReturn, &call->target); - } - for (auto** ptr : FindAllPointers(func->body).list) { - auto* call = (*ptr)->cast(); - restoreBeforeTailCall(ptr, call->isReturn, &call->target); - } // add stack restores to the body if (func->body->type == Type::none) { block->list.push_back(func->body); block->list.push_back(makeStackRestore()); } else if (func->body->type == Type::unreachable) { block->list.push_back(func->body); - // There is no fallthrough. Returns and tail calls restore the stack above. + // no need to restore the old stack value, we're gone anyhow } else { // save the return value auto temp = builder.addVar(func, func->getResults()); diff --git a/src/passes/SpillPointers.cpp b/src/passes/SpillPointers.cpp index 003df90b3c0..d1ed4900162 100644 --- a/src/passes/SpillPointers.cpp +++ b/src/passes/SpillPointers.cpp @@ -64,7 +64,7 @@ struct LocalizePointerOperands return; } needsLocals |= pointer && hasCalls.contains(child); - pointer |= child->type == pointerType && !child->is(); + pointer |= child->type == pointerType; } if (!needsLocals) { return; diff --git a/test/lit/passes/spill-pointers-stack.wast b/test/lit/passes/spill-pointers-stack.wast new file mode 100644 index 00000000000..3c5e0c690e7 --- /dev/null +++ b/test/lit/passes/spill-pointers-stack.wast @@ -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) + ) + ) +) diff --git a/test/passes/spill-pointers.passes b/test/passes/spill-pointers.passes deleted file mode 100644 index f74f9d20acd..00000000000 --- a/test/passes/spill-pointers.passes +++ /dev/null @@ -1 +0,0 @@ -spill-pointers_enable-tail-call diff --git a/test/passes/spill-pointers.txt b/test/passes/spill-pointers.txt index 7c2870b54d5..98650041a3a 100644 --- a/test/passes/spill-pointers.txt +++ b/test/passes/spill-pointers.txt @@ -1,9 +1,9 @@ (module (type $0 (func)) (type $1 (func (param i32) (result i32))) - (type $2 (func (param i32))) + (type $2 (func (result i32))) (type $ii (func (param i32 i32))) - (type $4 (func (result i32))) + (type $4 (func (param i32))) (type $5 (func (param f64))) (import "env" "STACKTOP" (global $STACKTOP$asm2wasm$import i32)) (import "env" "segfault" (func $segfault (param i32))) @@ -525,19 +525,27 @@ (local.get $x) ) (drop - (return - (block - (local.set $2 - (i32.const 1) + (block + (global.set $__stack_pointer + (i32.add + (local.get $1) + (i32.const 16) ) - (global.set $__stack_pointer - (i32.add - (local.get $1) - (i32.const 16) + ) + (return + (block + (local.set $2 + (i32.const 1) + ) + (global.set $__stack_pointer + (i32.add + (local.get $1) + (i32.const 16) + ) + ) + (return + (local.get $2) ) - ) - (return - (local.get $2) ) ) ) @@ -671,235 +679,6 @@ ) ) ) - (func $spill-stack-operand - (local $0 i32) - (local $1 i32) - (local $2 i32) - (local $3 i32) - (global.set $__stack_pointer - (local.tee $2 - (i32.sub - (global.get $__stack_pointer) - (i32.const 16) - ) - ) - ) - (block - (local.set $0 - (call $spill-call-call0 - (i32.const 1) - ) - ) - (local.set $1 - (block (result i32) - (local.set $3 - (i32.const 2) - ) - (i32.store - (local.get $2) - (local.get $0) - ) - (call $spill-call-call0 - (local.get $3) - ) - ) - ) - (call $spill-args - (local.get $0) - (local.get $1) - ) - ) - (global.set $__stack_pointer - (i32.add - (local.get $2) - (i32.const 16) - ) - ) - ) - (func $spill-local-operand (param $p i32) - (local $1 i32) - (local $2 i32) - (local $3 i32) - (local $4 i32) - (global.set $__stack_pointer - (local.tee $3 - (i32.sub - (global.get $__stack_pointer) - (i32.const 16) - ) - ) - ) - (block - (local.set $1 - (local.get $p) - ) - (local.set $2 - (block (result i32) - (local.set $p - (i32.const 0) - ) - (block (result i32) - (local.set $4 - (i32.const 2) - ) - (i32.store offset=4 - (local.get $3) - (local.get $1) - ) - (call $spill-call-call0 - (local.get $4) - ) - ) - ) - ) - (call $spill-args - (local.get $1) - (local.get $2) - ) - ) - (global.set $__stack_pointer - (i32.add - (local.get $3) - (i32.const 16) - ) - ) - ) - (func $spill-stack-binary - (local $0 i32) - (local $1 i32) - (local $2 i32) - (local $3 i32) - (global.set $__stack_pointer - (local.tee $2 - (i32.sub - (global.get $__stack_pointer) - (i32.const 16) - ) - ) - ) - (drop - (block (result i32) - (local.set $0 - (call $spill-call-call0 - (i32.const 1) - ) - ) - (local.set $1 - (block (result i32) - (local.set $3 - (i32.const 2) - ) - (i32.store - (local.get $2) - (local.get $0) - ) - (call $spill-call-call0 - (local.get $3) - ) - ) - ) - (i32.add - (local.get $0) - (local.get $1) - ) - ) - ) - (global.set $__stack_pointer - (i32.add - (local.get $2) - (i32.const 16) - ) - ) - ) - (func $spill-stack-indirect-target (param $p i32) - (local $1 i32) - (local $2 i32) - (local $3 i32) - (local $4 i32) - (local $5 i32) - (global.set $__stack_pointer - (local.tee $4 - (i32.sub - (global.get $__stack_pointer) - (i32.const 16) - ) - ) - ) - (block - (local.set $1 - (local.get $p) - ) - (local.set $2 - (i32.const 0) - ) - (local.set $3 - (block (result i32) - (local.set $5 - (i32.const 2) - ) - (i32.store offset=4 - (local.get $4) - (local.get $1) - ) - (i32.store offset=8 - (local.get $4) - (local.get $2) - ) - (call $spill-call-call0 - (local.get $5) - ) - ) - ) - (call_indirect (type $ii) - (local.get $1) - (local.get $2) - (local.get $3) - ) - ) - (global.set $__stack_pointer - (i32.add - (local.get $4) - (i32.const 16) - ) - ) - ) - (func $spill-tail-operands (param $x i32) (result i32) - (local $1 i32) - (local $2 i32) - (global.set $__stack_pointer - (local.tee $1 - (i32.sub - (global.get $__stack_pointer) - (i32.const 16) - ) - ) - ) - (return - (return_call $spill-call-call0 - (block (result i32) - (local.set $2 - (block (result i32) - (block - (i32.store - (local.get $1) - (local.get $x) - ) - (call $nothing) - ) - (local.get $x) - ) - ) - (global.set $__stack_pointer - (i32.add - (local.get $1) - (i32.const 16) - ) - ) - (local.get $2) - ) - ) - ) - ) ) (module (type $0 (func)) @@ -1431,19 +1210,27 @@ (local.get $x) ) (drop - (return - (block - (local.set $2 - (i32.const 1) + (block + (global.set $__stack_pointer + (i32.add + (local.get $1) + (i32.const 16) ) - (global.set $__stack_pointer - (i32.add - (local.get $1) - (i32.const 16) + ) + (return + (block + (local.set $2 + (i32.const 1) + ) + (global.set $__stack_pointer + (i32.add + (local.get $1) + (i32.const 16) + ) + ) + (return + (local.get $2) ) - ) - (return - (local.get $2) ) ) ) diff --git a/test/passes/spill-pointers.wast b/test/passes/spill-pointers.wast index 42cfbbff1a7..de15ffb001a 100644 --- a/test/passes/spill-pointers.wast +++ b/test/passes/spill-pointers.wast @@ -168,46 +168,6 @@ ) (drop (local.get $x)) ) - (func $spill-stack-operand - (call $spill-args - (call $spill-call-call0 (i32.const 1)) - (call $spill-call-call0 (i32.const 2)) - ) - ) - (func $spill-local-operand (param $p i32) - (call $spill-args - (local.get $p) - (block (result i32) - (local.set $p (i32.const 0)) - (call $spill-call-call0 (i32.const 2)) - ) - ) - ) - (func $spill-stack-binary - (drop - (i32.add - (call $spill-call-call0 (i32.const 1)) - (call $spill-call-call0 (i32.const 2)) - ) - ) - ) - (func $spill-stack-indirect-target (param $p i32) - (call_indirect (type $ii) - (local.get $p) - (i32.const 0) - (call $spill-call-call0 (i32.const 2)) - ) - ) - (func $spill-tail-operands (param $x i32) (result i32) - (return - (return_call $spill-call-call0 - (block (result i32) - (call $nothing) - (local.get $x) - ) - ) - ) - ) ) (module