Skip to content

[SpillPointers] Spill operand stack pointers across calls - #9432

Merged
kripken merged 3 commits into
WebAssembly:mainfrom
akasakariko:fix/spill-pointer-operands
Dec 1, 2026
Merged

kripken merged 3 commits into
WebAssembly:mainfrom
akasakariko:fix/spill-pointer-operands

Conversation

@akasakariko

@akasakariko akasakariko commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

--spill-pointers can miss a pointer held as an earlier operand while a later operand calls, allowing conservative GC to reclaim a live object

Capture these values in fresh locals before liveness analysis, preserving evaluation order and values from locals that later operands overwrite, including constants, indirect-call targets and non-call expressions

This is an original contribution

Fixes #9307

Comment thread test/passes/spill-pointers.passes Outdated
@@ -0,0 +1 @@
spill-pointers_enable-tail-call

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.

Please add new tests as lit tests, maybe test/lit/passes/spill-pointers-stack.wast. We are moving tests to lit over time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved the new tests to test/lit/passes/spill-pointers-stack.wast and restored test/passes/spill-pointers.wast and test/passes/spill-pointers.txt

Comment thread test/passes/spill-pointers.wast Outdated
(call $spill-args
(call $spill-call-call0 (i32.const 1))
(call $spill-call-call0 (i32.const 2))
)

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 will be easier to read this once it is a lit test, but please also add brief comments in each test, like here, maybe

;; The i32 constants might be pointers, so we must spill them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added brief comments to each test function in test/lit/passes/spill-pointers-stack.wast explaining why each operand needs or avoids spilling

Comment thread src/passes/SpillPointers.cpp Outdated
return;
}
needsLocals |= pointer && hasCalls.contains(child);
pointer |= child->type == pointerType && !child->is<Const>();

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.

Why are consts excluded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, constants could still represent pointers (for example after wasm-ctor-eval or constant propagation) and the rest of SpillPointers spills them as well, so I removed the Const exclusion and added $spill-const-operand to test it

Comment thread src/abi/stack.h Outdated
} 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.

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.

Are the changes to this file related to the main change to SpillPointers? They look like they could be separated out, or maybe I don't quite understand them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Those changes were addressing a separate issue in getStackSpace around restoring the stack for tail calls and unreachable return values, which is independent of spilling operand stack pointers in SpillPointers, so I reverted src/abi/stack.h from this PR

Comment thread src/passes/SpillPointers.cpp Outdated
LocalizePointerOperands(Type pointerType) : pointerType(pointerType) {}

void visitExpression(Expression* curr) {
bool calls = curr->is<Call>() || curr->is<CallIndirect>();

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.

This should include CallRef too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added CallRef handling in LocalizePointerOperands as well as in SpillPointers itself (including non-nullable reference operands) and added $spill-call-ref in test/lit/passes/spill-pointers-stack.wast

hasCalls.insert(curr);
}

bool pointer = false;

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.

Please add a comment here. IIUC what this is doing is

Suggested change
bool pointer = false;
// Check if a pointer appears before a call. In that case, it must be spilled.
bool pointer = false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added the comment above bool pointer = false

return;
}
needsLocals |= pointer && hasCalls.contains(child);
pointer |= child->type == pointerType;

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.

Please add a test for the ordering here, that is, where a pointer appears after the call, so nothing is spilled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added $no-spill-pointer-after-call in test/lit/passes/spill-pointers-stack.wast to verify that nothing is spilled when a pointer operand appears after the call

Comment thread src/passes/SpillPointers.cpp Resolved
@kripken
kripken merged commit cf660d0 into WebAssembly:main Dec 1, 2026
35 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--spill-pointers does not spill a value held on the Wasm value stack across a call

2 participants