LLVM  9.0.0svn
PassTimingInfo.cpp
Go to the documentation of this file.
1 //===- PassTimingInfo.cpp - LLVM Pass Timing Implementation ---------------===//
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 //
9 // This file implements the LLVM Pass Timing infrastructure for both
10 // new and legacy pass managers.
11 //
12 // PassTimingInfo Class - This class is used to calculate information about the
13 // amount of time each pass takes to execute. This only happens when
14 // -time-passes is enabled on the command line.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/IR/PassTimingInfo.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Pass.h"
25 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/Mutex.h"
29 #include "llvm/Support/Timer.h"
31 #include <memory>
32 #include <string>
33 
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "time-passes"
37 
38 namespace llvm {
39 
40 bool TimePassesIsEnabled = false;
41 
43  "time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
44  cl::desc("Time each pass, printing elapsed time for each on exit"));
45 
46 namespace {
47 namespace legacy {
48 
49 //===----------------------------------------------------------------------===//
50 // Legacy pass manager's PassTimingInfo implementation
51 
52 /// Provides an interface for collecting pass timing information.
53 ///
54 /// It was intended to be generic but now we decided to split
55 /// interfaces completely. This is now exclusively for legacy-pass-manager use.
56 class PassTimingInfo {
57 public:
58  using PassInstanceID = void *;
59 
60 private:
61  StringMap<unsigned> PassIDCountMap; ///< Map that counts instances of passes
62  DenseMap<PassInstanceID, std::unique_ptr<Timer>> TimingData; ///< timers for pass instances
63  TimerGroup TG;
64 
65 public:
66  /// Default constructor for yet-inactive timeinfo.
67  /// Use \p init() to activate it.
68  PassTimingInfo();
69 
70  /// Print out timing information and release timers.
71  ~PassTimingInfo();
72 
73  /// Initializes the static \p TheTimeInfo member to a non-null value when
74  /// -time-passes is enabled. Leaves it null otherwise.
75  ///
76  /// This method may be called multiple times.
77  static void init();
78 
79  /// Prints out timing information and then resets the timers.
80  void print();
81 
82  /// Returns the timer for the specified pass if it exists.
83  Timer *getPassTimer(Pass *, PassInstanceID);
84 
85  static PassTimingInfo *TheTimeInfo;
86 
87 private:
88  Timer *newPassTimer(StringRef PassID, StringRef PassDesc);
89 };
90 
91 static ManagedStatic<sys::SmartMutex<true>> TimingInfoMutex;
92 
93 PassTimingInfo::PassTimingInfo()
94  : TG("pass", "... Pass execution timing report ...") {}
95 
96 PassTimingInfo::~PassTimingInfo() {
97  // Deleting the timers accumulates their info into the TG member.
98  // Then TG member is (implicitly) deleted, actually printing the report.
99  TimingData.clear();
100 }
101 
102 void PassTimingInfo::init() {
103  if (!TimePassesIsEnabled || TheTimeInfo)
104  return;
105 
106  // Constructed the first time this is called, iff -time-passes is enabled.
107  // This guarantees that the object will be constructed after static globals,
108  // thus it will be destroyed before them.
110  TheTimeInfo = &*TTI;
111 }
112 
113 /// Prints out timing information and then resets the timers.
114 void PassTimingInfo::print() { TG.print(*CreateInfoOutputFile()); }
115 
116 Timer *PassTimingInfo::newPassTimer(StringRef PassID, StringRef PassDesc) {
117  unsigned &num = PassIDCountMap[PassID];
118  num++;
119  // Appending description with a pass-instance number for all but the first one
120  std::string PassDescNumbered =
121  num <= 1 ? PassDesc.str() : formatv("{0} #{1}", PassDesc, num).str();
122  return new Timer(PassID, PassDescNumbered, TG);
123 }
124 
125 Timer *PassTimingInfo::getPassTimer(Pass *P, PassInstanceID Pass) {
126  if (P->getAsPMDataManager())
127  return nullptr;
128 
129  init();
130  sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
131  std::unique_ptr<Timer> &T = TimingData[Pass];
132 
133  if (!T) {
134  StringRef PassName = P->getPassName();
135  StringRef PassArgument;
136  if (const PassInfo *PI = Pass::lookupPassInfo(P->getPassID()))
137  PassArgument = PI->getPassArgument();
138  T.reset(newPassTimer(PassArgument.empty() ? PassName : PassArgument, PassName));
139  }
140  return T.get();
141 }
142 
143 PassTimingInfo *PassTimingInfo::TheTimeInfo;
144 } // namespace legacy
145 } // namespace
146 
149  if (legacy::PassTimingInfo::TheTimeInfo)
150  return legacy::PassTimingInfo::TheTimeInfo->getPassTimer(P, P);
151  return nullptr;
152 }
153 
154 /// If timing is enabled, report the times collected up to now and then reset
155 /// them.
157  if (legacy::PassTimingInfo::TheTimeInfo)
158  legacy::PassTimingInfo::TheTimeInfo->print();
159 }
160 
161 //===----------------------------------------------------------------------===//
162 // Pass timing handling for the New Pass Manager
163 //===----------------------------------------------------------------------===//
164 
165 /// Returns the timer for the specified pass invocation of \p PassID.
166 /// Each time it creates a new timer.
167 Timer &TimePassesHandler::getPassTimer(StringRef PassID) {
168  // Bump counts for each request of the timer.
169  unsigned Count = nextPassID(PassID);
170 
171  // Unconditionally appending description with a pass-invocation number.
172  std::string FullDesc = formatv("{0} #{1}", PassID, Count).str();
173 
174  PassInvocationID UID{PassID, Count};
175  Timer *T = new Timer(PassID, FullDesc, TG);
176  auto Pair = TimingData.try_emplace(UID, T);
177  assert(Pair.second && "should always create a new timer");
178  return *(Pair.first->second.get());
179 }
180 
182  : TG("pass", "... Pass execution timing report ..."), Enabled(Enabled) {}
183 
185 
186 LLVM_DUMP_METHOD void TimePassesHandler::dump() const {
187  dbgs() << "Dumping timers for " << getTypeName<TimePassesHandler>()
188  << ":\n\tRunning:\n";
189  for (auto &I : TimingData) {
190  const Timer *MyTimer = I.second.get();
191  if (!MyTimer || MyTimer->isRunning())
192  dbgs() << "\tTimer " << MyTimer << " for pass " << I.first.first << "("
193  << I.first.second << ")\n";
194  }
195  dbgs() << "\tTriggered:\n";
196  for (auto &I : TimingData) {
197  const Timer *MyTimer = I.second.get();
198  if (!MyTimer || (MyTimer->hasTriggered() && !MyTimer->isRunning()))
199  dbgs() << "\tTimer " << MyTimer << " for pass " << I.first.first << "("
200  << I.first.second << ")\n";
201  }
202 }
203 
204 void TimePassesHandler::startTimer(StringRef PassID) {
205  Timer &MyTimer = getPassTimer(PassID);
206  TimerStack.push_back(&MyTimer);
207  if (!MyTimer.isRunning())
208  MyTimer.startTimer();
209 }
210 
211 void TimePassesHandler::stopTimer(StringRef PassID) {
212  assert(TimerStack.size() > 0 && "empty stack in popTimer");
213  Timer *MyTimer = TimerStack.pop_back_val();
214  assert(MyTimer && "timer should be present");
215  if (MyTimer->isRunning())
216  MyTimer->stopTimer();
217 }
218 
219 static bool matchPassManager(StringRef PassID) {
220  size_t prefix_pos = PassID.find('<');
221  if (prefix_pos == StringRef::npos)
222  return false;
223  StringRef Prefix = PassID.substr(0, prefix_pos);
224  return Prefix.endswith("PassManager") || Prefix.endswith("PassAdaptor") ||
225  Prefix.endswith("AnalysisManagerProxy");
226 }
227 
228 bool TimePassesHandler::runBeforePass(StringRef PassID) {
229  if (matchPassManager(PassID))
230  return true;
231 
232  startTimer(PassID);
233 
234  LLVM_DEBUG(dbgs() << "after runBeforePass(" << PassID << ")\n");
235  LLVM_DEBUG(dump());
236 
237  // we are not going to skip this pass, thus return true.
238  return true;
239 }
240 
241 void TimePassesHandler::runAfterPass(StringRef PassID) {
242  if (matchPassManager(PassID))
243  return;
244 
245  stopTimer(PassID);
246 
247  LLVM_DEBUG(dbgs() << "after runAfterPass(" << PassID << ")\n");
248  LLVM_DEBUG(dump());
249 }
250 
252  if (!Enabled)
253  return;
254 
256  [this](StringRef P, Any) { return this->runBeforePass(P); });
258  [this](StringRef P, Any) { this->runAfterPass(P); });
260  [this](StringRef P) { this->runAfterPass(P); });
262  [this](StringRef P, Any) { this->runBeforePass(P); });
264  [this](StringRef P, Any) { this->runAfterPass(P); });
265 }
266 
267 } // namespace llvm
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
Definition: Any.h:26
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
void registerCallbacks(PassInstrumentationCallbacks &PIC)
static cl::opt< bool, true > EnableTiming("time-passes", cl::location(TimePassesIsEnabled), cl::Hidden, cl::desc("Time each pass, printing elapsed time for each on exit"))
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:267
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:464
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:111
void print()
Prints out timing information and then resets the timers.
static sys::Mutex Lock
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
bool isRunning() const
Check if the timer is currently running.
Definition: Timer.h:113
void reportAndResetTimings()
If -time-passes has been specified, report the timings immediately and then reset the timers to zero...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:74
Timer * getPassTimer(Pass *)
Request the timer for this legacy-pass-manager&#39;s pass instance.
This header defines classes/functions to handle pass execution timing information with interfaces for...
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:578
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
TimePassesHandler(bool Enabled=TimePassesIsEnabled)
This class is used to track the amount of time spent between invocations of its startTimer()/stopTime...
Definition: Timer.h:76
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
std::unique_ptr< raw_fd_ostream > CreateInfoOutputFile()
Return a file stream to print our output on.
Definition: Timer.cpp:54
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:208
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:422
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:99
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:285
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:30
void print(raw_ostream &OS)
Print any started timers in this group.
Definition: Timer.cpp:359
print lazy value Lazy Value Info Printer Pass
void startTimer()
Start the timer running.
Definition: Timer.cpp:133
static bool Enabled
Definition: Statistic.cpp:50
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void registerAfterPassInvalidatedCallback(CallableT C)
static const size_t npos
Definition: StringRef.h:50
#define I(x, y, z)
Definition: MD5.cpp:58
The TimerGroup class is used to group together related timers into a single report that is printed wh...
Definition: Timer.h:170
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:60
bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
Definition: Pass.h:356
#define LLVM_DEBUG(X)
Definition: Debug.h:122
static bool matchPassManager(StringRef PassID)
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:438
bool hasTriggered() const
Check if startTimer() has ever been called on this timer.
Definition: Timer.h:116