From 779947520d0aa8fd75deb0f1440f61004299f5cd Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Thu, 10 Nov 2022 15:45:51 -0500 Subject: [PATCH] [playground] new HIR tab Instead of hacking into the babel plugin passes, this now takes a new approach for the HIR tab: - The different passes are exported from the babel plugin (for simplicity) - The tab actually runs the compiler steps based on local config in the tab. - Re-purposed the CompilerFlags component to configure what passes to run. This is currently mostly causing different errors, but could be useful going forward as a direction. --- .../components/Editor/HIRTabContent.tsx | 149 ++++++++++++++++++ .../playground/components/Editor/Output.tsx | 3 +- .../playground/components/Editor/index.tsx | 8 +- compiler/forget/src/CompilerDriver.ts | 5 - compiler/forget/src/CompilerOutputs.ts | 9 +- compiler/forget/src/HIR/DumpHIRPass.ts | 56 ------- compiler/forget/src/index.ts | 45 ++++++ 7 files changed, 202 insertions(+), 73 deletions(-) create mode 100644 compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx delete mode 100644 compiler/forget/src/HIR/DumpHIRPass.ts diff --git a/compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx b/compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx new file mode 100644 index 0000000000..e164fde0e4 --- /dev/null +++ b/compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx @@ -0,0 +1,149 @@ +/** + * 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 clsx from "clsx"; + +import { HIR } from "babel-plugin-react-forget"; +import { useState } from "react"; + +const { + parseFunctions, + Environment, + enterSSA, + eliminateRedundantPhi, + inferReferenceEffects, + inferMutableRanges, + leaveSSA, + lower, + printHIR, +} = HIR; + +export default function HIRTabContent({ source }: { source: string }) { + const astFunctions = parseFunctions(source); + + const [flags, setFlags] = useState({ + eliminateRedundantPhi: true, + inferReferenceEffects: true, + inferMutableRanges: true, + leaveSSA: true, + }); + + return ( +
+
+ {astFunctions.map((func, index): React.ReactNode => { + let body; + try { + 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.leaveSSA) { + leaveSSA(ir); + } + const textHIR = printHIR(ir.body); + body =
{textHIR}
; + } catch (e: any) { + body =
error: ${e.toString()}
; + } + const name = func.node.id?.name ?? "anonymous"; + return ( +
+

Function: {name}

+ {body} +
+ ); + })} +
+ +
+ ); +} + +type Flags = { + eliminateRedundantPhi: boolean; + inferReferenceEffects: boolean; + inferMutableRanges: boolean; + leaveSSA: boolean; +}; + +function CompilerFlagsEditor({ + flags, + setFlags, +}: { + flags: Flags; + setFlags: (flags: Flags) => void; +}) { + return ( +
+

Compiler Options

+
+ { + setFlags({ ...flags, eliminateRedundantPhi }); + }} + /> + { + setFlags({ ...flags, inferReferenceEffects }); + }} + /> + { + setFlags({ ...flags, inferMutableRanges }); + }} + /> + { + setFlags({ ...flags, leaveSSA }); + }} + /> +
+
+ ); +} + +function LabeledCheckbox({ + label, + value, + onChange, +}: { + label: string; + value: boolean; + onChange: (value: boolean) => void; +}) { + return ( + + ); +} diff --git a/compiler/forget/packages/playground/components/Editor/Output.tsx b/compiler/forget/packages/playground/components/Editor/Output.tsx index 11d40cfbbc..efc8125eeb 100644 --- a/compiler/forget/packages/playground/components/Editor/Output.tsx +++ b/compiler/forget/packages/playground/components/Editor/Output.tsx @@ -19,6 +19,7 @@ import { getSelectedFile, type Store } from "../../lib/stores"; import GraphView from "../GraphView"; import Preview from "../Preview"; import { monacoOptions } from "./monacoOptions"; +import HIRTabContent from "./HIRTabContent"; const MemoizedOutput = memo(Output); @@ -43,7 +44,7 @@ function Output({ store, updateDiagnostics }: Props) { tabs={{ Preview: , IR: , - HIR: , + HIR: , CFG: , ValGraph: ( diff --git a/compiler/forget/packages/playground/components/Editor/index.tsx b/compiler/forget/packages/playground/components/Editor/index.tsx index 1e45c80a5d..be4657098f 100644 --- a/compiler/forget/packages/playground/components/Editor/index.tsx +++ b/compiler/forget/packages/playground/components/Editor/index.tsx @@ -1,5 +1,8 @@ -/* - * Copyright (c) Facebook, Inc. and its affiliates. +/** + * 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 { useDrag } from "@use-gesture/react"; @@ -89,7 +92,6 @@ export default function Editor() { })} > -