1 //===- PassTimingInfo.h - pass execution timing -----------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// 11 /// This header defines classes/functions to handle pass execution timing 12 /// information with interfaces for both pass managers. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_IR_PASSTIMINGINFO_H 17 #define LLVM_IR_PASSTIMINGINFO_H 18 19 #include "llvm/ADT/Any.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/Support/Timer.h" 24 #include "llvm/Support/TypeName.h" 25 #include <memory> 26 namespace llvm { 27 28 class Pass; 29 class PassInstrumentationCallbacks; 30 31 /// If -time-passes has been specified, report the timings immediately and then 32 /// reset the timers to zero. 33 void reportAndResetTimings(); 34 35 /// Request the timer for this legacy-pass-manager's pass instance. 36 Timer *getPassTimer(Pass *); 37 38 /// If the user specifies the -time-passes argument on an LLVM tool command line 39 /// then the value of this boolean will be true, otherwise false. 40 /// This is the storage for the -time-passes option. 41 extern bool TimePassesIsEnabled; 42 43 /// This class implements -time-passes functionality for new pass manager. 44 /// It provides the pass-instrumentation callbacks that measure the pass 45 /// execution time. They collect timing info into individual timers as 46 /// passes are being run. At the end of its life-time it prints the resulting 47 /// timing report. 48 class TimePassesHandler { 49 /// Value of this type is capable of uniquely identifying pass invocations. 50 /// It is a pair of string Pass-Identifier (which for now is common 51 /// to all the instance of a given pass) + sequential invocation counter. 52 using PassInvocationID = std::pair<StringRef, unsigned>; 53 54 /// A group of all pass-timing timers. 55 TimerGroup TG; 56 57 /// Map of timers for pass invocations 58 DenseMap<PassInvocationID, std::unique_ptr<Timer>> TimingData; 59 60 /// Map that counts invocations of passes, for use in UniqPassID construction. 61 StringMap<unsigned> PassIDCountMap; 62 63 /// Stack of currently active timers. 64 SmallVector<Timer *, 8> TimerStack; 65 66 bool Enabled; 67 68 public: 69 TimePassesHandler(bool Enabled = TimePassesIsEnabled); 70 71 /// Destructor handles the print action if it has not been handled before. ~TimePassesHandler()72 ~TimePassesHandler() { 73 // First destroying the timers from TimingData, which deploys all their 74 // collected data into the TG time group member, which later prints itself 75 // when being destroyed. 76 TimingData.clear(); 77 } 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 private: 89 /// Dumps information for running/triggered timers, useful for debugging 90 LLVM_DUMP_METHOD void dump() const; 91 92 /// Returns the new timer for each new run of the pass. 93 Timer &getPassTimer(StringRef PassID); 94 95 /// Returns the incremented counter for the next invocation of \p PassID. nextPassID(StringRef PassID)96 unsigned nextPassID(StringRef PassID) { return ++PassIDCountMap[PassID]; } 97 98 void startTimer(StringRef PassID); 99 void stopTimer(StringRef PassID); 100 101 // Implementation of pass instrumentation callbacks. 102 bool runBeforePass(StringRef PassID); 103 void runAfterPass(StringRef PassID); 104 }; 105 106 } // namespace llvm 107 108 #endif 109