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/Any.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/Support/Timer.h" 24 #include <memory> 25 #include <utility> 26 27 namespace llvm { 28 29 class Pass; 30 class PassInstrumentationCallbacks; 31 class raw_ostream; 32 33 /// If -time-passes has been specified, report the timings immediately and then 34 /// reset the timers to zero. By default it uses the stream created by 35 /// CreateInfoOutputFile(). 36 void reportAndResetTimings(raw_ostream *OutStream = nullptr); 37 38 /// Request the timer for this legacy-pass-manager's pass instance. 39 Timer *getPassTimer(Pass *); 40 41 /// This class implements -time-passes functionality for new pass manager. 42 /// It provides the pass-instrumentation callbacks that measure the pass 43 /// execution time. They collect timing info into individual timers as 44 /// passes are being run. At the end of its life-time it prints the resulting 45 /// timing report. 46 class TimePassesHandler { 47 /// Value of this type is capable of uniquely identifying pass invocations. 48 /// It is a pair of string Pass-Identifier (which for now is common 49 /// to all the instance of a given pass) + sequential invocation counter. 50 using PassInvocationID = std::pair<StringRef, unsigned>; 51 52 /// A group of all pass-timing timers. 53 TimerGroup TG; 54 55 using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>; 56 /// Map of timers for pass invocations 57 StringMap<TimerVector> TimingData; 58 59 /// Stack of currently active timers. 60 SmallVector<Timer *, 8> TimerStack; 61 62 /// Custom output stream to print timing information into. 63 /// By default (== nullptr) we emit time report into the stream created by 64 /// CreateInfoOutputFile(). 65 raw_ostream *OutStream = nullptr; 66 67 bool Enabled; 68 bool PerRun; 69 70 public: 71 TimePassesHandler(); 72 TimePassesHandler(bool Enabled, bool PerRun = false); 73 74 /// Destructor handles the print action if it has not been handled before. ~TimePassesHandler()75 ~TimePassesHandler() { print(); } 76 77 /// Prints out timing information and then resets the timers. 78 void print(); 79 80 // We intend this to be unique per-compilation, thus no copies. 81 TimePassesHandler(const TimePassesHandler &) = delete; 82 void operator=(const TimePassesHandler &) = delete; 83 84 void registerCallbacks(PassInstrumentationCallbacks &PIC); 85 86 /// Set a custom output stream for subsequent reporting. 87 void setOutStream(raw_ostream &OutStream); 88 89 private: 90 /// Dumps information for running/triggered timers, useful for debugging 91 LLVM_DUMP_METHOD void dump() const; 92 93 /// Returns the new timer for each new run of the pass. 94 Timer &getPassTimer(StringRef PassID); 95 96 void startTimer(StringRef PassID); 97 void stopTimer(StringRef PassID); 98 99 // Implementation of pass instrumentation callbacks. 100 void runBeforePass(StringRef PassID); 101 void runAfterPass(StringRef PassID); 102 }; 103 104 } // namespace llvm 105 106 #endif 107