From 5fd19fbe147eaff6e65e7dc3b63b324d80f19a05 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Thu, 27 Aug 2026 16:05:37 +0800 Subject: [CodeGen] Remove machine action replay --- backend/include/rcc/IR/PassInstrumentation.h | 42 ------------- backend/lib/CodeGen/MachinePassManager.cpp | 60 ------------------ backend/lib/CodeGen/MachinePseudoExpansion.cpp | 77 ++++------------------- backend/lib/CodeGen/PrologueEpilogueInsertion.cpp | 35 +---------- backend/lib/Target/RISCV/RISCVOptWInstrs.cpp | 16 +---- 5 files changed, 16 insertions(+), 214 deletions(-) diff --git a/backend/include/rcc/IR/PassInstrumentation.h b/backend/include/rcc/IR/PassInstrumentation.h index 082ec81..f90df37 100644 --- a/backend/include/rcc/IR/PassInstrumentation.h +++ b/backend/include/rcc/IR/PassInstrumentation.h @@ -180,48 +180,6 @@ private: template friend struct PassManagerCallbacks; }; -class MachineActionReplay final { -public: - template - explicit MachineActionReplay(AnalysisManagerT &Analyses) - : Request([&Analyses](std::string_view Rule, const MachineFunction &Function, - MachineInstrStableID Instruction, std::uint64_t Cost) { - return Analyses.requestAction(Rule, Function, Instruction, Cost); - }) {} - - template - MachineActionReplay( - AnalysisManagerT &Analyses, - const std::unordered_map &SourceToClone) - : MachineActionReplay(Analyses) { - for (const auto &[Source, Clone] : SourceToClone) - RecordingInstructionIDs.try_emplace(Clone, Source); - } - - [[nodiscard]] Expected request(std::string_view Rule, const MachineFunction &Function, - MachineInstrStableID Instruction = {}, - std::uint64_t Cost = 1); - void beginReplay() noexcept; - [[nodiscard]] Expected finishReplay() const; - -private: - struct Decision final { - std::string Rule; - MachineFunctionStableID Function; - MachineInstrStableID Instruction; - std::uint64_t Cost = 0; - bool Accepted = false; - }; - - std::function(std::string_view, const MachineFunction &, MachineInstrStableID, - std::uint64_t)> - Request; - std::unordered_map RecordingInstructionIDs; - std::vector Decisions; - std::size_t NextDecision = 0; - bool IsReplaying = false; -}; - } // namespace rcc #endif // RCC_IR_PASSINSTRUMENTATION_H diff --git a/backend/lib/CodeGen/MachinePassManager.cpp b/backend/lib/CodeGen/MachinePassManager.cpp index 2cd81b9..236457e 100644 --- a/backend/lib/CodeGen/MachinePassManager.cpp +++ b/backend/lib/CodeGen/MachinePassManager.cpp @@ -199,66 +199,6 @@ std::string MachineActionID::toString() const { return Result; } -Expected MachineActionReplay::request(std::string_view Rule, const MachineFunction &Function, - MachineInstrStableID Instruction, std::uint64_t Cost) { - if (!IsReplaying) { - const auto Position = RecordingInstructionIDs.find(Instruction); - const MachineInstrStableID CanonicalInstruction = - Position == RecordingInstructionIDs.end() ? Instruction : Position->second; - auto Accepted = Request(Rule, Function, CanonicalInstruction, Cost); - if (!Accepted) - return std::unexpected(Accepted.error()); - Decisions.push_back({ - .Rule = std::string(Rule), - .Function = Function.stableID(), - .Instruction = CanonicalInstruction, - .Cost = Cost, - .Accepted = *Accepted, - }); - return *Accepted; - } - - if (NextDecision >= Decisions.size()) { - return makeUnexpected(ErrorCode::InternalError, - "machine action replay requested an unrecorded decision"); - } - const Decision &Recorded = Decisions[NextDecision++]; - if (Recorded.Rule != Rule || Recorded.Function != Function.stableID() || - Recorded.Instruction != Instruction || Recorded.Cost != Cost) { - std::string Message = "machine action replay diverged from transaction preflight: recorded "; - Message.append(Recorded.Rule); - Message.append(":f"); - Message.append(std::to_string(Recorded.Function.value())); - Message.append(":i"); - Message.append(std::to_string(Recorded.Instruction.value())); - Message.append(" cost="); - Message.append(std::to_string(Recorded.Cost)); - Message.append(", requested "); - Message.append(Rule); - Message.append(":f"); - Message.append(std::to_string(Function.stableID().value())); - Message.append(":i"); - Message.append(std::to_string(Instruction.value())); - Message.append(" cost="); - Message.append(std::to_string(Cost)); - return makeUnexpected(ErrorCode::InternalError, std::move(Message)); - } - return Recorded.Accepted; -} - -void MachineActionReplay::beginReplay() noexcept { - IsReplaying = true; - NextDecision = 0; -} - -Expected MachineActionReplay::finishReplay() const { - if (!IsReplaying || NextDecision != Decisions.size()) { - return makeUnexpected(ErrorCode::InternalError, - "machine action replay did not consume every recorded decision"); - } - return {}; -} - void MachinePassInstrumentation::addBeforeCallback(BeforeCallback Callback) { if (!Callback) return; diff --git a/backend/lib/CodeGen/MachinePseudoExpansion.cpp b/backend/lib/CodeGen/MachinePseudoExpansion.cpp index 36a4b75..21bcf29 100644 --- a/backend/lib/CodeGen/MachinePseudoExpansion.cpp +++ b/backend/lib/CodeGen/MachinePseudoExpansion.cpp @@ -7,7 +7,6 @@ #include "rcc/CodeGen/MachineBasicBlock.h" #include "rcc/CodeGen/MachineFrameInfo.h" #include "rcc/CodeGen/MachineFunction.h" -#include "rcc/CodeGen/MachineFunctionCloner.h" #include "rcc/CodeGen/MachineInstr.h" #include "rcc/CodeGen/MachineOperand.h" #include "rcc/CodeGen/MachineRegisterInfo.h" @@ -202,7 +201,8 @@ using BlockSizeSnapshot = std::vector prepareImplementation(MachineFunction &Function, const TargetDescription &Target, - const TargetPseudoExpansionInfo &Info, MachineActionReplay &Actions) { + const TargetPseudoExpansionInfo &Info, + MachineFunctionAnalysisManager &Analyses) { bool Changed = false; for (MachineInstr *Instruction : layoutInstructions(Function)) { const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode()); @@ -213,7 +213,7 @@ prepareImplementation(MachineFunction &Function, const TargetDescription &Target continue; std::string Rule = "prepare-pseudo."; Rule.append(Descriptor->Name); - auto Accepted = Actions.request(Rule, Function, Instruction->stableID()); + auto Accepted = Analyses.requestAction(Rule, Function, Instruction->stableID()); if (!Accepted) return std::unexpected(Accepted.error()); if (!*Accepted) { @@ -239,7 +239,8 @@ prepareImplementation(MachineFunction &Function, const TargetDescription &Target [[nodiscard]] Expected expandImplementation(MachineFunction &Function, const TargetDescription &Target, const TargetPseudoExpansionInfo &Info, MachinePseudoExpansionPhase Phase, - const TargetInstrInfo *BranchInfo, MachineActionReplay &Actions) { + const TargetInstrInfo *BranchInfo, + MachineFunctionAnalysisManager &Analyses) { const ResourceSnapshot Before = snapshotResources(Function); std::optional SizesBefore; if (Phase == MachinePseudoExpansionPhase::TargetLate) { @@ -267,7 +268,7 @@ expandImplementation(MachineFunction &Function, const TargetDescription &Target, } std::string Rule = "expand-pseudo."; Rule.append(Descriptor->Name); - auto Accepted = Actions.request(Rule, Function, Instruction->stableID()); + auto Accepted = Analyses.requestAction(Rule, Function, Instruction->stableID()); if (!Accepted) return std::unexpected(Accepted.error()); if (!*Accepted) { @@ -345,58 +346,6 @@ expandImplementation(MachineFunction &Function, const TargetDescription &Target, return Result; } -[[nodiscard]] Expected -runPreparationTransaction(MachineFunction &Function, const TargetDescription &Target, - const TargetPseudoExpansionInfo &Info, - MachineFunctionAnalysisManager &Analyses) { - auto Trial = cloneMachineFunction( - Function, Target, - MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt}); - if (!Trial) - return std::unexpected(withErrorContext(Trial.error(), "pseudo preparation setup failed: ")); - MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs); - auto TrialResult = prepareImplementation(*Trial->ClonedFunction, Target, Info, Actions); - if (!TrialResult) - return std::unexpected(TrialResult.error()); - Actions.beginReplay(); - auto Result = prepareImplementation(Function, Target, Info, Actions); - if (!Result) { - return std::unexpected(withErrorContext( - Result.error(), "pseudo preparation diverged after successful preflight: ")); - } - auto Replayed = Actions.finishReplay(); - if (!Replayed) - return std::unexpected(Replayed.error()); - return Result; -} - -[[nodiscard]] Expected -runExpansionTransaction(MachineFunction &Function, const TargetDescription &Target, - const TargetPseudoExpansionInfo &Info, MachinePseudoExpansionPhase Phase, - const TargetInstrInfo *BranchInfo, - MachineFunctionAnalysisManager &Analyses) { - auto Trial = cloneMachineFunction( - Function, Target, - MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt}); - if (!Trial) - return std::unexpected(withErrorContext(Trial.error(), "pseudo expansion setup failed: ")); - MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs); - auto TrialResult = - expandImplementation(*Trial->ClonedFunction, Target, Info, Phase, BranchInfo, Actions); - if (!TrialResult) - return std::unexpected(TrialResult.error()); - Actions.beginReplay(); - auto Result = expandImplementation(Function, Target, Info, Phase, BranchInfo, Actions); - if (!Result) { - return std::unexpected( - withErrorContext(Result.error(), "pseudo expansion diverged after successful preflight: ")); - } - auto Replayed = Actions.finishReplay(); - if (!Replayed) - return std::unexpected(Replayed.error()); - return Result; -} - } // namespace const MachinePassContract &PreRATargetPseudoExpansionPass::contract() const noexcept { @@ -406,8 +355,8 @@ const MachinePassContract &PreRATargetPseudoExpansionPass::contract() const noex Expected PreRATargetPseudoExpansionPass::run(MachineFunction &Function, const TargetDescription &Target, MachineFunctionAnalysisManager &Analyses) { - return runExpansionTransaction(Function, Target, *Info, MachinePseudoExpansionPhase::PreRA, - nullptr, Analyses); + return expandImplementation(Function, Target, *Info, MachinePseudoExpansionPhase::PreRA, nullptr, + Analyses); } const MachinePassContract &PreparePostRAPseudosPass::contract() const noexcept { @@ -417,7 +366,7 @@ const MachinePassContract &PreparePostRAPseudosPass::contract() const noexcept { Expected PreparePostRAPseudosPass::run(MachineFunction &Function, const TargetDescription &Target, MachineFunctionAnalysisManager &Analyses) { - return runPreparationTransaction(Function, Target, *Info, Analyses); + return prepareImplementation(Function, Target, *Info, Analyses); } const MachinePassContract &ExpandPostRAPseudosPass::contract() const noexcept { @@ -427,8 +376,8 @@ const MachinePassContract &ExpandPostRAPseudosPass::contract() const noexcept { Expected ExpandPostRAPseudosPass::run(MachineFunction &Function, const TargetDescription &Target, MachineFunctionAnalysisManager &Analyses) { - return runExpansionTransaction(Function, Target, *Info, MachinePseudoExpansionPhase::PostRA, - nullptr, Analyses); + return expandImplementation(Function, Target, *Info, MachinePseudoExpansionPhase::PostRA, + nullptr, Analyses); } const MachinePassContract &TargetLatePseudoExpansionPass::contract() const noexcept { @@ -438,8 +387,8 @@ const MachinePassContract &TargetLatePseudoExpansionPass::contract() const noexc Expected TargetLatePseudoExpansionPass::run(MachineFunction &Function, const TargetDescription &Target, MachineFunctionAnalysisManager &Analyses) { - return runExpansionTransaction(Function, Target, *Info, MachinePseudoExpansionPhase::TargetLate, - BranchInfo, Analyses); + return expandImplementation(Function, Target, *Info, MachinePseudoExpansionPhase::TargetLate, + BranchInfo, Analyses); } } // namespace rcc diff --git a/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp b/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp index 568ee68..2469039 100644 --- a/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp +++ b/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp @@ -5,15 +5,12 @@ #include "rcc/CodeGen/FrameIndexElimination.h" #include "rcc/CodeGen/MachineBasicBlock.h" #include "rcc/CodeGen/MachineFunction.h" -#include "rcc/CodeGen/MachineFunctionCloner.h" #include "rcc/CodeGen/MachineInstr.h" #include "rcc/CodeGen/MachineMemOperand.h" #include "rcc/CodeGen/MachineOperand.h" -#include "rcc/CodeGen/MachineVerifier.h" #include "rcc/CodeGen/RegisterScavenger.h" #include "rcc/CodeGen/TargetFrameLowering.h" -#include #include #include @@ -44,37 +41,7 @@ Expected PrologueEpilogueInsertionPass::run(MachineFunction & if (Info == nullptr) return makeUnexpected(ErrorCode::InvalidArgument, "PrologueEpilogueInsertion requires target frame lowering"); - auto Trial = cloneMachineFunction( - Function, Target, - MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt}); - if (!Trial) - return std::unexpected(withErrorContext(Trial.error(), "frame transaction setup failed: ")); - auto TrialRun = runImplementation(*Trial->ClonedFunction, Target, {}); - if (!TrialRun) - return std::unexpected( - withErrorContext(TrialRun.error(), "frame transaction rejected input: ")); - auto Transition = Trial->ClonedFunction->properties().applyTransition( - contract().Requires, contract().Sets, contract().Clears); - if (!Transition) { - return std::unexpected( - withErrorContext(Transition.error(), "frame transaction property update failed: ")); - } - auto Verified = - MachineVerifier::verify(*Trial->ClonedFunction, Target, - MachineVerifierOptions{.Mode = MachineVerifierMode::Generic, - .Stage = contract().VerificationStage, - .VerifyProperties = true}); - if (!Verified) { - return std::unexpected( - withErrorContext(Verified.error(), "frame transaction postcondition failed: ")); - } - - auto Result = runImplementation(Function, Target, Options); - if (!Result) { - return std::unexpected(withErrorContext( - Result.error(), "frame lowering diverged after successful transaction preflight: ")); - } - return Result; + return runImplementation(Function, Target, Options); } Expected diff --git a/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp b/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp index ea989d4..124a4f0 100644 --- a/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp +++ b/backend/lib/Target/RISCV/RISCVOptWInstrs.cpp @@ -4,7 +4,6 @@ #include "rcc/CodeGen/GenericOpcodes.h" #include "rcc/CodeGen/MachineBasicBlock.h" #include "rcc/CodeGen/MachineFunction.h" -#include "rcc/CodeGen/MachineFunctionCloner.h" #include "rcc/CodeGen/MachineInstr.h" #include "rcc/CodeGen/MachineMemOperand.h" #include "rcc/CodeGen/MachineOperand.h" @@ -593,20 +592,9 @@ public: "RISC-V W-instruction optimization requires a subtarget"); const bool PreferWordInstructions = Subtarget->features().has(RISCVSubtargetOption::PreferWordInstructions); - auto Trial = cloneMachineFunction( - Function, Target, - MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt}); - if (!Trial) - return std::unexpected(Trial.error()); - auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, PreferWordInstructions); - if (!TrialResult) - return std::unexpected(TrialResult.error()); auto Changed = runImplementation(Function, Target, PreferWordInstructions); - if (!Changed) { - return std::unexpected(withErrorContext( - Changed.error(), - "RISC-V W-instruction optimization diverged after transaction preflight: ")); - } + if (!Changed) + return std::unexpected(Changed.error()); MachinePassResult Result; Result.Changed = *Changed; -- cgit v1.2.3