1 //===- PassTimingInfo.h - pass execution timing -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This header defines classes/functions to handle pass execution timing
11 /// information with interfaces for both pass managers.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_PASSTIMINGINFO_H
16 #define LLVM_IR_PASSTIMINGINFO_H
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Timer.h"
22 #include <memory>
23 #include <utility>
24 
25 namespace llvm {
26 
27 class Pass;
28 class PassInstrumentationCallbacks;
29 class raw_ostream;
30 
31 /// If -time-passes has been specified, report the timings immediately and then
32 /// reset the timers to zero. By default it uses the stream created by
33 /// CreateInfoOutputFile().
34 void reportAndResetTimings(raw_ostream *OutStream = nullptr);
35 
36 /// Request the timer for this legacy-pass-manager's pass instance.
37 Timer *getPassTimer(Pass *);
38 
39 /// This class implements -time-passes functionality for new pass manager.
40 /// It provides the pass-instrumentation callbacks that measure the pass
41 /// execution time. They collect timing info into individual timers as
42 /// passes are being run. At the end of its life-time it prints the resulting
43 /// timing report.
44 class TimePassesHandler {
45   /// Value of this type is capable of uniquely identifying pass invocations.
46   /// It is a pair of string Pass-Identifier (which for now is common
47   /// to all the instance of a given pass) + sequential invocation counter.
48   using PassInvocationID = std::pair<StringRef, unsigned>;
49 
50   /// Groups of timers for passes and analyses.
51   TimerGroup PassTG;
52   TimerGroup AnalysisTG;
53 
54   using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
55   /// Map of timers for pass invocations
56   StringMap<TimerVector> TimingData;
57 
58   /// Currently active pass timer.
59   Timer *ActivePassTimer = nullptr;
60   /// Stack of currently active analysis timers. Analyses can request other
61   /// analyses.
62   SmallVector<Timer *, 8> AnalysisActiveTimerStack;
63 
64   /// Custom output stream to print timing information into.
65   /// By default (== nullptr) we emit time report into the stream created by
66   /// CreateInfoOutputFile().
67   raw_ostream *OutStream = nullptr;
68 
69   bool Enabled;
70   bool PerRun;
71 
72 public:
73   TimePassesHandler();
74   TimePassesHandler(bool Enabled, bool PerRun = false);
75 
76   /// Destructor handles the print action if it has not been handled before.
77   ~TimePassesHandler() { print(); }
78 
79   /// Prints out timing information and then resets the timers.
80   void print();
81 
82   // We intend this to be unique per-compilation, thus no copies.
83   TimePassesHandler(const TimePassesHandler &) = delete;
84   void operator=(const TimePassesHandler &) = delete;
85 
86   void registerCallbacks(PassInstrumentationCallbacks &PIC);
87 
88   /// Set a custom output stream for subsequent reporting.
89   void setOutStream(raw_ostream &OutStream);
90 
91 private:
92   /// Dumps information for running/triggered timers, useful for debugging
93   LLVM_DUMP_METHOD void dump() const;
94 
95   /// Returns the new timer for each new run of the pass.
96   Timer &getPassTimer(StringRef PassID, bool IsPass);
97 
98   void startAnalysisTimer(StringRef PassID);
99   void stopAnalysisTimer(StringRef PassID);
100   void startPassTimer(StringRef PassID);
101   void stopPassTimer(StringRef PassID);
102 };
103 
104 } // namespace llvm
105 
106 #endif
107