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 /// Stack of currently active pass timers. Passes can run other 59 /// passes. 60 SmallVector<Timer *, 8> PassActiveTimerStack; 61 /// Stack of currently active analysis timers. Analyses can request other 62 /// analyses. 63 SmallVector<Timer *, 8> AnalysisActiveTimerStack; 64 65 /// Custom output stream to print timing information into. 66 /// By default (== nullptr) we emit time report into the stream created by 67 /// CreateInfoOutputFile(). 68 raw_ostream *OutStream = nullptr; 69 70 bool Enabled; 71 bool PerRun; 72 73 public: 74 TimePassesHandler(); 75 TimePassesHandler(bool Enabled, bool PerRun = false); 76 77 /// Destructor handles the print action if it has not been handled before. 78 ~TimePassesHandler() { print(); } 79 80 /// Prints out timing information and then resets the timers. 81 void print(); 82 83 // We intend this to be unique per-compilation, thus no copies. 84 TimePassesHandler(const TimePassesHandler &) = delete; 85 void operator=(const TimePassesHandler &) = delete; 86 87 void registerCallbacks(PassInstrumentationCallbacks &PIC); 88 89 /// Set a custom output stream for subsequent reporting. 90 void setOutStream(raw_ostream &OutStream); 91 92 private: 93 /// Dumps information for running/triggered timers, useful for debugging 94 LLVM_DUMP_METHOD void dump() const; 95 96 /// Returns the new timer for each new run of the pass. 97 Timer &getPassTimer(StringRef PassID, bool IsPass); 98 99 void startAnalysisTimer(StringRef PassID); 100 void stopAnalysisTimer(StringRef PassID); 101 void startPassTimer(StringRef PassID); 102 void stopPassTimer(StringRef PassID); 103 }; 104 105 } // namespace llvm 106 107 #endif 108