Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/Support/DebugCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class DebugCounter {
std::string Desc;
SmallVector<Chunk> Chunks;
};
LLVM_ABI bool handleCounterIncrement(CounterInfo &Info);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need to be ABI? This doesn't seem to be used inline


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

bool ShouldPrintCounter = false;

bool ShouldPrintCounterQueries = false;

bool BreakOnLast = false;
};

Expand Down
56 changes: 36 additions & 20 deletions llvm/lib/Support/DebugCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ struct DebugCounterOwner : DebugCounter {
cl::location(this->ShouldPrintCounter),
cl::init(false),
cl::desc("Print out debug counter info after all counters accumulated")};
cl::opt<bool, true> PrintDebugCounterQueries{
"print-debug-counter-queries",
cl::Hidden,
cl::Optional,
cl::location(this->ShouldPrintCounterQueries),
cl::init(false),
cl::desc("Print out each query of an enabled debug counter")};
cl::opt<bool, true> BreakOnLastCount{
"debug-counter-break-on-last",
cl::Hidden,
Expand Down Expand Up @@ -221,31 +228,40 @@ void DebugCounter::print(raw_ostream &OS) const {
}
}

bool DebugCounter::handleCounterIncrement(CounterInfo &Info) {
int64_t CurrCount = Info.Count++;
uint64_t CurrIdx = Info.CurrChunkIdx;

if (Info.Chunks.empty())
return true;
if (CurrIdx >= Info.Chunks.size())
return false;

bool Res = Info.Chunks[CurrIdx].contains(CurrCount);
if (BreakOnLast && CurrIdx == (Info.Chunks.size() - 1) &&
CurrCount == Info.Chunks[CurrIdx].End) {
LLVM_BUILTIN_DEBUGTRAP;
}
if (CurrCount > Info.Chunks[CurrIdx].End) {
Info.CurrChunkIdx++;

/// Handle consecutive blocks.
if (Info.CurrChunkIdx < Info.Chunks.size() &&
CurrCount == Info.Chunks[Info.CurrChunkIdx].Begin)
return true;
}
return Res;
}

bool DebugCounter::shouldExecuteImpl(unsigned CounterName) {
auto &Us = instance();
auto Result = Us.Counters.find(CounterName);
if (Result != Us.Counters.end()) {
auto &CounterInfo = Result->second;
int64_t CurrCount = CounterInfo.Count++;
uint64_t CurrIdx = CounterInfo.CurrChunkIdx;

if (CounterInfo.Chunks.empty())
return true;
if (CurrIdx >= CounterInfo.Chunks.size())
return false;

bool Res = CounterInfo.Chunks[CurrIdx].contains(CurrCount);
if (Us.BreakOnLast && CurrIdx == (CounterInfo.Chunks.size() - 1) &&
CurrCount == CounterInfo.Chunks[CurrIdx].End) {
LLVM_BUILTIN_DEBUGTRAP;
}
if (CurrCount > CounterInfo.Chunks[CurrIdx].End) {
CounterInfo.CurrChunkIdx++;

/// Handle consecutive blocks.
if (CounterInfo.CurrChunkIdx < CounterInfo.Chunks.size() &&
CurrCount == CounterInfo.Chunks[CounterInfo.CurrChunkIdx].Begin)
return true;
bool Res = Us.handleCounterIncrement(CounterInfo);
if (Us.ShouldPrintCounterQueries && CounterInfo.IsSet) {
dbgs() << "DebugCounter " << Us.RegisteredCounters[CounterName] << "="
<< (CounterInfo.Count - 1) << (Res ? " execute" : " skip") << "\n";
}
return Res;
}
Expand Down
10 changes: 9 additions & 1 deletion llvm/test/Other/debugcounter-dce.ll
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
; REQUIRES: asserts
; RUN: opt -passes=dce -S -debug-counter=dce-transform=1-2 < %s | FileCheck %s
; RUN: opt -passes=dce -S -debug-counter=dce-transform=1-2 < %s | FileCheck %s --check-prefixes=CHECK,NO-PRINT
; RUN: opt -passes=dce -S -debug-counter=dce-transform=1-2 -print-debug-counter-queries < %s 2>&1 | FileCheck %s --check-prefixes=CHECK,PRINT
;; Test that, with debug counters on, we will skip the first DCE opportunity, perform next 2,
;; and ignore all the others left.

; NO-PRINT-NOT: DebugCounter
; PRINT: DebugCounter dce-transform=0 skip
; PRINT-NEXT: DebugCounter dce-transform=1 execute
; PRINT-NEXT: DebugCounter dce-transform=2 execute
; PRINT-NEXT: DebugCounter dce-transform=3 skip
; PRINT-NEXT: DebugCounter dce-transform=4 skip

; CHECK-LABEL: @test
; CHECK-NEXT: %add1 = add i32 1, 2
; CHECK-NEXT: %sub1 = sub i32 %add1, 1
Expand Down