LLVM 22.0.0git
DebugCounter.cpp
Go to the documentation of this file.
2
3#include "DebugOptions.h"
4
8
9using namespace llvm;
10
11namespace llvm {
12
14 if (Begin == End)
15 OS << Begin;
16 else
17 OS << Begin << "-" << End;
18}
19
21 if (Chunks.empty()) {
22 OS << "empty";
23 } else {
24 bool IsFirst = true;
25 for (auto E : Chunks) {
26 if (!IsFirst)
27 OS << ':';
28 else
29 IsFirst = false;
30 E.print(OS);
31 }
32 }
33}
34
36 StringRef Remaining = Str;
37
38 auto ConsumeInt = [&]() -> int64_t {
40 Remaining.take_until([](char c) { return c < '0' || c > '9'; });
41 int64_t Res;
42 if (Number.getAsInteger(10, Res)) {
43 errs() << "Failed to parse int at : " << Remaining << "\n";
44 return -1;
45 }
46 Remaining = Remaining.drop_front(Number.size());
47 return Res;
48 };
49
50 while (1) {
51 int64_t Num = ConsumeInt();
52 if (Num == -1)
53 return true;
54 if (!Chunks.empty() && Num <= Chunks[Chunks.size() - 1].End) {
55 errs() << "Expected Chunks to be in increasing order " << Num
56 << " <= " << Chunks[Chunks.size() - 1].End << "\n";
57 return true;
58 }
59 if (Remaining.starts_with("-")) {
60 Remaining = Remaining.drop_front();
61 int64_t Num2 = ConsumeInt();
62 if (Num2 == -1)
63 return true;
64 if (Num >= Num2) {
65 errs() << "Expected " << Num << " < " << Num2 << " in " << Num << "-"
66 << Num2 << "\n";
67 return true;
68 }
69
70 Chunks.push_back({Num, Num2});
71 } else {
72 Chunks.push_back({Num, Num});
73 }
74 if (Remaining.starts_with(":")) {
75 Remaining = Remaining.drop_front();
76 continue;
77 }
78 if (Remaining.empty())
79 break;
80 errs() << "Failed to parse at : " << Remaining;
81 return true;
82 }
83 return false;
84}
85
86} // namespace llvm
87
88namespace {
89// This class overrides the default list implementation of printing so we
90// can pretty print the list of debug counter options. This type of
91// dynamic option is pretty rare (basically this and pass lists).
92class DebugCounterList : public cl::list<std::string, DebugCounter> {
93private:
95
96public:
97 template <class... Mods>
98 explicit DebugCounterList(Mods &&... Ms) : Base(std::forward<Mods>(Ms)...) {}
99
100private:
101 void printOptionInfo(size_t GlobalWidth) const override {
102 // This is a variant of from generic_parser_base::printOptionInfo. Sadly,
103 // it's not easy to make it more usable. We could get it to print these as
104 // options if we were a cl::opt and registered them, but lists don't have
105 // options, nor does the parser for std::string. The other mechanisms for
106 // options are global and would pollute the global namespace with our
107 // counters. Rather than go that route, we have just overridden the
108 // printing, which only a few things call anyway.
109 outs() << " -" << ArgStr;
110 // All of the other options in CommandLine.cpp use ArgStr.size() + 6 for
111 // width, so we do the same.
112 Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
113 const auto &CounterInstance = DebugCounter::instance();
114 for (const auto &Entry : CounterInstance) {
115 const auto &[Name, Desc] = CounterInstance.getCounterDesc(Entry.second);
116 size_t NumSpaces = GlobalWidth - Name.size() - 8;
117 outs() << " =" << Name;
118 outs().indent(NumSpaces) << " - " << Desc << '\n';
119 }
120 }
121};
122
123// All global objects associated to the DebugCounter, including the DebugCounter
124// itself, are owned by a single global instance of the DebugCounterOwner
125// struct. This makes it easier to control the order in which constructors and
126// destructors are run.
127struct DebugCounterOwner : DebugCounter {
128 DebugCounterList DebugCounterOption{
129 "debug-counter", cl::Hidden,
130 cl::desc("Comma separated list of debug counter skip and count"),
132 cl::opt<bool, true> PrintDebugCounter{
133 "print-debug-counter",
136 cl::location(this->ShouldPrintCounter),
137 cl::init(false),
138 cl::desc("Print out debug counter info after all counters accumulated"),
139 cl::callback([&](const bool &Value) {
140 if (Value)
141 activateAllCounters();
142 })};
143 cl::opt<bool, true> PrintDebugCounterQueries{
144 "print-debug-counter-queries",
147 cl::location(this->ShouldPrintCounterQueries),
148 cl::init(false),
149 cl::desc("Print out each query of an enabled debug counter")};
150 cl::opt<bool, true> BreakOnLastCount{
151 "debug-counter-break-on-last",
154 cl::location(this->BreakOnLast),
155 cl::init(false),
156 cl::desc("Insert a break point on the last enabled count of a "
157 "chunks list")};
158
159 DebugCounterOwner() {
160 // Our destructor uses the debug stream. By referencing it here, we
161 // ensure that its destructor runs after our destructor.
162 (void)dbgs();
163 }
164
165 // Print information when destroyed, iff command line option is specified.
166 ~DebugCounterOwner() {
167 if (ShouldPrintCounter)
168 print(dbgs());
169 }
170};
171
172} // anonymous namespace
173
174// Use ManagedStatic instead of function-local static variable to ensure
175// the destructor (which accesses counters and streams) runs during
176// llvm_shutdown() rather than at some unspecified point.
178
180
182
183// This is called by the command line parser when it sees a value for the
184// debug-counter option defined above.
185void DebugCounter::push_back(const std::string &Val) {
186 if (Val.empty())
187 return;
188
189 // The strings should come in as counter=chunk_list
190 auto CounterPair = StringRef(Val).split('=');
191 if (CounterPair.second.empty()) {
192 errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
193 return;
194 }
195 StringRef CounterName = CounterPair.first;
196 SmallVector<Chunk> Chunks;
197
198 if (parseChunks(CounterPair.second, Chunks)) {
199 return;
200 }
201
202 CounterInfo *Counter = getCounterInfo(CounterName);
203 if (!Counter) {
204 errs() << "DebugCounter Error: " << CounterName
205 << " is not a registered counter\n";
206 return;
207 }
208
209 Counter->Active = Counter->IsSet = true;
210 Counter->Chunks = std::move(Chunks);
211}
212
214 SmallVector<StringRef, 16> CounterNames(Counters.keys());
215 sort(CounterNames);
216
217 OS << "Counters and values:\n";
218 for (StringRef CounterName : CounterNames) {
219 const CounterInfo *C = getCounterInfo(CounterName);
220 OS << left_justify(C->Name, 32) << ": {" << C->Count << ",";
221 printChunks(OS, C->Chunks);
222 OS << "}\n";
223 }
224}
225
227 int64_t CurrCount = Info.Count++;
228 uint64_t CurrIdx = Info.CurrChunkIdx;
229
230 if (Info.Chunks.empty())
231 return true;
232 if (CurrIdx >= Info.Chunks.size())
233 return false;
234
235 bool Res = Info.Chunks[CurrIdx].contains(CurrCount);
236 if (BreakOnLast && CurrIdx == (Info.Chunks.size() - 1) &&
237 CurrCount == Info.Chunks[CurrIdx].End) {
239 }
240 if (CurrCount > Info.Chunks[CurrIdx].End) {
241 Info.CurrChunkIdx++;
242
243 /// Handle consecutive blocks.
244 if (Info.CurrChunkIdx < Info.Chunks.size() &&
245 CurrCount == Info.Chunks[Info.CurrChunkIdx].Begin)
246 return true;
247 }
248 return Res;
249}
250
252 auto &Us = instance();
253 bool Res = Us.handleCounterIncrement(Counter);
254 if (Us.ShouldPrintCounterQueries && Counter.IsSet) {
255 dbgs() << "DebugCounter " << Counter.Name << "=" << (Counter.Count - 1)
256 << (Res ? " execute" : " skip") << "\n";
257 }
258 return Res;
259}
260
261#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
263 print(dbgs());
264}
265#endif
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_BUILTIN_DEBUGTRAP
LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to an expression which causes the pro...
Definition Compiler.h:493
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
static ManagedStatic< DebugCounterOwner > Owner
This file provides an implementation of debug counters.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
Struct to store counter info.
CounterInfo * getCounterInfo(StringRef Name) const
static LLVM_ABI void printChunks(raw_ostream &OS, ArrayRef< Chunk >)
bool handleCounterIncrement(CounterInfo &Info)
LLVM_ABI void push_back(const std::string &)
static LLVM_ABI bool parseChunks(StringRef Str, SmallVector< Chunk > &Res)
Return true on parsing error and print the error message on the llvm::errs()
static LLVM_ABI bool shouldExecuteImpl(CounterInfo &Counter)
static LLVM_ABI DebugCounter & instance()
Returns a reference to the singleton instance.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_DUMP_METHOD void dump() const
MapVector< StringRef, CounterInfo * > Counters
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:702
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:261
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:611
StringRef take_until(function_ref< bool(char)> F) const
Return the longest prefix of 'this' such that no character in the prefix satisfies the given predicat...
Definition StringRef.h:605
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
@ Entry
Definition COFF.h:862
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
cb< typename detail::callback_traits< F >::result_type, typename detail::callback_traits< F >::arg_type > callback(F CB)
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
void initDebugCounterOptions()
Op::Description Desc
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
FormattedString left_justify(StringRef Str, unsigned Width)
left_justify - append spaces after string so total output is Width characters.
Definition Format.h:150
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
LLVM_ABI void print(llvm::raw_ostream &OS)