Skip to content

Commit 8f78bb9

Browse files
authored
Fix memory size calculation in MemoryPacking (#2548)
Because `memory.size` returns the size in number of pages, we have to multiply the size with the page size when converting `memory.init`.
1 parent 3f6fd58 commit 8f78bb9

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/passes/MemoryPacking.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ struct MemoryPacking : public Pass {
140140
builder.makeBinary(
141141
OrInt32,
142142
builder.makeBinary(
143-
GtUInt32, curr->dest, builder.makeHost(MemorySize, Name(), {})),
143+
GtUInt32,
144+
curr->dest,
145+
builder.makeBinary(
146+
MulInt32,
147+
builder.makeConst(Literal(Memory::kPageSize)),
148+
builder.makeHost(MemorySize, Name(), {}))),
144149
builder.makeBinary(OrInt32, curr->offset, curr->size)),
145150
builder.makeUnreachable()));
146151
changed = true;

test/passes/memory-packing_all-features.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
(i32.or
2727
(i32.gt_u
2828
(i32.const 0)
29-
(memory.size)
29+
(i32.mul
30+
(i32.const 65536)
31+
(memory.size)
32+
)
3033
)
3134
(i32.or
3235
(i32.const 0)

0 commit comments

Comments
 (0)