2006-08-16 00:41:52 +08:00
|
|
|
//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-16 00:41:52 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-09-02 09:21:37 +08:00
|
|
|
// This file defines an interface that allows bugpoint to choose different
|
|
|
|
// combinations of optimizations to run on the selected input. Bugpoint will
|
2006-08-16 00:41:52 +08:00
|
|
|
// run these optimizations and record the success/failure of each. This way
|
|
|
|
// we can hopefully spot bugs in the optimizations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-08-18 02:49:52 +08:00
|
|
|
|
2006-08-16 00:41:52 +08:00
|
|
|
#include "BugDriver.h"
|
2013-06-18 04:48:36 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2009-08-24 06:45:37 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-16 22:37:03 +08:00
|
|
|
#include <random>
|
2006-08-16 00:41:52 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Error
|
|
|
|
BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {
|
2008-02-14 13:01:46 +08:00
|
|
|
setPassesToRun(AllPasses);
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Starting bug finding procedure...\n\n";
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2006-08-16 00:41:52 +08:00
|
|
|
// Creating a reference output if necessary
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Error E = initializeExecutionEnvironment())
|
|
|
|
return E;
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n";
|
2006-08-16 00:41:52 +08:00
|
|
|
if (ReferenceOutputFile.empty()) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Generating reference output from raw program: \n";
|
2018-02-15 05:44:34 +08:00
|
|
|
if (Error E = createReferenceFile(*Program))
|
2016-09-07 01:18:22 +08:00
|
|
|
return E;
|
2006-08-16 00:41:52 +08:00
|
|
|
}
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2017-02-16 22:37:03 +08:00
|
|
|
std::mt19937 randomness(std::random_device{}());
|
2006-08-18 02:49:52 +08:00
|
|
|
unsigned num = 1;
|
2016-09-02 09:21:37 +08:00
|
|
|
while (1) {
|
2006-08-16 00:41:52 +08:00
|
|
|
//
|
|
|
|
// Step 1: Randomize the order of the optimizer passes.
|
|
|
|
//
|
2017-02-16 22:37:03 +08:00
|
|
|
std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2006-08-16 00:41:52 +08:00
|
|
|
//
|
|
|
|
// Step 2: Run optimizer passes on the program and check for success.
|
|
|
|
//
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Running selected passes on program to test for crash: ";
|
2016-09-02 09:21:37 +08:00
|
|
|
for (int i = 0, e = PassesToRun.size(); i != e; i++) {
|
2010-08-08 11:55:08 +08:00
|
|
|
outs() << "-" << PassesToRun[i] << " ";
|
2006-08-16 00:41:52 +08:00
|
|
|
}
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2006-08-16 00:41:52 +08:00
|
|
|
std::string Filename;
|
2018-02-15 05:44:34 +08:00
|
|
|
if (runPasses(*Program, PassesToRun, Filename, false)) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n";
|
|
|
|
outs() << "Optimizer passes caused failure!\n\n";
|
2016-09-07 01:18:22 +08:00
|
|
|
return debugOptimizerCrash();
|
2006-08-18 02:49:52 +08:00
|
|
|
} else {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Combination " << num << " optimized successfully!\n";
|
2006-08-16 00:41:52 +08:00
|
|
|
}
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2006-08-16 00:41:52 +08:00
|
|
|
//
|
|
|
|
// Step 3: Compile the optimized code.
|
|
|
|
//
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Running the code generator to test for a crash: ";
|
2018-02-15 05:44:34 +08:00
|
|
|
if (Error E = compileProgram(*Program)) {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n*** compileProgram threw an exception: ";
|
2016-09-07 01:18:22 +08:00
|
|
|
outs() << toString(std::move(E));
|
|
|
|
return debugCodeGeneratorCrash();
|
2006-08-16 00:41:52 +08:00
|
|
|
}
|
2010-04-12 13:08:25 +08:00
|
|
|
outs() << '\n';
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2006-08-16 00:41:52 +08:00
|
|
|
//
|
2016-09-02 09:21:37 +08:00
|
|
|
// Step 4: Run the program and compare its output to the reference
|
2006-08-16 00:41:52 +08:00
|
|
|
// output (created above).
|
|
|
|
//
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "*** Checking if passes caused miscompliation:\n";
|
2018-02-15 05:44:34 +08:00
|
|
|
Expected<bool> Diff = diffProgram(*Program, Filename, "", false);
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Error E = Diff.takeError()) {
|
|
|
|
errs() << toString(std::move(E));
|
|
|
|
return debugCodeGeneratorCrash();
|
2010-04-12 13:08:25 +08:00
|
|
|
}
|
2016-09-07 01:18:22 +08:00
|
|
|
if (*Diff) {
|
|
|
|
outs() << "\n*** diffProgram returned true!\n";
|
|
|
|
Error E = debugMiscompilation();
|
|
|
|
if (!E)
|
|
|
|
return Error::success();
|
2006-08-16 00:41:52 +08:00
|
|
|
}
|
2010-04-12 13:08:25 +08:00
|
|
|
outs() << "\n*** diff'd output matches!\n";
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2013-06-18 04:48:36 +08:00
|
|
|
sys::fs::remove(Filename);
|
2016-09-02 09:21:37 +08:00
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n\n";
|
2006-08-16 00:41:52 +08:00
|
|
|
num++;
|
2016-09-02 09:21:37 +08:00
|
|
|
} // end while
|
|
|
|
|
2006-08-18 02:49:52 +08:00
|
|
|
// Unreachable.
|
2006-08-16 00:41:52 +08:00
|
|
|
}
|