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 ranges). `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"
51#include <string>
52
53namespace llvm {
54
55class raw_ostream;
56
58public:
59 struct Chunk {
60 int64_t Begin;
61 int64_t End;
63 bool contains(int64_t Idx) const { return Idx >= Begin && Idx <= End; }
64 };
65
66 /// Struct to store counter info.
68 friend class DebugCounter;
69
70 /// Whether counting should be enabled, either due to -debug-counter or
71 /// -print-debug-counter.
72 bool Active = false;
73 /// Whether chunks for the counter are set (differs from Active in that
74 /// -print-debug-counter uses Active=true, IsSet=false).
75 bool IsSet = false;
76
77 int64_t Count = 0;
78 uint64_t CurrChunkIdx = 0;
79 StringRef Name;
80 StringRef Desc;
81 SmallVector<Chunk> Chunks;
82
83 public:
84 CounterInfo(StringRef Name, StringRef Desc) : Name(Name), Desc(Desc) {
86 }
87 };
88
90
91 /// Return true on parsing error and print the error message on the
92 /// llvm::errs()
93 LLVM_ABI static bool parseChunks(StringRef Str, SmallVector<Chunk> &Res);
94
95 /// Returns a reference to the singleton instance.
97
98 // Used by the command line option parser to push a new value it parsed.
99 LLVM_ABI void push_back(const std::string &);
100
101 // Register a counter with the specified counter information.
102 //
103 // FIXME: Currently, counter registration is required to happen before command
104 // line option parsing. The main reason to register counters is to produce a
105 // nice list of them on the command line, but i'm not sure this is worth it.
109 LLVM_ABI static bool shouldExecuteImpl(CounterInfo &Counter);
110
111 inline static bool shouldExecute(CounterInfo &Counter) {
112 if (!Counter.Active)
113 return true;
114 return shouldExecuteImpl(Counter);
115 }
116
117 // Return true if a given counter had values set (either programatically or on
118 // the command line). This will return true even if those values are
119 // currently in a state where the counter will always execute.
120 static bool isCounterSet(CounterInfo &Info) { return Info.IsSet; }
121
123 int64_t Count;
125 };
126
127 // Return the state of a counter. This only works for set counters.
129 return {Info.Count, Info.CurrChunkIdx};
130 }
131
132 // Set a registered counter to a given state.
134 Info.Count = State.Count;
135 Info.CurrChunkIdx = State.ChunkIdx;
136 }
137
138#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
139 // Dump or print the current counter set into llvm::dbgs().
140 LLVM_DUMP_METHOD void dump() const;
141#endif
142
143 LLVM_ABI void print(raw_ostream &OS) const;
144
145 // Get the counter info for a given named counter,
146 // or return null if none is found.
148 return Counters.lookup(Name);
149 }
150
151 // Return the number of registered counters.
152 unsigned int getNumCounters() const { return Counters.size(); }
153
154 // Return the name and description of the counter with the given info.
155 std::pair<StringRef, StringRef> getCounterDesc(CounterInfo *Info) const {
156 return {Info->Name, Info->Desc};
157 }
158
159 // Iterate through the registered counters
166
168 for (auto &[_, Counter] : Counters)
169 Counter->Active = true;
170 }
171
172protected:
174 bool handleCounterIncrement(CounterInfo &Info);
175
177
178 bool ShouldPrintCounter = false;
179
181
182 bool BreakOnLast = false;
183};
184
185#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \
186 static DebugCounter::CounterInfo VARNAME(COUNTERNAME, DESC)
187
188} // namespace llvm
189#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 CounterState getCounterState(CounterInfo &Info)
static void setCounterState(CounterInfo &Info, CounterState State)
static LLVM_ABI void printChunks(raw_ostream &OS, ArrayRef< Chunk >)
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 bool shouldExecute(CounterInfo &Counter)
static bool isCounterSet(CounterInfo &Info)
static LLVM_ABI bool parseChunks(StringRef Str, SmallVector< Chunk > &Res)
Return true on parsing error and print the error message on the llvm::errs()
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
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.
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
This is an optimization pass for GlobalISel generic memory operations.
bool contains(int64_t Idx) const
LLVM_ABI void print(llvm::raw_ostream &OS)