Skip to content

Commit

Permalink
[compiler][hir] Correctly remove non-existent terminal preds when pru…
Browse files Browse the repository at this point in the history
…ning labels

Missed this initially in `pruneUnusedLabelsHIR`. It wasn't an active bug as `preds` wasn't referenced by later passes, until #30079

ghstack-source-id: 3e151b74c31554299e870f001c0ac3f72706318c
Pull Request resolved: #30076
  • Loading branch information
mofeiZ committed Jun 25, 2024
1 parent 9262761 commit 7d9861e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
HIRFunction,
ReactiveFunction,
assertConsistentIdentifiers,
assertTerminalPredsExist,
assertTerminalSuccessorsExist,
assertValidBlockNesting,
assertValidMutableRanges,
Expand Down Expand Up @@ -303,6 +304,8 @@ function* runWithEnvironment(
name: "FlattenScopesWithHooksOrUseHIR",
value: hir,
});
assertTerminalSuccessorsExist(hir);
assertTerminalPredsExist(hir);
}

const reactiveFunction = buildReactiveFunction(hir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { CompilerError } from "../CompilerError";
import { GeneratedSource, HIRFunction } from "./HIR";
import { printTerminal } from "./PrintHIR";
import { mapTerminalSuccessors } from "./visitors";
import { eachTerminalSuccessor, mapTerminalSuccessors } from "./visitors";

export function assertTerminalSuccessorsExist(fn: HIRFunction): void {
for (const [, block] of fn.body.blocks) {
Expand All @@ -25,3 +25,23 @@ export function assertTerminalSuccessorsExist(fn: HIRFunction): void {
});
}
}

export function assertTerminalPredsExist(fn: HIRFunction): void {
for (const [, block] of fn.body.blocks) {
for (const pred of block.preds) {
const predBlock = fn.body.blocks.get(pred);
CompilerError.invariant(predBlock != null, {
reason: "Expected predecessor block to exist",
description: `Block ${block.id} references non-existent ${pred}`,
loc: GeneratedSource,
});
CompilerError.invariant(
[...eachTerminalSuccessor(predBlock.terminal)].includes(block.id),
{
reason: "Terminal successor does not reference correct predecessor",
loc: GeneratedSource,
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ export function pruneUnusedLabelsHIR(fn: HIRFunction): void {
fn.body.blocks.delete(fallthroughId);
rewrites.set(fallthroughId, labelId);
}

for (const [_, block] of fn.body.blocks) {
for (const pred of block.preds) {
const rewritten = rewrites.get(pred);
if (rewritten != null) {
block.preds.delete(pred);
block.preds.add(rewritten);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/

export { assertConsistentIdentifiers } from "./AssertConsistentIdentifiers";
export { assertTerminalSuccessorsExist } from "./AssertTerminalSuccessorsExist";
export {
assertTerminalSuccessorsExist,
assertTerminalPredsExist,
} from "./AssertTerminalBlocksExist";
export { assertValidBlockNesting } from "./AssertValidBlockNesting";
export { assertValidMutableRanges } from "./AssertValidMutableRanges";
export { lower } from "./BuildHIR";
Expand Down

0 comments on commit 7d9861e

Please sign in to comment.