PassTimingInfo.h 3.6 KB

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