PassTimingInfo.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PassTimingInfo.h - pass execution timing -----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. ///
  15. /// This header defines classes/functions to handle pass execution timing
  16. /// information with interfaces for both pass managers.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_PASSTIMINGINFO_H
  20. #define LLVM_IR_PASSTIMINGINFO_H
  21. #include "llvm/ADT/Any.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/StringMap.h"
  25. #include "llvm/ADT/StringRef.h"
  26. #include "llvm/Support/Timer.h"
  27. #include <memory>
  28. #include <utility>
  29. namespace llvm {
  30. class Pass;
  31. class PassInstrumentationCallbacks;
  32. class raw_ostream;
  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. /// Request the timer for this legacy-pass-manager's pass instance.
  38. Timer *getPassTimer(Pass *);
  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. /// A group of all pass-timing timers.
  50. TimerGroup TG;
  51. using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
  52. /// Map of timers for pass invocations
  53. StringMap<TimerVector> TimingData;
  54. /// Stack of currently active timers.
  55. SmallVector<Timer *, 8> TimerStack;
  56. /// Custom output stream to print timing information into.
  57. /// By default (== nullptr) we emit time report into the stream created by
  58. /// CreateInfoOutputFile().
  59. raw_ostream *OutStream = nullptr;
  60. bool Enabled;
  61. bool PerRun;
  62. public:
  63. TimePassesHandler();
  64. TimePassesHandler(bool Enabled, bool PerRun = false);
  65. /// Destructor handles the print action if it has not been handled before.
  66. ~TimePassesHandler() { print(); }
  67. /// Prints out timing information and then resets the timers.
  68. void print();
  69. // We intend this to be unique per-compilation, thus no copies.
  70. TimePassesHandler(const TimePassesHandler &) = delete;
  71. void operator=(const TimePassesHandler &) = delete;
  72. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  73. /// Set a custom output stream for subsequent reporting.
  74. void setOutStream(raw_ostream &OutStream);
  75. private:
  76. /// Dumps information for running/triggered timers, useful for debugging
  77. LLVM_DUMP_METHOD void dump() const;
  78. /// Returns the new timer for each new run of the pass.
  79. Timer &getPassTimer(StringRef PassID);
  80. void startTimer(StringRef PassID);
  81. void stopTimer(StringRef PassID);
  82. // Implementation of pass instrumentation callbacks.
  83. void runBeforePass(StringRef PassID);
  84. void runAfterPass(StringRef PassID);
  85. };
  86. } // namespace llvm
  87. #endif
  88. #ifdef __GNUC__
  89. #pragma GCC diagnostic pop
  90. #endif