Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 1 | //===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 9 | // |
| 10 | // This class is to be used as a base class for operations that want to zero in |
| 11 | // on a subset of the input which still causes the bug we are tracking. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_TOOLS_BUGPOINT_LISTREDUCER_H |
| 16 | #define LLVM_TOOLS_BUGPOINT_LISTREDUCER_H |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 17 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Error.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 21 | #include <cstdlib> |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 22 | #include <random> |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 23 | #include <vector> |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 24 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 27 | extern bool BugpointIsInterrupted; |
| 28 | |
| 29 | template <typename ElTy> struct ListReducer { |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 30 | enum TestResult { |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 31 | NoFailure, // No failure of the predicate was detected |
| 32 | KeepSuffix, // The suffix alone satisfies the predicate |
| 33 | KeepPrefix // The prefix alone satisfies the predicate |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Chris Lattner | d7d574d | 2004-05-11 20:41:07 +0000 | [diff] [blame] | 36 | virtual ~ListReducer() {} |
| 37 | |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 38 | /// This virtual function should be overriden by subclasses to implement the |
| 39 | /// test desired. The testcase is only required to test to see if the Kept |
| 40 | /// list still satisfies the property, but if it is going to check the prefix |
| 41 | /// anyway, it can. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 42 | virtual Expected<TestResult> doTest(std::vector<ElTy> &Prefix, |
| 43 | std::vector<ElTy> &Kept) = 0; |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 44 | |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 45 | /// This function attempts to reduce the length of the specified list while |
| 46 | /// still maintaining the "test" property. This is the core of the "work" |
| 47 | /// that bugpoint does. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 48 | Expected<bool> reduceList(std::vector<ElTy> &TheList) { |
Misha Brukman | 87154fd | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 49 | std::vector<ElTy> empty; |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 50 | std::mt19937 randomness(0x6e5ea738); // Seed the random number generator |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 51 | Expected<TestResult> Result = doTest(TheList, empty); |
| 52 | if (Error E = Result.takeError()) |
| 53 | return std::move(E); |
| 54 | switch (*Result) { |
Misha Brukman | 87154fd | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 55 | case KeepPrefix: |
| 56 | if (TheList.size() == 1) // we are done, it's the base case and it fails |
| 57 | return true; |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 58 | else |
Misha Brukman | 87154fd | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 59 | break; // there's definitely an error, but we need to narrow it down |
| 60 | |
| 61 | case KeepSuffix: |
| 62 | // cannot be reached! |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 63 | llvm_unreachable("bugpoint ListReducer internal error: " |
| 64 | "selected empty set."); |
Misha Brukman | 87154fd | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 65 | |
| 66 | case NoFailure: |
| 67 | return false; // there is no failure with the full set of passes/funcs! |
| 68 | } |
| 69 | |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 70 | // Maximal number of allowed splitting iterations, |
| 71 | // before the elements are randomly shuffled. |
| 72 | const unsigned MaxIterationsWithoutProgress = 3; |
Tobias Grosser | 56eab36 | 2015-07-26 15:18:45 +0000 | [diff] [blame] | 73 | |
| 74 | // Maximal number of allowed single-element trim iterations. We add a |
Simon Pilgrim | dae11f7 | 2016-11-20 13:31:13 +0000 | [diff] [blame] | 75 | // threshold here as single-element reductions may otherwise take a |
Tobias Grosser | 56eab36 | 2015-07-26 15:18:45 +0000 | [diff] [blame] | 76 | // very long time to complete. |
| 77 | const unsigned MaxTrimIterationsWithoutBackJump = 3; |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 78 | bool ShufflingEnabled = true; |
| 79 | |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 80 | Backjump: |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 81 | unsigned MidTop = TheList.size(); |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 82 | unsigned MaxIterations = MaxIterationsWithoutProgress; |
| 83 | unsigned NumOfIterationsWithoutProgress = 0; |
| 84 | while (MidTop > 1) { // Binary split reduction loop |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 85 | // Halt if the user presses ctrl-c. |
| 86 | if (BugpointIsInterrupted) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 87 | errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 88 | return true; |
| 89 | } |
Bill Wendling | 8ff4320 | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 91 | // If the loop doesn't make satisfying progress, try shuffling. |
| 92 | // The purpose of shuffling is to avoid the heavy tails of the |
| 93 | // distribution (improving the speed of convergence). |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 94 | if (ShufflingEnabled && NumOfIterationsWithoutProgress > MaxIterations) { |
Bill Wendling | 8ff4320 | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 95 | std::vector<ElTy> ShuffledList(TheList); |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 96 | std::shuffle(ShuffledList.begin(), ShuffledList.end(), randomness); |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 97 | errs() << "\n\n*** Testing shuffled set...\n\n"; |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 98 | // Check that random shuffle doesn't lose the bug |
| 99 | Expected<TestResult> Result = doTest(ShuffledList, empty); |
| 100 | // TODO: Previously, this error was ignored and we treated it as if |
| 101 | // shuffling hid the bug. This should really either be consumeError if |
| 102 | // that behaviour was sensible, or we should propagate the error. |
| 103 | assert(!Result.takeError() && "Shuffling caused internal error?"); |
| 104 | |
| 105 | if (*Result == KeepPrefix) { |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 106 | // If the bug is still here, use the shuffled list. |
| 107 | TheList.swap(ShuffledList); |
| 108 | MidTop = TheList.size(); |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 109 | // Must increase the shuffling treshold to avoid the small |
Simon Pilgrim | dae11f7 | 2016-11-20 13:31:13 +0000 | [diff] [blame] | 110 | // probability of infinite looping without making progress. |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 111 | MaxIterations += 2; |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 112 | errs() << "\n\n*** Shuffling does not hide the bug...\n\n"; |
Bill Wendling | 8ff4320 | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 113 | } else { |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 114 | ShufflingEnabled = false; // Disable shuffling further on |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 115 | errs() << "\n\n*** Shuffling hides the bug...\n\n"; |
Bill Wendling | 8ff4320 | 2008-02-26 10:46:10 +0000 | [diff] [blame] | 116 | } |
| 117 | NumOfIterationsWithoutProgress = 0; |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 118 | } |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 120 | unsigned Mid = MidTop / 2; |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 121 | std::vector<ElTy> Prefix(TheList.begin(), TheList.begin() + Mid); |
| 122 | std::vector<ElTy> Suffix(TheList.begin() + Mid, TheList.end()); |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 123 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 124 | Expected<TestResult> Result = doTest(Prefix, Suffix); |
| 125 | if (Error E = Result.takeError()) |
| 126 | return std::move(E); |
| 127 | switch (*Result) { |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 128 | case KeepSuffix: |
| 129 | // The property still holds. We can just drop the prefix elements, and |
| 130 | // shorten the list to the "kept" elements. |
Chris Lattner | 0347cda | 2003-04-25 03:16:33 +0000 | [diff] [blame] | 131 | TheList.swap(Suffix); |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 132 | MidTop = TheList.size(); |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 133 | // Reset progress treshold and progress counter |
| 134 | MaxIterations = MaxIterationsWithoutProgress; |
| 135 | NumOfIterationsWithoutProgress = 0; |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 136 | break; |
| 137 | case KeepPrefix: |
| 138 | // The predicate still holds, shorten the list to the prefix elements. |
| 139 | TheList.swap(Prefix); |
| 140 | MidTop = TheList.size(); |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 141 | // Reset progress treshold and progress counter |
| 142 | MaxIterations = MaxIterationsWithoutProgress; |
| 143 | NumOfIterationsWithoutProgress = 0; |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 144 | break; |
| 145 | case NoFailure: |
| 146 | // Otherwise the property doesn't hold. Some of the elements we removed |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 147 | // must be necessary to maintain the property. |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 148 | MidTop = Mid; |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 149 | NumOfIterationsWithoutProgress++; |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 154 | // Probability of backjumping from the trimming loop back to the binary |
| 155 | // split reduction loop. |
| 156 | const int BackjumpProbability = 10; |
| 157 | |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 158 | // Okay, we trimmed as much off the top and the bottom of the list as we |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 159 | // could. If there is more than two elements in the list, try deleting |
Reid Spencer | 9d59a82 | 2006-01-08 22:40:10 +0000 | [diff] [blame] | 160 | // interior elements and testing that. |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 161 | // |
Chris Lattner | 7fd27e2 | 2004-11-18 19:42:50 +0000 | [diff] [blame] | 162 | if (TheList.size() > 2) { |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 163 | bool Changed = true; |
| 164 | std::vector<ElTy> EmptyList; |
Tobias Grosser | 56eab36 | 2015-07-26 15:18:45 +0000 | [diff] [blame] | 165 | unsigned TrimIterations = 0; |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 166 | while (Changed) { // Trimming loop. |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 167 | Changed = false; |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 169 | // If the binary split reduction loop made an unfortunate sequence of |
| 170 | // splits, the trimming loop might be left off with a huge number of |
| 171 | // remaining elements (large search space). Backjumping out of that |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 172 | // search space and attempting a different split can significantly |
Chris Lattner | 8adfe92 | 2006-10-10 21:42:25 +0000 | [diff] [blame] | 173 | // improve the convergence speed. |
| 174 | if (std::rand() % 100 < BackjumpProbability) |
| 175 | goto Backjump; |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 176 | |
| 177 | for (unsigned i = 1; i < TheList.size() - 1; ++i) { |
| 178 | // Check interior elts |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 179 | if (BugpointIsInterrupted) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 180 | errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n"; |
Chris Lattner | beb01fa | 2005-08-02 02:16:17 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 184 | std::vector<ElTy> TestList(TheList); |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 185 | TestList.erase(TestList.begin() + i); |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 186 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 187 | Expected<TestResult> Result = doTest(EmptyList, TestList); |
| 188 | if (Error E = Result.takeError()) |
| 189 | return std::move(E); |
| 190 | if (*Result == KeepSuffix) { |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 191 | // We can trim down the list! |
| 192 | TheList.swap(TestList); |
Justin Bogner | 06d466a | 2016-09-01 21:04:36 +0000 | [diff] [blame] | 193 | --i; // Don't skip an element of the list |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 194 | Changed = true; |
Justin Bogner | 46b1a9a | 2016-09-06 04:04:13 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 196 | } |
Tobias Grosser | 56eab36 | 2015-07-26 15:18:45 +0000 | [diff] [blame] | 197 | if (TrimIterations >= MaxTrimIterationsWithoutBackJump) |
| 198 | break; |
| 199 | TrimIterations++; |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Misha Brukman | 87154fd | 2003-07-30 20:15:56 +0000 | [diff] [blame] | 202 | |
| 203 | return true; // there are some failure and we've narrowed them down |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 204 | } |
| 205 | }; |
| 206 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 207 | } // End llvm namespace |
| 208 | |
Chris Lattner | 9f5a197 | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 209 | #endif |