[llvm] [DebugCounter] Add support for non-continous ranges. (PR #89470)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 19 15:53:03 PDT 2024
https://github.com/Ralender updated https://github.com/llvm/llvm-project/pull/89470
>From 79ce20262579862fb347bc5523d880468f2af1fc Mon Sep 17 00:00:00 2001
From: Tyker <tyker1 at outlook.com>
Date: Sat, 20 Apr 2024 00:38:26 +0200
Subject: [PATCH] [DebugCounter] Add support for non-continous ranges.
+ Script for bisecting though a build
---
llvm/docs/ProgrammersManual.rst | 33 +++-
llvm/include/llvm/IR/PassManager.h | 5 +
llvm/include/llvm/Support/DebugCounter.h | 54 +++++--
llvm/lib/IR/PassManager.cpp | 9 ++
llvm/lib/Support/DebugCounter.cpp | 162 ++++++++++++++------
llvm/tools/delta-driver/CMakeLists.txt | 9 ++
llvm/tools/delta-driver/delta-driver.cpp | 151 ++++++++++++++++++
llvm/unittests/Support/DebugCounterTest.cpp | 28 ++--
llvm/utils/bisector/bisector.py | 110 +++++++++++++
llvm/utils/bisector/bisector_demo.sh | 35 +++++
10 files changed, 514 insertions(+), 82 deletions(-)
create mode 100644 llvm/tools/delta-driver/CMakeLists.txt
create mode 100644 llvm/tools/delta-driver/delta-driver.cpp
create mode 100755 llvm/utils/bisector/bisector.py
create mode 100755 llvm/utils/bisector/bisector_demo.sh
diff --git a/llvm/docs/ProgrammersManual.rst b/llvm/docs/ProgrammersManual.rst
index 491e6b1dd2498b..cb0796226310e5 100644
--- a/llvm/docs/ProgrammersManual.rst
+++ b/llvm/docs/ProgrammersManual.rst
@@ -1362,14 +1362,12 @@ Whatever code you want that control, use ``DebugCounter::shouldExecute`` to cont
if (DebugCounter::shouldExecute(DeleteAnInstruction))
I->eraseFromParent();
-That's all you have to do. Now, using opt, you can control when this code triggers using
-the '``--debug-counter``' option. There are two counters provided, ``skip`` and ``count``.
-``skip`` is the number of times to skip execution of the codepath. ``count`` is the number
-of times, once we are done skipping, to execute the codepath.
+That's all you have to do. Now, using opt, you can control when this code triggers using
+the '``--debug-counter``' Options.To specify when to execute the codepath.
.. code-block:: none
- $ opt --debug-counter=passname-delete-instruction-skip=1,passname-delete-instruction-count=2 -passname
+ $ opt --debug-counter=passname-delete-instruction=1-3 -passname
This will skip the above code the first time we hit it, then execute it twice, then skip the rest of the executions.
@@ -1384,9 +1382,28 @@ So if executed on the following code:
It would delete number ``%2`` and ``%3``.
-A utility is provided in `utils/bisect-skip-count` to binary search
-skip and count arguments. It can be used to automatically minimize the
-skip and count for a debug-counter variable.
+A utility is provided in `llvm/tools/delta-driver/delta-driver.cpp` to drive the automated delta debugging.
+How to use delta-driver:
+First, Figure out the number of calls to the debug counter you want to minimize.
+To do so, run the compilation command causing issue with `-print-debug-counter` adding a `-mllvm` if needed.
+Than find the line with the counter of interest. it should look like:
+.. code-block:: none
+
+ my-counter : {5678,empty}
+
+The number of calls to `my-counter` is 5678
+
+Than Find the minimal uses of the debug counter with `delta-driver`.
+Build a reproducer script like:
+.. code-block:: bash
+
+ #! /bin/bash
+ opt -debug-counter=my-counter=$1
+ # ... Test result of the command. Failure of the script is considered interesting
+
+Than run `delta-driver my-script.sh 0-5678 2>&1 | tee dump_bisect`
+This command may take some time.
+but when it is done, it will print the result like: `Minimal Chunks = 0:1:5:11-12:33-34`
.. _ViewGraph:
diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index d701481202f8db..5fb6a59380a792 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -64,6 +64,9 @@ extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
namespace llvm {
+// Used for debug counter of adding a pass to the pipeline
+bool shouldAddNewPMPass();
+
// Forward declare the analysis manager template.
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
@@ -247,6 +250,8 @@ class PassManager : public PassInfoMixin<
// FIXME: Revert to enable_if style when gcc >= 11.1
template <typename PassT> LLVM_ATTRIBUTE_MINSIZE void addPass(PassT &&Pass) {
+ if (!shouldAddNewPMPass())
+ return;
using PassModelT =
detail::PassModel<IRUnitT, PassT, AnalysisManagerT, ExtraArgTs...>;
if constexpr (!std::is_same_v<PassT, PassManager>) {
diff --git a/llvm/include/llvm/Support/DebugCounter.h b/llvm/include/llvm/Support/DebugCounter.h
index 9fa4620ade3c8f..5da3deb621703b 100644
--- a/llvm/include/llvm/Support/DebugCounter.h
+++ b/llvm/include/llvm/Support/DebugCounter.h
@@ -43,6 +43,7 @@
#ifndef LLVM_SUPPORT_DEBUGCOUNTER_H
#define LLVM_SUPPORT_DEBUGCOUNTER_H
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/UniqueVector.h"
@@ -53,6 +54,18 @@ namespace llvm {
class raw_ostream;
+struct Chunk {
+ int64_t Begin;
+ int64_t End;
+ void print(llvm::raw_ostream &OS);
+ bool contains(int64_t Idx) { return Idx >= Begin && Idx <= End; }
+};
+
+void printChunks(raw_ostream &OS, ArrayRef<Chunk>);
+
+/// Return true on parsing error and print the error message on the llvm::errs()
+bool parseChunks(StringRef Str, SmallVector<Chunk> &Res);
+
class DebugCounter {
public:
/// Returns a reference to the singleton instance.
@@ -77,18 +90,28 @@ class DebugCounter {
auto Result = Us.Counters.find(CounterName);
if (Result != Us.Counters.end()) {
auto &CounterInfo = Result->second;
- ++CounterInfo.Count;
+ int64_t CurrCount = CounterInfo.Count++;
+ uint64_t CurrIdx = CounterInfo.CurrChunkIdx;
- // We only execute while the Skip is not smaller than Count,
- // and the StopAfter + Skip is larger than Count.
- // Negative counters always execute.
- if (CounterInfo.Skip < 0)
+ if (CounterInfo.Chunks.empty())
return true;
- if (CounterInfo.Skip >= CounterInfo.Count)
+ if (CurrIdx >= CounterInfo.Chunks.size())
return false;
- if (CounterInfo.StopAfter < 0)
- return true;
- return CounterInfo.StopAfter + CounterInfo.Skip >= CounterInfo.Count;
+
+ 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;
+ }
+ return Res;
}
// Didn't find the counter, should we warn?
return true;
@@ -152,11 +175,11 @@ class DebugCounter {
#ifdef NDEBUG
return false;
#else
- return instance().Enabled;
+ return instance().Enabled || instance().ShouldPrintCounter;
#endif
}
-private:
+protected:
unsigned addCounter(const std::string &Name, const std::string &Desc) {
unsigned Result = RegisteredCounters.insert(Name);
Counters[Result] = {};
@@ -166,17 +189,22 @@ class DebugCounter {
// Struct to store counter info.
struct CounterInfo {
int64_t Count = 0;
- int64_t Skip = 0;
- int64_t StopAfter = -1;
+ uint64_t CurrChunkIdx = 0;
bool IsSet = false;
std::string Desc;
+ SmallVector<Chunk> Chunks;
};
+
DenseMap<unsigned, CounterInfo> Counters;
CounterVector RegisteredCounters;
// Whether we should do DebugCounting at all. DebugCounters aren't
// thread-safe, so this should always be false in multithreaded scenarios.
bool Enabled = false;
+
+ bool ShouldPrintCounter = false;
+
+ bool BreakOnLast = false;
};
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC) \
diff --git a/llvm/lib/IR/PassManager.cpp b/llvm/lib/IR/PassManager.cpp
index cbddf3dfb056cf..5dc53b78f5d66a 100644
--- a/llvm/lib/IR/PassManager.cpp
+++ b/llvm/lib/IR/PassManager.cpp
@@ -8,11 +8,20 @@
#include "llvm/IR/PassManager.h"
#include "llvm/IR/PassManagerImpl.h"
+#include "llvm/Support/DebugCounter.h"
#include <optional>
using namespace llvm;
namespace llvm {
+
+DEBUG_COUNTER(AddNewPMPassCounter, "new-pm-pass-counter",
+ "Control what passes get run");
+
+bool shouldAddNewPMPass() {
+ return DebugCounter::shouldExecute(AddNewPMPassCounter);
+}
+
// Explicit template instantiations and specialization defininitions for core
// template typedefs.
template class AllAnalysesOn<Module>;
diff --git a/llvm/lib/Support/DebugCounter.cpp b/llvm/lib/Support/DebugCounter.cpp
index 502665d2a8348e..d62f1889fafe6f 100644
--- a/llvm/lib/Support/DebugCounter.cpp
+++ b/llvm/lib/Support/DebugCounter.cpp
@@ -7,6 +7,83 @@
using namespace llvm;
+namespace llvm {
+
+void Chunk::print(llvm::raw_ostream &OS) {
+ if (Begin == End)
+ OS << Begin;
+ else
+ OS << Begin << "-" << End;
+}
+
+void printChunks(raw_ostream &OS, ArrayRef<Chunk> Chunks) {
+ if (Chunks.empty()) {
+ OS << "empty";
+ } else {
+ bool IsFirst = true;
+ for (auto E : Chunks) {
+ if (!IsFirst)
+ OS << ":";
+ else
+ IsFirst = false;
+ E.print(OS);
+ }
+ }
+}
+
+bool parseChunks(StringRef Str, SmallVector<Chunk> &Chunks) {
+ StringRef Remaining = Str;
+
+ auto ConsumeInt = [&]() -> int64_t {
+ StringRef Number =
+ Remaining.take_until([](char c) { return c < '0' || c > '9'; });
+ int64_t Res;
+ if (Number.getAsInteger(10, Res)) {
+ errs() << "Failed to parse int at : " << Remaining << "\n";
+ return -1;
+ }
+ Remaining = Remaining.drop_front(Number.size());
+ return Res;
+ };
+
+ while (1) {
+ int64_t Num = ConsumeInt();
+ if (Num == -1) {
+ return true;
+ }
+ if (!Chunks.empty() && Num <= Chunks[Chunks.size() - 1].End) {
+ errs() << "Expected Chunks to be in increasing order " << Num
+ << " <= " << Chunks[Chunks.size() - 1].End << "\n";
+ return true;
+ }
+ if (Remaining.starts_with("-")) {
+ Remaining = Remaining.drop_front();
+ int64_t Num2 = ConsumeInt();
+ if (Num2 == -1)
+ return true;
+ if (Num >= Num2) {
+ errs() << "Expected " << Num << " < " << Num2 << " in " << Num << "-"
+ << Num2 << "\n";
+ return true;
+ }
+
+ Chunks.push_back({Num, Num2});
+ } else {
+ Chunks.push_back({Num, Num});
+ }
+ if (Remaining.starts_with(":")) {
+ Remaining = Remaining.drop_front();
+ continue;
+ }
+ if (Remaining.empty())
+ break;
+ errs() << "Failed to parse at : " << Remaining;
+ return true;
+ }
+ return false;
+}
+} // namespace llvm
+
namespace {
// This class overrides the default list implementation of printing so we
// can pretty print the list of debug counter options. This type of
@@ -47,15 +124,26 @@ class DebugCounterList : public cl::list<std::string, DebugCounter> {
// itself, are owned by a single global instance of the DebugCounterOwner
// struct. This makes it easier to control the order in which constructors and
// destructors are run.
-struct DebugCounterOwner {
- DebugCounter DC;
+struct DebugCounterOwner : DebugCounter {
DebugCounterList DebugCounterOption{
"debug-counter", cl::Hidden,
cl::desc("Comma separated list of debug counter skip and count"),
- cl::CommaSeparated, cl::location(DC)};
- cl::opt<bool> PrintDebugCounter{
- "print-debug-counter", cl::Hidden, cl::init(false), cl::Optional,
+ cl::CommaSeparated, cl::location<DebugCounter>(*this)};
+ cl::opt<bool, true> PrintDebugCounter{
+ "print-debug-counter",
+ cl::Hidden,
+ cl::Optional,
+ cl::location(this->ShouldPrintCounter),
+ cl::init(false),
cl::desc("Print out debug counter info after all counters accumulated")};
+ cl::opt<bool, true> BreakOnLastCount{
+ "debug-counter-break-on-last",
+ cl::Hidden,
+ cl::Optional,
+ cl::location(this->BreakOnLast),
+ cl::init(false),
+ cl::desc("Insert a break point on the last enabled count of a "
+ "chunks list")};
DebugCounterOwner() {
// Our destructor uses the debug stream. By referencing it here, we
@@ -65,8 +153,8 @@ struct DebugCounterOwner {
// Print information when destroyed, iff command line option is specified.
~DebugCounterOwner() {
- if (DC.isCountingEnabled() && PrintDebugCounter)
- DC.print(dbgs());
+ if (ShouldPrintCounter)
+ print(dbgs());
}
};
@@ -76,7 +164,7 @@ void llvm::initDebugCounterOptions() { (void)DebugCounter::instance(); }
DebugCounter &DebugCounter::instance() {
static DebugCounterOwner O;
- return O.DC;
+ return O;
}
// This is called by the command line parser when it sees a value for the
@@ -84,52 +172,31 @@ DebugCounter &DebugCounter::instance() {
void DebugCounter::push_back(const std::string &Val) {
if (Val.empty())
return;
- // The strings should come in as counter=value
+
+ // The strings should come in as counter=chunk_list
auto CounterPair = StringRef(Val).split('=');
if (CounterPair.second.empty()) {
errs() << "DebugCounter Error: " << Val << " does not have an = in it\n";
return;
}
- // Now we have counter=value.
- // First, process value.
- int64_t CounterVal;
- if (CounterPair.second.getAsInteger(0, CounterVal)) {
- errs() << "DebugCounter Error: " << CounterPair.second
- << " is not a number\n";
+ StringRef CounterName = CounterPair.first;
+ SmallVector<Chunk> Chunks;
+
+ if (parseChunks(CounterPair.second, Chunks)) {
return;
}
- // Now we need to see if this is the skip or the count, remove the suffix, and
- // add it to the counter values.
- if (CounterPair.first.ends_with("-skip")) {
- auto CounterName = CounterPair.first.drop_back(5);
- unsigned CounterID = getCounterId(std::string(CounterName));
- if (!CounterID) {
- errs() << "DebugCounter Error: " << CounterName
- << " is not a registered counter\n";
- return;
- }
- enableAllCounters();
- CounterInfo &Counter = Counters[CounterID];
- Counter.Skip = CounterVal;
- Counter.IsSet = true;
- } else if (CounterPair.first.ends_with("-count")) {
- auto CounterName = CounterPair.first.drop_back(6);
- unsigned CounterID = getCounterId(std::string(CounterName));
- if (!CounterID) {
- errs() << "DebugCounter Error: " << CounterName
- << " is not a registered counter\n";
- return;
- }
- enableAllCounters();
-
- CounterInfo &Counter = Counters[CounterID];
- Counter.StopAfter = CounterVal;
- Counter.IsSet = true;
- } else {
- errs() << "DebugCounter Error: " << CounterPair.first
- << " does not end with -skip or -count\n";
+ unsigned CounterID = getCounterId(std::string(CounterName));
+ if (!CounterID) {
+ errs() << "DebugCounter Error: " << CounterName
+ << " is not a registered counter\n";
+ return;
}
+ enableAllCounters();
+
+ CounterInfo &Counter = Counters[CounterID];
+ Counter.IsSet = true;
+ Counter.Chunks = std::move(Chunks);
}
void DebugCounter::print(raw_ostream &OS) const {
@@ -142,8 +209,9 @@ void DebugCounter::print(raw_ostream &OS) const {
for (auto &CounterName : CounterNames) {
unsigned CounterID = getCounterId(std::string(CounterName));
OS << left_justify(RegisteredCounters[CounterID], 32) << ": {"
- << Us.Counters[CounterID].Count << "," << Us.Counters[CounterID].Skip
- << "," << Us.Counters[CounterID].StopAfter << "}\n";
+ << Us.Counters[CounterID].Count << ",";
+ printChunks(OS, Us.Counters[CounterID].Chunks);
+ OS << "}\n";
}
}
diff --git a/llvm/tools/delta-driver/CMakeLists.txt b/llvm/tools/delta-driver/CMakeLists.txt
new file mode 100644
index 00000000000000..54ffbb12b6fb05
--- /dev/null
+++ b/llvm/tools/delta-driver/CMakeLists.txt
@@ -0,0 +1,9 @@
+set(LLVM_LINK_COMPONENTS
+ Support
+ )
+
+add_llvm_tool(delta-driver
+ delta-driver.cpp
+
+ DEPENDS
+ )
diff --git a/llvm/tools/delta-driver/delta-driver.cpp b/llvm/tools/delta-driver/delta-driver.cpp
new file mode 100644
index 00000000000000..6562493f967f0c
--- /dev/null
+++ b/llvm/tools/delta-driver/delta-driver.cpp
@@ -0,0 +1,151 @@
+//===-- delta-driver.cpp - Tool to drive Automated Delta Debugging --------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// See the llvm-project/llvm/docs/ProgrammersManual.rst to see how to use this
+// tool
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/DenseSet.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/DebugCounter.h"
+#include "llvm/Support/Program.h"
+
+using namespace llvm;
+
+cl::opt<std::string> ReproductionCmd(cl::Positional, cl::Required);
+
+cl::opt<std::string> StartChunks(cl::Positional, cl::Required);
+
+cl::opt<bool> Pessimist("pessimist", cl::init(false));
+
+SmallVector<Chunk> simplifyChunksList(const SmallVector<Chunk> &Chunks) {
+ SmallVector<Chunk> Res;
+ Res.push_back(Chunks.front());
+ for (unsigned Idx = 1; Idx < Chunks.size(); Idx++) {
+ if (Chunks[Idx].Begin == Res.back().End + 1)
+ Res.back().End = Chunks[Idx].End;
+ else
+ Res.push_back(Chunks[Idx]);
+ }
+ return Res;
+}
+
+bool stillReproducesIssue(const SmallVector<Chunk> &Chunks) {
+ SmallVector<Chunk> SimpleChunks = simplifyChunksList(Chunks);
+
+ std::string ChunkStr;
+ {
+ raw_string_ostream OS(ChunkStr);
+ printChunks(OS, SimpleChunks);
+ }
+
+ errs() << "Checking with: " << ChunkStr << "\n";
+
+ std::vector<StringRef> Argv;
+ Argv.push_back(ReproductionCmd);
+ Argv.push_back(ChunkStr);
+
+ std::string ErrMsg;
+ bool ExecutionFailed;
+ int Result = sys::ExecuteAndWait(Argv[0], Argv, std::nullopt, {}, 0, 0,
+ &ErrMsg, &ExecutionFailed);
+ if (ExecutionFailed) {
+ errs() << "failed to execute : " << Argv[0] << " : " << ErrMsg << "\n";
+ exit(1);
+ }
+
+ bool Res = Result != 0;
+ if (Res) {
+ errs() << "SUCCESS : Still Interesting\n";
+ } else {
+ errs() << "FAILURE : Not Interesting\n";
+ }
+ return Res;
+}
+
+static bool increaseGranularity(SmallVector<Chunk> &Chunks) {
+ errs() << "Increasing granularity\n";
+ SmallVector<Chunk> NewChunks;
+ bool SplitOne = false;
+
+ for (auto &C : Chunks) {
+ if (C.Begin == C.End)
+ NewChunks.push_back(C);
+ else {
+ int Half = (C.Begin + C.End) / 2;
+ NewChunks.push_back({C.Begin, Half});
+ NewChunks.push_back({Half + 1, C.End});
+ SplitOne = true;
+ }
+ }
+ if (SplitOne) {
+ Chunks = std::move(NewChunks);
+ }
+ return SplitOne;
+}
+
+int main(int argc, char **argv) {
+ cl::ParseCommandLineOptions(argc, argv);
+
+ SmallVector<Chunk> CurrChunks;
+ if (parseChunks(StartChunks, CurrChunks)) {
+ return 1;
+ }
+
+ auto Program = sys::findProgramByName(ReproductionCmd);
+ if (!Program) {
+ errs() << "failed to find command : " << ReproductionCmd << "\n";
+ return 1;
+ }
+ ReproductionCmd.setValue(Program.get());
+
+ errs() << "Input Checking:\n";
+ if (!stillReproducesIssue(CurrChunks)) {
+ errs() << "starting chunks are not interesting\n";
+ return 1;
+ }
+ if (CurrChunks.size() == 1)
+ increaseGranularity(CurrChunks);
+ if (Pessimist)
+ while (increaseGranularity(CurrChunks)) {
+ }
+ while (1) {
+ SmallDenseSet<unsigned> NotNeedChunks;
+ auto FilteredCopy = [&]() {
+ SmallVector<Chunk> CopiedChunks;
+ for (unsigned SubIdx = 0; SubIdx < CurrChunks.size(); SubIdx++)
+ if (!NotNeedChunks.count(SubIdx))
+ CopiedChunks.push_back(CurrChunks[SubIdx]);
+ return CopiedChunks;
+ };
+
+ for (int Idx = (CurrChunks.size() - 1); Idx >= 0; Idx--) {
+ if (NotNeedChunks.size() + 1 == CurrChunks.size())
+ break;
+
+ errs() << "Trying to remove : ";
+ CurrChunks[Idx].print(errs());
+ errs() << "\n";
+
+ NotNeedChunks.insert(Idx);
+ SmallVector<Chunk> NextChunks = FilteredCopy();
+ if (!stillReproducesIssue(NextChunks)) {
+ NotNeedChunks.erase(Idx);
+ }
+ }
+ CurrChunks = FilteredCopy();
+ bool HasSplit = increaseGranularity(CurrChunks);
+ if (!HasSplit)
+ break;
+ }
+
+ errs() << "Minimal Chunks = ";
+ printChunks(llvm::errs(), simplifyChunksList(CurrChunks));
+ errs() << "\n";
+}
diff --git a/llvm/unittests/Support/DebugCounterTest.cpp b/llvm/unittests/Support/DebugCounterTest.cpp
index e7345b13cc1729..820b1ce5ca0d37 100644
--- a/llvm/unittests/Support/DebugCounterTest.cpp
+++ b/llvm/unittests/Support/DebugCounterTest.cpp
@@ -13,28 +13,28 @@
using namespace llvm;
#ifndef NDEBUG
-TEST(DebugCounterTest, CounterCheck) {
+TEST(DebugCounterTest, Basic) {
DEBUG_COUNTER(TestCounter, "test-counter", "Counter used for unit test");
EXPECT_FALSE(DebugCounter::isCounterSet(TestCounter));
-
auto DC = &DebugCounter::instance();
- DC->push_back("test-counter-skip=1");
- DC->push_back("test-counter-count=3");
+ DC->push_back("test-counter=1:3-5:78:79:89:100-102:150");
EXPECT_TRUE(DebugCounter::isCounterSet(TestCounter));
- EXPECT_EQ(0, DebugCounter::getCounterValue(TestCounter));
- EXPECT_FALSE(DebugCounter::shouldExecute(TestCounter));
-
- EXPECT_EQ(1, DebugCounter::getCounterValue(TestCounter));
- EXPECT_TRUE(DebugCounter::shouldExecute(TestCounter));
+ SmallVector<unsigned> Res;
+ for (unsigned Idx = 0; Idx < 200; Idx++) {
+ if (DebugCounter::shouldExecute(TestCounter))
+ Res.push_back(Idx);
+ }
- DebugCounter::setCounterValue(TestCounter, 3);
- EXPECT_TRUE(DebugCounter::shouldExecute(TestCounter));
- EXPECT_FALSE(DebugCounter::shouldExecute(TestCounter));
+ SmallVector<unsigned> Expected = {1, 3, 4, 5, 78, 79, 89, 100, 101, 102, 150};
+ EXPECT_EQ(Expected, Res);
- DebugCounter::setCounterValue(TestCounter, 100);
- EXPECT_FALSE(DebugCounter::shouldExecute(TestCounter));
+ std::string Str;
+ llvm::raw_string_ostream OS(Str);
+ DC->print(OS);
+ EXPECT_TRUE(StringRef(Str).contains("{200,1:3-5:78:79:89:100-102:150}"));
}
+
#endif
diff --git a/llvm/utils/bisector/bisector.py b/llvm/utils/bisector/bisector.py
new file mode 100755
index 00000000000000..94105ee508b433
--- /dev/null
+++ b/llvm/utils/bisector/bisector.py
@@ -0,0 +1,110 @@
+#!/usr/bin/env python3
+
+import subprocess
+import sys
+import os
+
+LINKER=os.environ["BISECTOR_LINKER"]
+
+# The bisector finds guilty translation units so we ignore link steps
+if not "-c" in sys.argv:
+ res = subprocess.run(LINKER.split() + sys.argv[1:])
+ exit(res.returncode)
+
+SAFE_COMPILER=os.environ["BISECTOR_SAFE_COMPILER"]
+UNSAFE_COMPILER=os.environ["BISECTOR_UNSAFE_COMPILER"]
+
+# List of bisector commands that will be run
+CMD_LIST=os.environ["BISECTOR_CMD_LIST"]
+if not os.path.exists(CMD_LIST):
+ os.mknod(CMD_LIST)
+
+# List of chunks that should use the unsafe tool
+CHUNKS = os.environ["BISECTOR_CHUNKS"]
+
+verbose=0
+if os.environ["BISECTOR_VERBOSE"]:
+ verbose=int(os.environ["BISECTOR_VERBOSE"])
+
+def log(level=1, *args, **kwargs):
+ if verbose >= level:
+ print(*args, **kwargs)
+
+# The signature is the working directory + the arguments passed to the bisector
+cmd_signature = f"cd {os.getcwd()} && \"" + "\" \"".join(sys.argv) + "\""
+
+if "BISECTOR_DUMP_CMD" in os.environ:
+ with open(os.environ["BISECTOR_DUMP_CMD"], 'a') as f:
+ f.write(cmd_signature)
+
+# Start of the Chunks list parser
+def consume_int():
+ global CHUNKS
+ idx = 0
+ int_str = ''
+ while len(CHUNKS) != 0 and ord(CHUNKS[0]) >= ord('0') and ord(CHUNKS[0]) <= ord('9'):
+ idx += 1
+ int_str += CHUNKS[0]
+ CHUNKS = CHUNKS[1:]
+ return int(int_str)
+
+def consume_char(C):
+ global CHUNKS
+ if len(CHUNKS) != 0 and CHUNKS[0] == C:
+ CHUNKS = CHUNKS[1:]
+ return True
+ return False
+
+INT_SET = set()
+
+while (1):
+ Start = consume_int()
+ if (consume_char('-')):
+ End = consume_int()
+ INT_SET |= set([I for I in range(Start, End + 1)])
+ else:
+ INT_SET |= {Start}
+
+ if consume_char(':'):
+ continue
+
+ if len(CHUNKS) == 0:
+ break
+# End of the Chunks list parser
+# The result of the chunk list is in INT_SET
+
+args = sys.argv[1:]
+found_signature = False
+should_use_unsafe = False
+
+# Traverse the CMD_LIST to look for the signature
+idx = 0
+with open(CMD_LIST) as file:
+ for line in file:
+ line = line[:-1]
+ if cmd_signature == line:
+ found_signature = True
+ if idx in INT_SET:
+ should_use_unsafe = True
+
+ # Once we found the command we have nothing else to do
+ break
+ idx += 1
+
+# If we didn't find the signature in the CMD_LIST file we add it to the CMD_LIST
+if not found_signature:
+ if idx in INT_SET:
+ should_use_unsafe = True
+ log(1, f"failed to find \"{cmd_signature}\" inside {CMD_LIST}")
+ with open(CMD_LIST, "a") as file:
+ file.write(cmd_signature)
+ file.write("\n")
+
+if should_use_unsafe:
+ log(1, f"using unsafe for: {cmd_signature}")
+ res = subprocess.run(UNSAFE_COMPILER.split() + args)
+ exit(res.returncode)
+else:
+ log(1, f"using safe: {cmd_signature}")
+ res = subprocess.run(SAFE_COMPILER.split() + args)
+ exit(res.returncode)
diff --git a/llvm/utils/bisector/bisector_demo.sh b/llvm/utils/bisector/bisector_demo.sh
new file mode 100755
index 00000000000000..ee9b0380d5a63b
--- /dev/null
+++ b/llvm/utils/bisector/bisector_demo.sh
@@ -0,0 +1,35 @@
+#!/usr/bin/bash
+# This is test / demo of the bisector + delta-driver
+# run: ./delta-driver ./llvm/utils/bisector/bisector_demo.sh 0-5
+# The execution of the delta-driver should finish by:
+# Minimal Chunks = 2:4
+# because of the artificial condition at the bottom of the scirpt
+
+set -e
+
+# Configure the bisector.py with environement variable
+
+export BISECTOR_CHUNKS=$1
+# File used to store all commands. line number is used as
+export BISECTOR_CMD_LIST=$0.cmd_list
+# In real word example the safe and unsafe compiler should be under ccache
+export BISECTOR_SAFE_COMPILER="/usr/bin/echo safe"
+export BISECTOR_UNSAFE_COMPILER="/usr/bin/echo unsafe"
+# There is no link step in out fake build
+export BISECTOR_LINKER=
+export BISECTOR_VERBOSE=2
+
+TMP_FILE=$0.tmp
+
+rm -rf $TMP_FILE
+
+# This simulate a build system calling with 6 translation units
+for i in $(seq 0 5); do
+ # -c here is to prevent the call from being interpreted as a link step
+ ./llvm/utils/bisector/bisector.py -c $i >> $TMP_FILE
+done
+
+# This simulate the test-suite of the software trying to reproduce the bug
+# We fail if translation unit 4 used the safe compiler
+# This inform the delta-driver the the test is not intersting without translation unit 4 using the unsafe compiler
+grep -w "safe -c 2" $TMP_FILE || grep -w "safe -c 4" $TMP_FILE
More information about the llvm-commits
mailing list