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/Compiler.h" 22 #include "llvm/Support/Timer.h" 23 #include <memory> 24 #include <utility> 25 26 namespace llvm { 27 28 class Pass; 29 class PassInstrumentationCallbacks; 30 class raw_ostream; 31 32 /// If -time-passes has been specified, report the timings immediately and then 33 /// reset the timers to zero. By default it uses the stream created by 34 /// CreateInfoOutputFile(). 35 LLVM_ABI void reportAndResetTimings(raw_ostream *OutStream = nullptr); 36 37 /// Request the timer for this legacy-pass-manager's pass instance. 38 LLVM_ABI Timer *getPassTimer(Pass *); 39 40 /// This class implements -time-passes functionality for new pass manager. 41 /// It provides the pass-instrumentation callbacks that measure the pass 42 /// execution time. They collect timing info into individual timers as 43 /// passes are being run. 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 NamedRegionTimer::getNamedTimerGroup(PassGroupName, PassGroupDesc); 53 TimerGroup &AnalysisTG = NamedRegionTimer::getNamedTimerGroup( 54 AnalysisGroupName, AnalysisGroupDesc); 55 56 using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>; 57 /// Map of timers for pass invocations 58 StringMap<TimerVector> TimingData; 59 60 /// Stack of currently active pass timers. Passes can run other 61 /// passes. 62 SmallVector<Timer *, 8> PassActiveTimerStack; 63 /// Stack of currently active analysis timers. Analyses can request other 64 /// analyses. 65 SmallVector<Timer *, 8> AnalysisActiveTimerStack; 66 67 /// Custom output stream to print timing information into. 68 /// By default (== nullptr) we emit time report into the stream created by 69 /// CreateInfoOutputFile(). 70 raw_ostream *OutStream = nullptr; 71 72 bool Enabled; 73 bool PerRun; 74 75 public: 76 static constexpr StringRef PassGroupName = "pass"; 77 static constexpr StringRef AnalysisGroupName = "analysis"; 78 static constexpr StringRef PassGroupDesc = "Pass execution timing report"; 79 static constexpr StringRef AnalysisGroupDesc = 80 "Analysis execution timing report"; 81 82 LLVM_ABI TimePassesHandler(); 83 LLVM_ABI TimePassesHandler(bool Enabled, bool PerRun = false); 84 85 /// Prints out timing information and then resets the timers. 86 LLVM_ABI void print(); 87 88 // We intend this to be unique per-compilation, thus no copies. 89 TimePassesHandler(const TimePassesHandler &) = delete; 90 void operator=(const TimePassesHandler &) = delete; 91 92 LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC); 93 94 /// Set a custom output stream for subsequent reporting. 95 LLVM_ABI void setOutStream(raw_ostream &OutStream); 96 97 private: 98 /// Dumps information for running/triggered timers, useful for debugging 99 LLVM_DUMP_METHOD void dump() const; 100 101 /// Returns the new timer for each new run of the pass. 102 Timer &getPassTimer(StringRef PassID, bool IsPass); 103 104 void startAnalysisTimer(StringRef PassID); 105 void stopAnalysisTimer(StringRef PassID); 106 void startPassTimer(StringRef PassID); 107 void stopPassTimer(StringRef PassID); 108 }; 109 110 } // namespace llvm 111 112 #endif 113