Skip to content

Add GC finalization hook #1256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 15 additions & 4 deletions std/assembly/rt/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ function __visit(ref: usize, cookie: i32): void {
}
}

/** Finalizes the specified block, giving it back to the memory manager. */
function finalize(s: Block): void {
if (isDefined(__finalize)) {
let info = s.gcInfo | 1; // pretend RC>=1
s.gcInfo = info;
__finalize(changetype<usize>(s) + BLOCK_OVERHEAD);
assert(s.gcInfo == info);
}
freeBlock(ROOT, s);
}

/** Increments the reference count of the specified block by one.*/
function increment(s: Block): void {
var info = s.gcInfo;
Expand All @@ -124,10 +135,10 @@ function decrement(s: Block): void {
__visit_members(changetype<usize>(s) + BLOCK_OVERHEAD, VISIT_DECREMENT);
if (isDefined(__GC_ALL_ACYCLIC)) {
if (DEBUG) assert(!(info & BUFFERED_MASK));
freeBlock(ROOT, s);
finalize(s);
} else {
if (!(info & BUFFERED_MASK)) {
freeBlock(ROOT, s);
finalize(s);
} else {
s.gcInfo = BUFFERED_MASK | COLOR_BLACK | 0;
}
Expand Down Expand Up @@ -205,7 +216,7 @@ export function __collect(): void {
cur += sizeof<usize>();
} else {
if ((info & COLOR_MASK) == COLOR_BLACK && !(info & REFCOUNT_MASK)) {
freeBlock(ROOT, s);
finalize(s);
} else {
s.gcInfo = info & ~BUFFERED_MASK;
}
Expand Down Expand Up @@ -261,7 +272,7 @@ function collectWhite(s: Block): void {
if ((info & COLOR_MASK) == COLOR_WHITE && !(info & BUFFERED_MASK)) {
s.gcInfo = (info & ~COLOR_MASK) | COLOR_BLACK;
__visit_members(changetype<usize>(s) + BLOCK_OVERHEAD, VISIT_COLLECTWHITE);
freeBlock(ROOT, s);
finalize(s);
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/compiler/do.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@
if
i32.const 0
i32.const 1184
i32.const 109
i32.const 120
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -1024,7 +1024,7 @@
if
i32.const 0
i32.const 1184
i32.const 112
i32.const 123
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1416,7 +1416,7 @@
if
i32.const 0
i32.const 1184
i32.const 122
i32.const 133
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1458,7 +1458,7 @@
if
i32.const 0
i32.const 1184
i32.const 126
i32.const 137
i32.const 18
call $~lib/builtins/abort
unreachable
Expand All @@ -1481,7 +1481,7 @@
if
i32.const 0
i32.const 1184
i32.const 136
i32.const 147
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down
20 changes: 13 additions & 7 deletions tests/compiler/do.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,7 @@
if
i32.const 0
i32.const 176
i32.const 109
i32.const 120
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -1915,7 +1915,7 @@
if
i32.const 0
i32.const 176
i32.const 112
i32.const 123
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2296,6 +2296,13 @@
local.get $1
call $~lib/rt/rtrace/onfree
)
(func $~lib/rt/pure/finalize (param $0 i32)
i32.const 0
drop
global.get $~lib/rt/tlsf/ROOT
local.get $0
call $~lib/rt/tlsf/freeBlock
)
(func $~lib/rt/pure/decrement (param $0 i32)
(local $1 i32)
(local $2 i32)
Expand All @@ -2321,7 +2328,7 @@
if
i32.const 0
i32.const 176
i32.const 122
i32.const 133
i32.const 14
call $~lib/builtins/abort
unreachable
Expand All @@ -2347,14 +2354,13 @@
if
i32.const 0
i32.const 176
i32.const 126
i32.const 137
i32.const 18
call $~lib/builtins/abort
unreachable
end
global.get $~lib/rt/tlsf/ROOT
local.get $0
call $~lib/rt/tlsf/freeBlock
call $~lib/rt/pure/finalize
else
i32.const 1
drop
Expand All @@ -2365,7 +2371,7 @@
if
i32.const 0
i32.const 176
i32.const 136
i32.const 147
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler/extends-baseaggregate.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@
if
i32.const 0
i32.const 1248
i32.const 109
i32.const 120
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -1088,7 +1088,7 @@
if
i32.const 0
i32.const 1248
i32.const 112
i32.const 123
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1940,7 +1940,7 @@
if
i32.const 0
i32.const 1248
i32.const 122
i32.const 133
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1973,7 +1973,7 @@
if
i32.const 0
i32.const 1248
i32.const 136
i32.const 147
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down
24 changes: 14 additions & 10 deletions tests/compiler/extends-baseaggregate.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,7 @@
if
i32.const 0
i32.const 240
i32.const 109
i32.const 120
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -1508,7 +1508,7 @@
if
i32.const 0
i32.const 240
i32.const 112
i32.const 123
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3377,6 +3377,13 @@
call $~lib/rt/__visit_members
end
)
(func $~lib/rt/pure/finalize (param $0 i32)
i32.const 0
drop
global.get $~lib/rt/tlsf/ROOT
local.get $0
call $~lib/rt/tlsf/freeBlock
)
(func $~lib/rt/pure/scanBlack (param $0 i32)
local.get $0
local.get $0
Expand Down Expand Up @@ -3464,9 +3471,8 @@
i32.add
i32.const 5
call $~lib/rt/__visit_members
global.get $~lib/rt/tlsf/ROOT
local.get $0
call $~lib/rt/tlsf/freeBlock
call $~lib/rt/pure/finalize
end
)
(func $~lib/rt/pure/__collect
Expand Down Expand Up @@ -3539,9 +3545,8 @@
i32.const 0
end
if
global.get $~lib/rt/tlsf/ROOT
local.get $5
call $~lib/rt/tlsf/freeBlock
call $~lib/rt/pure/finalize
else
local.get $5
local.get $6
Expand Down Expand Up @@ -3740,7 +3745,7 @@
if
i32.const 0
i32.const 240
i32.const 122
i32.const 133
i32.const 14
call $~lib/builtins/abort
unreachable
Expand All @@ -3761,9 +3766,8 @@
i32.and
i32.eqz
if
global.get $~lib/rt/tlsf/ROOT
local.get $0
call $~lib/rt/tlsf/freeBlock
call $~lib/rt/pure/finalize
else
local.get $0
i32.const -2147483648
Expand All @@ -3783,7 +3787,7 @@
if
i32.const 0
i32.const 240
i32.const 136
i32.const 147
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down
10 changes: 5 additions & 5 deletions tests/compiler/for.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@
if
i32.const 0
i32.const 1184
i32.const 109
i32.const 120
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -1021,7 +1021,7 @@
if
i32.const 0
i32.const 1184
i32.const 112
i32.const 123
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1425,7 +1425,7 @@
if
i32.const 0
i32.const 1184
i32.const 122
i32.const 133
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -1467,7 +1467,7 @@
if
i32.const 0
i32.const 1184
i32.const 126
i32.const 137
i32.const 18
call $~lib/builtins/abort
unreachable
Expand All @@ -1490,7 +1490,7 @@
if
i32.const 0
i32.const 1184
i32.const 136
i32.const 147
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down
20 changes: 13 additions & 7 deletions tests/compiler/for.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,7 @@
if
i32.const 0
i32.const 176
i32.const 109
i32.const 120
i32.const 3
call $~lib/builtins/abort
unreachable
Expand All @@ -1928,7 +1928,7 @@
if
i32.const 0
i32.const 176
i32.const 112
i32.const 123
i32.const 14
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2341,6 +2341,13 @@
local.get $1
call $~lib/rt/rtrace/onfree
)
(func $~lib/rt/pure/finalize (param $0 i32)
i32.const 0
drop
global.get $~lib/rt/tlsf/ROOT
local.get $0
call $~lib/rt/tlsf/freeBlock
)
(func $~lib/rt/pure/decrement (param $0 i32)
(local $1 i32)
(local $2 i32)
Expand All @@ -2366,7 +2373,7 @@
if
i32.const 0
i32.const 176
i32.const 122
i32.const 133
i32.const 14
call $~lib/builtins/abort
unreachable
Expand All @@ -2392,14 +2399,13 @@
if
i32.const 0
i32.const 176
i32.const 126
i32.const 137
i32.const 18
call $~lib/builtins/abort
unreachable
end
global.get $~lib/rt/tlsf/ROOT
local.get $0
call $~lib/rt/tlsf/freeBlock
call $~lib/rt/pure/finalize
else
i32.const 1
drop
Expand All @@ -2410,7 +2416,7 @@
if
i32.const 0
i32.const 176
i32.const 136
i32.const 147
i32.const 16
call $~lib/builtins/abort
unreachable
Expand Down
Loading