blob: 40502cbf949514c9087959133e8b8f8276d37d54 [file] [log] [blame]
Patrick Jenkins38a197a2006-08-15 16:41:52 +00001//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Patrick Jenkins38a197a2006-08-15 16:41:52 +00007//
8//===----------------------------------------------------------------------===//
9//
Justin Bogner8d0a0812016-09-02 01:21:37 +000010// This file defines an interface that allows bugpoint to choose different
11// combinations of optimizations to run on the selected input. Bugpoint will
Patrick Jenkins38a197a2006-08-15 16:41:52 +000012// run these optimizations and record the success/failure of each. This way
13// we can hopefully spot bugs in the optimizations.
14//
15//===----------------------------------------------------------------------===//
Chris Lattner6e81c962006-08-17 18:49:52 +000016
Patrick Jenkins38a197a2006-08-15 16:41:52 +000017#include "BugDriver.h"
Rafael Espindola6ce17a42013-06-17 20:48:36 +000018#include "llvm/Support/FileSystem.h"
Chris Lattnerc521f542009-08-23 22:45:37 +000019#include "llvm/Support/raw_ostream.h"
Marshall Clowe9110d72017-02-16 14:37:03 +000020#include <random>
Patrick Jenkins38a197a2006-08-15 16:41:52 +000021using namespace llvm;
22
Justin Bogner1c039152016-09-06 17:18:22 +000023Error
24BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {
Nick Lewycky8102d472008-02-14 05:01:46 +000025 setPassesToRun(AllPasses);
Dan Gohmanee051522009-07-16 15:30:09 +000026 outs() << "Starting bug finding procedure...\n\n";
Justin Bogner8d0a0812016-09-02 01:21:37 +000027
Patrick Jenkins38a197a2006-08-15 16:41:52 +000028 // Creating a reference output if necessary
Justin Bogner1c039152016-09-06 17:18:22 +000029 if (Error E = initializeExecutionEnvironment())
30 return E;
Justin Bogner8d0a0812016-09-02 01:21:37 +000031
Dan Gohmanee051522009-07-16 15:30:09 +000032 outs() << "\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000033 if (ReferenceOutputFile.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +000034 outs() << "Generating reference output from raw program: \n";
Justin Bogner1c039152016-09-06 17:18:22 +000035 if (Error E = createReferenceFile(Program))
36 return E;
Patrick Jenkins38a197a2006-08-15 16:41:52 +000037 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000038
Marshall Clowe9110d72017-02-16 14:37:03 +000039 std::mt19937 randomness(std::random_device{}());
Chris Lattner6e81c962006-08-17 18:49:52 +000040 unsigned num = 1;
Justin Bogner8d0a0812016-09-02 01:21:37 +000041 while (1) {
Patrick Jenkins38a197a2006-08-15 16:41:52 +000042 //
43 // Step 1: Randomize the order of the optimizer passes.
44 //
Marshall Clowe9110d72017-02-16 14:37:03 +000045 std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
Justin Bogner8d0a0812016-09-02 01:21:37 +000046
Patrick Jenkins38a197a2006-08-15 16:41:52 +000047 //
48 // Step 2: Run optimizer passes on the program and check for success.
49 //
Dan Gohmanee051522009-07-16 15:30:09 +000050 outs() << "Running selected passes on program to test for crash: ";
Justin Bogner8d0a0812016-09-02 01:21:37 +000051 for (int i = 0, e = PassesToRun.size(); i != e; i++) {
Rafael Espindola33e81a82010-08-08 03:55:08 +000052 outs() << "-" << PassesToRun[i] << " ";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000053 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000054
Patrick Jenkins38a197a2006-08-15 16:41:52 +000055 std::string Filename;
Justin Bogner8d0a0812016-09-02 01:21:37 +000056 if (runPasses(Program, PassesToRun, Filename, false)) {
Dan Gohmanee051522009-07-16 15:30:09 +000057 outs() << "\n";
58 outs() << "Optimizer passes caused failure!\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +000059 return debugOptimizerCrash();
Chris Lattner6e81c962006-08-17 18:49:52 +000060 } else {
Dan Gohmanee051522009-07-16 15:30:09 +000061 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000062 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000063
Patrick Jenkins38a197a2006-08-15 16:41:52 +000064 //
65 // Step 3: Compile the optimized code.
66 //
Dan Gohmanee051522009-07-16 15:30:09 +000067 outs() << "Running the code generator to test for a crash: ";
Justin Bogner1c039152016-09-06 17:18:22 +000068 if (Error E = compileProgram(Program)) {
Dan Gohmanee051522009-07-16 15:30:09 +000069 outs() << "\n*** compileProgram threw an exception: ";
Justin Bogner1c039152016-09-06 17:18:22 +000070 outs() << toString(std::move(E));
71 return debugCodeGeneratorCrash();
Patrick Jenkins38a197a2006-08-15 16:41:52 +000072 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +000073 outs() << '\n';
Justin Bogner8d0a0812016-09-02 01:21:37 +000074
Patrick Jenkins38a197a2006-08-15 16:41:52 +000075 //
Justin Bogner8d0a0812016-09-02 01:21:37 +000076 // Step 4: Run the program and compare its output to the reference
Patrick Jenkins38a197a2006-08-15 16:41:52 +000077 // output (created above).
78 //
Dan Gohmanee051522009-07-16 15:30:09 +000079 outs() << "*** Checking if passes caused miscompliation:\n";
Justin Bogner1c039152016-09-06 17:18:22 +000080 Expected<bool> Diff = diffProgram(Program, Filename, "", false);
81 if (Error E = Diff.takeError()) {
82 errs() << toString(std::move(E));
83 return debugCodeGeneratorCrash();
Nick Lewycky6ba630b2010-04-12 05:08:25 +000084 }
Justin Bogner1c039152016-09-06 17:18:22 +000085 if (*Diff) {
86 outs() << "\n*** diffProgram returned true!\n";
87 Error E = debugMiscompilation();
88 if (!E)
89 return Error::success();
Patrick Jenkins38a197a2006-08-15 16:41:52 +000090 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +000091 outs() << "\n*** diff'd output matches!\n";
Justin Bogner8d0a0812016-09-02 01:21:37 +000092
Rafael Espindola6ce17a42013-06-17 20:48:36 +000093 sys::fs::remove(Filename);
Justin Bogner8d0a0812016-09-02 01:21:37 +000094
Dan Gohmanee051522009-07-16 15:30:09 +000095 outs() << "\n\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000096 num++;
Justin Bogner8d0a0812016-09-02 01:21:37 +000097 } // end while
98
Chris Lattner6e81c962006-08-17 18:49:52 +000099 // Unreachable.
Patrick Jenkins38a197a2006-08-15 16:41:52 +0000100}