2003-04-25 04:16:29 +08:00
|
|
|
//===- ListReducer.h - Trim down list while retaining property --*- C++ -*-===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-04-25 04:16:29 +08:00
|
|
|
//
|
|
|
|
// This class is to be used as a base class for operations that want to zero in
|
|
|
|
// on a subset of the input which still causes the bug we are tracking.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_TOOLS_BUGPOINT_LISTREDUCER_H
|
|
|
|
#define LLVM_TOOLS_BUGPOINT_LISTREDUCER_H
|
2003-04-25 04:16:29 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-10-11 05:42:25 +08:00
|
|
|
#include <algorithm>
|
2012-12-04 18:44:52 +08:00
|
|
|
#include <cstdlib>
|
2017-02-16 22:37:03 +08:00
|
|
|
#include <random>
|
2012-12-04 18:44:52 +08:00
|
|
|
#include <vector>
|
2003-04-25 04:16:29 +08:00
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2016-09-02 05:04:36 +08:00
|
|
|
extern bool BugpointIsInterrupted;
|
|
|
|
|
|
|
|
template <typename ElTy> struct ListReducer {
|
2003-04-25 04:16:29 +08:00
|
|
|
enum TestResult {
|
2016-09-07 01:18:22 +08:00
|
|
|
NoFailure, // No failure of the predicate was detected
|
|
|
|
KeepSuffix, // The suffix alone satisfies the predicate
|
|
|
|
KeepPrefix // The prefix alone satisfies the predicate
|
2003-04-25 04:16:29 +08:00
|
|
|
};
|
|
|
|
|
2004-05-12 04:41:07 +08:00
|
|
|
virtual ~ListReducer() {}
|
|
|
|
|
2016-09-02 05:04:36 +08:00
|
|
|
/// This virtual function should be overriden by subclasses to implement the
|
|
|
|
/// test desired. The testcase is only required to test to see if the Kept
|
|
|
|
/// list still satisfies the property, but if it is going to check the prefix
|
|
|
|
/// anyway, it can.
|
2016-09-07 01:18:22 +08:00
|
|
|
virtual Expected<TestResult> doTest(std::vector<ElTy> &Prefix,
|
|
|
|
std::vector<ElTy> &Kept) = 0;
|
2003-04-25 04:16:29 +08:00
|
|
|
|
2016-09-02 05:04:36 +08:00
|
|
|
/// This function attempts to reduce the length of the specified list while
|
|
|
|
/// still maintaining the "test" property. This is the core of the "work"
|
|
|
|
/// that bugpoint does.
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> reduceList(std::vector<ElTy> &TheList) {
|
2003-07-31 04:15:56 +08:00
|
|
|
std::vector<ElTy> empty;
|
2017-02-16 22:37:03 +08:00
|
|
|
std::mt19937 randomness(0x6e5ea738); // Seed the random number generator
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> Result = doTest(TheList, empty);
|
|
|
|
if (Error E = Result.takeError())
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2016-09-07 01:18:22 +08:00
|
|
|
switch (*Result) {
|
2003-07-31 04:15:56 +08:00
|
|
|
case KeepPrefix:
|
|
|
|
if (TheList.size() == 1) // we are done, it's the base case and it fails
|
|
|
|
return true;
|
2005-04-22 08:00:37 +08:00
|
|
|
else
|
2003-07-31 04:15:56 +08:00
|
|
|
break; // there's definitely an error, but we need to narrow it down
|
|
|
|
|
|
|
|
case KeepSuffix:
|
|
|
|
// cannot be reached!
|
2010-04-12 13:08:25 +08:00
|
|
|
llvm_unreachable("bugpoint ListReducer internal error: "
|
|
|
|
"selected empty set.");
|
2003-07-31 04:15:56 +08:00
|
|
|
|
|
|
|
case NoFailure:
|
|
|
|
return false; // there is no failure with the full set of passes/funcs!
|
|
|
|
}
|
|
|
|
|
2006-10-11 05:42:25 +08:00
|
|
|
// Maximal number of allowed splitting iterations,
|
|
|
|
// before the elements are randomly shuffled.
|
|
|
|
const unsigned MaxIterationsWithoutProgress = 3;
|
2015-07-26 23:18:45 +08:00
|
|
|
|
|
|
|
// Maximal number of allowed single-element trim iterations. We add a
|
2016-11-20 21:31:13 +08:00
|
|
|
// threshold here as single-element reductions may otherwise take a
|
2015-07-26 23:18:45 +08:00
|
|
|
// very long time to complete.
|
|
|
|
const unsigned MaxTrimIterationsWithoutBackJump = 3;
|
2006-10-11 05:42:25 +08:00
|
|
|
bool ShufflingEnabled = true;
|
|
|
|
|
2016-09-02 05:04:36 +08:00
|
|
|
Backjump:
|
2003-04-25 04:16:29 +08:00
|
|
|
unsigned MidTop = TheList.size();
|
2006-10-11 05:42:25 +08:00
|
|
|
unsigned MaxIterations = MaxIterationsWithoutProgress;
|
|
|
|
unsigned NumOfIterationsWithoutProgress = 0;
|
|
|
|
while (MidTop > 1) { // Binary split reduction loop
|
2005-08-02 10:16:17 +08:00
|
|
|
// Halt if the user presses ctrl-c.
|
|
|
|
if (BugpointIsInterrupted) {
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
|
2005-08-02 10:16:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-02-26 18:46:10 +08:00
|
|
|
|
2006-10-11 05:42:25 +08:00
|
|
|
// If the loop doesn't make satisfying progress, try shuffling.
|
|
|
|
// The purpose of shuffling is to avoid the heavy tails of the
|
|
|
|
// distribution (improving the speed of convergence).
|
2016-09-02 05:04:36 +08:00
|
|
|
if (ShufflingEnabled && NumOfIterationsWithoutProgress > MaxIterations) {
|
2008-02-26 18:46:10 +08:00
|
|
|
std::vector<ElTy> ShuffledList(TheList);
|
2017-02-16 22:37:03 +08:00
|
|
|
std::shuffle(ShuffledList.begin(), ShuffledList.end(), randomness);
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "\n\n*** Testing shuffled set...\n\n";
|
2016-09-07 01:18:22 +08:00
|
|
|
// Check that random shuffle doesn't lose the bug
|
|
|
|
Expected<TestResult> Result = doTest(ShuffledList, empty);
|
|
|
|
// TODO: Previously, this error was ignored and we treated it as if
|
|
|
|
// shuffling hid the bug. This should really either be consumeError if
|
|
|
|
// that behaviour was sensible, or we should propagate the error.
|
|
|
|
assert(!Result.takeError() && "Shuffling caused internal error?");
|
|
|
|
|
|
|
|
if (*Result == KeepPrefix) {
|
2006-10-11 05:42:25 +08:00
|
|
|
// If the bug is still here, use the shuffled list.
|
|
|
|
TheList.swap(ShuffledList);
|
|
|
|
MidTop = TheList.size();
|
2016-09-02 05:04:36 +08:00
|
|
|
// Must increase the shuffling treshold to avoid the small
|
2016-11-20 21:31:13 +08:00
|
|
|
// probability of infinite looping without making progress.
|
2006-10-11 05:42:25 +08:00
|
|
|
MaxIterations += 2;
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "\n\n*** Shuffling does not hide the bug...\n\n";
|
2008-02-26 18:46:10 +08:00
|
|
|
} else {
|
2006-10-11 05:42:25 +08:00
|
|
|
ShufflingEnabled = false; // Disable shuffling further on
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "\n\n*** Shuffling hides the bug...\n\n";
|
2008-02-26 18:46:10 +08:00
|
|
|
}
|
|
|
|
NumOfIterationsWithoutProgress = 0;
|
2006-10-11 05:42:25 +08:00
|
|
|
}
|
2016-09-02 05:04:36 +08:00
|
|
|
|
2003-04-25 04:16:29 +08:00
|
|
|
unsigned Mid = MidTop / 2;
|
2016-09-02 05:04:36 +08:00
|
|
|
std::vector<ElTy> Prefix(TheList.begin(), TheList.begin() + Mid);
|
|
|
|
std::vector<ElTy> Suffix(TheList.begin() + Mid, TheList.end());
|
2003-04-25 04:16:29 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> Result = doTest(Prefix, Suffix);
|
|
|
|
if (Error E = Result.takeError())
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2016-09-07 01:18:22 +08:00
|
|
|
switch (*Result) {
|
2003-04-25 04:16:29 +08:00
|
|
|
case KeepSuffix:
|
|
|
|
// The property still holds. We can just drop the prefix elements, and
|
|
|
|
// shorten the list to the "kept" elements.
|
2003-04-25 11:16:33 +08:00
|
|
|
TheList.swap(Suffix);
|
2003-04-25 04:16:29 +08:00
|
|
|
MidTop = TheList.size();
|
2006-10-11 05:42:25 +08:00
|
|
|
// Reset progress treshold and progress counter
|
|
|
|
MaxIterations = MaxIterationsWithoutProgress;
|
|
|
|
NumOfIterationsWithoutProgress = 0;
|
2003-04-25 04:16:29 +08:00
|
|
|
break;
|
|
|
|
case KeepPrefix:
|
|
|
|
// The predicate still holds, shorten the list to the prefix elements.
|
|
|
|
TheList.swap(Prefix);
|
|
|
|
MidTop = TheList.size();
|
2006-10-11 05:42:25 +08:00
|
|
|
// Reset progress treshold and progress counter
|
|
|
|
MaxIterations = MaxIterationsWithoutProgress;
|
|
|
|
NumOfIterationsWithoutProgress = 0;
|
2003-04-25 04:16:29 +08:00
|
|
|
break;
|
|
|
|
case NoFailure:
|
|
|
|
// Otherwise the property doesn't hold. Some of the elements we removed
|
2003-08-18 22:43:39 +08:00
|
|
|
// must be necessary to maintain the property.
|
2003-04-25 04:16:29 +08:00
|
|
|
MidTop = Mid;
|
2006-10-11 05:42:25 +08:00
|
|
|
NumOfIterationsWithoutProgress++;
|
2003-04-25 04:16:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-11 05:42:25 +08:00
|
|
|
// Probability of backjumping from the trimming loop back to the binary
|
|
|
|
// split reduction loop.
|
|
|
|
const int BackjumpProbability = 10;
|
|
|
|
|
2003-04-25 04:16:29 +08:00
|
|
|
// Okay, we trimmed as much off the top and the bottom of the list as we
|
2016-09-02 05:04:36 +08:00
|
|
|
// could. If there is more than two elements in the list, try deleting
|
2006-01-09 06:40:10 +08:00
|
|
|
// interior elements and testing that.
|
2003-04-25 04:16:29 +08:00
|
|
|
//
|
2004-11-19 03:42:50 +08:00
|
|
|
if (TheList.size() > 2) {
|
2003-04-25 04:16:29 +08:00
|
|
|
bool Changed = true;
|
|
|
|
std::vector<ElTy> EmptyList;
|
2015-07-26 23:18:45 +08:00
|
|
|
unsigned TrimIterations = 0;
|
2016-09-02 05:04:36 +08:00
|
|
|
while (Changed) { // Trimming loop.
|
2003-04-25 04:16:29 +08:00
|
|
|
Changed = false;
|
2016-09-02 05:04:36 +08:00
|
|
|
|
2006-10-11 05:42:25 +08:00
|
|
|
// If the binary split reduction loop made an unfortunate sequence of
|
|
|
|
// splits, the trimming loop might be left off with a huge number of
|
|
|
|
// remaining elements (large search space). Backjumping out of that
|
2016-09-02 05:04:36 +08:00
|
|
|
// search space and attempting a different split can significantly
|
2006-10-11 05:42:25 +08:00
|
|
|
// improve the convergence speed.
|
|
|
|
if (std::rand() % 100 < BackjumpProbability)
|
|
|
|
goto Backjump;
|
2016-09-02 05:04:36 +08:00
|
|
|
|
|
|
|
for (unsigned i = 1; i < TheList.size() - 1; ++i) {
|
|
|
|
// Check interior elts
|
2005-08-02 10:16:17 +08:00
|
|
|
if (BugpointIsInterrupted) {
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "\n\n*** Reduction Interrupted, cleaning up...\n\n";
|
2005-08-02 10:16:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-02 05:04:36 +08:00
|
|
|
|
2003-04-25 04:16:29 +08:00
|
|
|
std::vector<ElTy> TestList(TheList);
|
2016-09-02 05:04:36 +08:00
|
|
|
TestList.erase(TestList.begin() + i);
|
2003-04-25 04:16:29 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<TestResult> Result = doTest(EmptyList, TestList);
|
|
|
|
if (Error E = Result.takeError())
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2016-09-07 01:18:22 +08:00
|
|
|
if (*Result == KeepSuffix) {
|
2003-04-25 04:16:29 +08:00
|
|
|
// We can trim down the list!
|
|
|
|
TheList.swap(TestList);
|
2016-09-02 05:04:36 +08:00
|
|
|
--i; // Don't skip an element of the list
|
2003-04-25 04:16:29 +08:00
|
|
|
Changed = true;
|
2016-09-06 12:04:13 +08:00
|
|
|
}
|
2003-04-25 04:16:29 +08:00
|
|
|
}
|
2015-07-26 23:18:45 +08:00
|
|
|
if (TrimIterations >= MaxTrimIterationsWithoutBackJump)
|
|
|
|
break;
|
|
|
|
TrimIterations++;
|
2003-04-25 04:16:29 +08:00
|
|
|
}
|
|
|
|
}
|
2003-07-31 04:15:56 +08:00
|
|
|
|
|
|
|
return true; // there are some failure and we've narrowed them down
|
2003-04-25 04:16:29 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2003-04-25 04:16:29 +08:00
|
|
|
#endif
|