Skip to content

Commit 163365a

Browse files
committed
[compiler] Show outlined functions in logging, playground
ghstack-source-id: abda15e Pull Request resolved: #30344
1 parent b810442 commit 163365a

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

compiler/apps/playground/components/Editor/EditorImpl.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import {
4242
default as Output,
4343
PrintedCompilerPipelineValue,
4444
} from "./Output";
45+
import { printFunctionWithOutlined } from "babel-plugin-react-compiler/src/HIR/PrintHIR";
46+
import { printReactiveFunctionWithOutlined } from "babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction";
4547

4648
function parseInput(input: string, language: "flow" | "typescript") {
4749
// Extract the first line to quickly check for custom test directives
@@ -242,7 +244,7 @@ function compile(source: string): [CompilerOutput, "flow" | "typescript"] {
242244
kind: "hir",
243245
fnName,
244246
name: result.name,
245-
value: printHIR(result.value.body),
247+
value: printFunctionWithOutlined(result.value),
246248
});
247249
break;
248250
}
@@ -251,7 +253,7 @@ function compile(source: string): [CompilerOutput, "flow" | "typescript"] {
251253
kind: "reactive",
252254
fnName,
253255
name: result.name,
254-
value: printReactiveFunction(result.value),
256+
value: printReactiveFunctionWithOutlined(result.value),
255257
});
256258
break;
257259
}

compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ export type Options = {
4141
indent: number;
4242
};
4343

44+
export function printFunctionWithOutlined(fn: HIRFunction): string {
45+
const output = [printFunction(fn)];
46+
for (const outlined of fn.env.getOutlinedFunctions()) {
47+
output.push(`\nfunction ${outlined.fn.id}:\n${printHIR(outlined.fn.body)}`);
48+
}
49+
return output.join("\n");
50+
}
51+
4452
export function printFunction(fn: HIRFunction): string {
4553
const output = [];
4654
let definition = "";

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import { buildReactiveFunction } from "./BuildReactiveFunction";
5151
import { SINGLE_CHILD_FBT_TAGS } from "./MemoizeFbtAndMacroOperandsInSameScope";
5252
import { ReactiveFunctionVisitor, visitReactiveFunction } from "./visitors";
5353
import { ReactFunctionType } from "../HIR/Environment";
54-
import { logReactiveFunction } from "../Utils/logger";
5554

5655
export const MEMO_CACHE_SENTINEL = "react.memo_cache_sentinel";
5756
export const EARLY_RETURN_SENTINEL = "react.early_return_sentinel";
@@ -278,7 +277,6 @@ export function codegenFunction(
278277
pruneHoistedContexts(reactiveFunction);
279278

280279
const identifiers = renameVariables(reactiveFunction);
281-
logReactiveFunction("Outline", reactiveFunction);
282280
const codegen = codegenReactiveFunction(
283281
new Context(
284282
cx.env,

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/PrintReactiveFunction.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,32 @@ import {
1717
ReactiveValue,
1818
} from "../HIR/HIR";
1919
import {
20+
printFunction,
2021
printIdentifier,
2122
printInstructionValue,
2223
printPlace,
2324
printType,
2425
} from "../HIR/PrintHIR";
2526
import { assertExhaustive } from "../Utils/utils";
2627

28+
export function printReactiveFunctionWithOutlined(
29+
fn: ReactiveFunction
30+
): string {
31+
const writer = new Writer();
32+
writeReactiveFunction(fn, writer);
33+
for (const outlined of fn.env.getOutlinedFunctions()) {
34+
writer.writeLine("\nfunction " + printFunction(outlined.fn));
35+
}
36+
return writer.complete();
37+
}
38+
2739
export function printReactiveFunction(fn: ReactiveFunction): string {
2840
const writer = new Writer();
41+
writeReactiveFunction(fn, writer);
42+
return writer.complete();
43+
}
44+
45+
function writeReactiveFunction(fn: ReactiveFunction, writer: Writer): void {
2946
writer.writeLine(`function ${fn.id !== null ? fn.id : "<unknown>"}(`);
3047
writer.indented(() => {
3148
for (const param of fn.params) {
@@ -39,7 +56,6 @@ export function printReactiveFunction(fn: ReactiveFunction): string {
3956
writer.writeLine(") {");
4057
writeReactiveInstructions(writer, fn.body);
4158
writer.writeLine("}");
42-
return writer.complete();
4359
}
4460

4561
export function printReactiveScopeSummary(scope: ReactiveScope): string {

compiler/packages/babel-plugin-react-compiler/src/Utils/logger.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import generate from "@babel/generator";
99
import * as t from "@babel/types";
1010
import chalk from "chalk";
1111
import { HIR, HIRFunction, ReactiveFunction } from "../HIR/HIR";
12-
import { printFunction, printHIR } from "../HIR/PrintHIR";
13-
import { CodegenFunction, printReactiveFunction } from "../ReactiveScopes";
12+
import { printFunctionWithOutlined, printHIR } from "../HIR/PrintHIR";
13+
import { CodegenFunction } from "../ReactiveScopes";
14+
import { printReactiveFunctionWithOutlined } from "../ReactiveScopes/PrintReactiveFunction";
1415

1516
let ENABLED: boolean = false;
1617

@@ -79,7 +80,7 @@ export function logCodegenFunction(step: string, fn: CodegenFunction): void {
7980

8081
export function logHIRFunction(step: string, fn: HIRFunction): void {
8182
if (ENABLED) {
82-
const printed = printFunction(fn);
83+
const printed = printFunctionWithOutlined(fn);
8384
if (printed !== lastLogged) {
8485
lastLogged = printed;
8586
process.stdout.write(`${chalk.green(step)}:\n${printed}\n\n`);
@@ -91,7 +92,7 @@ export function logHIRFunction(step: string, fn: HIRFunction): void {
9192

9293
export function logReactiveFunction(step: string, fn: ReactiveFunction): void {
9394
if (ENABLED) {
94-
const printed = printReactiveFunction(fn);
95+
const printed = printReactiveFunctionWithOutlined(fn);
9596
if (printed !== lastLogged) {
9697
lastLogged = printed;
9798
process.stdout.write(`${chalk.green(step)}:\n${printed}\n\n`);

0 commit comments

Comments
 (0)