PassTimingInfo.h 3.7 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/SmallVector.h"
  22. #include "llvm/ADT/StringMap.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/Support/Timer.h"
  25. #include <memory>
  26. #include <utility>
  27. namespace llvm {
  28. class Pass;
  29. class PassInstrumentationCallbacks;
  30. class raw_ostream;
  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. /// Request the timer for this legacy-pass-manager's pass instance.
  36. Timer *getPassTimer(Pass *);
  37. /// This class implements -time-passes functionality for new pass manager.
  38. /// It provides the pass-instrumentation callbacks that measure the pass
  39. /// execution time. They collect timing info into individual timers as
  40. /// passes are being run. At the end of its life-time it prints the resulting
  41. /// timing report.
  42. class TimePassesHandler {
  43. /// Value of this type is capable of uniquely identifying pass invocations.
  44. /// It is a pair of string Pass-Identifier (which for now is common
  45. /// to all the instance of a given pass) + sequential invocation counter.
  46. using PassInvocationID = std::pair<StringRef, unsigned>;
  47. /// Groups of timers for passes and analyses.
  48. TimerGroup PassTG;
  49. TimerGroup AnalysisTG;
  50. using TimerVector = llvm::SmallVector<std::unique_ptr<Timer>, 4>;
  51. /// Map of timers for pass invocations
  52. StringMap<TimerVector> TimingData;
  53. /// Currently active pass timer.
  54. Timer *ActivePassTimer = nullptr;
  55. /// Stack of currently active analysis timers. Analyses can request other
  56. /// analyses.
  57. SmallVector<Timer *, 8> AnalysisActiveTimerStack;
  58. /// Custom output stream to print timing information into.
  59. /// By default (== nullptr) we emit time report into the stream created by
  60. /// CreateInfoOutputFile().
  61. raw_ostream *OutStream = nullptr;
  62. bool Enabled;
  63. bool PerRun;
  64. public:
  65. TimePassesHandler();
  66. TimePassesHandler(bool Enabled, bool PerRun = false);
  67. /// Destructor handles the print action if it has not been handled before.
  68. ~TimePassesHandler() { print(); }
  69. /// Prints out timing information and then resets the timers.
  70. void print();
  71. // We intend this to be unique per-compilation, thus no copies.
  72. TimePassesHandler(const TimePassesHandler &) = delete;
  73. void operator=(const TimePassesHandler &) = delete;
  74. void registerCallbacks(PassInstrumentationCallbacks &PIC);
  75. /// Set a custom output stream for subsequent reporting.
  76. void setOutStream(raw_ostream &OutStream);
  77. private:
  78. /// Dumps information for running/triggered timers, useful for debugging
  79. LLVM_DUMP_METHOD void dump() const;
  80. /// Returns the new timer for each new run of the pass.
  81. Timer &getPassTimer(StringRef PassID, bool IsPass);
  82. void startAnalysisTimer(StringRef PassID);
  83. void stopAnalysisTimer(StringRef PassID);
  84. void startPassTimer(StringRef PassID);
  85. void stopPassTimer(StringRef PassID);
  86. };
  87. } // namespace llvm
  88. #endif
  89. #ifdef __GNUC__
  90. #pragma GCC diagnostic pop
  91. #endif