xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Support/DebugCounter.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- llvm/Support/DebugCounter.h - Debug counter support ------*- 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 /// This file provides an implementation of debug counters.  Debug
10 /// counters are a tool that let you narrow down a miscompilation to a specific
11 /// thing happening.
12 ///
13 /// To give a use case: Imagine you have a file, very large, and you
14 /// are trying to understand the minimal transformation that breaks it. Bugpoint
15 /// and bisection is often helpful here in narrowing it down to a specific pass,
16 /// but it's still a very large file, and a very complicated pass to try to
17 /// debug.  That is where debug counting steps in.  You can instrument the pass
18 /// with a debug counter before it does a certain thing, and depending on the
19 /// counts, it will either execute that thing or not.  The debug counter itself
20 /// consists of a list of chunks (inclusive numeric ranges). `shouldExecute`
21 /// returns true iff the list is empty or the current count is in one of the
22 /// chunks.
23 ///
24 /// Note that a counter set to a negative number will always execute. For a
25 /// concrete example, during predicateinfo creation, the renaming pass replaces
26 /// each use with a renamed use.
27 ////
28 /// If I use DEBUG_COUNTER to create a counter called "predicateinfo", and
29 /// variable name RenameCounter, and then instrument this renaming with a debug
30 /// counter, like so:
31 ///
32 /// if (!DebugCounter::shouldExecute(RenameCounter)
33 /// <continue or return or whatever not executing looks like>
34 ///
35 /// Now I can, from the command line, make it rename or not rename certain uses
36 /// by setting the chunk list.
37 /// So for example
38 /// bin/opt -debug-counter=predicateinfo=47
39 /// will skip renaming the first 47 uses, then rename one, then skip the rest.
40 //===----------------------------------------------------------------------===//
41 
42 #ifndef LLVM_SUPPORT_DEBUGCOUNTER_H
43 #define LLVM_SUPPORT_DEBUGCOUNTER_H
44 
45 #include "llvm/ADT/ArrayRef.h"
46 #include "llvm/ADT/DenseMap.h"
47 #include "llvm/ADT/StringRef.h"
48 #include "llvm/ADT/UniqueVector.h"
49 #include "llvm/Support/Debug.h"
50 #include <string>
51 
52 namespace llvm {
53 
54 class raw_ostream;
55 
56 class DebugCounter {
57 public:
58   struct Chunk {
59     int64_t Begin;
60     int64_t End;
61     void print(llvm::raw_ostream &OS);
containsChunk62     bool contains(int64_t Idx) { return Idx >= Begin && Idx <= End; }
63   };
64 
65   static void printChunks(raw_ostream &OS, ArrayRef<Chunk>);
66 
67   /// Return true on parsing error and print the error message on the
68   /// llvm::errs()
69   static bool parseChunks(StringRef Str, SmallVector<Chunk> &Res);
70 
71   /// Returns a reference to the singleton instance.
72   static DebugCounter &instance();
73 
74   // Used by the command line option parser to push a new value it parsed.
75   void push_back(const std::string &);
76 
77   // Register a counter with the specified name.
78   //
79   // FIXME: Currently, counter registration is required to happen before command
80   // line option parsing. The main reason to register counters is to produce a
81   // nice list of them on the command line, but i'm not sure this is worth it.
registerCounter(StringRef Name,StringRef Desc)82   static unsigned registerCounter(StringRef Name, StringRef Desc) {
83     return instance().addCounter(std::string(Name), std::string(Desc));
84   }
85   static bool shouldExecuteImpl(unsigned CounterName);
86 
shouldExecute(unsigned CounterName)87   inline static bool shouldExecute(unsigned CounterName) {
88     if (!isCountingEnabled())
89       return true;
90     return shouldExecuteImpl(CounterName);
91   }
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.
isCounterSet(unsigned ID)96   static bool isCounterSet(unsigned ID) {
97     return instance().Counters[ID].IsSet;
98   }
99 
100   struct CounterState {
101     int64_t Count;
102     uint64_t ChunkIdx;
103   };
104 
105   // Return the state of a counter. This only works for set counters.
getCounterState(unsigned ID)106   static CounterState getCounterState(unsigned ID) {
107     auto &Us = instance();
108     auto Result = Us.Counters.find(ID);
109     assert(Result != Us.Counters.end() && "Asking about a non-set counter");
110     return {Result->second.Count, Result->second.CurrChunkIdx};
111   }
112 
113   // Set a registered counter to a given state.
setCounterState(unsigned ID,CounterState State)114   static void setCounterState(unsigned ID, CounterState State) {
115     auto &Us = instance();
116     auto &Counter = Us.Counters[ID];
117     Counter.Count = State.Count;
118     Counter.CurrChunkIdx = State.ChunkIdx;
119   }
120 
121   // Dump or print the current counter set into llvm::dbgs().
122   LLVM_DUMP_METHOD void dump() const;
123 
124   void print(raw_ostream &OS) const;
125 
126   // Get the counter ID for a given named counter, or return 0 if none is found.
getCounterId(const std::string & Name)127   unsigned getCounterId(const std::string &Name) const {
128     return RegisteredCounters.idFor(Name);
129   }
130 
131   // Return the number of registered counters.
getNumCounters()132   unsigned int getNumCounters() const { return RegisteredCounters.size(); }
133 
134   // Return the name and description of the counter with the given ID.
getCounterInfo(unsigned ID)135   std::pair<std::string, std::string> getCounterInfo(unsigned ID) const {
136     return std::make_pair(RegisteredCounters[ID], Counters.lookup(ID).Desc);
137   }
138 
139   // Iterate through the registered counters
140   typedef UniqueVector<std::string> CounterVector;
begin()141   CounterVector::const_iterator begin() const {
142     return RegisteredCounters.begin();
143   }
end()144   CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
145 
146   // Force-enables counting all DebugCounters.
147   //
148   // Since DebugCounters are incompatible with threading (not only do they not
149   // make sense, but we'll also see data races), this should only be used in
150   // contexts where we're certain we won't spawn threads.
enableAllCounters()151   static void enableAllCounters() { instance().Enabled = true; }
152 
isCountingEnabled()153   static bool isCountingEnabled() {
154 // Compile to nothing when debugging is off
155 #ifdef NDEBUG
156     return false;
157 #else
158     return instance().Enabled || instance().ShouldPrintCounter;
159 #endif
160   }
161 
162 protected:
addCounter(const std::string & Name,const std::string & Desc)163   unsigned addCounter(const std::string &Name, const std::string &Desc) {
164     unsigned Result = RegisteredCounters.insert(Name);
165     Counters[Result] = {};
166     Counters[Result].Desc = Desc;
167     return Result;
168   }
169   // Struct to store counter info.
170   struct CounterInfo {
171     int64_t Count = 0;
172     uint64_t CurrChunkIdx = 0;
173     bool IsSet = false;
174     std::string Desc;
175     SmallVector<Chunk> Chunks;
176   };
177 
178   DenseMap<unsigned, CounterInfo> Counters;
179   CounterVector RegisteredCounters;
180 
181   // Whether we should do DebugCounting at all. DebugCounters aren't
182   // thread-safe, so this should always be false in multithreaded scenarios.
183   bool Enabled = false;
184 
185   bool ShouldPrintCounter = false;
186 
187   bool BreakOnLast = false;
188 };
189 
190 #define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)                              \
191   static const unsigned VARNAME =                                              \
192       DebugCounter::registerCounter(COUNTERNAME, DESC)
193 
194 } // namespace llvm
195 #endif
196