@samitouri / QOS-React / commits / f7b871a8d5

[compiler] Add pruned-scope terminal in HIR

Adds the HIR equivalent of a pruned-scope, allowing us to start porting the scope-pruning passes to operate on HIR. ghstack-source-id: dbbdc43219123467acc1a531d8276e8b9cc91e14 Pull Request resolved: https://github.com/facebook/react/pull/29837

Joe Savona committed Jun 10, 2024 at 13:22 UTC f7b871a8d5d216f23b9ee1178a6e7d8cd5ee3003
4 files changed +31 -8
compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
+11 -1
@@ -369,7 +369,8 @@ export type Terminal =
369 | SequenceTerminal
370 | MaybeThrowTerminal
371 | TryTerminal
372 - | ReactiveScopeTerminal;
372 + | ReactiveScopeTerminal
373 + | PrunedScopeTerminal;
374
375 export type TerminalWithFallthrough = Terminal & { fallthrough: BlockId };
376
@@ -603,6 +604,15 @@ export type ReactiveScopeTerminal = {
604 loc: SourceLocation;
605 };
606
607 +export type PrunedScopeTerminal = {
608 + kind: "pruned-scope";
609 + fallthrough: BlockId;
610 + block: BlockId;
611 + scope: ReactiveScope;
612 + id: InstructionId;
613 + loc: SourceLocation;
614 +};
615 +
616 /*
617 * Instructions generally represent expressions but with all nesting flattened away,
618 * such that all operands to each instruction are either primitive values OR are
compiler/packages/babel-plugin-react-compiler/src/HIR/PrintHIR.ts
+8 -1
@@ -127,7 +127,8 @@ export function printMixedHIR(
127 case "do-while":
128 case "for-in":
129 case "for-of":
130 - case "scope": {
130 + case "scope":
131 + case "pruned-scope": {
132 const terminal = printTerminal(value);
133 if (Array.isArray(terminal)) {
134 return terminal.join("; ");
@@ -280,6 +281,12 @@ export function printTerminal(terminal: Terminal): Array<string> | string {
281 } fallthrough=bb${terminal.fallthrough}`;
282 break;
283 }
284 + case "pruned-scope": {
285 + value = `<pruned> Scope ${printReactiveScopeSummary(terminal.scope)} block=bb${
286 + terminal.block
287 + } fallthrough=bb${terminal.fallthrough}`;
288 + break;
289 + }
290 case "try": {
291 value = `Try block=bb${terminal.block} handler=bb${terminal.handler}${
292 terminal.handlerBinding !== null
compiler/packages/babel-plugin-react-compiler/src/HIR/visitors.ts
+10 -5
@@ -851,7 +851,8 @@ export function mapTerminalSuccessors(
851 loc: terminal.loc,
852 };
853 }
854 - case "scope": {
854 + case "scope":
855 + case "pruned-scope": {
856 const block = fn(terminal.block);
857 const fallthrough = fn(terminal.fallthrough);
858 return {
@@ -904,7 +905,8 @@ export function terminalHasFallthrough<
905 case "switch":
906 case "ternary":
907 case "while":
907 - case "scope": {
908 + case "scope":
909 + case "pruned-scope": {
910 const _: BlockId = terminal.fallthrough;
911 return true;
912 }
@@ -1006,7 +1008,8 @@ export function* eachTerminalSuccessor(terminal: Terminal): Iterable<BlockId> {
1008 yield terminal.block;
1009 break;
1010 }
1009 - case "scope": {
1011 + case "scope":
1012 + case "pruned-scope": {
1013 yield terminal.block;
1014 break;
1015 }
@@ -1072,7 +1075,8 @@ export function mapTerminalOperands(
1075 case "goto":
1076 case "unreachable":
1077 case "unsupported":
1075 - case "scope": {
1078 + case "scope":
1079 + case "pruned-scope": {
1080 // no-op
1081 break;
1082 }
@@ -1130,7 +1134,8 @@ export function* eachTerminalOperand(terminal: Terminal): Iterable<Place> {
1134 case "goto":
1135 case "unreachable":
1136 case "unsupported":
1133 - case "scope": {
1137 + case "scope":
1138 + case "pruned-scope": {
1139 // no-op
1140 break;
1141 }
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/BuildReactiveFunction.ts
+2 -1
@@ -806,6 +806,7 @@ class Driver {
806 }
807 break;
808 }
809 + case "pruned-scope":
810 case "scope": {
811 const fallthroughId = !this.cx.isScheduled(terminal.fallthrough)
812 ? terminal.fallthrough
@@ -828,7 +829,7 @@ class Driver {
829
830 this.cx.unscheduleAll(scheduleIds);
831 blockValue.push({
831 - kind: "scope",
832 + kind: terminal.kind,
833 instructions: block,
834 scope: terminal.scope,
835 });