| 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 | /// A group of all pass-timing timers. |
| 51 | TimerGroup TG; |
| 52 | |
| 53 | using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>; |
| 54 | /// Map of timers for pass invocations |
| 55 | StringMap<TimerVector> TimingData; |
| 56 | |
| 57 | /// Stack of currently active timers. |
| 58 | SmallVector<Timer *, 8> TimerStack; |
| 59 | |
| 60 | /// Custom output stream to print timing information into. |
| 61 | /// By default (== nullptr) we emit time report into the stream created by |
| 62 | /// CreateInfoOutputFile(). |
| 63 | raw_ostream *OutStream = nullptr; |
| 64 | |
| 65 | bool Enabled; |
| 66 | bool PerRun; |
| 67 | |
| 68 | public: |
| 69 | TimePassesHandler(); |
| 70 | TimePassesHandler(bool Enabled, bool PerRun = false); |
| 71 | |
| 72 | /// Destructor handles the print action if it has not been handled before. |
| 73 | ~TimePassesHandler() { print(); } |
| 74 | |
| 75 | /// Prints out timing information and then resets the timers. |
| 76 | void print(); |
| 77 | |
| 78 | // We intend this to be unique per-compilation, thus no copies. |
| 79 | TimePassesHandler(const TimePassesHandler &) = delete; |
| 80 | void operator=(const TimePassesHandler &) = delete; |
| 81 | |
| 82 | void registerCallbacks(PassInstrumentationCallbacks &PIC); |
| 83 | |
| 84 | /// Set a custom output stream for subsequent reporting. |
| 85 | void setOutStream(raw_ostream &OutStream); |
| 86 | |
| 87 | private: |
| 88 | /// Dumps information for running/triggered timers, useful for debugging |
| 89 | LLVM_DUMP_METHOD void dump() const; |
| 90 | |
| 91 | /// Returns the new timer for each new run of the pass. |
| 92 | Timer &getPassTimer(StringRef PassID); |
| 93 | |
| 94 | void startTimer(StringRef PassID); |
| 95 | void stopTimer(StringRef PassID); |
| 96 | |
| 97 | // Implementation of pass instrumentation callbacks. |
| 98 | void runBeforePass(StringRef PassID); |
| 99 | void runAfterPass(StringRef PassID); |
| 100 | }; |
| 101 | |
| 102 | } // namespace llvm |
| 103 | |
| 104 | #endif |