PassTimingInfo.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef LLVM_IR_PASSTIMINGINFO_H
  15. #define LLVM_IR_PASSTIMINGINFO_H
  16. #include "llvm/ADT/Any.h"
  17. #include "llvm/ADT/DenseMap.h"
  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. namespace llvm {
  25. class Pass;
  26. class PassInstrumentationCallbacks;
  27. class raw_ostream;
  28. /// If -time-passes has been specified, report the timings immediately and then
  29. /// reset the timers to zero. By default it uses the stream created by
  30. /// CreateInfoOutputFile().
  31. void reportAndResetTimings(raw_ostream *OutStream = nullptr);
  32. /// Request the timer for this legacy-pass-manager's pass instance.
  33. Timer *getPassTimer(Pass *);
  34. /// This class implements -time-passes functionality for new pass manager.
  35. /// It provides the pass-instrumentation callbacks that measure the pass
  36. /// execution time. They collect timing info into individual timers as
  37. /// passes are being run. At the end of its life-time it prints the resulting
  38. /// timing report.
  39. class TimePassesHandler {
  40. /// Value of this type is capable of uniquely identifying pass invocations.
  41. /// It is a pair of string Pass-Identifier (which for now is common
  42. /// to all the instance of a given pass) + sequential invocation counter.
  43. using PassInvocationID = std::pair<StringRef, unsigned>;
  44. /// A group of all pass-timing timers.
  45. TimerGroup TG;
  46. using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
  47. /// Map of timers for pass invocations
  48. StringMap<TimerVector> TimingData;
  49. /// Stack of currently active timers.
  50. SmallVector<Timer *, 8> TimerStack;
  51. /// Custom output stream to print timing information into.
  52. /// By default (== nullptr) we emit time report into the stream created by
  53. /// CreateInfoOutputFile().
  54. raw_ostream *OutStream = nullptr;
  55. bool Enabled;
  56. bool PerRun;
  57. public:
  58. TimePassesHandler();
  59. TimePassesHandler(bool Enabled, bool PerRun = false);
  60. /// Destructor handles the print action if it has not been handled before.
  61. ~TimePassesHandler() { print(); }
  62. /// Prints out timing information and then resets the timers.
  63. void print();
  64. // We intend this to be unique per-compilation, thus no copies.
  65. TimePassesHandler(const TimePassesHandler &) = delete;
  66. void operator=(const TimePassesHandler &) = delete;
  67. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  68. /// Set a custom output stream for subsequent reporting.
  69. void setOutStream(raw_ostream &OutStream);
  70. private:
  71. /// Dumps information for running/triggered timers, useful for debugging
  72. LLVM_DUMP_METHOD void dump() const;
  73. /// Returns the new timer for each new run of the pass.
  74. Timer &getPassTimer(StringRef PassID);
  75. void startTimer(StringRef PassID);
  76. void stopTimer(StringRef PassID);
  77. // Implementation of pass instrumentation callbacks.
  78. void runBeforePass(StringRef PassID);
  79. void runAfterPass(StringRef PassID);
  80. };
  81. } // namespace llvm
  82. #endif