2017-02-19 05:29:50 +01:00
|
|
|
//===- llvm/Support/DebugCounter.h - Debug counter support ------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-19 05:29:50 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-03-10 21:44:39 +01:00
|
|
|
/// \file
|
2018-05-01 17:54:18 +02:00
|
|
|
/// This file provides an implementation of debug counters. Debug
|
2017-03-18 16:41:13 +01:00
|
|
|
/// counters are a tool that let you narrow down a miscompilation to a specific
|
|
|
|
/// thing happening.
|
2017-02-19 05:29:50 +01:00
|
|
|
///
|
2017-03-10 21:44:39 +01:00
|
|
|
/// To give a use case: Imagine you have a file, very large, and you
|
2017-03-18 16:41:13 +01:00
|
|
|
/// are trying to understand the minimal transformation that breaks it. Bugpoint
|
2017-03-10 21:44:39 +01:00
|
|
|
/// and bisection is often helpful here in narrowing it down to a specific pass,
|
|
|
|
/// but it's still a very large file, and a very complicated pass to try to
|
|
|
|
/// debug. That is where debug counting steps in. You can instrument the pass
|
|
|
|
/// with a debug counter before it does a certain thing, and depending on the
|
|
|
|
/// counts, it will either execute that thing or not. The debug counter itself
|
|
|
|
/// consists of a skip and a count. Skip is the number of times shouldExecute
|
|
|
|
/// needs to be called before it returns true. Count is the number of times to
|
|
|
|
/// return true once Skip is 0. So a skip=47, count=2 ,would skip the first 47
|
|
|
|
/// executions by returning false from shouldExecute, then execute twice, and
|
|
|
|
/// then return false again.
|
|
|
|
/// Note that a counter set to a negative number will always execute.
|
|
|
|
/// For a concrete example, during predicateinfo creation, the renaming pass
|
|
|
|
/// replaces each use with a renamed use.
|
|
|
|
////
|
|
|
|
/// If I use DEBUG_COUNTER to create a counter called "predicateinfo", and
|
|
|
|
/// variable name RenameCounter, and then instrument this renaming with a debug
|
|
|
|
/// counter, like so:
|
|
|
|
///
|
|
|
|
/// if (!DebugCounter::shouldExecute(RenameCounter)
|
|
|
|
/// <continue or return or whatever not executing looks like>
|
|
|
|
///
|
|
|
|
/// Now I can, from the command line, make it rename or not rename certain uses
|
|
|
|
/// by setting the skip and count.
|
|
|
|
/// So for example
|
|
|
|
/// bin/opt -debug-counter=predicateinfo-skip=47,predicateinfo-count=1
|
|
|
|
/// will skip renaming the first 47 uses, then rename one, then skip the rest.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-02-19 05:29:50 +01:00
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_DEBUGCOUNTER_H
|
|
|
|
#define LLVM_SUPPORT_DEBUGCOUNTER_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2020-05-10 21:33:51 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-02-19 05:29:50 +01:00
|
|
|
#include "llvm/ADT/UniqueVector.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2020-05-10 21:33:51 +02:00
|
|
|
class raw_ostream;
|
|
|
|
|
2017-02-19 05:29:50 +01:00
|
|
|
class DebugCounter {
|
|
|
|
public:
|
2018-10-23 23:51:56 +02:00
|
|
|
~DebugCounter();
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Returns a reference to the singleton instance.
|
2017-02-19 05:29:50 +01:00
|
|
|
static DebugCounter &instance();
|
|
|
|
|
|
|
|
// Used by the command line option parser to push a new value it parsed.
|
|
|
|
void push_back(const std::string &);
|
|
|
|
|
|
|
|
// Register a counter with the specified name.
|
|
|
|
//
|
|
|
|
// FIXME: Currently, counter registration is required to happen before command
|
|
|
|
// line option parsing. The main reason to register counters is to produce a
|
|
|
|
// nice list of them on the command line, but i'm not sure this is worth it.
|
|
|
|
static unsigned registerCounter(StringRef Name, StringRef Desc) {
|
2020-01-28 20:23:46 +01:00
|
|
|
return instance().addCounter(std::string(Name), std::string(Desc));
|
2017-02-19 05:29:50 +01:00
|
|
|
}
|
|
|
|
inline static bool shouldExecute(unsigned CounterName) {
|
2018-08-02 21:50:27 +02:00
|
|
|
if (!isCountingEnabled())
|
|
|
|
return true;
|
|
|
|
|
2017-02-19 05:29:50 +01:00
|
|
|
auto &Us = instance();
|
|
|
|
auto Result = Us.Counters.find(CounterName);
|
|
|
|
if (Result != Us.Counters.end()) {
|
2018-07-23 23:49:36 +02:00
|
|
|
auto &CounterInfo = Result->second;
|
|
|
|
++CounterInfo.Count;
|
|
|
|
|
|
|
|
// We only execute while the Skip is not smaller than Count,
|
|
|
|
// and the StopAfter + Skip is larger than Count.
|
2017-02-19 05:29:50 +01:00
|
|
|
// Negative counters always execute.
|
2018-07-23 23:49:36 +02:00
|
|
|
if (CounterInfo.Skip < 0)
|
2017-02-19 05:29:50 +01:00
|
|
|
return true;
|
2018-07-23 23:49:36 +02:00
|
|
|
if (CounterInfo.Skip >= CounterInfo.Count)
|
2017-02-19 05:29:50 +01:00
|
|
|
return false;
|
2018-07-23 23:49:36 +02:00
|
|
|
if (CounterInfo.StopAfter < 0)
|
2017-02-19 05:29:50 +01:00
|
|
|
return true;
|
2018-07-23 23:49:36 +02:00
|
|
|
return CounterInfo.StopAfter + CounterInfo.Skip >= CounterInfo.Count;
|
2017-02-19 05:29:50 +01:00
|
|
|
}
|
|
|
|
// Didn't find the counter, should we warn?
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-18 16:41:13 +01:00
|
|
|
// Return true if a given counter had values set (either programatically or on
|
|
|
|
// the command line). This will return true even if those values are
|
|
|
|
// currently in a state where the counter will always execute.
|
|
|
|
static bool isCounterSet(unsigned ID) {
|
2018-07-23 23:49:36 +02:00
|
|
|
return instance().Counters[ID].IsSet;
|
2017-03-18 16:41:13 +01:00
|
|
|
}
|
|
|
|
|
2018-07-23 23:49:36 +02:00
|
|
|
// Return the Count for a counter. This only works for set counters.
|
|
|
|
static int64_t getCounterValue(unsigned ID) {
|
2017-03-18 16:41:13 +01:00
|
|
|
auto &Us = instance();
|
|
|
|
auto Result = Us.Counters.find(ID);
|
|
|
|
assert(Result != Us.Counters.end() && "Asking about a non-set counter");
|
2018-07-23 23:49:36 +02:00
|
|
|
return Result->second.Count;
|
2017-03-18 16:41:13 +01:00
|
|
|
}
|
|
|
|
|
2018-07-23 23:49:36 +02:00
|
|
|
// Set a registered counter to a given Count value.
|
|
|
|
static void setCounterValue(unsigned ID, int64_t Count) {
|
2017-03-18 16:41:13 +01:00
|
|
|
auto &Us = instance();
|
2018-07-23 23:49:36 +02:00
|
|
|
Us.Counters[ID].Count = Count;
|
2017-03-18 16:41:13 +01:00
|
|
|
}
|
|
|
|
|
2017-06-14 21:16:22 +02:00
|
|
|
// Dump or print the current counter set into llvm::dbgs().
|
|
|
|
LLVM_DUMP_METHOD void dump() const;
|
2017-02-19 05:29:50 +01:00
|
|
|
|
2017-06-14 21:16:22 +02:00
|
|
|
void print(raw_ostream &OS) const;
|
2017-03-18 16:41:13 +01:00
|
|
|
|
2017-02-19 05:29:50 +01:00
|
|
|
// Get the counter ID for a given named counter, or return 0 if none is found.
|
|
|
|
unsigned getCounterId(const std::string &Name) const {
|
|
|
|
return RegisteredCounters.idFor(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of registered counters.
|
|
|
|
unsigned int getNumCounters() const { return RegisteredCounters.size(); }
|
|
|
|
|
|
|
|
// Return the name and description of the counter with the given ID.
|
2017-02-20 19:33:40 +01:00
|
|
|
std::pair<std::string, std::string> getCounterInfo(unsigned ID) const {
|
2018-07-23 23:49:36 +02:00
|
|
|
return std::make_pair(RegisteredCounters[ID], Counters.lookup(ID).Desc);
|
2017-02-19 05:29:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the registered counters
|
|
|
|
typedef UniqueVector<std::string> CounterVector;
|
|
|
|
CounterVector::const_iterator begin() const {
|
|
|
|
return RegisteredCounters.begin();
|
|
|
|
}
|
|
|
|
CounterVector::const_iterator end() const { return RegisteredCounters.end(); }
|
|
|
|
|
2018-08-02 21:50:27 +02:00
|
|
|
// Force-enables counting all DebugCounters.
|
|
|
|
//
|
|
|
|
// Since DebugCounters are incompatible with threading (not only do they not
|
|
|
|
// make sense, but we'll also see data races), this should only be used in
|
|
|
|
// contexts where we're certain we won't spawn threads.
|
|
|
|
static void enableAllCounters() { instance().Enabled = true; }
|
|
|
|
|
2017-02-19 05:29:50 +01:00
|
|
|
private:
|
2018-08-02 21:50:27 +02:00
|
|
|
static bool isCountingEnabled() {
|
|
|
|
// Compile to nothing when debugging is off
|
|
|
|
#ifdef NDEBUG
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return instance().Enabled;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-19 05:29:50 +01:00
|
|
|
unsigned addCounter(const std::string &Name, const std::string &Desc) {
|
|
|
|
unsigned Result = RegisteredCounters.insert(Name);
|
2018-07-23 23:49:36 +02:00
|
|
|
Counters[Result] = {};
|
|
|
|
Counters[Result].Desc = Desc;
|
2017-02-19 05:29:50 +01:00
|
|
|
return Result;
|
|
|
|
}
|
2018-07-23 23:49:36 +02:00
|
|
|
// Struct to store counter info.
|
|
|
|
struct CounterInfo {
|
|
|
|
int64_t Count = 0;
|
|
|
|
int64_t Skip = 0;
|
|
|
|
int64_t StopAfter = -1;
|
|
|
|
bool IsSet = false;
|
|
|
|
std::string Desc;
|
|
|
|
};
|
|
|
|
DenseMap<unsigned, CounterInfo> Counters;
|
2017-02-19 05:29:50 +01:00
|
|
|
CounterVector RegisteredCounters;
|
2018-08-02 21:50:27 +02:00
|
|
|
|
|
|
|
// Whether we should do DebugCounting at all. DebugCounters aren't
|
|
|
|
// thread-safe, so this should always be false in multithreaded scenarios.
|
|
|
|
bool Enabled = false;
|
2017-02-19 05:29:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \
|
|
|
|
static const unsigned VARNAME = \
|
2017-08-10 19:48:11 +02:00
|
|
|
DebugCounter::registerCounter(COUNTERNAME, DESC)
|
2017-02-19 05:29:50 +01:00
|
|
|
|
|
|
|
} // namespace llvm
|
|
|
|
#endif
|