@samitouri / QOS-React / commits / b9ce8bdcfd

[SSA] Fix identifierID hack

Rather than starting from 1000, start from the last used identifier id.

Sathya Gunasekaran committed Oct 18, 2022 at 18:57 UTC b9ce8bdcfde51a4f7b4f1068e58ba99450efbb47
16 files changed +184 -156
compiler/forget/src/HIR/BuildHIR.ts
+6 -3
@@ -20,7 +20,7 @@ import {
20 Terminal,
21 ThrowTerminal,
22 } from "./HIR";
23 -import HIRBuilder from "./HIRBuilder";
23 +import HIRBuilder, { Environment } from "./HIRBuilder";
24 import todo, { todoInvariant } from "./todo";
25
26 // *******************************************************************************************
@@ -46,8 +46,11 @@ const GLOBALS: Map<string, t.Identifier> = new Map([
46 * TODO: consider modeling control-flow at expression level for even more fine-
47 * grained reactivity.
48 */
49 -export function lower(func: NodePath<t.Function>): HIRFunction {
50 - const builder = new HIRBuilder();
49 +export function lower(
50 + func: NodePath<t.Function>,
51 + env: Environment
52 +): HIRFunction {
53 + const builder = new HIRBuilder(env);
54
55 const id =
56 func.isFunctionDeclaration() && func.node.id != null
compiler/forget/src/HIR/HIR.ts
-8
@@ -292,11 +292,3 @@ export function makeScopeId(id: number): ScopeId {
292 */
293 const opaqueIdentifierId = Symbol();
294 export type IdentifierId = number & { [opaqueIdentifierId]: "IdentifierId" };
295 -
296 -export function makeIdentifierId(id: number): IdentifierId {
297 - invariant(
298 - id >= 0 && Number.isInteger(id),
299 - "Expected identifier id to be a non-negative integer"
300 - );
301 - return id as IdentifierId;
302 -}
compiler/forget/src/HIR/HIRBuilder.ts
+27 -4
@@ -16,7 +16,6 @@ import {
16 IdentifierId,
17 Instruction,
18 makeBlockId,
19 - makeIdentifierId,
19 Terminal,
20 } from "./HIR";
21
@@ -56,6 +55,22 @@ function newBlock(id: BlockId): WipBlock {
55 return { id, instructions: [] };
56 }
57
58 +export class Environment {
59 + #nextIdentifer: number = 0;
60 +
61 + #makeIdentifierId(id: number): IdentifierId {
62 + invariant(
63 + id >= 0 && Number.isInteger(id),
64 + "Expected identifier id to be a non-negative integer"
65 + );
66 + return id as IdentifierId;
67 + }
68 +
69 + get nextIdentifierId(): IdentifierId {
70 + return this.#makeIdentifierId(this.#nextIdentifer++);
71 + }
72 +}
73 +
74 /**
75 * Helper class for constructing a CFG
76 */
@@ -65,8 +80,16 @@ export default class HIRBuilder {
80 #current: WipBlock = newBlock(makeBlockId(0));
81 #entry: BlockId = makeBlockId(0);
82 #scopes: Array<Scope> = [];
68 - #nextIdentifier: IdentifierId = makeIdentifierId(0);
83 #bindings: Map<t.Identifier, Identifier> = new Map();
84 + #env: Environment;
85 +
86 + get nextIdentifierId() {
87 + return this.#env.nextIdentifierId;
88 + }
89 +
90 + constructor(env: Environment) {
91 + this.#env = env;
92 + }
93
94 debug(): string {
95 return JSON.stringify(
@@ -88,7 +111,7 @@ export default class HIRBuilder {
111 }
112
113 makeTemporary(): Identifier {
91 - const id = makeIdentifierId(this.#nextIdentifier++);
114 + const id = this.nextIdentifierId;
115 return {
116 id,
117 name: null,
@@ -98,7 +121,7 @@ export default class HIRBuilder {
121 resolveIdentifier(node: t.Identifier): Identifier {
122 let identifier = this.#bindings.get(node);
123 if (identifier == null) {
101 - const id = makeIdentifierId(this.#nextIdentifier++);
124 + const id = this.nextIdentifierId;
125 identifier = { id, name: node.name };
126 this.#bindings.set(node, identifier);
127 }
compiler/forget/src/HIR/SSAify.ts
+14 -6
@@ -8,10 +8,10 @@ import {
8 IdentifierId,
9 Instruction,
10 InstructionKind,
11 - makeIdentifierId,
11 Phi,
12 Place,
13 } from "./HIR";
14 +import { Environment } from "./HIRBuilder";
15
16 type IncompletePhi = {
17 old: Place;
@@ -29,6 +29,15 @@ class SSABuilder {
29 #states: Map<BasicBlock, State> = new Map();
30 #current: BasicBlock | null = null;
31 visitedBlocks: Set<BasicBlock> = new Set();
32 + #env: Environment;
33 +
34 + constructor(env: Environment) {
35 + this.#env = env;
36 + }
37 +
38 + get nextIdentifierId() {
39 + return this.#env.nextIdentifierId;
40 + }
41
42 // Hack(gsn): Start from the last stored id in HIRBuilder.
43 // Need to refactor makeTemporary and relevant state out of HIR Builder.
@@ -47,7 +56,7 @@ class SSABuilder {
56 makePlace(oldPlace: Place): Place {
57 const identifier = {
58 ...oldPlace.identifier,
50 - id: makeIdentifierId(this.#id++),
59 + id: this.nextIdentifierId,
60 };
61 return {
62 ...oldPlace,
@@ -58,7 +67,7 @@ class SSABuilder {
67 makePlaceForPhi(oldPlace: Place): Place {
68 const identifier = {
69 ...oldPlace.identifier,
61 - id: makeIdentifierId(this.#id++),
70 + id: this.nextIdentifierId,
71 };
72 return {
73 identifier,
@@ -173,9 +182,8 @@ class SSABuilder {
182 }
183 }
184
176 -export default function buildSSA(func: HIRFunction) {
177 - const builder = new SSABuilder();
178 -
185 +export default function buildSSA(func: HIRFunction, env: Environment) {
186 + const builder = new SSABuilder(env);
187 function visit(blockId: BlockId) {
188 const block = func.body.blocks.get(blockId)!;
189 if (builder.visitedBlocks.has(block)) {
compiler/forget/src/__tests__/fixtures/hir/ssa-complex-multiple-if.expect.md
+21 -21
@@ -21,30 +21,30 @@ function foo() {
21
22 ```
23 bb0:
24 - Let mutate x$1000 = 1
25 - Let mutate y$1001 = 2
26 - Const mutate $1002 = 2
27 - Const mutate $1003 = Binary mutate y$1001 === mutate $1002
28 - If (mutate $1003) then:bb2 else:bb1
24 + Let mutate x$7 = 1
25 + Let mutate y$8 = 2
26 + Const mutate $9 = 2
27 + Const mutate $10 = Binary mutate y$8 === mutate $9
28 + If (mutate $10) then:bb2 else:bb1
29 bb2:
30 predecessor blocks: bb0
31 - Reassign mutate x$1011 = 3
31 + Reassign mutate x$18 = 3
32 Goto bb1
33 bb1:
34 predecessor blocks: bb0 bb2
35 - Const mutate y$1005: phi(bb0: mutate y$1001, bb2: mutate y$1001)
36 - Const mutate x$1010: phi(bb0: mutate x$1000, bb2: mutate x$1011)
37 - Const mutate $1004 = 3
38 - Const mutate $1006 = Binary mutate y$1005 === mutate $1004
39 - If (mutate $1006) then:bb4 else:bb3
35 + Const mutate y$12: phi(bb0: mutate y$8, bb2: mutate y$8)
36 + Const mutate x$17: phi(bb0: mutate x$7, bb2: mutate x$18)
37 + Const mutate $11 = 3
38 + Const mutate $13 = Binary mutate y$12 === mutate $11
39 + If (mutate $13) then:bb4 else:bb3
40 bb4:
41 predecessor blocks: bb1
42 - Reassign mutate x$1009 = 5
42 + Reassign mutate x$16 = 5
43 Goto bb3
44 bb3:
45 predecessor blocks: bb1 bb4
46 - Const mutate x$1007: phi(bb1: mutate x$1010, bb4: mutate x$1009)
47 - Reassign mutate y$1008 = mutate x$1007
46 + Const mutate x$14: phi(bb1: mutate x$17, bb4: mutate x$16)
47 + Reassign mutate y$15 = mutate x$14
48 Return
49 ```
50
@@ -52,17 +52,17 @@ bb3:
52
53 ```javascript
54 function foo$0() {
55 - let x$1000 = 1;
56 - let y$1001 = 2;
57 - if (y$1001 === 2) {
58 - x$1011 = 3;
55 + let x$7 = 1;
56 + let y$8 = 2;
57 + if (y$8 === 2) {
58 + x$18 = 3;
59 ("<<TODO: handle complex control flow in codegen>>");
60 }
61 - if (y$1005 === 3) {
62 - x$1009 = 5;
61 + if (y$12 === 3) {
62 + x$16 = 5;
63 ("<<TODO: handle complex control flow in codegen>>");
64 }
65 - y$1008 = x$1007;
65 + y$15 = x$14;
66 return;
67 }
68
compiler/forget/src/__tests__/fixtures/hir/ssa-complex-single-if.expect.md
+13 -13
@@ -18,19 +18,19 @@ function foo() {
18
19 ```
20 bb0:
21 - Let mutate x$1000 = 1
22 - Let mutate y$1001 = 2
23 - Const mutate $1002 = 2
24 - Const mutate $1003 = Binary mutate y$1001 === mutate $1002
25 - If (mutate $1003) then:bb2 else:bb1
21 + Let mutate x$5 = 1
22 + Let mutate y$6 = 2
23 + Const mutate $7 = 2
24 + Const mutate $8 = Binary mutate y$6 === mutate $7
25 + If (mutate $8) then:bb2 else:bb1
26 bb2:
27 predecessor blocks: bb0
28 - Reassign mutate x$1006 = 3
28 + Reassign mutate x$11 = 3
29 Goto bb1
30 bb1:
31 predecessor blocks: bb0 bb2
32 - Const mutate x$1004: phi(bb0: mutate x$1000, bb2: mutate x$1006)
33 - Reassign mutate y$1005 = mutate x$1004
32 + Const mutate x$9: phi(bb0: mutate x$5, bb2: mutate x$11)
33 + Reassign mutate y$10 = mutate x$9
34 Return
35 ```
36
@@ -38,13 +38,13 @@ bb1:
38
39 ```javascript
40 function foo$0() {
41 - let x$1000 = 1;
42 - let y$1001 = 2;
43 - if (y$1001 === 2) {
44 - x$1006 = 3;
41 + let x$5 = 1;
42 + let y$6 = 2;
43 + if (y$6 === 2) {
44 + x$11 = 3;
45 ("<<TODO: handle complex control flow in codegen>>");
46 }
47 - y$1005 = x$1004;
47 + y$10 = x$9;
48 return;
49 }
50
compiler/forget/src/__tests__/fixtures/hir/ssa-for.expect.md
+9 -9
@@ -16,29 +16,29 @@ function foo() {
16
17 ```
18 bb0:
19 - Let mutate x$1000 = 0
19 + Let mutate x$5 = 0
20 Goto bb1
21 bb1:
22 predecessor blocks: bb0 bb4
23 - Const mutate x$1002: phi(bb0: mutate x$1000, bb4: mutate x$1005)
24 - Const mutate $1001 = 10
25 - Const mutate $1003 = Binary mutate x$1002 < mutate $1001
26 - If (mutate $1003) then:bb4 else:bb2
23 + Const mutate x$7: phi(bb0: mutate x$5, bb4: mutate x$10)
24 + Const mutate $6 = 10
25 + Const mutate $8 = Binary mutate x$7 < mutate $6
26 + If (mutate $8) then:bb4 else:bb2
27 bb4:
28 predecessor blocks: bb1
29 - Const mutate $1004 = 1
30 - Reassign mutate x$1005 = Binary mutate x$1002 + mutate $1004
29 + Const mutate $9 = 1
30 + Reassign mutate x$10 = Binary mutate x$7 + mutate $9
31 Goto bb1
32 bb2:
33 predecessor blocks: bb1
34 - Return mutate x$1002
34 + Return mutate x$7
35 ```
36
37 ## Code
38
39 ```javascript
40 function foo$0() {
41 - let x$1000 = 0;
41 + let x$5 = 0;
42 ("<<TODO: handle complex control flow in codegen>>");
43 }
44
compiler/forget/src/__tests__/fixtures/hir/ssa-if-else.expect.md
+10 -10
@@ -19,16 +19,16 @@ function foo() {
19
20 ```
21 bb0:
22 - Let mutate x$1000 = 1
23 - Let mutate y$1001 = 2
24 - If (mutate y$1001) then:bb2 else:bb3
22 + Let mutate x$5 = 1
23 + Let mutate y$6 = 2
24 + If (mutate y$6) then:bb2 else:bb3
25 bb2:
26 predecessor blocks: bb0
27 - Let mutate z$1003 = Binary mutate x$1000 + mutate y$1001
27 + Let mutate z$8 = Binary mutate x$5 + mutate y$6
28 Goto bb1
29 bb3:
30 predecessor blocks: bb0
31 - Let mutate z$1002 = mutate x$1000
31 + Let mutate z$7 = mutate x$5
32 Goto bb1
33 bb1:
34 predecessor blocks: bb3 bb2
@@ -39,13 +39,13 @@ bb1:
39
40 ```javascript
41 function foo$0() {
42 - let x$1000 = 1;
43 - let y$1001 = 2;
44 - if (y$1001) {
45 - let z$1003 = x$1000 + y$1001;
42 + let x$5 = 1;
43 + let y$6 = 2;
44 + if (y$6) {
45 + let z$8 = x$5 + y$6;
46 ("<<TODO: handle complex control flow in codegen>>");
47 } else {
48 - let z$1002 = x$1000;
48 + let z$7 = x$5;
49 ("<<TODO: handle complex control flow in codegen>>");
50 }
51 return;
compiler/forget/src/__tests__/fixtures/hir/ssa-return.expect.md
+11 -11
@@ -17,30 +17,30 @@ function foo() {
17
18 ```
19 bb0:
20 - Let mutate x$1000 = 1
21 - Const mutate $1001 = 1
22 - Const mutate $1002 = Binary mutate x$1000 === mutate $1001
23 - If (mutate $1002) then:bb2 else:bb1
20 + Let mutate x$4 = 1
21 + Const mutate $5 = 1
22 + Const mutate $6 = Binary mutate x$4 === mutate $5
23 + If (mutate $6) then:bb2 else:bb1
24 bb2:
25 predecessor blocks: bb0
26 - Reassign mutate x$1004 = 2
26 + Reassign mutate x$8 = 2
27 Goto bb1
28 bb1:
29 predecessor blocks: bb0 bb2
30 - Const mutate x$1003: phi(bb0: mutate x$1000, bb2: mutate x$1004)
31 - Return mutate x$1003
30 + Const mutate x$7: phi(bb0: mutate x$4, bb2: mutate x$8)
31 + Return mutate x$7
32 ```
33
34 ## Code
35
36 ```javascript
37 function foo$0() {
38 - let x$1000 = 1;
39 - if (x$1000 === 1) {
40 - x$1004 = 2;
38 + let x$4 = 1;
39 + if (x$4 === 1) {
40 + x$8 = 2;
41 ("<<TODO: handle complex control flow in codegen>>");
42 }
43 - return x$1003;
43 + return x$7;
44 }
45
46 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-simple-phi.expect.md
+13 -13
@@ -20,22 +20,22 @@ function foo() {
20
21 ```
22 bb0:
23 - Let mutate y$1000 = 2
24 - Const mutate $1001 = 1
25 - Const mutate $1002 = Binary mutate y$1000 > mutate $1001
26 - If (mutate $1002) then:bb2 else:bb3
23 + Let mutate y$5 = 2
24 + Const mutate $6 = 1
25 + Const mutate $7 = Binary mutate y$5 > mutate $6
26 + If (mutate $7) then:bb2 else:bb3
27 bb2:
28 predecessor blocks: bb0
29 - Reassign mutate y$1006 = 1
29 + Reassign mutate y$11 = 1
30 Goto bb1
31 bb3:
32 predecessor blocks: bb0
33 - Reassign mutate y$1003 = 2
33 + Reassign mutate y$8 = 2
34 Goto bb1
35 bb1:
36 predecessor blocks: bb3 bb2
37 - Const mutate y$1004: phi(bb3: mutate y$1003, bb2: mutate y$1006)
38 - Let mutate x$1005 = mutate y$1004
37 + Const mutate y$9: phi(bb3: mutate y$8, bb2: mutate y$11)
38 + Let mutate x$10 = mutate y$9
39 Return
40 ```
41
@@ -43,15 +43,15 @@ bb1:
43
44 ```javascript
45 function foo$0() {
46 - let y$1000 = 2;
47 - if (y$1000 > 1) {
48 - y$1006 = 1;
46 + let y$5 = 2;
47 + if (y$5 > 1) {
48 + y$11 = 1;
49 ("<<TODO: handle complex control flow in codegen>>");
50 } else {
51 - y$1003 = 2;
51 + y$8 = 2;
52 ("<<TODO: handle complex control flow in codegen>>");
53 }
54 - let x$1005 = y$1004;
54 + let x$10 = y$9;
55 return;
56 }
57
compiler/forget/src/__tests__/fixtures/hir/ssa-simple.expect.md
+4 -4
@@ -13,8 +13,8 @@ function foo() {
13
14 ```
15 bb0:
16 - Let mutate x$1000 = 1
17 - Let mutate y$1001 = 2
16 + Let mutate x$3 = 1
17 + Let mutate y$4 = 2
18 Return
19 ```
20
@@ -22,8 +22,8 @@ bb0:
22
23 ```javascript
24 function foo$0() {
25 - let x$1000 = 1;
26 - let y$1001 = 2;
25 + let x$3 = 1;
26 + let y$4 = 2;
27 return;
28 }
29
compiler/forget/src/__tests__/fixtures/hir/ssa-single-if.expect.md
+8 -8
@@ -17,12 +17,12 @@ function foo() {
17
18 ```
19 bb0:
20 - Let mutate x$1000 = 1
21 - Let mutate y$1001 = 2
22 - If (mutate y$1001) then:bb2 else:bb1
20 + Let mutate x$4 = 1
21 + Let mutate y$5 = 2
22 + If (mutate y$5) then:bb2 else:bb1
23 bb2:
24 predecessor blocks: bb0
25 - Let mutate z$1002 = Binary mutate x$1000 + mutate y$1001
25 + Let mutate z$6 = Binary mutate x$4 + mutate y$5
26 Goto bb1
27 bb1:
28 predecessor blocks: bb0 bb2
@@ -33,10 +33,10 @@ bb1:
33
34 ```javascript
35 function foo$0() {
36 - let x$1000 = 1;
37 - let y$1001 = 2;
38 - if (y$1001) {
39 - let z$1002 = x$1000 + y$1001;
36 + let x$4 = 1;
37 + let y$5 = 2;
38 + if (y$5) {
39 + let z$6 = x$4 + y$5;
40 ("<<TODO: handle complex control flow in codegen>>");
41 }
42 return;
compiler/forget/src/__tests__/fixtures/hir/ssa-switch.expect.md
+24 -24
@@ -28,34 +28,34 @@ function foo() {
28
29 ```
30 bb0:
31 - Let mutate x$1000 = 1
32 - Const mutate $1001 = 2
33 - Const mutate $1002 = Binary mutate x$1000 === mutate $1001
34 - Const mutate $1003 = 1
35 - Const mutate $1004 = Binary mutate x$1000 === mutate $1003
36 - Switch (mutate x$1000)
37 - Case mutate $1004: bb5
38 - Case mutate $1002: bb3
31 + Let mutate x$10 = 1
32 + Const mutate $11 = 2
33 + Const mutate $12 = Binary mutate x$10 === mutate $11
34 + Const mutate $13 = 1
35 + Const mutate $14 = Binary mutate x$10 === mutate $13
36 + Switch (mutate x$10)
37 + Case mutate $14: bb5
38 + Case mutate $12: bb3
39 Default: bb2
40 bb5:
41 predecessor blocks: bb0
42 - Const mutate $1011 = 1
43 - Reassign mutate x$1012 = Binary mutate x$1000 + mutate $1011
42 + Const mutate $21 = 1
43 + Reassign mutate x$22 = Binary mutate x$10 + mutate $21
44 Goto bb1
45 bb3:
46 predecessor blocks: bb0
47 - Const mutate $1009 = 2
48 - Reassign mutate x$1010 = Binary mutate x$1000 + mutate $1009
47 + Const mutate $19 = 2
48 + Reassign mutate x$20 = Binary mutate x$10 + mutate $19
49 Goto bb1
50 bb2:
51 predecessor blocks: bb0
52 - Const mutate $1005 = 3
53 - Reassign mutate x$1006 = Binary mutate x$1000 + mutate $1005
52 + Const mutate $15 = 3
53 + Reassign mutate x$16 = Binary mutate x$10 + mutate $15
54 Goto bb1
55 bb1:
56 predecessor blocks: bb5 bb3 bb2
57 - Const mutate x$1007: phi(bb5: mutate x$1012, bb3: mutate x$1010, bb2: mutate x$1006)
58 - Let mutate y$1008 = mutate x$1007
57 + Const mutate x$17: phi(bb5: mutate x$22, bb3: mutate x$20, bb2: mutate x$16)
58 + Let mutate y$18 = mutate x$17
59 Return
60 ```
61
@@ -63,22 +63,22 @@ bb1:
63
64 ```javascript
65 function foo$0() {
66 - let x$1000 = 1;
67 - switch (x$1000) {
68 - case x$1000 === 1: {
69 - x$1012 = x$1000 + 1;
66 + let x$10 = 1;
67 + switch (x$10) {
68 + case x$10 === 1: {
69 + x$22 = x$10 + 1;
70 ("<<TODO: handle complex control flow in codegen>>");
71 }
72 - case x$1000 === 2: {
73 - x$1010 = x$1000 + 2;
72 + case x$10 === 2: {
73 + x$20 = x$10 + 2;
74 ("<<TODO: handle complex control flow in codegen>>");
75 }
76 default: {
77 - x$1006 = x$1000 + 3;
77 + x$16 = x$10 + 3;
78 ("<<TODO: handle complex control flow in codegen>>");
79 }
80 }
81 - let y$1008 = x$1007;
81 + let y$18 = x$17;
82 return;
83 }
84
compiler/forget/src/__tests__/fixtures/hir/ssa-throw.expect.md
+11 -11
@@ -16,30 +16,30 @@ function foo() {
16
17 ```
18 bb0:
19 - Let mutate x$1000 = 1
20 - Const mutate $1001 = 1
21 - Const mutate $1002 = Binary mutate x$1000 === mutate $1001
22 - If (mutate $1002) then:bb2 else:bb1
19 + Let mutate x$4 = 1
20 + Const mutate $5 = 1
21 + Const mutate $6 = Binary mutate x$4 === mutate $5
22 + If (mutate $6) then:bb2 else:bb1
23 bb2:
24 predecessor blocks: bb0
25 - Reassign mutate x$1004 = 2
25 + Reassign mutate x$8 = 2
26 Goto bb1
27 bb1:
28 predecessor blocks: bb0 bb2
29 - Const mutate x$1003: phi(bb0: mutate x$1000, bb2: mutate x$1004)
30 - Throw mutate x$1003
29 + Const mutate x$7: phi(bb0: mutate x$4, bb2: mutate x$8)
30 + Throw mutate x$7
31 ```
32
33 ## Code
34
35 ```javascript
36 function foo$0() {
37 - let x$1000 = 1;
38 - if (x$1000 === 1) {
39 - x$1004 = 2;
37 + let x$4 = 1;
38 + if (x$4 === 1) {
39 + x$8 = 2;
40 ("<<TODO: handle complex control flow in codegen>>");
41 }
42 - throw x$1003;
42 + throw x$7;
43 }
44
45 ```
compiler/forget/src/__tests__/fixtures/hir/ssa-while.expect.md
+9 -9
@@ -17,29 +17,29 @@ function foo() {
17
18 ```
19 bb0:
20 - Let mutate x$1000 = 1
20 + Let mutate x$5 = 1
21 Goto bb1
22 bb1:
23 predecessor blocks: bb0 bb3
24 - Const mutate x$1002: phi(bb0: mutate x$1000, bb3: mutate x$1005)
25 - Const mutate $1001 = 10
26 - Const mutate $1003 = Binary mutate x$1002 < mutate $1001
27 - If (mutate $1003) then:bb3 else:bb2
24 + Const mutate x$7: phi(bb0: mutate x$5, bb3: mutate x$10)
25 + Const mutate $6 = 10
26 + Const mutate $8 = Binary mutate x$7 < mutate $6
27 + If (mutate $8) then:bb3 else:bb2
28 bb3:
29 predecessor blocks: bb1
30 - Const mutate $1004 = 1
31 - Reassign mutate x$1005 = Binary mutate x$1002 + mutate $1004
30 + Const mutate $9 = 1
31 + Reassign mutate x$10 = Binary mutate x$7 + mutate $9
32 Goto bb1
33 bb2:
34 predecessor blocks: bb1
35 - Return mutate x$1002
35 + Return mutate x$7
36 ```
37
38 ## Code
39
40 ```javascript
41 function foo$0() {
42 - let x$1000 = 1;
42 + let x$5 = 1;
43 ("<<TODO: handle complex control flow in codegen>>");
44 }
45
compiler/forget/src/__tests__/hir-test.ts
+4 -2
@@ -17,6 +17,7 @@ import prettier from "prettier";
17 import { lower } from "../HIR/BuildHIR";
18 import codegen from "../HIR/Codegen";
19 import { HIRFunction } from "../HIR/HIR";
20 +import { Environment } from "../HIR/HIRBuilder";
21 import inferReferenceEffects from "../HIR/InferReferenceEffects";
22 import printHIR from "../HIR/PrintHIR";
23 import buildSSA from "../HIR/SSAify";
@@ -44,10 +45,11 @@ describe("React Forget (HIR version)", () => {
45 traverse(ast, {
46 FunctionDeclaration: {
47 enter(nodePath) {
47 - const ir: HIRFunction = lower(nodePath);
48 + const env: Environment = new Environment();
49 + const ir: HIRFunction = lower(nodePath, env);
50 inferReferenceEffects(ir);
51 if (file.startsWith("ssa")) {
50 - buildSSA(ir);
52 + buildSSA(ir, env);
53 }
54 // const lifetimeGraph = buildDefUseGraph(ir);
55 const textHIR = printHIR(ir.body);