Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler][hir] Correctly remove non-existent terminal preds when pruning labels #30076

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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