Skip to content

Commit 2e39ef7

Browse files
committed
[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-poisoned]
1 parent d172bda commit 2e39ef7

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ export type Terminal =
369369
| SequenceTerminal
370370
| MaybeThrowTerminal
371371
| TryTerminal
372-
| ReactiveScopeTerminal;
372+
| ReactiveScopeTerminal
373+
| PrunedScopeTerminal;
373374

374375
export type TerminalWithFallthrough = Terminal & { fallthrough: BlockId };
375376

@@ -603,6 +604,15 @@ export type ReactiveScopeTerminal = {
603604
loc: SourceLocation;
604605
};
605606

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+
606616
/*
607617
* Instructions generally represent expressions but with all nesting flattened away,
608618
* such that all operands to each instruction are either primitive values OR are

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export function printMixedHIR(
127127
case "do-while":
128128
case "for-in":
129129
case "for-of":
130-
case "scope": {
130+
case "scope":
131+
case "pruned-scope": {
131132
const terminal = printTerminal(value);
132133
if (Array.isArray(terminal)) {
133134
return terminal.join("; ");
@@ -280,6 +281,12 @@ export function printTerminal(terminal: Terminal): Array<string> | string {
280281
} fallthrough=bb${terminal.fallthrough}`;
281282
break;
282283
}
284+
case "pruned-scope": {
285+
value = `<pruned> Scope ${printReactiveScopeSummary(terminal.scope)} block=bb${
286+
terminal.block
287+
} fallthrough=bb${terminal.fallthrough}`;
288+
break;
289+
}
283290
case "try": {
284291
value = `Try block=bb${terminal.block} handler=bb${terminal.handler}${
285292
terminal.handlerBinding !== null

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,8 @@ export function mapTerminalSuccessors(
851851
loc: terminal.loc,
852852
};
853853
}
854-
case "scope": {
854+
case "scope":
855+
case "pruned-scope": {
855856
const block = fn(terminal.block);
856857
const fallthrough = fn(terminal.fallthrough);
857858
return {
@@ -904,7 +905,8 @@ export function terminalHasFallthrough<
904905
case "switch":
905906
case "ternary":
906907
case "while":
907-
case "scope": {
908+
case "scope":
909+
case "pruned-scope": {
908910
const _: BlockId = terminal.fallthrough;
909911
return true;
910912
}
@@ -1006,7 +1008,8 @@ export function* eachTerminalSuccessor(terminal: Terminal): Iterable<BlockId> {
10061008
yield terminal.block;
10071009
break;
10081010
}
1009-
case "scope": {
1011+
case "scope":
1012+
case "pruned-scope": {
10101013
yield terminal.block;
10111014
break;
10121015
}
@@ -1072,7 +1075,8 @@ export function mapTerminalOperands(
10721075
case "goto":
10731076
case "unreachable":
10741077
case "unsupported":
1075-
case "scope": {
1078+
case "scope":
1079+
case "pruned-scope": {
10761080
// no-op
10771081
break;
10781082
}
@@ -1130,7 +1134,8 @@ export function* eachTerminalOperand(terminal: Terminal): Iterable<Place> {
11301134
case "goto":
11311135
case "unreachable":
11321136
case "unsupported":
1133-
case "scope": {
1137+
case "scope":
1138+
case "pruned-scope": {
11341139
// no-op
11351140
break;
11361141
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ class Driver {
806806
}
807807
break;
808808
}
809+
case "pruned-scope":
809810
case "scope": {
810811
const fallthroughId = !this.cx.isScheduled(terminal.fallthrough)
811812
? terminal.fallthrough
@@ -828,7 +829,7 @@ class Driver {
828829

829830
this.cx.unscheduleAll(scheduleIds);
830831
blockValue.push({
831-
kind: "scope",
832+
kind: terminal.kind,
832833
instructions: block,
833834
scope: terminal.scope,
834835
});

0 commit comments

Comments
 (0)