blob: 04f2207a31ed793e50858bb8097fbf1a0add918e [file] [log] [blame]
Chris Lattner9f5a1972003-04-24 20:16:29 +00001//===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
John Criswell09344dc2003-10-20 17:47:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner9f5a1972003-04-24 20:16:29 +00008//
9// This class is to be used as a base class for operations that want to zero in
10// on a subset of the input which still causes the bug we are tracking.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_TOOLS_BUGPOINT_LISTREDUCER_H
15#define LLVM_TOOLS_BUGPOINT_LISTREDUCER_H
Chris Lattner9f5a1972003-04-24 20:16:29 +000016
Justin Bogner1c039152016-09-06 17:18:22 +000017#include "llvm/Support/Error.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/Support/raw_ostream.h"
Chris Lattner8adfe922006-10-10 21:42:25 +000019#include <algorithm>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include <cstdlib>
Marshall Clowe9110d72017-02-16 14:37:03 +000021#include <random>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000022#include <vector>
Chris Lattner9f5a1972003-04-24 20:16:29 +000023
Brian Gaeke960707c2003-11-11 22:41:34 +000024namespace llvm {
25
Justin Bogner06d466a2016-09-01 21:04:36 +000026extern bool BugpointIsInterrupted;
27
28template <typename ElTy> struct ListReducer {
Chris Lattner9f5a1972003-04-24 20:16:29 +000029 enum TestResult {
Justin Bogner1c039152016-09-06 17:18:22 +000030 NoFailure, // No failure of the predicate was detected
31 KeepSuffix, // The suffix alone satisfies the predicate
32 KeepPrefix // The prefix alone satisfies the predicate
Chris Lattner9f5a1972003-04-24 20:16:29 +000033 };
34
Chris Lattnerd7d574d2004-05-11 20:41:07 +000035 virtual ~ListReducer() {}
36
Justin Bogner06d466a2016-09-01 21:04:36 +000037 /// This virtual function should be overriden by subclasses to implement the
38 /// test desired. The testcase is only required to test to see if the Kept
39 /// list still satisfies the property, but if it is going to check the prefix
40 /// anyway, it can.
Justin Bogner1c039152016-09-06 17:18:22 +000041 virtual Expected<TestResult> doTest(std::vector<ElTy> &Prefix,
42 std::vector<ElTy> &Kept) = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +000043
Justin Bogner06d466a2016-09-01 21:04:36 +000044 /// This function attempts to reduce the length of the specified list while
45 /// still maintaining the "test" property. This is the core of the "work"
46 /// that bugpoint does.
Justin Bogner1c039152016-09-06 17:18:22 +000047 Expected<bool> reduceList(std::vector<ElTy> &TheList) {
Misha Brukman87154fd2003-07-30 20:15:56 +000048 std::vector<ElTy> empty;
Marshall Clowe9110d72017-02-16 14:37:03 +000049 std::mt19937 randomness(0x6e5ea738); // Seed the random number generator
Justin Bogner1c039152016-09-06 17:18:22 +000050 Expected<TestResult> Result = doTest(TheList, empty);
51 if (Error E = Result.takeError())
Bill Wendlingc55cf4a2020-02-10 07:06:45 -080052 return std::move(E);
Justin Bogner1c039152016-09-06 17:18:22 +000053 switch (*Result) {
Misha Brukman87154fd2003-07-30 20:15:56 +000054 case KeepPrefix:
55 if (TheList.size() == 1) // we are done, it's the base case and it fails
56 return true;
Misha Brukman650ba8e2005-04-22 00:00:37 +000057 else
Misha Brukman87154fd2003-07-30 20:15:56 +000058 break; // there's definitely an error, but we need to narrow it down
59
60 case KeepSuffix:
61 // cannot be reached!
Nick Lewycky6ba630b2010-04-12 05:08:25 +000062 llvm_unreachable("bugpoint ListReducer internal error: "
63 "selected empty set.");
Misha Brukman87154fd2003-07-30 20:15:56 +000064
65 case NoFailure:
66 return false; // there is no failure with the full set of passes/funcs!
67 }
68
Chris Lattner8adfe922006-10-10 21:42:25 +000069 // Maximal number of allowed splitting iterations,
70 // before the elements are randomly shuffled.
71 const unsigned MaxIterationsWithoutProgress = 3;
Tobias Grosser56eab362015-07-26 15:18:45 +000072
73 // Maximal number of allowed single-element trim iterations. We add a
Simon Pilgrimdae11f72016-11-20 13:31:13 +000074 // threshold here as single-element reductions may otherwise take a
Tobias Grosser56eab362015-07-26 15:18:45 +000075 // very long time to complete.
76 const unsigned MaxTrimIterationsWithoutBackJump = 3;
Chris Lattner8adfe922006-10-10 21:42:25 +000077 bool ShufflingEnabled = true;
78
Justin Bogner06d466a2016-09-01 21:04:36 +000079 Backjump:
Chris Lattner9f5a1972003-04-24 20:16:29 +000080 unsigned MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +000081 unsigned MaxIterations = MaxIterationsWithoutProgress;
82 unsigned NumOfIterationsWithoutProgress = 0;
83 while (MidTop > 1) { // Binary split reduction loop
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000084 // Halt if the user presses ctrl-c.
85 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +000086 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +000087 return true;
88 }
Bill Wendling8ff43202008-02-26 10:46:10 +000089
Chris Lattner8adfe922006-10-10 21:42:25 +000090 // If the loop doesn't make satisfying progress, try shuffling.
91 // The purpose of shuffling is to avoid the heavy tails of the
92 // distribution (improving the speed of convergence).
Justin Bogner06d466a2016-09-01 21:04:36 +000093 if (ShufflingEnabled && NumOfIterationsWithoutProgress > MaxIterations) {
Bill Wendling8ff43202008-02-26 10:46:10 +000094 std::vector<ElTy> ShuffledList(TheList);
Marshall Clowe9110d72017-02-16 14:37:03 +000095 std::shuffle(ShuffledList.begin(), ShuffledList.end(), randomness);
Dan Gohmand8db3762009-07-15 16:35:29 +000096 errs() << "\n\n*** Testing shuffled set...\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +000097 // Check that random shuffle doesn't lose the bug
98 Expected<TestResult> Result = doTest(ShuffledList, empty);
99 // TODO: Previously, this error was ignored and we treated it as if
100 // shuffling hid the bug. This should really either be consumeError if
101 // that behaviour was sensible, or we should propagate the error.
102 assert(!Result.takeError() && "Shuffling caused internal error?");
103
104 if (*Result == KeepPrefix) {
Chris Lattner8adfe922006-10-10 21:42:25 +0000105 // If the bug is still here, use the shuffled list.
106 TheList.swap(ShuffledList);
107 MidTop = TheList.size();
Justin Bogner06d466a2016-09-01 21:04:36 +0000108 // Must increase the shuffling treshold to avoid the small
Simon Pilgrimdae11f72016-11-20 13:31:13 +0000109 // probability of infinite looping without making progress.
Chris Lattner8adfe922006-10-10 21:42:25 +0000110 MaxIterations += 2;
Dan Gohmand8db3762009-07-15 16:35:29 +0000111 errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000112 } else {
Chris Lattner8adfe922006-10-10 21:42:25 +0000113 ShufflingEnabled = false; // Disable shuffling further on
Dan Gohmand8db3762009-07-15 16:35:29 +0000114 errs() << "\n\n*** Shuffling hides the bug...\n\n";
Bill Wendling8ff43202008-02-26 10:46:10 +0000115 }
116 NumOfIterationsWithoutProgress = 0;
Chris Lattner8adfe922006-10-10 21:42:25 +0000117 }
Justin Bogner06d466a2016-09-01 21:04:36 +0000118
Chris Lattner9f5a1972003-04-24 20:16:29 +0000119 unsigned Mid = MidTop / 2;
Justin Bogner06d466a2016-09-01 21:04:36 +0000120 std::vector<ElTy> Prefix(TheList.begin(), TheList.begin() + Mid);
121 std::vector<ElTy> Suffix(TheList.begin() + Mid, TheList.end());
Chris Lattner9f5a1972003-04-24 20:16:29 +0000122
Justin Bogner1c039152016-09-06 17:18:22 +0000123 Expected<TestResult> Result = doTest(Prefix, Suffix);
124 if (Error E = Result.takeError())
Bill Wendlingc55cf4a2020-02-10 07:06:45 -0800125 return std::move(E);
Justin Bogner1c039152016-09-06 17:18:22 +0000126 switch (*Result) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000127 case KeepSuffix:
128 // The property still holds. We can just drop the prefix elements, and
129 // shorten the list to the "kept" elements.
Chris Lattner0347cda2003-04-25 03:16:33 +0000130 TheList.swap(Suffix);
Chris Lattner9f5a1972003-04-24 20:16:29 +0000131 MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +0000132 // Reset progress treshold and progress counter
133 MaxIterations = MaxIterationsWithoutProgress;
134 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000135 break;
136 case KeepPrefix:
137 // The predicate still holds, shorten the list to the prefix elements.
138 TheList.swap(Prefix);
139 MidTop = TheList.size();
Chris Lattner8adfe922006-10-10 21:42:25 +0000140 // Reset progress treshold and progress counter
141 MaxIterations = MaxIterationsWithoutProgress;
142 NumOfIterationsWithoutProgress = 0;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000143 break;
144 case NoFailure:
145 // Otherwise the property doesn't hold. Some of the elements we removed
Misha Brukman7eb05a12003-08-18 14:43:39 +0000146 // must be necessary to maintain the property.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000147 MidTop = Mid;
Chris Lattner8adfe922006-10-10 21:42:25 +0000148 NumOfIterationsWithoutProgress++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000149 break;
150 }
151 }
152
Chris Lattner8adfe922006-10-10 21:42:25 +0000153 // Probability of backjumping from the trimming loop back to the binary
154 // split reduction loop.
155 const int BackjumpProbability = 10;
156
Chris Lattner9f5a1972003-04-24 20:16:29 +0000157 // Okay, we trimmed as much off the top and the bottom of the list as we
Justin Bogner06d466a2016-09-01 21:04:36 +0000158 // could. If there is more than two elements in the list, try deleting
Reid Spencer9d59a822006-01-08 22:40:10 +0000159 // interior elements and testing that.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000160 //
Chris Lattner7fd27e22004-11-18 19:42:50 +0000161 if (TheList.size() > 2) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000162 bool Changed = true;
163 std::vector<ElTy> EmptyList;
Tobias Grosser56eab362015-07-26 15:18:45 +0000164 unsigned TrimIterations = 0;
Justin Bogner06d466a2016-09-01 21:04:36 +0000165 while (Changed) { // Trimming loop.
Chris Lattner9f5a1972003-04-24 20:16:29 +0000166 Changed = false;
Justin Bogner06d466a2016-09-01 21:04:36 +0000167
Chris Lattner8adfe922006-10-10 21:42:25 +0000168 // If the binary split reduction loop made an unfortunate sequence of
169 // splits, the trimming loop might be left off with a huge number of
170 // remaining elements (large search space). Backjumping out of that
Justin Bogner06d466a2016-09-01 21:04:36 +0000171 // search space and attempting a different split can significantly
Chris Lattner8adfe922006-10-10 21:42:25 +0000172 // improve the convergence speed.
173 if (std::rand() % 100 < BackjumpProbability)
174 goto Backjump;
Justin Bogner06d466a2016-09-01 21:04:36 +0000175
176 for (unsigned i = 1; i < TheList.size() - 1; ++i) {
177 // Check interior elts
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000178 if (BugpointIsInterrupted) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000179 errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
Chris Lattnerbeb01fa2005-08-02 02:16:17 +0000180 return true;
181 }
Justin Bogner06d466a2016-09-01 21:04:36 +0000182
Chris Lattner9f5a1972003-04-24 20:16:29 +0000183 std::vector<ElTy> TestList(TheList);
Justin Bogner06d466a2016-09-01 21:04:36 +0000184 TestList.erase(TestList.begin() + i);
Chris Lattner9f5a1972003-04-24 20:16:29 +0000185
Justin Bogner1c039152016-09-06 17:18:22 +0000186 Expected<TestResult> Result = doTest(EmptyList, TestList);
187 if (Error E = Result.takeError())
Bill Wendlingc55cf4a2020-02-10 07:06:45 -0800188 return std::move(E);
Justin Bogner1c039152016-09-06 17:18:22 +0000189 if (*Result == KeepSuffix) {
Chris Lattner9f5a1972003-04-24 20:16:29 +0000190 // We can trim down the list!
191 TheList.swap(TestList);
Justin Bogner06d466a2016-09-01 21:04:36 +0000192 --i; // Don't skip an element of the list
Chris Lattner9f5a1972003-04-24 20:16:29 +0000193 Changed = true;
Justin Bogner46b1a9a2016-09-06 04:04:13 +0000194 }
Chris Lattner9f5a1972003-04-24 20:16:29 +0000195 }
Tobias Grosser56eab362015-07-26 15:18:45 +0000196 if (TrimIterations >= MaxTrimIterationsWithoutBackJump)
197 break;
198 TrimIterations++;
Chris Lattner9f5a1972003-04-24 20:16:29 +0000199 }
200 }
Misha Brukman87154fd2003-07-30 20:15:56 +0000201
202 return true; // there are some failure and we've narrowed them down
Chris Lattner9f5a1972003-04-24 20:16:29 +0000203 }
204};
205
Brian Gaeke960707c2003-11-11 22:41:34 +0000206} // End llvm namespace
207
Chris Lattner9f5a1972003-04-24 20:16:29 +0000208#endif