[playground] include HIR output (#756)
Jan Kassens committed
Nov 9, 2022 at 13:39 UTC
e336ea44ed14fa20568b4c9fd63e4667437e6e85
4 files changed
+68
-2
compiler/forget/src/CompilerDriver.ts
+5
@@ -10,6 +10,7 @@ import * as t from "@babel/types";
10
import * as BE from "./BackEnd";
11
import { CompilerContext } from "./CompilerContext";
12
import { CompilerOptions } from "./CompilerOptions";
13
+import DumpHIRPass from "./HIR/DumpHIRPass";
14
import * as ME from "./MiddleEnd";
15
import { PassManager } from "./PassManager";
16
import * as Validation from "./Validation";
@@ -37,6 +38,10 @@ export function createCompilerDriver(
38
compile() {
39
const passManager = new PassManager(program, context);
40
41
+ // New architecture pass to print HIR, unrelated to the
42
+ // other passes here and just here for the playground.
43
+ passManager.addPass(DumpHIRPass);
44
+
45
// Syntax Analysis and IR Generation.
46
passManager.addPass(ME.ReactFuncsInfer);
47
passManager.addPass(ME.ParamAnalysis);
compiler/forget/src/CompilerOutputs.ts
+8
-2
@@ -16,6 +16,7 @@ import { SCCGraphSnapshot, ValGraphSnapshot } from "./DepGraph";
16
*/
17
export enum OutputKind {
18
IR = "DumpIR",
19
+ HIR = "DumpHIR",
20
CFG = "DumpCFG",
21
ValGraph = "DumpValGraph",
22
SCCGraph = "DumpSCCGraph",
@@ -33,6 +34,7 @@ export function isSyntacticallyValidJS(kind: OutputKind) {
34
35
export interface CompilerOutputs {
36
[OutputKind.IR]: string;
37
+ [OutputKind.HIR]: string;
38
[OutputKind.CFG]: string;
39
[OutputKind.ValGraph]: ValGraphSnapshot[];
40
[OutputKind.SCCGraph]: SCCGraphSnapshot[];
@@ -44,6 +46,7 @@ export interface CompilerOutputs {
46
export function createCompilerOutputs(): CompilerOutputs {
47
return {
48
[OutputKind.IR]: "",
49
+ [OutputKind.HIR]: "",
50
[OutputKind.CFG]: "",
51
[OutputKind.ValGraph]: [],
52
[OutputKind.SCCGraph]: [],
@@ -64,6 +67,7 @@ export function stringifyCompilerOutputs(
67
): PrettyCompilerOutputs {
68
const outputs: PrettyCompilerOutputs = {
69
[OutputKind.IR]: undefined,
70
+ [OutputKind.HIR]: undefined,
71
[OutputKind.CFG]: undefined,
72
[OutputKind.ValGraph]: undefined,
73
[OutputKind.SCCGraph]: undefined,
@@ -91,8 +95,10 @@ export function stringifyCompilerOutputs(
95
console.warn("Error formatting output:", e);
96
}
97
}
94
- } else if (outputKind === OutputKind.CFG) {
95
- invariant(typeof output === "string", "CFG output must be a string");
98
+ } else if (
99
+ outputKind === OutputKind.CFG ||
100
+ outputKind === OutputKind.HIR
101
+ ) {
102
outputs[outputKind] = output;
103
} else {
104
invariant(Array.isArray(output), "Graph outputs are arrays.");
compiler/forget/src/HIR/DumpHIRPass.ts
new
+54
@@ -0,0 +1,54 @@
1
+/**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+import type { NodePath } from "@babel/traverse";
9
+import * as t from "@babel/types";
10
+import type { CompilerContext } from "../CompilerContext";
11
+import { OutputKind } from "../CompilerOutputs";
12
+import { PassKind, PassName } from "../Pass";
13
+
14
+import { lower } from "../HIR/BuildHIR";
15
+import enterSSA from "../HIR/EnterSSA";
16
+import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi";
17
+import inferReferenceEffects from "../HIR/InferReferenceEffects";
18
+import printHIR from "../HIR/PrintHIR";
19
+import { inferMutableRanges } from "../HIR/InferMutableLifetimes";
20
+import { Environment } from "./HIRBuilder";
21
+
22
+export default {
23
+ name: PassName.DumpHIR,
24
+ kind: PassKind.Prog as const,
25
+ run,
26
+};
27
+
28
+/**
29
+ * A step in the first approach compiler pipeline that outputs
30
+ * the new architecture's HIR for the playground.
31
+ */
32
+export function run(program: NodePath<t.Program>, context: CompilerContext) {
33
+ if (!context.opts.outputKinds.includes(OutputKind.HIR)) {
34
+ return;
35
+ }
36
+ const results: string[] = [];
37
+ program.traverse({
38
+ Function(func) {
39
+ try {
40
+ const env = new Environment();
41
+ const ir = lower(func, env);
42
+ enterSSA(ir, env);
43
+ eliminateRedundantPhi(ir);
44
+ inferReferenceEffects(ir);
45
+ inferMutableRanges(ir);
46
+ const textHIR = printHIR(ir.body);
47
+ results.push(textHIR);
48
+ } catch (e) {
49
+ results.push(`Error: ${e}`);
50
+ }
51
+ },
52
+ });
53
+ context.outputs[OutputKind.HIR] = results.join("\n\n");
54
+}
compiler/forget/src/Pass.ts
+1
@@ -20,6 +20,7 @@ export enum PassName {
20
SketchyCodeCheck,
21
RefKindInfer,
22
DumpIR,
23
+ DumpHIR,
24
IRCheck,
25
DepGraphAnalysis,
26
// BackEnd