Skip to content
Merged
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
27 changes: 25 additions & 2 deletions src/passes/Memory64Lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,31 @@ struct Memory64Lowering : public WalkerPass<PostWalker<Memory64Lowering>> {
if (table->is64()) {
wrapTableAddress64(curr->delta, curr->table);
auto* size = static_cast<Expression*>(curr);
extendTableAddress64(size, curr->table);
replaceCurrent(size);
// TableGrow returns -1 in case of failure. We cannot just use
// extend_32_u in this case so we handle it the same way as MemoryGrow:
//
// (if (result i64)
// (i32.eq (i32.const -1) (local.tee $tmp (table.grow X)))
// (then
// (i64.const -1)
// )
// (else
// (i64.extend_i32_u (local.get $tmp))
// )
// )
Builder builder(module);
auto tmp = builder.addVar(getFunction(), Type::i32);
Expression* isMinusOne =
builder.makeBinary(EqInt32,
builder.makeConst(int32_t(-1)),
builder.makeLocalTee(tmp, size, Type::i32));
auto* newSize = builder.makeLocalGet(tmp, Type::i32);
Expression* ifExp =
builder.makeIf(isMinusOne,
builder.makeConst(int64_t(-1)),
builder.makeUnary(UnaryOp::ExtendUInt32, newSize));
curr->type = Type::i32;
replaceCurrent(ifExp);
}
}

Expand Down
27 changes: 22 additions & 5 deletions test/lit/passes/memory64-lowering.wast
Original file line number Diff line number Diff line change
Expand Up @@ -399,16 +399,33 @@
)

;; CHECK: (func $test_table_grow (result i64)
;; CHECK-NEXT: (i64.extend_i32_u
;; CHECK-NEXT: (table.grow $t64
;; CHECK-NEXT: (ref.null nofunc)
;; CHECK-NEXT: (i32.wrap_i64
;; CHECK-NEXT: (i64.const 10)
;; CHECK-NEXT: (local $0 i32)
;; CHECK-NEXT: (if (result i64)
;; CHECK-NEXT: (i32.eq
;; CHECK-NEXT: (i32.const -1)
;; CHECK-NEXT: (local.tee $0
;; CHECK-NEXT: (table.grow $t64
;; CHECK-NEXT: (ref.null nofunc)
;; CHECK-NEXT: (i32.wrap_i64
;; CHECK-NEXT: (i64.const 10)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; 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 $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $test_table_grow (result i64)
;; table.grow returns -1 on failure. The lowering must check for -1 and
;; return i64(-1) instead of i64.extend_i32_u(i32(-1)) which would be
;; 4294967295.
(table.grow $t64 (ref.null func) (i64.const 10))
)

Expand Down
Loading