[hir] Don't redefine context refs in EnterSSA
Context references are special since reassignments need to update the original reference, not define a new place.
Sathya Gunasekaran committed
Mar 3, 2023 at 13:35 UTC
1fffc842bccd0754f4f5712240579afc8d33f8ba
3 files changed
+19
-16
compiler/forget/src/SSA/EnterSSA.ts
+13
-1
@@ -36,6 +36,7 @@ class SSABuilder {
36
#blocks: Map<BlockId, BasicBlock>;
37
#env: Environment;
38
#unknown: Set<Identifier> = new Set();
39
+ #context: Set<Identifier> = new Set();
40
41
constructor(env: Environment, blocks: Map<BlockId, BasicBlock>) {
42
this.#blocks = blocks;
@@ -67,6 +68,12 @@ class SSABuilder {
68
};
69
}
70
71
+ defineContext(oldPlace: Place): Place {
72
+ const newPlace = this.definePlace(oldPlace);
73
+ this.#context.add(oldPlace.identifier);
74
+ return newPlace;
75
+ }
76
+
77
definePlace(oldPlace: Place): Place {
78
const oldId = oldPlace.identifier;
79
if (this.#unknown.has(oldId)) {
@@ -78,6 +85,11 @@ class SSABuilder {
85
);
86
}
87
88
+ // Do not redefine context references.
89
+ if (this.#context.has(oldId)) {
90
+ return this.getPlace(oldPlace);
91
+ }
92
+
93
const newId = this.makeId(oldId);
94
this.state().defs.set(oldId, newId);
95
return {
@@ -206,7 +218,7 @@ export default function enterSSA(func: HIRFunction): void {
218
builder.startBlock(block);
219
220
if (func.body.entry === blockId) {
209
- func.context = func.context.map((p) => builder.definePlace(p));
221
+ func.context = func.context.map((p) => builder.defineContext(p));
222
func.params = func.params.map((p) => builder.definePlace(p));
223
}
224
compiler/forget/src/__tests__/fixtures/hir/capturing-func-simple-alias.expect.md
renamed
+6
-15
@@ -18,29 +18,20 @@ function component(a) {
18
19
```javascript
20
function component(a) {
21
- const $ = React.unstable_useMemoCache(4);
21
+ const $ = React.unstable_useMemoCache(2);
22
const c_0 = $[0] !== a;
23
- let t0;
24
- if (c_0) {
25
- t0 = { a: a };
26
- $[0] = a;
27
- $[1] = t0;
28
- } else {
29
- t0 = $[1];
30
- }
31
- const x = t0;
32
- const c_2 = $[2] !== x;
23
let y;
34
- if (c_2) {
24
+ if (c_0) {
25
+ const x = { a: a };
26
y = {};
27
(function () {
28
y = x;
29
})();
30
mutate(y);
40
- $[2] = x;
41
- $[3] = y;
31
+ $[0] = a;
32
+ $[1] = y;
33
} else {
43
- y = $[3];
34
+ y = $[1];
35
}
36
return y;
37
}