@samitouri / QOS-React-2 / commits / 61e0186950

Add compiler pipeline (#795)

Sathya Gunasekaran committed Nov 21, 2022 at 18:42 UTC 61e018695029cc8b2ee5032831550f3880e29b4d
2 files changed +80 -20
compiler/forget/src/HIR/Pipeline.ts new
+64
@@ -0,0 +1,64 @@
1 +/**
2 + * Copyright (c) Facebook, Inc. and its 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 +import { NodePath } from "@babel/traverse";
8 +import * as t from "@babel/types";
9 +import { lower } from "../HIR/BuildHIR";
10 +import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi";
11 +import enterSSA from "../HIR/EnterSSA";
12 +import { Environment } from "../HIR/HIRBuilder";
13 +import { inferMutableRanges } from "../HIR/InferMutableLifetimes";
14 +import inferReferenceEffects from "../HIR/InferReferenceEffects";
15 +import leaveSSA from "../HIR/LeaveSSA";
16 +import codegen from "./Codegen";
17 +import { HIRFunction } from "./HIR";
18 +import { inferReactiveScopeVariables } from "./InferReactiveScopeVariables";
19 +
20 +export type CompilerFlags = {
21 + eliminateRedundantPhi: boolean;
22 + inferReferenceEffects: boolean;
23 + inferMutableRanges: boolean;
24 + inferReactiveScopeVariables: boolean;
25 + leaveSSA: boolean;
26 + codegen: boolean;
27 +};
28 +
29 +export type CompilerResult = {
30 + ir: HIRFunction;
31 + ast: t.Function | null;
32 +};
33 +
34 +export default function (
35 + func: NodePath<t.FunctionDeclaration>,
36 + flags: CompilerFlags
37 +): CompilerResult {
38 + const env = new Environment();
39 + const ir = lower(func, env);
40 + enterSSA(ir, env);
41 + if (flags.eliminateRedundantPhi) {
42 + eliminateRedundantPhi(ir);
43 + }
44 + if (flags.inferReferenceEffects) {
45 + inferReferenceEffects(ir);
46 + }
47 + if (flags.inferMutableRanges) {
48 + inferMutableRanges(ir);
49 + }
50 + if (flags.inferReactiveScopeVariables) {
51 + inferReactiveScopeVariables(ir);
52 + }
53 + if (flags.leaveSSA) {
54 + leaveSSA(ir);
55 + }
56 + if (flags.codegen) {
57 + return {
58 + ast: codegen(ir),
59 + ir: ir,
60 + };
61 + }
62 +
63 + return { ast: null, ir: ir };
64 +}
compiler/forget/src/__tests__/hir-test.ts
+16 -20
@@ -14,18 +14,9 @@ import { wasmFolder } from "@hpcc-js/wasm";
14 import invariant from "invariant";
15 import path from "path";
16 import prettier from "prettier";
17 -import { lower } from "../HIR/BuildHIR";
18 -import codegen from "../HIR/Codegen";
19 -import { eliminateRedundantPhi } from "../HIR/EliminateRedundantPhi";
20 -import enterSSA from "../HIR/EnterSSA";
21 -import { HIRFunction } from "../HIR/HIR";
22 -import { Environment } from "../HIR/HIRBuilder";
23 -import { inferMutableRanges } from "../HIR/InferMutableLifetimes";
24 -import { inferReactiveScopeVariables } from "../HIR/InferReactiveScopeVariables";
25 -import inferReferenceEffects from "../HIR/InferReferenceEffects";
26 -import leaveSSA from "../HIR/LeaveSSA";
17 import printHIR from "../HIR/PrintHIR";
18 import visualizeHIRMermaid from "../HIR/VisualizeHIRMermaid";
19 +import run from "./../HIR/Pipeline";
20 import generateTestsFromFixtures from "./test-utils/generateTestsFromFixtures";
21
22 function wrapWithTripleBackticks(s: string, ext?: string) {
@@ -65,18 +56,23 @@ describe("React Forget (HIR version)", () => {
56 traverse(ast, {
57 FunctionDeclaration: {
58 enter(nodePath) {
68 - const env: Environment = new Environment();
69 - const ir: HIRFunction = lower(nodePath, env);
70 - enterSSA(ir, env);
71 - eliminateRedundantPhi(ir);
72 - inferReferenceEffects(ir);
73 - inferMutableRanges(ir);
74 - inferReactiveScopeVariables(ir);
75 - leaveSSA(ir);
59 + const compilerFlags = {
60 + eliminateRedundantPhi: true,
61 + inferReferenceEffects: true,
62 + inferMutableRanges: true,
63 + leaveSSA: true,
64 + inferReactiveScopeVariables: true,
65 + codegen: true,
66 + };
67 +
68 + const { ast, ir } = run(nodePath, compilerFlags);
69 + invariant(
70 + ast !== null,
71 + "ast is null when codegen option is enabled"
72 + );
73 +
74 const textHIR = printHIR(ir.body);
75 const visualization = visualizeHIRMermaid(ir);
78 -
79 - const ast = codegen(ir);
76 const text = prettier.format(
77 generate(ast).code.replace("\n\n", "\n"),
78 {