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 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
1 change: 1 addition & 0 deletions std/assembly/rt/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ declare function __visit_globals(cookie: u32): void;
declare function __visit_members(ref: usize, cookie: u32): void;
declare function __allocBuffer(size: usize, id: u32, data?: usize): usize;
declare function __allocArray(length: i32, alignLog2: usize, id: u32, data?: usize): usize;
declare function __finalize(ref: usize): void;
declare const ASC_RTRACE: bool;
declare const __GC_ALL_ACYCLIC: bool;
16 changes: 12 additions & 4 deletions std/assembly/rt/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,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 All @@ -149,6 +149,14 @@ function decrement(s: Block): void {
}
}

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

/** Buffer of possible roots. */
// @ts-ignore: decorator
@lazy var ROOTS: usize;
Expand Down Expand Up @@ -205,7 +213,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 +269,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: 8 additions & 2 deletions tests/compiler/do.untouched.wat
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -2352,9 +2359,8 @@
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 Down
16 changes: 10 additions & 6 deletions tests/compiler/extends-baseaggregate.untouched.wat
Original file line number Diff line number Diff line change
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 @@ -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 Down
10 changes: 8 additions & 2 deletions tests/compiler/for.untouched.wat
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -2397,9 +2404,8 @@
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 Down
12 changes: 9 additions & 3 deletions tests/compiler/implicit-getter-setter.untouched.wat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(type $i32_i32_=>_none (func (param i32 i32)))
(type $i32_=>_none (func (param i32)))
(type $i32_i32_=>_none (func (param i32 i32)))
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
Expand Down Expand Up @@ -1636,6 +1636,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 Down Expand Up @@ -1692,9 +1699,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/issues/1095.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,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 Down Expand Up @@ -1676,9 +1683,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/issues/1225.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,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 Down Expand Up @@ -1697,9 +1704,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/logical.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2171,6 +2171,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 Down Expand Up @@ -2227,9 +2234,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/managed-cast.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,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 Down Expand Up @@ -1940,9 +1947,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/object-literal.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3914,6 +3914,13 @@
drop
return
)
(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 Down Expand Up @@ -3970,9 +3977,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/rc/global-init.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,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 Down Expand Up @@ -790,9 +797,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/rc/local-init.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,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 Down Expand Up @@ -1638,9 +1645,8 @@
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 Down
10 changes: 8 additions & 2 deletions tests/compiler/rc/logical-and-mismatch.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,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 Down Expand Up @@ -1683,9 +1690,8 @@
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 Down
Loading