@samitouri / QOS-React-2 / commits / 3603ee00a6

Retain original structure of labeled blocks

Handles some edge-cases where we previously flattened away some of the structure of a labeled block, instead ensuring that we retain the original shape. See the output. ## Test Plan Tested on the internal app we're focused on (w useMemo inlining enabled), it works fine.

Joe Savona committed May 17, 2023 at 15:55 UTC 3603ee00a65f292d9d08cf36c31a9cf5f8843467
5 files changed +27 -15
compiler/forget/src/HIR/MergeConsecutiveBlocks.ts
+4
@@ -14,6 +14,7 @@ import {
14 Instruction,
15 } from "./HIR";
16 import { markPredecessors, removeUnreachableFallthroughs } from "./HIRBuilder";
17 +import { mapOptionalFallthroughs } from "./visitors";
18
19 /**
20 * Merges sequences of blocks that will always execute consecutively —
@@ -86,6 +87,9 @@ export function mergeConsecutiveBlocks(fn: HIRFunction): void {
87 fn.body.blocks.delete(block.id);
88 }
89 markPredecessors(fn.body);
90 + for (const [, block] of fn.body.blocks) {
91 + mapOptionalFallthroughs(block.terminal, (blockId) => merged.get(blockId));
92 + }
93 removeUnreachableFallthroughs(fn.body);
94 }
95
compiler/forget/src/ReactiveScopes/BuildReactiveFunction.ts
+13 -9
@@ -573,15 +573,18 @@ class Driver {
573 case "optional":
574 case "ternary":
575 case "logical": {
576 - const fallthroughId = terminal.fallthrough;
577 - invariant(
578 - !this.cx.isScheduled(fallthroughId),
579 - "Logical terminal fallthrough cannot have been scheduled"
580 - );
581 - const scheduleId = this.cx.schedule(fallthroughId, "if");
582 - scheduleIds.push(scheduleId);
576 + const fallthroughId =
577 + terminal.fallthrough !== null &&
578 + !this.cx.isScheduled(terminal.fallthrough)
579 + ? terminal.fallthrough
580 + : null;
581 + if (fallthroughId !== null) {
582 + const scheduleId = this.cx.schedule(fallthroughId, "if");
583 + scheduleIds.push(scheduleId);
584 + }
585
586 const { place, value } = this.visitValueBlockTerminal(terminal);
587 + this.cx.unscheduleAll(scheduleIds);
588 blockValue.push({
589 kind: "instruction",
590 instruction: {
@@ -592,8 +595,9 @@ class Driver {
595 },
596 });
597
595 - this.cx.unschedule(scheduleId);
596 - this.visitBlock(this.cx.ir.blocks.get(fallthroughId)!, blockValue);
598 + if (fallthroughId !== null) {
599 + this.visitBlock(this.cx.ir.blocks.get(fallthroughId)!, blockValue);
600 + }
601 break;
602 }
603 case "goto": {
compiler/forget/src/__tests__/fixtures/compiler/dominator.expect.md
+2 -1
@@ -47,10 +47,11 @@ function Component(props) {
47 x = 1;
48 } else {
49 if (props.b) {
50 - x = 3;
50 } else {
51 break bb1;
52 }
53 +
54 + x = 3;
55 }
56 bb10: bb12: switch (props.c) {
57 case "a": {
compiler/forget/src/__tests__/fixtures/compiler/early-return.expect.md
-2
@@ -19,8 +19,6 @@ function MyApp(props) {
19 function MyApp(props) {
20 if (props.cond) {
21 return;
22 - } else {
23 - return;
22 }
23 }
24
compiler/forget/src/__tests__/fixtures/compiler/useMemo-inverted-if.expect.md
+8 -3
@@ -23,11 +23,16 @@ function Component(props) {
23 function Component(props) {
24 let t17 = undefined;
25 bb10: {
26 - if (props.cond) {
27 - t17 = props.b;
26 + bb5: {
27 + if (props.cond) {
28 + break bb5;
29 + }
30 +
31 + t17 = props.a;
32 break bb10;
33 }
30 - t17 = props.a;
34 +
35 + t17 = props.b;
36 }
37 const x = t17;
38 return x;