Skip to content

Commit

Permalink
[compiler] Add pruned-scope terminal in HIR
Browse files Browse the repository at this point in the history
Adds the HIR equivalent of a pruned-scope, allowing us to start porting the scope-pruning passes to operate on HIR.

[ghstack-poisoned]
  • Loading branch information
josephsavona committed Jun 10, 2024
1 parent d172bda commit 2e39ef7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
12 changes: 11 additions & 1 deletion compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ export type Terminal =
| SequenceTerminal
| MaybeThrowTerminal
| TryTerminal
| ReactiveScopeTerminal;
| ReactiveScopeTerminal
| PrunedScopeTerminal;

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

Expand Down Expand Up @@ -603,6 +604,15 @@ export type ReactiveScopeTerminal = {
loc: SourceLocation;
};

export type PrunedScopeTerminal = {
kind: "pruned-scope";
fallthrough: BlockId;
block: BlockId;
scope: ReactiveScope;
id: InstructionId;
loc: SourceLocation;
};

/*
* Instructions generally represent expressions but with all nesting flattened away,
* such that all operands to each instruction are either primitive values OR are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ export function printMixedHIR(
case "do-while":
case "for-in":
case "for-of":
case "scope": {
case "scope":
case "pruned-scope": {
const terminal = printTerminal(value);
if (Array.isArray(terminal)) {
return terminal.join("; ");
Expand Down Expand Up @@ -280,6 +281,12 @@ export function printTerminal(terminal: Terminal): Array<string> | string {
} fallthrough=bb${terminal.fallthrough}`;
break;
}
case "pruned-scope": {
value = `<pruned> Scope ${printReactiveScopeSummary(terminal.scope)} block=bb${
terminal.block
} fallthrough=bb${terminal.fallthrough}`;
break;
}
case "try": {
value = `Try block=bb${terminal.block} handler=bb${terminal.handler}${
terminal.handlerBinding !== null
Expand Down
15 changes: 10 additions & 5 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ export function mapTerminalSuccessors(
loc: terminal.loc,
};
}
case "scope": {
case "scope":
case "pruned-scope": {
const block = fn(terminal.block);
const fallthrough = fn(terminal.fallthrough);
return {
Expand Down Expand Up @@ -904,7 +905,8 @@ export function terminalHasFallthrough<
case "switch":
case "ternary":
case "while":
case "scope": {
case "scope":
case "pruned-scope": {
const _: BlockId = terminal.fallthrough;
return true;
}
Expand Down Expand Up @@ -1006,7 +1008,8 @@ export function* eachTerminalSuccessor(terminal: Terminal): Iterable<BlockId> {
yield terminal.block;
break;
}
case "scope": {
case "scope":
case "pruned-scope": {
yield terminal.block;
break;
}
Expand Down Expand Up @@ -1072,7 +1075,8 @@ export function mapTerminalOperands(
case "goto":
case "unreachable":
case "unsupported":
case "scope": {
case "scope":
case "pruned-scope": {
// no-op
break;
}
Expand Down Expand Up @@ -1130,7 +1134,8 @@ export function* eachTerminalOperand(terminal: Terminal): Iterable<Place> {
case "goto":
case "unreachable":
case "unsupported":
case "scope": {
case "scope":
case "pruned-scope": {
// no-op
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ class Driver {
}
break;
}
case "pruned-scope":
case "scope": {
const fallthroughId = !this.cx.isScheduled(terminal.fallthrough)
? terminal.fallthrough
Expand All @@ -828,7 +829,7 @@ class Driver {

this.cx.unscheduleAll(scheduleIds);
blockValue.push({
kind: "scope",
kind: terminal.kind,
instructions: block,
scope: terminal.scope,
});
Expand Down

0 comments on commit 2e39ef7

Please sign in to comment.