Skip to content

Commit b44cac9

Browse files
committed
[compiler] Show outlined functions in logging, playground
ghstack-source-id: 4813899 Pull Request resolved: #30344
1 parent a182079 commit b44cac9

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Pipeline.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
lower,
2222
mergeConsecutiveBlocks,
2323
mergeOverlappingReactiveScopesHIR,
24+
printFunction,
2425
pruneUnusedLabelsHIR,
2526
} from "../HIR";
2627
import {

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

Lines changed: 9 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 = "";
@@ -66,6 +74,7 @@ export function printFunction(fn: HIRFunction): string {
6674
}
6775
output.push(printHIR(fn.body));
6876
output.push(...fn.directives);
77+
6978
return output.join("\n");
7079
}
7180

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ export function codegenFunction(
278278
pruneHoistedContexts(reactiveFunction);
279279

280280
const identifiers = renameVariables(reactiveFunction);
281-
logReactiveFunction("Outline", reactiveFunction);
282281
const codegen = codegenReactiveFunction(
283282
new Context(
284283
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("\n" + 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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ 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";
12+
import {
13+
printFunction,
14+
printFunctionWithOutlined,
15+
printHIR,
16+
} from "../HIR/PrintHIR";
1317
import { CodegenFunction, printReactiveFunction } from "../ReactiveScopes";
18+
import { printReactiveFunctionWithOutlined } from "../ReactiveScopes/PrintReactiveFunction";
1419

1520
let ENABLED: boolean = false;
1621

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

8085
export function logHIRFunction(step: string, fn: HIRFunction): void {
8186
if (ENABLED) {
82-
const printed = printFunction(fn);
87+
const printed = printFunctionWithOutlined(fn);
8388
if (printed !== lastLogged) {
8489
lastLogged = printed;
8590
process.stdout.write(`${chalk.green(step)}:\n${printed}\n\n`);
@@ -91,7 +96,7 @@ export function logHIRFunction(step: string, fn: HIRFunction): void {
9196

9297
export function logReactiveFunction(step: string, fn: ReactiveFunction): void {
9398
if (ENABLED) {
94-
const printed = printReactiveFunction(fn);
99+
const printed = printReactiveFunctionWithOutlined(fn);
95100
if (printed !== lastLogged) {
96101
lastLogged = printed;
97102
process.stdout.write(`${chalk.green(step)}:\n${printed}\n\n`);

0 commit comments

Comments
 (0)