Skip to content

Commit 382aabe

Browse files
authored
fix: Add BLOCK_OVERHEAD before round size (#2652)
1 parent a0c27fa commit 382aabe

File tree

153 files changed

+4712
-4302
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+4712
-4302
lines changed

Diff for: std/assembly/rt/tlsf.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ function removeBlock(root: Root, block: Block): void {
307307
// must perform those updates.
308308
}
309309

310+
function roundSize(size: usize): usize {
311+
const halfMaxSize = BLOCK_MAXSIZE >> 1; // don't round last fl
312+
const inv: usize = sizeof<usize>() * 8 - 1;
313+
const invRound = inv - SL_BITS;
314+
return size < halfMaxSize
315+
? size + (1 << (invRound - clz<usize>(size))) - 1
316+
: size;
317+
}
318+
310319
/** Searches for a free block of at least the specified size. */
311320
function searchBlock(root: Root, size: usize): Block | null {
312321
// size was already asserted by caller
@@ -317,13 +326,8 @@ function searchBlock(root: Root, size: usize): Block | null {
317326
fl = 0;
318327
sl = <u32>(size >> AL_BITS);
319328
} else {
320-
const halfMaxSize = BLOCK_MAXSIZE >> 1; // don't round last fl
321-
const inv: usize = sizeof<usize>() * 8 - 1;
322-
const invRound = inv - SL_BITS;
323-
let requestSize = size < halfMaxSize
324-
? size + (1 << (invRound - clz<usize>(size))) - 1
325-
: size;
326-
fl = inv - clz<usize>(requestSize);
329+
const requestSize = roundSize(size);
330+
fl = sizeof<usize>() * 8 - 1 - clz<usize>(requestSize);
327331
sl = <u32>((requestSize >> (fl - SL_BITS)) ^ (1 << SL_BITS));
328332
fl -= SB_BITS - 1;
329333
}
@@ -428,10 +432,8 @@ function growMemory(root: Root, size: usize): void {
428432
return;
429433
}
430434
// Here, both rounding performed in searchBlock ...
431-
const halfMaxSize = BLOCK_MAXSIZE >> 1;
432-
if (size < halfMaxSize) { // don't round last fl
433-
const invRound = (sizeof<usize>() * 8 - 1) - SL_BITS;
434-
size += (1 << (invRound - clz<usize>(size))) - 1;
435+
if (size >= SB_SIZE) {
436+
size = roundSize(size);
435437
}
436438
// and additional BLOCK_OVERHEAD must be taken into account. If we are going
437439
// to merge with the tail block, that's one time, otherwise it's two times.

Diff for: tests/compiler/bindings/esm.debug.wat

+16-16
Original file line numberDiff line numberDiff line change
@@ -2026,22 +2026,6 @@
20262026
(local $pagesAfter i32)
20272027
i32.const 0
20282028
drop
2029-
local.get $size
2030-
i32.const 536870910
2031-
i32.lt_u
2032-
if
2033-
local.get $size
2034-
i32.const 1
2035-
i32.const 27
2036-
local.get $size
2037-
i32.clz
2038-
i32.sub
2039-
i32.shl
2040-
i32.const 1
2041-
i32.sub
2042-
i32.add
2043-
local.set $size
2044-
end
20452029
memory.size $0
20462030
local.set $pagesBefore
20472031
local.get $size
@@ -2063,6 +2047,22 @@
20632047
i32.add
20642048
local.set $size
20652049
local.get $size
2050+
i32.const 536870910
2051+
i32.lt_u
2052+
if
2053+
local.get $size
2054+
i32.const 1
2055+
i32.const 27
2056+
local.get $size
2057+
i32.clz
2058+
i32.sub
2059+
i32.shl
2060+
i32.const 1
2061+
i32.sub
2062+
i32.add
2063+
local.set $size
2064+
end
2065+
local.get $size
20662066
i32.const 65535
20672067
i32.add
20682068
i32.const 65535

Diff for: tests/compiler/bindings/esm.release.wat

+31-28
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@
13861386
call $~lib/rt/tlsf/initialize
13871387
end
13881388
global.get $~lib/rt/tlsf/ROOT
1389-
local.set $4
1389+
local.set $3
13901390
local.get $0
13911391
i32.const 16
13921392
i32.add
@@ -1401,7 +1401,7 @@
14011401
call $~lib/builtins/abort
14021402
unreachable
14031403
end
1404-
local.get $4
1404+
local.get $3
14051405
i32.const 12
14061406
local.get $2
14071407
i32.const 19
@@ -1421,8 +1421,9 @@
14211421
if
14221422
memory.size $0
14231423
local.tee $2
1424+
local.get $5
14241425
i32.const 4
1425-
local.get $4
1426+
local.get $3
14261427
i32.load $0 offset=1568
14271428
local.get $2
14281429
i32.const 16
@@ -1431,46 +1432,48 @@
14311432
i32.sub
14321433
i32.ne
14331434
i32.shl
1434-
local.get $5
1435-
i32.const 1
1436-
i32.const 27
1437-
local.get $5
1438-
i32.clz
1439-
i32.sub
1440-
i32.shl
1441-
i32.const 1
1442-
i32.sub
14431435
i32.add
1444-
local.get $5
1445-
local.get $5
1436+
local.tee $4
14461437
i32.const 536870910
14471438
i32.lt_u
1448-
select
1449-
i32.add
1439+
if (result i32)
1440+
local.get $4
1441+
i32.const 1
1442+
i32.const 27
1443+
local.get $4
1444+
i32.clz
1445+
i32.sub
1446+
i32.shl
1447+
i32.const 1
1448+
i32.sub
1449+
i32.add
1450+
else
1451+
local.get $4
1452+
end
14501453
i32.const 65535
14511454
i32.add
14521455
i32.const -65536
14531456
i32.and
14541457
i32.const 16
14551458
i32.shr_u
1456-
local.tee $3
1459+
local.tee $4
14571460
local.get $2
1458-
local.get $3
1461+
local.get $4
14591462
i32.gt_s
14601463
select
14611464
memory.grow $0
14621465
i32.const 0
14631466
i32.lt_s
14641467
if
1465-
local.get $3
1468+
local.get $4
14661469
memory.grow $0
14671470
i32.const 0
14681471
i32.lt_s
14691472
if
14701473
unreachable
14711474
end
14721475
end
1473-
local.get $4
1476+
local.get $3
14741477
local.get $2
14751478
i32.const 16
14761479
i32.shl
@@ -1479,7 +1482,7 @@
14791482
i64.const 16
14801483
i64.shl
14811484
call $~lib/rt/tlsf/addMemory
1482-
local.get $4
1485+
local.get $3
14831486
local.get $5
14841487
call $~lib/rt/tlsf/searchBlock
14851488
local.tee $2
@@ -1507,12 +1510,12 @@
15071510
call $~lib/builtins/abort
15081511
unreachable
15091512
end
1510-
local.get $4
1513+
local.get $3
15111514
local.get $2
15121515
call $~lib/rt/tlsf/removeBlock
15131516
local.get $2
15141517
i32.load $0
1515-
local.set $3
1518+
local.set $4
15161519
local.get $5
15171520
i32.const 4
15181521
i32.add
@@ -1526,7 +1529,7 @@
15261529
call $~lib/builtins/abort
15271530
unreachable
15281531
end
1529-
local.get $3
1532+
local.get $4
15301533
i32.const -4
15311534
i32.and
15321535
local.get $5
@@ -1537,7 +1540,7 @@
15371540
if
15381541
local.get $2
15391542
local.get $5
1540-
local.get $3
1543+
local.get $4
15411544
i32.const 2
15421545
i32.and
15431546
i32.or
@@ -1547,19 +1550,19 @@
15471550
i32.add
15481551
local.get $5
15491552
i32.add
1550-
local.tee $3
1553+
local.tee $4
15511554
local.get $6
15521555
i32.const 4
15531556
i32.sub
15541557
i32.const 1
15551558
i32.or
15561559
i32.store $0
1557-
local.get $4
15581560
local.get $3
1561+
local.get $4
15591562
call $~lib/rt/tlsf/insertBlock
15601563
else
15611564
local.get $2
1562-
local.get $3
1565+
local.get $4
15631566
i32.const -2
15641567
i32.and
15651568
i32.store $0

0 commit comments

Comments
 (0)