mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[playground] include HIR output (#756)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -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<t.Program>, 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");
|
||||
}
|
||||
@@ -20,6 +20,7 @@ export enum PassName {
|
||||
SketchyCodeCheck,
|
||||
RefKindInfer,
|
||||
DumpIR,
|
||||
DumpHIR,
|
||||
IRCheck,
|
||||
DepGraphAnalysis,
|
||||
// BackEnd
|
||||
|
||||
Reference in New Issue
Block a user