DebugCounter.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/DebugCounter.h - Debug counter support ------*- 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. /// This file provides an implementation of debug counters. Debug
  15. /// counters are a tool that let you narrow down a miscompilation to a specific
  16. /// thing happening.
  17. ///
  18. /// To give a use case: Imagine you have a file, very large, and you
  19. /// are trying to understand the minimal transformation that breaks it. Bugpoint
  20. /// and bisection is often helpful here in narrowing it down to a specific pass,
  21. /// but it's still a very large file, and a very complicated pass to try to
  22. /// debug. That is where debug counting steps in. You can instrument the pass
  23. /// with a debug counter before it does a certain thing, and depending on the
  24. /// counts, it will either execute that thing or not. The debug counter itself
  25. /// consists of a skip and a count. Skip is the number of times shouldExecute
  26. /// needs to be called before it returns true. Count is the number of times to
  27. /// return true once Skip is 0. So a skip=47, count=2 ,would skip the first 47
  28. /// executions by returning false from shouldExecute, then execute twice, and
  29. /// then return false again.
  30. /// Note that a counter set to a negative number will always execute.
  31. /// For a concrete example, during predicateinfo creation, the renaming pass
  32. /// replaces each use with a renamed use.
  33. ////
  34. /// If I use DEBUG_COUNTER to create a counter called "predicateinfo", and
  35. /// variable name RenameCounter, and then instrument this renaming with a debug
  36. /// counter, like so:
  37. ///
  38. /// if (!DebugCounter::shouldExecute(RenameCounter)
  39. /// <continue or return or whatever not executing looks like>
  40. ///
  41. /// Now I can, from the command line, make it rename or not rename certain uses
  42. /// by setting the skip and count.
  43. /// So for example
  44. /// bin/opt -debug-counter=predicateinfo-skip=47,predicateinfo-count=1
  45. /// will skip renaming the first 47 uses, then rename one, then skip the rest.
  46. //===----------------------------------------------------------------------===//
  47. #ifndef LLVM_SUPPORT_DEBUGCOUNTER_H
  48. #define LLVM_SUPPORT_DEBUGCOUNTER_H
  49. #include "llvm/ADT/DenseMap.h"
  50. #include "llvm/ADT/StringRef.h"
  51. #include "llvm/ADT/UniqueVector.h"
  52. #include "llvm/Support/Debug.h"
  53. #include <string>
  54. namespace llvm {
  55. class raw_ostream;
  56. class DebugCounter {
  57. public:
  58. ~DebugCounter();
  59. /// Returns a reference to the singleton instance.
  60. static DebugCounter &instance();
  61. // Used by the command line option parser to push a new value it parsed.
  62. void push_back(const std::string &);
  63. // Register a counter with the specified name.
  64. //
  65. // FIXME: Currently, counter registration is required to happen before command
  66. // line option parsing. The main reason to register counters is to produce a
  67. // nice list of them on the command line, but i'm not sure this is worth it.
  68. static unsigned registerCounter(StringRef Name, StringRef Desc) {
  69. return instance().addCounter(std::string(Name), std::string(Desc));
  70. }
  71. inline static bool shouldExecute(unsigned CounterName) {
  72. if (!isCountingEnabled())
  73. return true;
  74. auto &Us = instance();
  75. auto Result = Us.Counters.find(CounterName);
  76. if (Result != Us.Counters.end()) {
  77. auto &CounterInfo = Result->second;
  78. ++CounterInfo.Count;
  79. // We only execute while the Skip is not smaller than Count,
  80. // and the StopAfter + Skip is larger than Count.
  81. // Negative counters always execute.
  82. if (CounterInfo.Skip < 0)
  83. return true;
  84. if (CounterInfo.Skip >= CounterInfo.Count)
  85. return false;
  86. if (CounterInfo.StopAfter < 0)
  87. return true;
  88. return CounterInfo.StopAfter + CounterInfo.Skip >= CounterInfo.Count;
  89. }
  90. // Didn't find the counter, should we warn?
  91. return true;
  92. }
  93. // Return true if a given counter had values set (either programatically or on
  94. // the command line). This will return true even if those values are
  95. // currently in a state where the counter will always execute.
  96. static bool isCounterSet(unsigned ID) {
  97. return instance().Counters[ID].IsSet;
  98. }
  99. // Return the Count for a counter. This only works for set counters.
  100. static int64_t getCounterValue(unsigned ID) {
  101. auto &Us = instance();
  102. auto Result = Us.Counters.find(ID);
  103. assert(Result != Us.Counters.end() && "Asking about a non-set counter");
  104. return Result->second.Count;
  105. }
  106. // Set a registered counter to a given Count value.
  107. static void setCounterValue(unsigned ID, int64_t Count) {
  108. auto &Us = instance();
  109. Us.Counters[ID].Count = Count;
  110. }
  111. // Dump or print the current counter set into llvm::dbgs().
  112. LLVM_DUMP_METHOD void dump() const;
  113. void print(raw_ostream &OS) const;
  114. // Get the counter ID for a given named counter, or return 0 if none is found.
  115. unsigned getCounterId(const std::string &Name) const {
  116. return RegisteredCounters.idFor(Name);
  117. }
  118. // Return the number of registered counters.
  119. unsigned int getNumCounters() const { return RegisteredCounters.size(); }
  120. // Return the name and description of the counter with the given ID.
  121. std::pair<std::string, std::string> getCounterInfo(unsigned ID) const {
  122. return std::make_pair(RegisteredCounters[ID], Counters.lookup(ID).Desc);
  123. }
  124. // Iterate through the registered counters
  125. typedef UniqueVector<std::string> CounterVector;
  126. CounterVector::const_iterator begin() const {
  127. return RegisteredCounters.begin();
  128. }
  129. CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
  130. // Force-enables counting all DebugCounters.
  131. //
  132. // Since DebugCounters are incompatible with threading (not only do they not
  133. // make sense, but we'll also see data races), this should only be used in
  134. // contexts where we're certain we won't spawn threads.
  135. static void enableAllCounters() { instance().Enabled = true; }
  136. private:
  137. static bool isCountingEnabled() {
  138. // Compile to nothing when debugging is off
  139. #ifdef NDEBUG
  140. return false;
  141. #else
  142. return instance().Enabled;
  143. #endif
  144. }
  145. unsigned addCounter(const std::string &Name, const std::string &Desc) {
  146. unsigned Result = RegisteredCounters.insert(Name);
  147. Counters[Result] = {};
  148. Counters[Result].Desc = Desc;
  149. return Result;
  150. }
  151. // Struct to store counter info.
  152. struct CounterInfo {
  153. int64_t Count = 0;
  154. int64_t Skip = 0;
  155. int64_t StopAfter = -1;
  156. bool IsSet = false;
  157. std::string Desc;
  158. };
  159. DenseMap<unsigned, CounterInfo> Counters;
  160. CounterVector RegisteredCounters;
  161. // Whether we should do DebugCounting at all. DebugCounters aren't
  162. // thread-safe, so this should always be false in multithreaded scenarios.
  163. bool Enabled = false;
  164. };
  165. #define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \
  166. static const unsigned VARNAME = \
  167. DebugCounter::registerCounter(COUNTERNAME, DESC)
  168. } // namespace llvm
  169. #endif
  170. #ifdef __GNUC__
  171. #pragma GCC diagnostic pop
  172. #endif