Skip to content

Commit

Permalink
Fix --spill-pointers for the stack growing down (#6294)
Browse files Browse the repository at this point in the history
The LLVM wasm backend grows the stack downwards, and this pass did not
fully account for that before.
  • Loading branch information
yamt authored Feb 13, 2024
1 parent 32a2782 commit 78516ae
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 201 deletions.
22 changes: 11 additions & 11 deletions src/abi/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ inline Index stackAlign(Index size) {
// Allocate some space on the stack, and assign it to a local.
// The local will have the same constant value in all the function, so you can
// just local.get it anywhere there.
//
// FIXME: This function assumes that the stack grows upward, per the convention
// used by fastcomp. The stack grows downward when using the WASM backend.

inline void
getStackSpace(Index local, Function* func, Index size, Module& wasm) {
Expand All @@ -55,22 +52,25 @@ getStackSpace(Index local, Function* func, Index size, Module& wasm) {
// TODO: find existing stack usage, and add on top of that - carefully
Builder builder(wasm);
auto* block = builder.makeBlock();
block->list.push_back(builder.makeLocalSet(
local, builder.makeGlobalGet(stackPointer->name, pointerType)));
// TODO: add stack max check
Expression* added;
if (pointerType == Type::i32) {
// The stack goes downward in the LLVM wasm backend.
added = builder.makeBinary(SubInt32,
builder.makeLocalGet(local, pointerType),
builder.makeConst(int32_t(size)));
added =
builder.makeBinary(SubInt32,
builder.makeGlobalGet(stackPointer->name, pointerType),
builder.makeConst(int32_t(size)));
} else {
WASM_UNREACHABLE("unhandled pointerType");
}
block->list.push_back(builder.makeGlobalSet(stackPointer->name, added));
block->list.push_back(builder.makeGlobalSet(
stackPointer->name, builder.makeLocalTee(local, added, pointerType)));
auto makeStackRestore = [&]() {
return builder.makeGlobalSet(stackPointer->name,
builder.makeLocalGet(local, pointerType));
return builder.makeGlobalSet(
stackPointer->name,
builder.makeBinary(AddInt32,
builder.makeLocalGet(local, pointerType),
builder.makeConst(int32_t(size))));
};
// add stack restores to the returns
FindAllPointers<Return> finder(func->body);
Expand Down
Loading

0 comments on commit 78516ae

Please sign in to comment.