Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 1 | //===- PassTimingInfo.h - pass execution timing -----------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 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/StringMap.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/Support/Timer.h" |
| 23 | #include "llvm/Support/TypeName.h" |
| 24 | #include <memory> |
| 25 | namespace llvm { |
| 26 | |
| 27 | class Pass; |
| 28 | class PassInstrumentationCallbacks; |
| 29 | |
| 30 | /// If -time-passes has been specified, report the timings immediately and then |
| 31 | /// reset the timers to zero. |
| 32 | void reportAndResetTimings(); |
| 33 | |
| 34 | /// Request the timer for this legacy-pass-manager's pass instance. |
| 35 | Timer *getPassTimer(Pass *); |
| 36 | |
| 37 | /// If the user specifies the -time-passes argument on an LLVM tool command line |
| 38 | /// then the value of this boolean will be true, otherwise false. |
| 39 | /// This is the storage for the -time-passes option. |
| 40 | extern bool TimePassesIsEnabled; |
| 41 | |
| 42 | /// This class implements -time-passes functionality for new pass manager. |
| 43 | /// It provides the pass-instrumentation callbacks that measure the pass |
| 44 | /// execution time. They collect timing info into individual timers as |
| 45 | /// passes are being run. At the end of its life-time it prints the resulting |
| 46 | /// timing report. |
| 47 | class TimePassesHandler { |
| 48 | /// Value of this type is capable of uniquely identifying pass invocations. |
| 49 | /// It is a pair of string Pass-Identifier (which for now is common |
| 50 | /// to all the instance of a given pass) + sequential invocation counter. |
| 51 | using PassInvocationID = std::pair<StringRef, unsigned>; |
| 52 | |
| 53 | /// A group of all pass-timing timers. |
| 54 | TimerGroup TG; |
| 55 | |
| 56 | /// Map of timers for pass invocations |
| 57 | DenseMap<PassInvocationID, std::unique_ptr<Timer>> TimingData; |
| 58 | |
| 59 | /// Map that counts invocations of passes, for use in UniqPassID construction. |
| 60 | StringMap<unsigned> PassIDCountMap; |
| 61 | |
| 62 | /// Stack of currently active timers. |
| 63 | SmallVector<Timer *, 8> TimerStack; |
| 64 | |
| 65 | bool Enabled; |
| 66 | |
| 67 | public: |
| 68 | TimePassesHandler(bool Enabled = TimePassesIsEnabled); |
| 69 | |
| 70 | /// Destructor handles the print action if it has not been handled before. |
| 71 | ~TimePassesHandler() { |
| 72 | // First destroying the timers from TimingData, which deploys all their |
| 73 | // collected data into the TG time group member, which later prints itself |
| 74 | // when being destroyed. |
| 75 | TimingData.clear(); |
| 76 | } |
| 77 | |
| 78 | /// Prints out timing information and then resets the timers. |
| 79 | void print(); |
| 80 | |
| 81 | // We intend this to be unique per-compilation, thus no copies. |
| 82 | TimePassesHandler(const TimePassesHandler &) = delete; |
| 83 | void operator=(const TimePassesHandler &) = delete; |
| 84 | |
| 85 | void registerCallbacks(PassInstrumentationCallbacks &PIC); |
| 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 | /// Returns the incremented counter for the next invocation of \p PassID. |
| 95 | unsigned nextPassID(StringRef PassID) { return ++PassIDCountMap[PassID]; } |
| 96 | |
| 97 | void startTimer(StringRef PassID); |
| 98 | void stopTimer(StringRef PassID); |
| 99 | |
| 100 | // Implementation of pass instrumentation callbacks. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 101 | bool runBeforePass(StringRef PassID); |
| 102 | void runAfterPass(StringRef PassID); |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace llvm |
| 106 | |
| 107 | #endif |