LLVM 22.0.0git
DebugCounter.h
Go to the documentation of this file.
1//===- llvm/Support/DebugCounter.h - Debug counter support ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file provides an implementation of debug counters. Debug
10/// counters are a tool that let you narrow down a miscompilation to a specific
11/// thing happening.
12///
13/// To give a use case: Imagine you have a file, very large, and you
14/// are trying to understand the minimal transformation that breaks it. Bugpoint
15/// and bisection is often helpful here in narrowing it down to a specific pass,
16/// but it's still a very large file, and a very complicated pass to try to
17/// debug. That is where debug counting steps in. You can instrument the pass
18/// with a debug counter before it does a certain thing, and depending on the
19/// counts, it will either execute that thing or not. The debug counter itself
20/// consists of a list of chunks (inclusive numeric intervals). `shouldExecute`
21/// returns true iff the list is empty or the current count is in one of the
22/// chunks.
23///
24/// Note that a counter set to a negative number will always execute. For a
25/// concrete example, during predicateinfo creation, the renaming pass replaces
26/// each use with a renamed use.
27////
28/// If I use DEBUG_COUNTER to create a counter called "predicateinfo", and
29/// variable name RenameCounter, and then instrument this renaming with a debug
30/// counter, like so:
31///
32/// if (!DebugCounter::shouldExecute(RenameCounter)
33/// <continue or return or whatever not executing looks like>
34///
35/// Now I can, from the command line, make it rename or not rename certain uses
36/// by setting the chunk list.
37/// So for example
38/// bin/opt -debug-counter=predicateinfo=47
39/// will skip renaming the first 47 uses, then rename one, then skip the rest.
40//===----------------------------------------------------------------------===//
41
42#ifndef LLVM_SUPPORT_DEBUGCOUNTER_H
43#define LLVM_SUPPORT_DEBUGCOUNTER_H
44
45#include "llvm/ADT/ArrayRef.h"
46#include "llvm/ADT/DenseMap.h"
47#include "llvm/ADT/MapVector.h"
48#include "llvm/ADT/StringRef.h"
50#include "llvm/Support/Debug.h"
52#include <string>
53
54namespace llvm {
55
56class raw_ostream;
57
59public:
60 /// Struct to store counter info.
62 friend class DebugCounter;
63
64 /// Whether counting should be enabled, either due to -debug-counter or
65 /// -print-debug-counter.
66 bool Active = false;
67 /// Whether chunks for the counter are set (differs from Active in that
68 /// -print-debug-counter uses Active=true, IsSet=false).
69 bool IsSet = false;
70
71 int64_t Count = 0;
72 uint64_t CurrChunkIdx = 0;
73 StringRef Name;
74 StringRef Desc;
76
77 public:
78 CounterInfo(StringRef Name, StringRef Desc) : Name(Name), Desc(Desc) {
80 }
81 };
82
83 LLVM_ABI static void
85
86 /// Return true on parsing error and print the error message on the
87 /// llvm::errs()
88 LLVM_ABI static bool
90
91 /// Returns a reference to the singleton instance.
93
94 // Used by the command line option parser to push a new value it parsed.
95 LLVM_ABI void push_back(const std::string &);
96
97 // Register a counter with the specified counter information.
98 //
99 // FIXME: Currently, counter registration is required to happen before command
100 // line option parsing. The main reason to register counters is to produce a
101 // nice list of them on the command line, but i'm not sure this is worth it.
105 LLVM_ABI static bool shouldExecuteImpl(CounterInfo &Counter);
106
107 inline static bool shouldExecute(CounterInfo &Counter) {
108 if (!Counter.Active)
109 return true;
110 return shouldExecuteImpl(Counter);
111 }
112
113 // Return true if a given counter had values set (either programatically or on
114 // the command line). This will return true even if those values are
115 // currently in a state where the counter will always execute.
116 static bool isCounterSet(CounterInfo &Info) { return Info.IsSet; }
117
119 int64_t Count;
121 };
122
123 // Return the state of a counter. This only works for set counters.
125 return {Info.Count, Info.CurrChunkIdx};
126 }
127
128 // Set a registered counter to a given state.
130 Info.Count = State.Count;
131 Info.CurrChunkIdx = State.ChunkIdx;
132 }
133
134#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
135 // Dump or print the current counter set into llvm::dbgs().
136 LLVM_DUMP_METHOD void dump() const;
137#endif
138
139 LLVM_ABI void print(raw_ostream &OS) const;
140
141 // Get the counter info for a given named counter,
142 // or return null if none is found.
144 return Counters.lookup(Name);
145 }
146
147 // Return the number of registered counters.
148 unsigned int getNumCounters() const { return Counters.size(); }
149
150 // Return the name and description of the counter with the given info.
151 std::pair<StringRef, StringRef> getCounterDesc(CounterInfo *Info) const {
152 return {Info->Name, Info->Desc};
153 }
154
155 // Iterate through the registered counters
162
164 for (auto &[_, Counter] : Counters)
165 Counter->Active = true;
166 }
167
168protected:
170 bool handleCounterIncrement(CounterInfo &Info);
171
173
174 bool ShouldPrintCounter = false;
175
177
178 bool BreakOnLast = false;
179};
180
181#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \
182 static DebugCounter::CounterInfo VARNAME(COUNTERNAME, DESC)
183
184} // namespace llvm
185#endif
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
This file defines the DenseMap class.
#define _
This file implements a map that provides insertion order iteration.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Struct to store counter info.
CounterInfo(StringRef Name, StringRef Desc)
CounterInfo * getCounterInfo(StringRef Name) const
static LLVM_ABI void printChunks(raw_ostream &OS, ArrayRef< IntegerInclusiveInterval > Intervals)
static CounterState getCounterState(CounterInfo &Info)
static void setCounterState(CounterInfo &Info, CounterState State)
MapVector< StringRef, CounterInfo * >::const_iterator end() const
void addCounter(CounterInfo *Info)
static void registerCounter(CounterInfo *Info)
bool handleCounterIncrement(CounterInfo &Info)
std::pair< StringRef, StringRef > getCounterDesc(CounterInfo *Info) const
MapVector< StringRef, CounterInfo * >::const_iterator begin() const
LLVM_ABI void push_back(const std::string &)
static LLVM_ABI bool parseChunks(StringRef Str, IntegerInclusiveIntervalUtils::IntervalList &Res)
Return true on parsing error and print the error message on the llvm::errs()
static bool shouldExecute(CounterInfo &Counter)
static bool isCounterSet(CounterInfo &Info)
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
unsigned int getNumCounters() const
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
typename VectorType::const_iterator const_iterator
Definition MapVector.h:43
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
SmallVector< IntegerInclusiveInterval, 8 > IntervalList
A list of integer intervals.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26