Skip to content

Fix undone autorelease in inlined context #1043

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 1 commit into from
Jan 9, 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
42 changes: 30 additions & 12 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6652,28 +6652,46 @@ export class Compiler extends DiagnosticEmitter {
): void {
// Differs from `performAutoreleases` in that concluding this flow also
// concludes all its parent flows, for example on a `return`.
var module = this.module;
if (flow.is(FlowFlags.INLINE_CONTEXT)) {
// Traverse to the top-most flow containing the inlined function's
// locals as scoped locals and release these instead of all the locals.
let current = flow;
let parent: Flow | null;
while (parent = flow.parent) flow = parent;
this.performAutoreleases(flow, stmts, /* finalize */ false);
while (parent = current.parent) current = parent;
let scopedLocals = current.scopedLocals;
if (scopedLocals) {
for (let local of scopedLocals.values()) {
this.maybeFinishAutorelease(local, flow, stmts);
}
}
} else {
for (let local of flow.parentFunction.localsByIndex) {
let localIndex = local.index;
if (flow.isAnyLocalFlag(localIndex, LocalFlags.ANY_RETAINED)) {
flow.unsetLocalFlag(localIndex, LocalFlags.ANY_RETAINED);
stmts.push(
this.makeRelease(
module.local_get(localIndex, local.type.toNativeType())
)
);
}
this.maybeFinishAutorelease(local, flow, stmts);
}
}
}

/** Finishes a single autorelease of the specified local. */
private maybeFinishAutorelease(
/** Local to finish autoreleasing. */
local: Local,
/** Flow releasing its queued autoreleases. */
flow: Flow,
/** Array of statements to append the releases to. */
stmts: ExpressionRef[]
): void {
var localIndex = local.index;
var module = this.module;
if (~localIndex && flow.isAnyLocalFlag(localIndex, LocalFlags.ANY_RETAINED)) {
flow.unsetLocalFlag(localIndex, LocalFlags.ANY_RETAINED);
stmts.push(
this.makeRelease(
module.local_get(localIndex, local.type.toNativeType())
)
);
}
}

// </reference-counting>

/** Creates a direct call to the specified function. */
Expand Down
24 changes: 9 additions & 15 deletions tests/compiler/std/typedarray.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3383,20 +3383,16 @@
block $~lib/typedarray/SORT<~lib/typedarray/Float64Array,f64>|inlined.0
local.get $0
call $~lib/rt/pure/__retain
local.tee $1
call $~lib/typedarray/Int64Array#get:length
local.tee $2
call $~lib/typedarray/Int64Array#get:length
local.tee $1
i32.const 1
i32.le_s
if
local.get $1
call $~lib/rt/pure/__release
br $~lib/typedarray/SORT<~lib/typedarray/Float64Array,f64>|inlined.0
end
local.get $1
br_if $~lib/typedarray/SORT<~lib/typedarray/Float64Array,f64>|inlined.0
local.get $2
i32.load offset=4
local.set $0
local.get $2
local.get $1
i32.const 2
i32.eq
if
Expand All @@ -3421,24 +3417,22 @@
local.get $3
f64.store
end
local.get $1
call $~lib/rt/pure/__release
br $~lib/typedarray/SORT<~lib/typedarray/Float64Array,f64>|inlined.0
end
local.get $2
local.get $1
i32.const 256
i32.lt_s
if
local.get $0
local.get $2
local.get $1
call $~lib/util/sort/insertionSort<f64>
else
local.get $0
local.get $2
local.get $1
call $~lib/util/sort/weakHeapSort<f64>
end
end
local.get $1
local.get $2
)
(func $~lib/util/sort/COMPARATOR<f64>~anonymous|0 (; 57 ;) (param $0 f64) (param $1 f64) (result i32)
(local $2 i64)
Expand Down
8 changes: 0 additions & 8 deletions tests/compiler/std/typedarray.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5246,10 +5246,6 @@
i32.le_s
if
local.get $3
local.set $5
local.get $3
call $~lib/rt/pure/__release
local.get $5
br $~lib/typedarray/SORT<~lib/typedarray/Float64Array,f64>|inlined.0
end
local.get $3
Expand Down Expand Up @@ -5282,10 +5278,6 @@
f64.store
end
local.get $3
local.set $8
local.get $3
call $~lib/rt/pure/__release
local.get $8
br $~lib/typedarray/SORT<~lib/typedarray/Float64Array,f64>|inlined.0
end
local.get $5
Expand Down