LLVM: lib/Support/DebugCounter.cpp Source File (original) (raw)
2
4
7
8using namespace llvm;
9
10namespace llvm {
11
15 else
17}
18
20 if (Chunks.empty()) {
21 OS << "empty";
22 } else {
23 bool IsFirst = true;
24 for (auto E : Chunks) {
25 if (!IsFirst)
26 OS << ':';
27 else
28 IsFirst = false;
29 E.print(OS);
30 }
31 }
32}
33
36
37 auto ConsumeInt = [&]() -> int64_t {
39 Remaining.take_until([](char c) { return c < '0' || c > '9'; });
40 int64_t Res;
41 if (Number.getAsInteger(10, Res)) {
42 errs() << "Failed to parse int at : " << Remaining << "\n";
43 return -1;
44 }
46 return Res;
47 };
48
49 while (1) {
50 int64_t Num = ConsumeInt();
51 if (Num == -1)
52 return true;
53 if (!Chunks.empty() && Num <= Chunks[Chunks.size() - 1].End) {
54 errs() << "Expected Chunks to be in increasing order " << Num
55 << " <= " << Chunks[Chunks.size() - 1].End << "\n";
56 return true;
57 }
60 int64_t Num2 = ConsumeInt();
61 if (Num2 == -1)
62 return true;
63 if (Num >= Num2) {
64 errs() << "Expected " << Num << " < " << Num2 << " in " << Num << "-"
65 << Num2 << "\n";
66 return true;
67 }
68
70 } else {
72 }
75 continue;
76 }
77 if (Remaining.empty())
78 break;
79 errs() << "Failed to parse at : " << Remaining;
80 return true;
81 }
82 return false;
83}
84
85}
86
87namespace {
88
89
90
91class DebugCounterList : public cl::list<std::string, DebugCounter> {
92private:
94
95public:
96 template <class... Mods>
97 explicit DebugCounterList(Mods &&... Ms) : Base(std::forward(Ms)...) {}
98
99private:
100 void printOptionInfo(size_t GlobalWidth) const override {
101
102
103
104
105
106
107
108 outs() << " -" << ArgStr;
109
110
111 Option::printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
113 for (const auto &Name : CounterInstance) {
114 const auto Info =
115 CounterInstance.getCounterInfo(CounterInstance.getCounterId(Name));
116 size_t NumSpaces = GlobalWidth - Info.first.size() - 8;
117 outs() << " =" << Info.first;
118 outs().indent(NumSpaces) << " - " << Info.second << '\n';
119 }
120 }
121};
122
123
124
125
126
128 DebugCounterList DebugCounterOption{
130 cl::desc("Comma separated list of debug counter skip and count"),
133 "print-debug-counter",
138 cl::desc("Print out debug counter info after all counters accumulated")};
140 "debug-counter-break-on-last",
145 cl::desc("Insert a break point on the last enabled count of a "
146 "chunks list")};
147
148 DebugCounterOwner() {
149
150
151 (void)dbgs();
152 }
153
154
155 ~DebugCounterOwner() {
156 if (ShouldPrintCounter)
158 }
159};
160
161}
162
164
166 static DebugCounterOwner O;
167 return O;
168}
169
170
171
173 if (Val.empty())
174 return;
175
176
178 if (CounterPair.second.empty()) {
179 errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
180 return;
181 }
182 StringRef CounterName = CounterPair.first;
184
185 if (parseChunks(CounterPair.second, Chunks)) {
186 return;
187 }
188
189 unsigned CounterID = getCounterId(std::string(CounterName));
190 if (!CounterID) {
191 errs() << "DebugCounter Error: " << CounterName
192 << " is not a registered counter\n";
193 return;
194 }
196
198 Counter.IsSet = true;
199 Counter.Chunks = std::move(Chunks);
200}
201
205 sort(CounterNames);
206
208 OS << "Counters and values:\n";
209 for (auto &CounterName : CounterNames) {
210 unsigned CounterID = getCounterId(std::string(CounterName));
212 << Us.Counters[CounterID].Count << ",";
214 OS << "}\n";
215 }
216}
217
220 auto Result = Us.Counters.find(CounterName);
221 if (Result != Us.Counters.end()) {
225
227 return true;
229 return false;
230
232 if (Us.BreakOnLast && CurrIdx == (CounterInfo.Chunks.size() - 1) &&
235 }
238
239
242 return true;
243 }
244 return Res;
245 }
246
247 return true;
248}
249
252}
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Analysis containing CSE Info
#define LLVM_BUILTIN_DEBUGTRAP
LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to an expression which causes the pro...
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file provides an implementation of debug counters.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
empty - Check if the array is empty.
static void printChunks(raw_ostream &OS, ArrayRef< Chunk >)
DenseMap< unsigned, CounterInfo > Counters
CounterVector RegisteredCounters
void push_back(const std::string &)
unsigned getCounterId(const std::string &Name) const
static bool shouldExecuteImpl(unsigned CounterName)
static void enableAllCounters()
static bool parseChunks(StringRef Str, SmallVector< Chunk > &Res)
Return true on parsing error and print the error message on the llvm::errs()
static DebugCounter & instance()
Returns a reference to the singleton instance.
void print(raw_ostream &OS) const
LLVM_DUMP_METHOD void dump() const
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.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
empty - Check if the string is empty.
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
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...
iterator end()
Return an iterator to the end of the vector.
iterator begin()
Return an iterator to the start of the vector.
This class implements an extremely fast bulk output stream that can only output to a stream.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
This is an optimization pass for GlobalISel generic memory operations.
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
void initDebugCounterOptions()
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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.
Implement std::hash so that hash_code can be used in STL containers.
void print(llvm::raw_ostream &OS)
SmallVector< Chunk > Chunks