Skip to content
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

[compiler][hir-rewrite] Cleanup Identifier -> IdentifierId #31034

Merged
merged 3 commits into from
Sep 30, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
Identifier,
IdentifierId,
InstructionId,
Place,
ReactiveScopeDependency,
ScopeId,
} from './HIR';
Expand Down Expand Up @@ -88,61 +87,6 @@ export type BlockInfo = {
assumedNonNullObjects: ReadonlySet<PropertyLoadNode>;
};

export function getProperty(
object: Place,
propertyName: string,
temporaries: ReadonlyMap<IdentifierId, ReactiveScopeDependency>,
): ReactiveScopeDependency {
/*
* (1) Get the base object either from the temporary sidemap (e.g. a LoadLocal)
* or a deep copy of an existing property dependency.
* Example 1:
* $0 = LoadLocal x
* $1 = PropertyLoad $0.y
* getProperty($0, ...) -> resolvedObject = x, resolvedDependency = null
*
* Example 2:
* $0 = LoadLocal x
* $1 = PropertyLoad $0.y
* $2 = PropertyLoad $1.z
* getProperty($1, ...) -> resolvedObject = null, resolvedDependency = x.y
*
* Example 3:
* $0 = Call(...)
* $1 = PropertyLoad $0.y
* getProperty($0, ...) -> resolvedObject = null, resolvedDependency = null
*/
const resolvedDependency = temporaries.get(object.identifier.id);

/**
* (2) Push the last PropertyLoad
* TODO(mofeiZ): understand optional chaining
*/
let property: ReactiveScopeDependency;
if (resolvedDependency == null) {
property = {
identifier: object.identifier,
path: [{property: propertyName, optional: false}],
};
} else {
property = {
identifier: resolvedDependency.identifier,
path: [
...resolvedDependency.path,
{property: propertyName, optional: false},
],
};
}
return property;
}

export function resolveTemporary(
place: Place,
temporaries: ReadonlyMap<IdentifierId, Identifier>,
): Identifier {
return temporaries.get(place.identifier.id) ?? place.identifier;
}

/**
* Tree data structure to dedupe property loads (e.g. a.b.c)
* and make computing sets intersections simpler.
Expand All @@ -152,7 +96,7 @@ type RootNode = {
parent: null;
// Recorded to make later computations simpler
fullPath: ReactiveScopeDependency;
root: Identifier;
root: IdentifierId;
};

type PropertyLoadNode =
Expand All @@ -164,26 +108,26 @@ type PropertyLoadNode =
| RootNode;

class Tree {
roots: Map<Identifier, RootNode> = new Map();
roots: Map<IdentifierId, RootNode> = new Map();

getOrCreateRoot(identifier: Identifier): PropertyLoadNode {
/**
* Reads from a statically scoped variable are always safe in JS,
* with the exception of TDZ (not addressed by this pass).
*/
let rootNode = this.roots.get(identifier);
let rootNode = this.roots.get(identifier.id);

if (rootNode === undefined) {
rootNode = {
root: identifier,
root: identifier.id,
properties: new Map(),
fullPath: {
identifier,
path: [],
},
parent: null,
};
this.roots.set(identifier, rootNode);
this.roots.set(identifier.id, rootNode);
}
return rootNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
import {
BlockInfo,
collectHoistablePropertyLoads,
getProperty,
} from './CollectHoistablePropertyLoads';
import {
ScopeBlockTraversal,
Expand Down Expand Up @@ -220,6 +219,54 @@ function collectTemporariesSidemap(
return temporaries;
}

function getProperty(
object: Place,
propertyName: string,
temporaries: ReadonlyMap<IdentifierId, ReactiveScopeDependency>,
): ReactiveScopeDependency {
/*
* (1) Get the base object either from the temporary sidemap (e.g. a LoadLocal)
* or a deep copy of an existing property dependency.
* Example 1:
* $0 = LoadLocal x
* $1 = PropertyLoad $0.y
* getProperty($0, ...) -> resolvedObject = x, resolvedDependency = null
*
* Example 2:
* $0 = LoadLocal x
* $1 = PropertyLoad $0.y
* $2 = PropertyLoad $1.z
* getProperty($1, ...) -> resolvedObject = null, resolvedDependency = x.y
*
* Example 3:
* $0 = Call(...)
* $1 = PropertyLoad $0.y
* getProperty($0, ...) -> resolvedObject = null, resolvedDependency = null
*/
const resolvedDependency = temporaries.get(object.identifier.id);

/**
* (2) Push the last PropertyLoad
* TODO(mofeiZ): understand optional chaining
*/
let property: ReactiveScopeDependency;
if (resolvedDependency == null) {
property = {
identifier: object.identifier,
path: [{property: propertyName, optional: false}],
};
} else {
property = {
identifier: resolvedDependency.identifier,
path: [
...resolvedDependency.path,
{property: propertyName, optional: false},
],
};
}
return property;
}

type Decl = {
id: InstructionId;
scope: Stack<ReactiveScope>;
Expand Down