From e336ea44ed14fa20568b4c9fd63e4667437e6e85 Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Wed, 9 Nov 2022 13:39:50 -0500 Subject: [PATCH] [playground] include HIR output (#756) --- compiler/forget/src/CompilerDriver.ts | 5 +++ compiler/forget/src/CompilerOutputs.ts | 10 ++++- compiler/forget/src/HIR/DumpHIRPass.ts | 54 ++++++++++++++++++++++++++ compiler/forget/src/Pass.ts | 1 + 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 compiler/forget/src/HIR/DumpHIRPass.ts diff --git a/compiler/forget/src/CompilerDriver.ts b/compiler/forget/src/CompilerDriver.ts index 2f83f05660..324aabb86a 100644 --- a/compiler/forget/src/CompilerDriver.ts +++ b/compiler/forget/src/CompilerDriver.ts @@ -10,6 +10,7 @@ import * as t from "@babel/types"; import * as BE from "./BackEnd"; import { CompilerContext } from "./CompilerContext"; import { CompilerOptions } from "./CompilerOptions"; +import DumpHIRPass from "./HIR/DumpHIRPass"; import * as ME from "./MiddleEnd"; import { PassManager } from "./PassManager"; import * as Validation from "./Validation"; @@ -37,6 +38,10 @@ export function createCompilerDriver( compile() { const passManager = new PassManager(program, context); + // New architecture pass to print HIR, unrelated to the + // other passes here and just here for the playground. + passManager.addPass(DumpHIRPass); + // Syntax Analysis and IR Generation. passManager.addPass(ME.ReactFuncsInfer); passManager.addPass(ME.ParamAnalysis); diff --git a/compiler/forget/src/CompilerOutputs.ts b/compiler/forget/src/CompilerOutputs.ts index bb8055ee70..9accbf52b4 100644 --- a/compiler/forget/src/CompilerOutputs.ts +++ b/compiler/forget/src/CompilerOutputs.ts @@ -16,6 +16,7 @@ import { SCCGraphSnapshot, ValGraphSnapshot } from "./DepGraph"; */ export enum OutputKind { IR = "DumpIR", + HIR = "DumpHIR", CFG = "DumpCFG", ValGraph = "DumpValGraph", SCCGraph = "DumpSCCGraph", @@ -33,6 +34,7 @@ export function isSyntacticallyValidJS(kind: OutputKind) { export interface CompilerOutputs { [OutputKind.IR]: string; + [OutputKind.HIR]: string; [OutputKind.CFG]: string; [OutputKind.ValGraph]: ValGraphSnapshot[]; [OutputKind.SCCGraph]: SCCGraphSnapshot[]; @@ -44,6 +46,7 @@ export interface CompilerOutputs { export function createCompilerOutputs(): CompilerOutputs { return { [OutputKind.IR]: "", + [OutputKind.HIR]: "", [OutputKind.CFG]: "", [OutputKind.ValGraph]: [], [OutputKind.SCCGraph]: [], @@ -64,6 +67,7 @@ export function stringifyCompilerOutputs( ): PrettyCompilerOutputs { const outputs: PrettyCompilerOutputs = { [OutputKind.IR]: undefined, + [OutputKind.HIR]: undefined, [OutputKind.CFG]: undefined, [OutputKind.ValGraph]: undefined, [OutputKind.SCCGraph]: undefined, @@ -91,8 +95,10 @@ export function stringifyCompilerOutputs( console.warn("Error formatting output:", e); } } - } else if (outputKind === OutputKind.CFG) { - invariant(typeof output === "string", "CFG output must be a string"); + } else if ( + outputKind === OutputKind.CFG || + outputKind === OutputKind.HIR + ) { outputs[outputKind] = output; } else { invariant(Array.isArray(output), "Graph outputs are arrays."); diff --git a/compiler/forget/src/HIR/DumpHIRPass.ts b/compiler/forget/src/HIR/DumpHIRPass.ts new file mode 100644 index 0000000000..6af4f37a12 --- /dev/null +++ b/compiler/forget/src/HIR/DumpHIRPass.ts @@ -0,0 +1,54 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import type { NodePath } from "@babel/traverse"; +import * as t from "@babel/types"; +import type { CompilerContext } from "../CompilerContext"; +import { OutputKind } from "../CompilerOutputs"; +import { PassKind, PassName } from "../Pass"; + +import { lower } from "../HIR/BuildHIR"; +import enterSSA from "../HIR/EnterSSA"; +import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi"; +import inferReferenceEffects from "../HIR/InferReferenceEffects"; +import printHIR from "../HIR/PrintHIR"; +import { inferMutableRanges } from "../HIR/InferMutableLifetimes"; +import { Environment } from "./HIRBuilder"; + +export default { + name: PassName.DumpHIR, + kind: PassKind.Prog as const, + run, +}; + +/** + * A step in the first approach compiler pipeline that outputs + * the new architecture's HIR for the playground. + */ +export function run(program: NodePath, context: CompilerContext) { + if (!context.opts.outputKinds.includes(OutputKind.HIR)) { + return; + } + const results: string[] = []; + program.traverse({ + Function(func) { + try { + const env = new Environment(); + const ir = lower(func, env); + enterSSA(ir, env); + eliminateRedundantPhi(ir); + inferReferenceEffects(ir); + inferMutableRanges(ir); + const textHIR = printHIR(ir.body); + results.push(textHIR); + } catch (e) { + results.push(`Error: ${e}`); + } + }, + }); + context.outputs[OutputKind.HIR] = results.join("\n\n"); +} diff --git a/compiler/forget/src/Pass.ts b/compiler/forget/src/Pass.ts index 5253b58c5a..a0c06ff6de 100644 --- a/compiler/forget/src/Pass.ts +++ b/compiler/forget/src/Pass.ts @@ -20,6 +20,7 @@ export enum PassName { SketchyCodeCheck, RefKindInfer, DumpIR, + DumpHIR, IRCheck, DepGraphAnalysis, // BackEnd