Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:09

0001 //===- PassTimingInfo.h - pass execution timing -----------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 ///
0010 /// This header defines classes/functions to handle pass execution timing
0011 /// information with interfaces for both pass managers.
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_IR_PASSTIMINGINFO_H
0016 #define LLVM_IR_PASSTIMINGINFO_H
0017 
0018 #include "llvm/ADT/SmallVector.h"
0019 #include "llvm/ADT/StringMap.h"
0020 #include "llvm/ADT/StringRef.h"
0021 #include "llvm/Support/Timer.h"
0022 #include <memory>
0023 #include <utility>
0024 
0025 namespace llvm {
0026 
0027 class Pass;
0028 class PassInstrumentationCallbacks;
0029 class raw_ostream;
0030 
0031 /// If -time-passes has been specified, report the timings immediately and then
0032 /// reset the timers to zero. By default it uses the stream created by
0033 /// CreateInfoOutputFile().
0034 void reportAndResetTimings(raw_ostream *OutStream = nullptr);
0035 
0036 /// Request the timer for this legacy-pass-manager's pass instance.
0037 Timer *getPassTimer(Pass *);
0038 
0039 /// This class implements -time-passes functionality for new pass manager.
0040 /// It provides the pass-instrumentation callbacks that measure the pass
0041 /// execution time. They collect timing info into individual timers as
0042 /// passes are being run. At the end of its life-time it prints the resulting
0043 /// timing report.
0044 class TimePassesHandler {
0045   /// Value of this type is capable of uniquely identifying pass invocations.
0046   /// It is a pair of string Pass-Identifier (which for now is common
0047   /// to all the instance of a given pass) + sequential invocation counter.
0048   using PassInvocationID = std::pair<StringRef, unsigned>;
0049 
0050   /// Groups of timers for passes and analyses.
0051   TimerGroup PassTG;
0052   TimerGroup AnalysisTG;
0053 
0054   using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
0055   /// Map of timers for pass invocations
0056   StringMap<TimerVector> TimingData;
0057 
0058   /// Stack of currently active pass timers. Passes can run other
0059   /// passes.
0060   SmallVector<Timer *, 8> PassActiveTimerStack;
0061   /// Stack of currently active analysis timers. Analyses can request other
0062   /// analyses.
0063   SmallVector<Timer *, 8> AnalysisActiveTimerStack;
0064 
0065   /// Custom output stream to print timing information into.
0066   /// By default (== nullptr) we emit time report into the stream created by
0067   /// CreateInfoOutputFile().
0068   raw_ostream *OutStream = nullptr;
0069 
0070   bool Enabled;
0071   bool PerRun;
0072 
0073 public:
0074   TimePassesHandler();
0075   TimePassesHandler(bool Enabled, bool PerRun = false);
0076 
0077   /// Destructor handles the print action if it has not been handled before.
0078   ~TimePassesHandler() { print(); }
0079 
0080   /// Prints out timing information and then resets the timers.
0081   void print();
0082 
0083   // We intend this to be unique per-compilation, thus no copies.
0084   TimePassesHandler(const TimePassesHandler &) = delete;
0085   void operator=(const TimePassesHandler &) = delete;
0086 
0087   void registerCallbacks(PassInstrumentationCallbacks &PIC);
0088 
0089   /// Set a custom output stream for subsequent reporting.
0090   void setOutStream(raw_ostream &OutStream);
0091 
0092 private:
0093   /// Dumps information for running/triggered timers, useful for debugging
0094   LLVM_DUMP_METHOD void dump() const;
0095 
0096   /// Returns the new timer for each new run of the pass.
0097   Timer &getPassTimer(StringRef PassID, bool IsPass);
0098 
0099   void startAnalysisTimer(StringRef PassID);
0100   void stopAnalysisTimer(StringRef PassID);
0101   void startPassTimer(StringRef PassID);
0102   void stopPassTimer(StringRef PassID);
0103 };
0104 
0105 } // namespace llvm
0106 
0107 #endif