[compiler][hir-rewrite] Cleanup Identifier -> IdentifierId
Since removing ExitSSA, Identifier and IdentifierId should mean the same thing ghstack-source-id: 076cacbe8360e716b0555088043502823f9ee72e Pull Request resolved: https://github.com/facebook/react/pull/31034
Mofei Zhang committed
Sep 30, 2024 at 12:24 UTC
58a3ca3b47f6a51cea48ea95ded26c9887baca38
2 files changed
+53
-62
compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts
+5
-61
@@ -9,7 +9,6 @@ import {
9
Identifier,
10
IdentifierId,
11
InstructionId,
12
- Place,
12
ReactiveScopeDependency,
13
ScopeId,
14
} from './HIR';
@@ -88,61 +87,6 @@ export type BlockInfo = {
87
assumedNonNullObjects: ReadonlySet<PropertyLoadNode>;
88
};
89
91
-export function getProperty(
92
- object: Place,
93
- propertyName: string,
94
- temporaries: ReadonlyMap<IdentifierId, ReactiveScopeDependency>,
95
-): ReactiveScopeDependency {
96
- /*
97
- * (1) Get the base object either from the temporary sidemap (e.g. a LoadLocal)
98
- * or a deep copy of an existing property dependency.
99
- * Example 1:
100
- * $0 = LoadLocal x
101
- * $1 = PropertyLoad $0.y
102
- * getProperty($0, ...) -> resolvedObject = x, resolvedDependency = null
103
- *
104
- * Example 2:
105
- * $0 = LoadLocal x
106
- * $1 = PropertyLoad $0.y
107
- * $2 = PropertyLoad $1.z
108
- * getProperty($1, ...) -> resolvedObject = null, resolvedDependency = x.y
109
- *
110
- * Example 3:
111
- * $0 = Call(...)
112
- * $1 = PropertyLoad $0.y
113
- * getProperty($0, ...) -> resolvedObject = null, resolvedDependency = null
114
- */
115
- const resolvedDependency = temporaries.get(object.identifier.id);
116
-
117
- /**
118
- * (2) Push the last PropertyLoad
119
- * TODO(mofeiZ): understand optional chaining
120
- */
121
- let property: ReactiveScopeDependency;
122
- if (resolvedDependency == null) {
123
- property = {
124
- identifier: object.identifier,
125
- path: [{property: propertyName, optional: false}],
126
- };
127
- } else {
128
- property = {
129
- identifier: resolvedDependency.identifier,
130
- path: [
131
- ...resolvedDependency.path,
132
- {property: propertyName, optional: false},
133
- ],
134
- };
135
- }
136
- return property;
137
-}
138
-
139
-export function resolveTemporary(
140
- place: Place,
141
- temporaries: ReadonlyMap<IdentifierId, Identifier>,
142
-): Identifier {
143
- return temporaries.get(place.identifier.id) ?? place.identifier;
144
-}
145
-
90
/**
91
* Tree data structure to dedupe property loads (e.g. a.b.c)
92
* and make computing sets intersections simpler.
@@ -152,7 +96,7 @@ type RootNode = {
96
parent: null;
97
// Recorded to make later computations simpler
98
fullPath: ReactiveScopeDependency;
155
- root: Identifier;
99
+ root: IdentifierId;
100
};
101
102
type PropertyLoadNode =
@@ -164,18 +108,18 @@ type PropertyLoadNode =
108
| RootNode;
109
110
class Tree {
167
- roots: Map<Identifier, RootNode> = new Map();
111
+ roots: Map<IdentifierId, RootNode> = new Map();
112
113
getOrCreateRoot(identifier: Identifier): PropertyLoadNode {
114
/**
115
* Reads from a statically scoped variable are always safe in JS,
116
* with the exception of TDZ (not addressed by this pass).
117
*/
174
- let rootNode = this.roots.get(identifier);
118
+ let rootNode = this.roots.get(identifier.id);
119
120
if (rootNode === undefined) {
121
rootNode = {
178
- root: identifier,
122
+ root: identifier.id,
123
properties: new Map(),
124
fullPath: {
125
identifier,
@@ -183,7 +127,7 @@ class Tree {
127
},
128
parent: null,
129
};
186
- this.roots.set(identifier, rootNode);
130
+ this.roots.set(identifier.id, rootNode);
131
}
132
return rootNode;
133
}
compiler/packages/babel-plugin-react-compiler/src/HIR/PropagateScopeDependenciesHIR.ts
+48
-1
@@ -20,7 +20,6 @@ import {
20
import {
21
BlockInfo,
22
collectHoistablePropertyLoads,
23
- getProperty,
23
} from './CollectHoistablePropertyLoads';
24
import {
25
ScopeBlockTraversal,
@@ -220,6 +219,54 @@ function collectTemporariesSidemap(
219
return temporaries;
220
}
221
222
+function getProperty(
223
+ object: Place,
224
+ propertyName: string,
225
+ temporaries: ReadonlyMap<IdentifierId, ReactiveScopeDependency>,
226
+): ReactiveScopeDependency {
227
+ /*
228
+ * (1) Get the base object either from the temporary sidemap (e.g. a LoadLocal)
229
+ * or a deep copy of an existing property dependency.
230
+ * Example 1:
231
+ * $0 = LoadLocal x
232
+ * $1 = PropertyLoad $0.y
233
+ * getProperty($0, ...) -> resolvedObject = x, resolvedDependency = null
234
+ *
235
+ * Example 2:
236
+ * $0 = LoadLocal x
237
+ * $1 = PropertyLoad $0.y
238
+ * $2 = PropertyLoad $1.z
239
+ * getProperty($1, ...) -> resolvedObject = null, resolvedDependency = x.y
240
+ *
241
+ * Example 3:
242
+ * $0 = Call(...)
243
+ * $1 = PropertyLoad $0.y
244
+ * getProperty($0, ...) -> resolvedObject = null, resolvedDependency = null
245
+ */
246
+ const resolvedDependency = temporaries.get(object.identifier.id);
247
+
248
+ /**
249
+ * (2) Push the last PropertyLoad
250
+ * TODO(mofeiZ): understand optional chaining
251
+ */
252
+ let property: ReactiveScopeDependency;
253
+ if (resolvedDependency == null) {
254
+ property = {
255
+ identifier: object.identifier,
256
+ path: [{property: propertyName, optional: false}],
257
+ };
258
+ } else {
259
+ property = {
260
+ identifier: resolvedDependency.identifier,
261
+ path: [
262
+ ...resolvedDependency.path,
263
+ {property: propertyName, optional: false},
264
+ ],
265
+ };
266
+ }
267
+ return property;
268
+}
269
+
270
type Decl = {
271
id: InstructionId;
272
scope: Stack<ReactiveScope>;