DebugCounter.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /// Returns a reference to the singleton instance.
  59. static DebugCounter &instance();
  60. // Used by the command line option parser to push a new value it parsed.
  61. void push_back(const std::string &);
  62. // Register a counter with the specified name.
  63. //
  64. // FIXME: Currently, counter registration is required to happen before command
  65. // line option parsing. The main reason to register counters is to produce a
  66. // nice list of them on the command line, but i'm not sure this is worth it.
  67. static unsigned registerCounter(StringRef Name, StringRef Desc) {
  68. return instance().addCounter(std::string(Name), std::string(Desc));
  69. }
  70. inline static bool shouldExecute(unsigned CounterName) {
  71. if (!isCountingEnabled())
  72. return true;
  73. auto &Us = instance();
  74. auto Result = Us.Counters.find(CounterName);
  75. if (Result != Us.Counters.end()) {
  76. auto &CounterInfo = Result->second;
  77. ++CounterInfo.Count;
  78. // We only execute while the Skip is not smaller than Count,
  79. // and the StopAfter + Skip is larger than Count.
  80. // Negative counters always execute.
  81. if (CounterInfo.Skip < 0)
  82. return true;
  83. if (CounterInfo.Skip >= CounterInfo.Count)
  84. return false;
  85. if (CounterInfo.StopAfter < 0)
  86. return true;
  87. return CounterInfo.StopAfter + CounterInfo.Skip >= CounterInfo.Count;
  88. }
  89. // Didn't find the counter, should we warn?
  90. return true;
  91. }
  92. // Return true if a given counter had values set (either programatically or on
  93. // the command line). This will return true even if those values are
  94. // currently in a state where the counter will always execute.
  95. static bool isCounterSet(unsigned ID) {
  96. return instance().Counters[ID].IsSet;
  97. }
  98. // Return the Count for a counter. This only works for set counters.
  99. static int64_t getCounterValue(unsigned ID) {
  100. auto &Us = instance();
  101. auto Result = Us.Counters.find(ID);
  102. assert(Result != Us.Counters.end() && "Asking about a non-set counter");
  103. return Result->second.Count;
  104. }
  105. // Set a registered counter to a given Count value.
  106. static void setCounterValue(unsigned ID, int64_t Count) {
  107. auto &Us = instance();
  108. Us.Counters[ID].Count = Count;
  109. }
  110. // Dump or print the current counter set into llvm::dbgs().
  111. LLVM_DUMP_METHOD void dump() const;
  112. void print(raw_ostream &OS) const;
  113. // Get the counter ID for a given named counter, or return 0 if none is found.
  114. unsigned getCounterId(const std::string &Name) const {
  115. return RegisteredCounters.idFor(Name);
  116. }
  117. // Return the number of registered counters.
  118. unsigned int getNumCounters() const { return RegisteredCounters.size(); }
  119. // Return the name and description of the counter with the given ID.
  120. std::pair<std::string, std::string> getCounterInfo(unsigned ID) const {
  121. return std::make_pair(RegisteredCounters[ID], Counters.lookup(ID).Desc);
  122. }
  123. // Iterate through the registered counters
  124. typedef UniqueVector<std::string> CounterVector;
  125. CounterVector::const_iterator begin() const {
  126. return RegisteredCounters.begin();
  127. }
  128. CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
  129. // Force-enables counting all DebugCounters.
  130. //
  131. // Since DebugCounters are incompatible with threading (not only do they not
  132. // make sense, but we'll also see data races), this should only be used in
  133. // contexts where we're certain we won't spawn threads.
  134. static void enableAllCounters() { instance().Enabled = true; }
  135. static bool isCountingEnabled() {
  136. // Compile to nothing when debugging is off
  137. #ifdef NDEBUG
  138. return false;
  139. #else
  140. return instance().Enabled;
  141. #endif
  142. }
  143. private:
  144. unsigned addCounter(const std::string &Name, const std::string &Desc) {
  145. unsigned Result = RegisteredCounters.insert(Name);
  146. Counters[Result] = {};
  147. Counters[Result].Desc = Desc;
  148. return Result;
  149. }
  150. // Struct to store counter info.
  151. struct CounterInfo {
  152. int64_t Count = 0;
  153. int64_t Skip = 0;
  154. int64_t StopAfter = -1;
  155. bool IsSet = false;
  156. std::string Desc;
  157. };
  158. DenseMap<unsigned, CounterInfo> Counters;
  159. CounterVector RegisteredCounters;
  160. // Whether we should do DebugCounting at all. DebugCounters aren't
  161. // thread-safe, so this should always be false in multithreaded scenarios.
  162. bool Enabled = false;
  163. };
  164. #define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \
  165. static const unsigned VARNAME = \
  166. DebugCounter::registerCounter(COUNTERNAME, DESC)
  167. } // namespace llvm
  168. #endif
  169. #ifdef __GNUC__
  170. #pragma GCC diagnostic pop
  171. #endif