aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineBlockPlacement.cpp
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-10 20:36:50 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-10 20:36:50 +0800
commitadcf8c9b1bd1b47e00bf859693112cea1f7e8c9f (patch)
tree59067aa71befab31678946dbf5e19f57131dd6f2 /backend/lib/CodeGen/MachineBlockPlacement.cpp
parent3a57627e59ba18b6f9f9aae7bb60c390c0d3575a (diff)
downloadrcc-project-adcf8c9b1bd1b47e00bf859693112cea1f7e8c9f.tar.gz
rcc-project-adcf8c9b1bd1b47e00bf859693112cea1f7e8c9f.tar.xz
[Tooling] Complete backend tooling and validation
Diffstat (limited to 'backend/lib/CodeGen/MachineBlockPlacement.cpp')
-rw-r--r--backend/lib/CodeGen/MachineBlockPlacement.cpp37
1 files changed, 28 insertions, 9 deletions
diff --git a/backend/lib/CodeGen/MachineBlockPlacement.cpp b/backend/lib/CodeGen/MachineBlockPlacement.cpp
index fea1283..daccba0 100644
--- a/backend/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/backend/lib/CodeGen/MachineBlockPlacement.cpp
@@ -114,21 +114,25 @@ Expected<MachinePassResult> MachineBlockPlacementPass::run(MachineFunction &Func
if (!Trial)
return std::unexpected(prefixedError(Trial.error(), "block placement setup failed: "));
MachineAnalysisManager TrialAnalyses;
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses);
+ MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
+ auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses, Actions);
if (!TrialResult)
return std::unexpected(TrialResult.error());
- auto Result = runImplementation(Function, Target, Analyses);
+ Actions.beginReplay();
+ auto Result = runImplementation(Function, Target, Analyses, Actions);
if (!Result) {
return std::unexpected(prefixedError(
Result.error(), "block placement diverged after a successful transaction preflight: "));
}
+ auto Replayed = Actions.finishReplay();
+ if (!Replayed)
+ return std::unexpected(Replayed.error());
return Result;
}
-Expected<MachinePassResult>
-MachineBlockPlacementPass::runImplementation(MachineFunction &Function,
- const TargetDescription &Target,
- MachineAnalysisManager &Analyses) const {
+Expected<MachinePassResult> MachineBlockPlacementPass::runImplementation(
+ MachineFunction &Function, const TargetDescription &Target, MachineAnalysisManager &Analyses,
+ MachineActionReplay &Actions) const {
auto Probabilities = Analyses.getResult<MachineBranchProbabilityAnalysis>(Function, Target);
if (!Probabilities)
return std::unexpected(Probabilities.error());
@@ -154,7 +158,7 @@ MachineBlockPlacementPass::runImplementation(MachineFunction &Function,
return Result;
}
- auto factsFor = [&Facts](const MachineBasicBlock &Block) -> const PlacementFacts & {
+ auto FactsFor = [&Facts](const MachineBasicBlock &Block) -> const PlacementFacts & {
return *std::ranges::find_if(
Facts, [&Block](const PlacementFacts &Value) { return Value.Block == &Block; });
};
@@ -186,7 +190,7 @@ MachineBlockPlacementPass::runImplementation(MachineFunction &Function,
continue;
if (BestSuccessor == nullptr || BestProbability < Successor.Probability ||
(BestProbability == Successor.Probability &&
- betterRoot(factsFor(*Successor.Block), factsFor(*BestSuccessor)))) {
+ betterRoot(FactsFor(*Successor.Block), FactsFor(*BestSuccessor)))) {
BestSuccessor = Successor.Block;
BestProbability = Successor.Probability;
}
@@ -223,7 +227,22 @@ MachineBlockPlacementPass::runImplementation(MachineFunction &Function,
bool Changed = false;
for (std::size_t Index = 1; Index < Order.size(); ++Index) {
- Changed |= Order[Index - 1U]->next() != Order[Index];
+ if (Order[Index - 1U]->next() != Order[Index]) {
+ Changed = true;
+ break;
+ }
+ }
+ if (Changed) {
+ auto Accepted = Actions.request("apply-block-layout", Function);
+ if (!Accepted)
+ return std::unexpected(Accepted.error());
+ if (!*Accepted) {
+ MachinePassResult Result;
+ Result.Preserved = PreservedAnalyses::all();
+ return Result;
+ }
+ }
+ for (std::size_t Index = 1; Index < Order.size(); ++Index) {
auto Moved = Function.moveBlockAfter(*Order[Index - 1U], *Order[Index]);
if (!Moved)
return std::unexpected(Moved.error());