Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 24 additions & 2 deletions src/passes/Memory64Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,31 @@ struct Memory64Lowering : public WalkerPass<PostWalker<Memory64Lowering>> {
if (memory->is64()) {
wrapAddress64(curr->delta, curr->memory);
auto* size = static_cast<Expression*>(curr);
extendAddress64(size, curr->memory);
// MemoryGrow returns -1 in the case of failure
// so we need to handle that
// local.set $tmp memory.grow X
// local.get
// i32.const -1
// if i32.eq
// i64.const -1
// else
// local.get
// i32.extend
Builder builder(module);
auto tmp = builder.addVar(getFunction(), Type::i32);
auto setLocal = builder.makeLocalSet(tmp, size);
Expression* isMinusOne =
builder.makeBinary(EqInt32,
builder.makeConst(int32_t(-1)),
builder.makeLocalGet(tmp, Type::i32));
auto* newSize = builder.makeLocalGet(tmp, Type::i32);
builder.makeUnary(UnaryOp::ExtendUInt32, newSize);
Expression* ifExp =
builder.makeIf(isMinusOne,
builder.makeConst(int64_t(-1)),
builder.makeUnary(UnaryOp::ExtendUInt32, newSize));
curr->type = Type::i32;
replaceCurrent(size);
replaceCurrent(builder.blockify(setLocal, ifExp));
}
}

Expand Down
25 changes: 21 additions & 4 deletions test/lit/passes/memory64-lowering.wast
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,33 @@

;; CHECK: (func $other
;; CHECK-NEXT: (local $0 i64)
;; CHECK-NEXT: (local $1 i32)
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (i64.extend_i32_u
;; CHECK-NEXT: (memory.size)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.set $0
;; CHECK-NEXT: (i64.extend_i32_u
;; CHECK-NEXT: (memory.grow
;; CHECK-NEXT: (i32.wrap_i64
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: (block (result i64)
;; CHECK-NEXT: (local.set $1
;; CHECK-NEXT: (memory.grow
;; CHECK-NEXT: (i32.wrap_i64
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (if (result i64)
;; CHECK-NEXT: (i32.eq
;; CHECK-NEXT: (i32.const -1)
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (then
;; CHECK-NEXT: (i64.const -1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (i64.extend_i32_u
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down