Skip to content

Commit 9ace311

Browse files
authored
[DebugCounter] Add -print-debug-counter-queries option (#162827)
Add a `-print-debug-counter-queries` option which prints the current value of the counter and whether it is executed/skipped each time it is queried. This is useful when interleaving the output with the usual transform debug output, in order to find the correct counter value to use to hit a specific point in the transform.
1 parent 9f552ee commit 9ace311

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

llvm/include/llvm/Support/DebugCounter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class DebugCounter {
178178
std::string Desc;
179179
SmallVector<Chunk> Chunks;
180180
};
181+
bool handleCounterIncrement(CounterInfo &Info);
181182

182183
DenseMap<unsigned, CounterInfo> Counters;
183184
CounterVector RegisteredCounters;
@@ -188,6 +189,8 @@ class DebugCounter {
188189

189190
bool ShouldPrintCounter = false;
190191

192+
bool ShouldPrintCounterQueries = false;
193+
191194
bool BreakOnLast = false;
192195
};
193196

llvm/lib/Support/DebugCounter.cpp

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ struct DebugCounterOwner : DebugCounter {
136136
cl::location(this->ShouldPrintCounter),
137137
cl::init(false),
138138
cl::desc("Print out debug counter info after all counters accumulated")};
139+
cl::opt<bool, true> PrintDebugCounterQueries{
140+
"print-debug-counter-queries",
141+
cl::Hidden,
142+
cl::Optional,
143+
cl::location(this->ShouldPrintCounterQueries),
144+
cl::init(false),
145+
cl::desc("Print out each query of an enabled debug counter")};
139146
cl::opt<bool, true> BreakOnLastCount{
140147
"debug-counter-break-on-last",
141148
cl::Hidden,
@@ -221,31 +228,40 @@ void DebugCounter::print(raw_ostream &OS) const {
221228
}
222229
}
223230

231+
bool DebugCounter::handleCounterIncrement(CounterInfo &Info) {
232+
int64_t CurrCount = Info.Count++;
233+
uint64_t CurrIdx = Info.CurrChunkIdx;
234+
235+
if (Info.Chunks.empty())
236+
return true;
237+
if (CurrIdx >= Info.Chunks.size())
238+
return false;
239+
240+
bool Res = Info.Chunks[CurrIdx].contains(CurrCount);
241+
if (BreakOnLast && CurrIdx == (Info.Chunks.size() - 1) &&
242+
CurrCount == Info.Chunks[CurrIdx].End) {
243+
LLVM_BUILTIN_DEBUGTRAP;
244+
}
245+
if (CurrCount > Info.Chunks[CurrIdx].End) {
246+
Info.CurrChunkIdx++;
247+
248+
/// Handle consecutive blocks.
249+
if (Info.CurrChunkIdx < Info.Chunks.size() &&
250+
CurrCount == Info.Chunks[Info.CurrChunkIdx].Begin)
251+
return true;
252+
}
253+
return Res;
254+
}
255+
224256
bool DebugCounter::shouldExecuteImpl(unsigned CounterName) {
225257
auto &Us = instance();
226258
auto Result = Us.Counters.find(CounterName);
227259
if (Result != Us.Counters.end()) {
228260
auto &CounterInfo = Result->second;
229-
int64_t CurrCount = CounterInfo.Count++;
230-
uint64_t CurrIdx = CounterInfo.CurrChunkIdx;
231-
232-
if (CounterInfo.Chunks.empty())
233-
return true;
234-
if (CurrIdx >= CounterInfo.Chunks.size())
235-
return false;
236-
237-
bool Res = CounterInfo.Chunks[CurrIdx].contains(CurrCount);
238-
if (Us.BreakOnLast && CurrIdx == (CounterInfo.Chunks.size() - 1) &&
239-
CurrCount == CounterInfo.Chunks[CurrIdx].End) {
240-
LLVM_BUILTIN_DEBUGTRAP;
241-
}
242-
if (CurrCount > CounterInfo.Chunks[CurrIdx].End) {
243-
CounterInfo.CurrChunkIdx++;
244-
245-
/// Handle consecutive blocks.
246-
if (CounterInfo.CurrChunkIdx < CounterInfo.Chunks.size() &&
247-
CurrCount == CounterInfo.Chunks[CounterInfo.CurrChunkIdx].Begin)
248-
return true;
261+
bool Res = Us.handleCounterIncrement(CounterInfo);
262+
if (Us.ShouldPrintCounterQueries && CounterInfo.IsSet) {
263+
dbgs() << "DebugCounter " << Us.RegisteredCounters[CounterName] << "="
264+
<< (CounterInfo.Count - 1) << (Res ? " execute" : " skip") << "\n";
249265
}
250266
return Res;
251267
}

llvm/test/Other/debugcounter-dce.ll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
; REQUIRES: asserts
2-
; RUN: opt -passes=dce -S -debug-counter=dce-transform=1-2 < %s | FileCheck %s
2+
; RUN: opt -passes=dce -S -debug-counter=dce-transform=1-2 < %s | FileCheck %s --check-prefixes=CHECK,NO-PRINT
3+
; RUN: opt -passes=dce -S -debug-counter=dce-transform=1-2 -print-debug-counter-queries < %s 2>&1 | FileCheck %s --check-prefixes=CHECK,PRINT
34
;; Test that, with debug counters on, we will skip the first DCE opportunity, perform next 2,
45
;; and ignore all the others left.
56

7+
; NO-PRINT-NOT: DebugCounter
8+
; PRINT: DebugCounter dce-transform=0 skip
9+
; PRINT-NEXT: DebugCounter dce-transform=1 execute
10+
; PRINT-NEXT: DebugCounter dce-transform=2 execute
11+
; PRINT-NEXT: DebugCounter dce-transform=3 skip
12+
; PRINT-NEXT: DebugCounter dce-transform=4 skip
13+
614
; CHECK-LABEL: @test
715
; CHECK-NEXT: %add1 = add i32 1, 2
816
; CHECK-NEXT: %sub1 = sub i32 %add1, 1

0 commit comments

Comments
 (0)