[llvm] r345085 - Print out DebugCounter info with -print-debug-counter

Zhizhou Yang via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 23 14:51:56 PDT 2018


Author: zhizhouy
Date: Tue Oct 23 14:51:56 2018
New Revision: 345085

URL: http://llvm.org/viewvc/llvm-project?rev=345085&view=rev
Log:
Print out DebugCounter info with -print-debug-counter

Summary:
This patch will print out {Counter, Skip, StopAfter} info of all passes which have DebugCounter set at destruction.

It can be used to monitor how many times does certain transformation happen in a pass, and also help check if -debug-counter option is set correctly.

Please refer to this [[ http://lists.llvm.org/pipermail/llvm-dev/2018-July/124722.html  | thread ]] for motivation.

Reviewers: george.burgess.iv, davide, greened

Reviewed By: greened

Subscribers: kristina, llozano, mgorny, llvm-commits, mgrang

Differential Revision: https://reviews.llvm.org/D50031

Added:
    llvm/trunk/test/Other/print-debug-counter.ll
Modified:
    llvm/trunk/include/llvm/Support/DebugCounter.h
    llvm/trunk/lib/Support/DebugCounter.cpp

Modified: llvm/trunk/include/llvm/Support/DebugCounter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/DebugCounter.h?rev=345085&r1=345084&r2=345085&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/DebugCounter.h (original)
+++ llvm/trunk/include/llvm/Support/DebugCounter.h Tue Oct 23 14:51:56 2018
@@ -55,6 +55,8 @@ namespace llvm {
 
 class DebugCounter {
 public:
+  ~DebugCounter();
+
   /// Returns a reference to the singleton instance.
   static DebugCounter &instance();
 

Modified: llvm/trunk/lib/Support/DebugCounter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/DebugCounter.cpp?rev=345085&r1=345084&r2=345085&view=diff
==============================================================================
--- llvm/trunk/lib/Support/DebugCounter.cpp (original)
+++ llvm/trunk/lib/Support/DebugCounter.cpp Tue Oct 23 14:51:56 2018
@@ -49,8 +49,18 @@ static DebugCounterList DebugCounterOpti
     cl::desc("Comma separated list of debug counter skip and count"),
     cl::CommaSeparated, cl::ZeroOrMore, cl::location(DebugCounter::instance()));
 
+static cl::opt<bool> PrintDebugCounter(
+    "print-debug-counter", cl::Hidden, cl::init(false), cl::Optional,
+    cl::desc("Print out debug counter info after all counters accumulated"));
+
 static ManagedStatic<DebugCounter> DC;
 
+// Print information when destroyed, iff command line option is specified.
+DebugCounter::~DebugCounter() {
+  if (isCountingEnabled() && PrintDebugCounter)
+    print(dbgs());
+}
+
 DebugCounter &DebugCounter::instance() { return *DC; }
 
 // This is called by the command line parser when it sees a value for the
@@ -107,11 +117,18 @@ void DebugCounter::push_back(const std::
 }
 
 void DebugCounter::print(raw_ostream &OS) const {
+  SmallVector<StringRef, 16> CounterNames(RegisteredCounters.begin(),
+                                          RegisteredCounters.end());
+  sort(CounterNames.begin(), CounterNames.end());
+
+  auto &Us = instance();
   OS << "Counters and values:\n";
-  for (const auto &KV : Counters)
-    OS << left_justify(RegisteredCounters[KV.first], 32) << ": {"
-       << KV.second.Count << "," << KV.second.Skip << ","
-       << KV.second.StopAfter << "}\n";
+  for (auto &CounterName : CounterNames) {
+    unsigned CounterID = getCounterId(CounterName);
+    OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
+       << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
+       << "," << Us.Counters[CounterID].StopAfter << "}\n";
+  }
 }
 
 LLVM_DUMP_METHOD void DebugCounter::dump() const {

Added: llvm/trunk/test/Other/print-debug-counter.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/print-debug-counter.ll?rev=345085&view=auto
==============================================================================
--- llvm/trunk/test/Other/print-debug-counter.ll (added)
+++ llvm/trunk/test/Other/print-debug-counter.ll Tue Oct 23 14:51:56 2018
@@ -0,0 +1,30 @@
+; RUN: opt -S -debug-counter=early-cse-skip=1,early-cse-count=1 -early-cse \
+; RUN:        -debug-counter=newgvn-vn-skip=1,newgvn-vn-count=2 -newgvn \
+; RUN:        -instcombine -print-debug-counter < %s 2>&1 | FileCheck %s
+;; Test debug counter prints correct info in right order.
+; CHECK-LABEL: Counters and values:
+; CHECK:       early-cse
+; CHECK-SAME:  {4,1,1}
+; CHECK:       instcombine-visit
+; CHECK-SAME:  {12,0,-1}
+; CHECK:       newgvn-vn
+; CHECK-SAME:  {9,1,2}
+define i32 @f1(i32 %a, i32 %b) {
+bb:
+  %add1 = add i32 %a, %b
+  %add2 = add i32 %a, %b
+  %add3 = add i32 %a, %b
+  %add4 = add i32 %a, %b
+  %ret1 = add i32 %add1, %add2
+  %ret2 = add i32 %add3, %add4
+  %ret = add i32 %ret1, %ret2
+  ret i32 %ret
+}
+
+define i32 @f2(i32 %a, i32 %b) {
+bb:
+  %add1 = add i32 %a, %b
+  %add2 = add i32 %a, %b
+  %ret = add i32 %add1, %add2
+  ret i32 %ret
+}




More information about the llvm-commits mailing list