aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/reduce-chunk-list/reduce-chunk-list.cpp
blob: dc99859b516dbf7f787c69b1861fd6a7d1e85797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//===-- reduce-chunk-list.cpp - Reduce a chunks list to its minimal size --===//
//
// 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;

static cl::opt<std::string> ReproductionCmd(cl::Positional, cl::Required);

static cl::opt<std::string> StartChunks(cl::Positional, cl::Required);

static cl::opt<bool> Pessimist("pessimist", cl::init(false));

using Chunk = DebugCounter::Chunk;

namespace {

SmallVector<Chunk> simplifyChunksList(ArrayRef<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 isStillInteresting(ArrayRef<Chunk> Chunks) {
  SmallVector<Chunk> SimpleChunks = simplifyChunksList(Chunks);

  std::string ChunkStr;
  {
    raw_string_ostream OS(ChunkStr);
    DebugCounter::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;
}

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;
}

} // namespace

int main(int argc, char **argv) {
  cl::ParseCommandLineOptions(argc, argv);

  SmallVector<Chunk> CurrChunks;
  if (DebugCounter::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 (!isStillInteresting(CurrChunks)) {
    errs() << "starting chunks are not interesting\n";
    return 1;
  }
  if (CurrChunks.size() == 1)
    increaseGranularity(CurrChunks);
  if (Pessimist)
    while (increaseGranularity(CurrChunks))
      /* empty body */;
  while (1) {
    for (int Idx = (CurrChunks.size() - 1); Idx >= 0; Idx--) {
      if (CurrChunks.size() == 1)
        break;

      Chunk Testing = CurrChunks[Idx];
      errs() << "Trying to remove : ";
      Testing.print(errs());
      errs() << "\n";

      CurrChunks.erase(CurrChunks.begin() + Idx);

      if (!isStillInteresting(CurrChunks))
        CurrChunks.insert(CurrChunks.begin() + Idx, Testing);
    }
    bool HasSplit = increaseGranularity(CurrChunks);
    if (!HasSplit)
      break;
  }

  errs() << "Minimal Chunks = ";
  DebugCounter::printChunks(llvm::errs(), simplifyChunksList(CurrChunks));
  errs() << "\n";
}