From 4813e41cb6765e1b87acf12c485f75c04c1938ff Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Fri, 28 Aug 2026 13:49:31 +0800 Subject: [CodeGen] Remove pass instrumentation snapshots --- backend/lib/CodeGen/MachinePassManager.cpp | 100 +++-------------------------- 1 file changed, 10 insertions(+), 90 deletions(-) (limited to 'backend/lib') diff --git a/backend/lib/CodeGen/MachinePassManager.cpp b/backend/lib/CodeGen/MachinePassManager.cpp index 236457e..fc3f6be 100644 --- a/backend/lib/CodeGen/MachinePassManager.cpp +++ b/backend/lib/CodeGen/MachinePassManager.cpp @@ -5,7 +5,6 @@ #include "rcc/CodeGen/CompilationUnit.h" #include "rcc/CodeGen/MachineBasicBlock.h" #include "rcc/CodeGen/MachineFunction.h" -#include "rcc/CodeGen/MachineFunctionCloner.h" #include "rcc/CodeGen/MachineInstr.h" #include "rcc/CodeGen/MachineOperand.h" #include "rcc/CodeGen/MachineVerifier.h" @@ -30,9 +29,6 @@ namespace rcc { -thread_local MachinePassInstrumentation::BufferedEvents *MachinePassInstrumentation::ActiveBuffer = - nullptr; - namespace { using Clock = std::chrono::steady_clock; @@ -322,51 +318,8 @@ InstrumentationStatistic &MachinePassInstrumentation::statistic(std::string_view return Position->second; } -bool MachinePassInstrumentation::bufferFunctionEvent( - const MachineFunction &Function, std::function Replay) const { - if (ActiveBuffer == nullptr) - return false; - - std::shared_ptr Snapshot; - auto Cloned = cloneMachineFunction( - Function, *ActiveBuffer->Target, - MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt}); - if (Cloned) { - Snapshot = std::shared_ptr(std::move(Cloned->ClonedFunction)); - } else { - Snapshot = - std::shared_ptr(&Function, [](const MachineFunction *) noexcept {}); - } - ActiveBuffer->Events.emplace_back( - [Snapshot = std::move(Snapshot), Replay = std::move(Replay)]() { Replay(*Snapshot); }); - return true; -} - -void MachinePassInstrumentation::beginBuffering(BufferedEvents &Buffer, - const TargetDescription &Target) const noexcept { - Buffer.Target = &Target; - Buffer.Events.clear(); - ActiveBuffer = &Buffer; -} - -void MachinePassInstrumentation::endBuffering() const noexcept { - ActiveBuffer = nullptr; -} - -void MachinePassInstrumentation::replayBufferedEvents(BufferedEvents &Buffer) const { - for (const std::function &Event : Buffer.Events) - Event(); - Buffer.Events.clear(); - Buffer.Target = nullptr; -} - void MachinePassInstrumentation::before(std::string_view Name, const MachineFunction &Function) const { - if (bufferFunctionEvent(Function, [this, Name = std::string(Name)](const MachineFunction &Input) { - before(Name, Input); - })) { - return; - } std::vector Callbacks; std::vector Printers; { @@ -383,12 +336,6 @@ void MachinePassInstrumentation::before(std::string_view Name, void MachinePassInstrumentation::after(std::string_view Name, const MachineFunction &Function, const MachinePassResult &Result, std::uint64_t Nanoseconds) const { - if (bufferFunctionEvent(Function, [this, Name = std::string(Name), Result, - Nanoseconds](const MachineFunction &Input) { - after(Name, Input, Result, Nanoseconds); - })) { - return; - } std::vector Callbacks; std::vector Printers; { @@ -408,12 +355,6 @@ void MachinePassInstrumentation::after(std::string_view Name, const MachineFunct void MachinePassInstrumentation::failure(std::string_view Name, const MachineFunction &Function, const Error &ErrorValue, std::uint64_t Nanoseconds) const { - if (bufferFunctionEvent(Function, [this, Name = std::string(Name), ErrorValue, - Nanoseconds](const MachineFunction &Input) { - failure(Name, Input, ErrorValue, Nanoseconds); - })) { - return; - } std::vector Callbacks; { std::scoped_lock Lock(Mutex); @@ -429,11 +370,6 @@ void MachinePassInstrumentation::failure(std::string_view Name, const MachineFun void MachinePassInstrumentation::beforeAnalysis(std::string_view Name, const MachineFunction &Function) const { - if (bufferFunctionEvent(Function, [this, Name = std::string(Name)](const MachineFunction &Input) { - beforeAnalysis(Name, Input); - })) { - return; - } std::vector Callbacks; { std::scoped_lock Lock(Mutex); @@ -446,12 +382,6 @@ void MachinePassInstrumentation::beforeAnalysis(std::string_view Name, void MachinePassInstrumentation::afterAnalysis(std::string_view Name, const MachineFunction &Function, bool CacheHit, std::uint64_t Nanoseconds) const { - if (bufferFunctionEvent(Function, [this, Name = std::string(Name), CacheHit, - Nanoseconds](const MachineFunction &Input) { - afterAnalysis(Name, Input, CacheHit, Nanoseconds); - })) { - return; - } std::vector Callbacks; { std::scoped_lock Lock(Mutex); @@ -469,12 +399,6 @@ void MachinePassInstrumentation::analysisFailure(std::string_view Name, const MachineFunction &Function, const Error &ErrorValue, std::uint64_t Nanoseconds) const { - if (bufferFunctionEvent(Function, [this, Name = std::string(Name), ErrorValue, - Nanoseconds](const MachineFunction &Input) { - analysisFailure(Name, Input, ErrorValue, Nanoseconds); - })) { - return; - } std::vector Callbacks; { std::scoped_lock Lock(Mutex); @@ -490,12 +414,6 @@ void MachinePassInstrumentation::analysisFailure(std::string_view Name, void MachinePassInstrumentation::change(std::string_view Name, const MachineFunction &Function, MachinePassChanges Changes, bool FingerprintChanged) const { - if (bufferFunctionEvent(Function, [this, Name = std::string(Name), Changes, - FingerprintChanged](const MachineFunction &Input) { - change(Name, Input, Changes, FingerprintChanged); - })) { - return; - } std::vector Callbacks; { std::scoped_lock Lock(Mutex); @@ -680,6 +598,12 @@ std::vector MachinePassInstrumentation::actionEvents() const return Result; } +bool MachinePassInstrumentation::requiresSerialFunctionExecution() const { + std::scoped_lock Lock(Mutex); + return !Before.empty() || !After.empty() || !Failure.empty() || !BeforeAnalysis.empty() || + !AfterAnalysis.empty() || !AnalysisFailure.empty() || !IRPrint.empty() || !Change.empty(); +} + Expected PassManagerCallbacks::validate( const MachineFunctionPass &Pass) { @@ -848,7 +772,10 @@ PassManagerCallbacksrequiresSerialFunctionExecution(); + const std::size_t WorkerCount = + RequiresSerialInstrumentation ? 1 : std::min(ThreadCount, FunctionCount); if (WorkerCount > 1) { for (const std::unique_ptr &Pass : Passes) { if (Pass->contract().ThreadSafety != PassThreadSafety::ThreadSafe) { @@ -871,7 +798,6 @@ PassManagerCallbacks> Failures(FunctionCount); - std::vector EventBuffers(FunctionCount); std::atomic NextFunction = 0; const auto Worker = [&]() { while (true) { @@ -884,12 +810,8 @@ PassManagerCallbacksbeginBuffering(EventBuffers[Index], Target); auto Result = PassManagerCallbacks::run(Passes, Options, *Function, Target, *Managers[Index], Instrumentation); - if (Instrumentation != nullptr) - Instrumentation->endBuffering(); if (!Result) Failures[Index] = Result.error(); } @@ -903,8 +825,6 @@ PassManagerCallbacksreplayBufferedEvents(EventBuffers[Index]); const auto &Failure = Failures[Index]; if (Failure) { return std::unexpected(withErrorContext( -- cgit v1.2.3