Add compiler pipeline (#795)

This commit is contained in:
Sathya Gunasekaran
2022-11-21 18:42:58 +00:00
parent 584eff2d38
commit 61e0186950
2 changed files with 80 additions and 20 deletions
+64
View File
@@ -0,0 +1,64 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { NodePath } from "@babel/traverse";
import * as t from "@babel/types";
import { lower } from "../HIR/BuildHIR";
import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi";
import enterSSA from "../HIR/EnterSSA";
import { Environment } from "../HIR/HIRBuilder";
import { inferMutableRanges } from "../HIR/InferMutableLifetimes";
import inferReferenceEffects from "../HIR/InferReferenceEffects";
import leaveSSA from "../HIR/LeaveSSA";
import codegen from "./Codegen";
import { HIRFunction } from "./HIR";
import { inferReactiveScopeVariables } from "./InferReactiveScopeVariables";
export type CompilerFlags = {
eliminateRedundantPhi: boolean;
inferReferenceEffects: boolean;
inferMutableRanges: boolean;
inferReactiveScopeVariables: boolean;
leaveSSA: boolean;
codegen: boolean;
};
export type CompilerResult = {
ir: HIRFunction;
ast: t.Function | null;
};
export default function (
func: NodePath<t.FunctionDeclaration>,
flags: CompilerFlags
): CompilerResult {
const env = new Environment();
const ir = lower(func, env);
enterSSA(ir, env);
if (flags.eliminateRedundantPhi) {
eliminateRedundantPhi(ir);
}
if (flags.inferReferenceEffects) {
inferReferenceEffects(ir);
}
if (flags.inferMutableRanges) {
inferMutableRanges(ir);
}
if (flags.inferReactiveScopeVariables) {
inferReactiveScopeVariables(ir);
}
if (flags.leaveSSA) {
leaveSSA(ir);
}
if (flags.codegen) {
return {
ast: codegen(ir),
ir: ir,
};
}
return { ast: null, ir: ir };
}
+16 -20
View File
@@ -14,18 +14,9 @@ import { wasmFolder } from "@hpcc-js/wasm";
import invariant from "invariant";
import path from "path";
import prettier from "prettier";
import { lower } from "../HIR/BuildHIR";
import codegen from "../HIR/Codegen";
import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi";
import enterSSA from "../HIR/EnterSSA";
import { HIRFunction } from "../HIR/HIR";
import { Environment } from "../HIR/HIRBuilder";
import { inferMutableRanges } from "../HIR/InferMutableLifetimes";
import { inferReactiveScopeVariables } from "../HIR/InferReactiveScopeVariables";
import inferReferenceEffects from "../HIR/InferReferenceEffects";
import leaveSSA from "../HIR/LeaveSSA";
import printHIR from "../HIR/PrintHIR";
import visualizeHIRMermaid from "../HIR/VisualizeHIRMermaid";
import run from "./../HIR/Pipeline";
import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures";
function wrapWithTripleBackticks(s: string, ext?: string) {
@@ -65,18 +56,23 @@ describe("React Forget (HIR version)", () => {
traverse(ast, {
FunctionDeclaration: {
enter(nodePath) {
const env: Environment = new Environment();
const ir: HIRFunction = lower(nodePath, env);
enterSSA(ir, env);
eliminateRedundantPhi(ir);
inferReferenceEffects(ir);
inferMutableRanges(ir);
inferReactiveScopeVariables(ir);
leaveSSA(ir);
const compilerFlags = {
eliminateRedundantPhi: true,
inferReferenceEffects: true,
inferMutableRanges: true,
leaveSSA: true,
inferReactiveScopeVariables: true,
codegen: true,
};
const { ast, ir } = run(nodePath, compilerFlags);
invariant(
ast !== null,
"ast is null when codegen option is enabled"
);
const textHIR = printHIR(ir.body);
const visualization = visualizeHIRMermaid(ir);
const ast = codegen(ir);
const text = prettier.format(
generate(ast).code.replace("\n\n", "\n"),
{