Skip to content

fix: avoid to use visit wrapper for __visit_globals #2934

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

Closed
Closed
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
25 changes: 20 additions & 5 deletions src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ import {
DecoratorFlags,
Class,
PropertyPrototype,
VariableLikeElement
VariableLikeElement,
Function,
} from "./program";

import {
Expand Down Expand Up @@ -10673,6 +10674,18 @@ builtinFunctions.set(BuiltinNames.i32x4_relaxed_dot_i8x16_i7x16_add_s, builtin_i

// === Internal helpers =======================================================================

// When no visit is needed, null is returned, otherwise the name of the visit function is returned.
function getVisitInstanceName(visitInstance: Function, classReference: Class): string | null {
if (classReference.hasDecorator(DecoratorFlags.Final)) {
if (classReference.visitRef != 0) {
return classReference.visitorFunctionName;
} else if (classReference.isPointerfree) {
return null; // no visitor for pointerfree classes
}
}
return visitInstance.internalName;
}

/** Compiles the `visit_globals` function. */
export function compileVisitGlobals(compiler: Compiler): void {
let module = compiler.module;
Expand All @@ -10695,11 +10708,13 @@ export function compileVisitGlobals(compiler: Compiler): void {
!classReference.hasDecorator(DecoratorFlags.Unmanaged) &&
global.is(CommonFlags.Compiled)
) {
let visitFunctionName = getVisitInstanceName(visitInstance, classReference);
if (visitFunctionName == null) continue;
if (global.is(CommonFlags.Inlined)) {
let value = global.constantIntegerValue;
if (i64_low(value) || i64_high(value)) {
exprs.push(
module.call(visitInstance.internalName, [
module.call(visitFunctionName, [
compiler.options.isWasm64
? module.i64(i64_low(value), i64_high(value))
: module.i32(i64_low(value)),
Expand All @@ -10714,7 +10729,7 @@ export function compileVisitGlobals(compiler: Compiler): void {
module.global_get(global.internalName, sizeTypeRef),
false // internal
),
module.call(visitInstance.internalName, [
module.call(visitFunctionName, [
module.local_get(1, sizeTypeRef), // tempRef != null
module.local_get(0, TypeRef.I32) // cookie
], TypeRef.None)
Expand Down Expand Up @@ -10750,7 +10765,7 @@ function ensureVisitMembersOf(compiler: Compiler, instance: Class): void {
let base = instance.base;
if (base) {
body.push(
module.call(`${base.internalName}~visit`, [
module.call(base.visitorFunctionName, [
module.local_get(0, sizeTypeRef), // this
module.local_get(1, TypeRef.I32) // cookie
], TypeRef.None)
Expand Down Expand Up @@ -10860,7 +10875,7 @@ export function compileVisitMembers(compiler: Compiler): void {
cases[i] = module.return();
} else {
cases[i] = module.block(null, [
module.call(`${instance.internalName}~visit`, [
module.call(instance.visitorFunctionName, [
module.local_get(0, sizeTypeRef), // this
module.local_get(1, TypeRef.I32) // cookie
], TypeRef.None),
Expand Down
4 changes: 3 additions & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,10 @@ export class Compiler extends DiagnosticEmitter {
// finalize runtime features
module.removeGlobal(BuiltinNames.rtti_base);
if (this.runtimeFeatures & RuntimeFeatures.Rtti) compileRTTI(this);
if (this.runtimeFeatures & RuntimeFeatures.visitGlobals) compileVisitGlobals(this);
// visit globals can rely on the result of visit members to visit globals of
// certain type directly thought the visit of the class.
if (this.runtimeFeatures & RuntimeFeatures.visitMembers) compileVisitMembers(this);
if (this.runtimeFeatures & RuntimeFeatures.visitGlobals) compileVisitGlobals(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is done so the classReference.visitRef != 0 check in getVisitInstanceName doesn't erroneously evaluate to false (due to ensureVisitMembersOf not being called before compileVisitGlobals if you didn't reorder these two lines).

In that case, maybe add a comment here or in getVisitInstanceName explaining why compileVisitMembers has to be called before compileVisitGlobals...after all, it took me a few minutes of thinking/code searching to make sure that the classReference.visitRef check is safe, and that led me back to this diff.


let memoryOffset = i64_align(this.memoryOffset, options.usizeType.byteSize);

Expand Down
4 changes: 4 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4910,6 +4910,10 @@ export class Class extends TypedElement {
}
return false;
}

get visitorFunctionName(): string {
return `${this.internalName}~visit`;
}
}

/** A yet unresolved interface. */
Expand Down
12 changes: 3 additions & 9 deletions tests/compiler/assignment-chain.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2333,15 +2333,6 @@
call $assignment-chain/setter_assignment_chain
call $assignment-chain/static_setter_assignment_chain
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
i32.const 224
local.get $0
call $~lib/rt/itcms/__visit
i32.const 32
local.get $0
call $~lib/rt/itcms/__visit
)
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
(local $2 i32)
local.get $0
Expand Down Expand Up @@ -2385,6 +2376,9 @@
end
unreachable
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
)
(func $~start
call $start:assignment-chain
)
Expand Down
4 changes: 0 additions & 4 deletions tests/compiler/assignment-chain.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
(func $~lib/rt/itcms/visitRoots
(local $0 i32)
(local $1 i32)
i32.const 1248
call $~lib/rt/itcms/__visit
i32.const 1056
call $~lib/rt/itcms/__visit
global.get $~lib/rt/itcms/pinSpace
local.tee $1
i32.load offset=4
Expand Down
38 changes: 3 additions & 35 deletions tests/compiler/bindings/esm.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2832,41 +2832,6 @@
i32.const 0
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
global.get $bindings/esm/stringGlobal
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/esm/mutableStringGlobal
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
i32.const 528
local.get $0
call $~lib/rt/itcms/__visit
i32.const 224
local.get $0
call $~lib/rt/itcms/__visit
i32.const 944
local.get $0
call $~lib/rt/itcms/__visit
i32.const 336
local.get $0
call $~lib/rt/itcms/__visit
i32.const 1072
local.get $0
call $~lib/rt/itcms/__visit
i32.const 1136
local.get $0
call $~lib/rt/itcms/__visit
)
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
(local $2 i32)
local.get $0
Expand Down Expand Up @@ -3038,6 +3003,9 @@
end
unreachable
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
)
(func $~setArgumentsLength (param $0 i32)
local.get $0
global.set $~argumentsLength
Expand Down
20 changes: 0 additions & 20 deletions tests/compiler/bindings/esm.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,6 @@
(func $~lib/rt/itcms/visitRoots
(local $0 i32)
(local $1 i32)
i32.const 1056
call $~lib/rt/itcms/__visit
global.get $bindings/esm/mutableStringGlobal
local.tee $0
if
local.get $0
call $~lib/rt/itcms/__visit
end
i32.const 1552
call $~lib/rt/itcms/__visit
i32.const 1248
call $~lib/rt/itcms/__visit
i32.const 1968
call $~lib/rt/itcms/__visit
i32.const 1360
call $~lib/rt/itcms/__visit
i32.const 2096
call $~lib/rt/itcms/__visit
i32.const 2160
call $~lib/rt/itcms/__visit
global.get $~lib/rt/itcms/pinSpace
local.tee $1
i32.load offset=4
Expand Down
71 changes: 24 additions & 47 deletions tests/compiler/bindings/noExportRuntime.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2415,53 +2415,6 @@
)
(func $bindings/noExportRuntime/takesFunction (param $fn i32)
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
global.get $bindings/noExportRuntime/isString
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/noExportRuntime/isBuffer
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/noExportRuntime/isTypedArray
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/noExportRuntime/isArrayOfBasic
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/noExportRuntime/isArrayOfArray
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
i32.const 368
local.get $0
call $~lib/rt/itcms/__visit
i32.const 64
local.get $0
call $~lib/rt/itcms/__visit
i32.const 176
local.get $0
call $~lib/rt/itcms/__visit
)
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
(local $2 i32)
local.get $0
Expand Down Expand Up @@ -2557,6 +2510,30 @@
end
unreachable
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
global.get $bindings/noExportRuntime/isTypedArray
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/noExportRuntime/isArrayOfBasic
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/noExportRuntime/isArrayOfArray
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
)
(func $~start
global.get $~started
if
Expand Down
14 changes: 0 additions & 14 deletions tests/compiler/bindings/noExportRuntime.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@
(func $~lib/rt/itcms/visitRoots
(local $0 i32)
(local $1 i32)
i32.const 1056
call $~lib/rt/itcms/__visit
global.get $bindings/noExportRuntime/isBuffer
local.tee $0
if
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/noExportRuntime/isTypedArray
local.tee $0
if
Expand All @@ -89,12 +81,6 @@
call $~lib/rt/itcms/__visit
i32.const 1712
call $~lib/rt/itcms/__visit
i32.const 1392
call $~lib/rt/itcms/__visit
i32.const 1088
call $~lib/rt/itcms/__visit
i32.const 1200
call $~lib/rt/itcms/__visit
global.get $~lib/rt/itcms/pinSpace
local.tee $1
i32.load offset=4
Expand Down
38 changes: 3 additions & 35 deletions tests/compiler/bindings/raw.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2835,41 +2835,6 @@
i32.const 0
drop
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
i32.const 528
local.get $0
call $~lib/rt/itcms/__visit
i32.const 224
local.get $0
call $~lib/rt/itcms/__visit
i32.const 944
local.get $0
call $~lib/rt/itcms/__visit
i32.const 336
local.get $0
call $~lib/rt/itcms/__visit
i32.const 1072
local.get $0
call $~lib/rt/itcms/__visit
i32.const 1136
local.get $0
call $~lib/rt/itcms/__visit
global.get $bindings/esm/stringGlobal
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
global.get $bindings/esm/mutableStringGlobal
local.tee $1
if
local.get $1
local.get $0
call $~lib/rt/itcms/__visit
end
)
(func $~lib/arraybuffer/ArrayBufferView~visit (param $0 i32) (param $1 i32)
(local $2 i32)
local.get $0
Expand Down Expand Up @@ -3041,6 +3006,9 @@
end
unreachable
)
(func $~lib/rt/__visit_globals (param $0 i32)
(local $1 i32)
)
(func $~setArgumentsLength (param $0 i32)
local.get $0
global.set $~argumentsLength
Expand Down
Loading
Loading