blob: a695e875b787f03279eb84dafe8ca6eec609a5f2 [file] [log] [blame]
Patrick Jenkins032091d2006-08-15 16:41:52 +00001//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Jenkins032091d2006-08-15 16:41:52 +00007//
8//===----------------------------------------------------------------------===//
9//
Justin Bogner388e8b92016-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 Jenkins032091d2006-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 Lattner7f275702006-08-17 18:49:52 +000016
Patrick Jenkins032091d2006-08-15 16:41:52 +000017#include "BugDriver.h"
Rafael Espindolacacfeed2013-06-17 20:48:36 +000018#include "llvm/Support/FileSystem.h"
Chris Lattner74382b72009-08-23 22:45:37 +000019#include "llvm/Support/raw_ostream.h"
Marshall Clowbdbb2952017-02-16 14:37:03 +000020#include <random>
Patrick Jenkins032091d2006-08-15 16:41:52 +000021using namespace llvm;
22
Justin Bognerd8090ae2016-09-06 17:18:22 +000023Error
24BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {
Nick Lewycky1d1ef142008-02-14 05:01:46 +000025 setPassesToRun(AllPasses);
Dan Gohmanac95cc72009-07-16 15:30:09 +000026 outs() << "Starting bug finding procedure...\n\n";
Justin Bogner388e8b92016-09-02 01:21:37 +000027
Patrick Jenkins032091d2006-08-15 16:41:52 +000028 // Creating a reference output if necessary
Justin Bognerd8090ae2016-09-06 17:18:22 +000029 if (Error E = initializeExecutionEnvironment())
30 return E;
Justin Bogner388e8b92016-09-02 01:21:37 +000031
Dan Gohmanac95cc72009-07-16 15:30:09 +000032 outs() << "\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000033 if (ReferenceOutputFile.empty()) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000034 outs() << "Generating reference output from raw program: \n";
Rafael Espindolacdac12c2018-02-14 21:44:34 +000035 if (Error E = createReferenceFile(*Program))
Justin Bognerd8090ae2016-09-06 17:18:22 +000036 return E;
Patrick Jenkins032091d2006-08-15 16:41:52 +000037 }
Justin Bogner388e8b92016-09-02 01:21:37 +000038
Marshall Clowbdbb2952017-02-16 14:37:03 +000039 std::mt19937 randomness(std::random_device{}());
Chris Lattner7f275702006-08-17 18:49:52 +000040 unsigned num = 1;
Justin Bogner388e8b92016-09-02 01:21:37 +000041 while (1) {
Patrick Jenkins032091d2006-08-15 16:41:52 +000042 //
43 // Step 1: Randomize the order of the optimizer passes.
44 //
Marshall Clowbdbb2952017-02-16 14:37:03 +000045 std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
Justin Bogner388e8b92016-09-02 01:21:37 +000046
Patrick Jenkins032091d2006-08-15 16:41:52 +000047 //
48 // Step 2: Run optimizer passes on the program and check for success.
49 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000050 outs() << "Running selected passes on program to test for crash: ";
Justin Bogner388e8b92016-09-02 01:21:37 +000051 for (int i = 0, e = PassesToRun.size(); i != e; i++) {
Rafael Espindola8261dfe2010-08-08 03:55:08 +000052 outs() << "-" << PassesToRun[i] << " ";
Patrick Jenkins032091d2006-08-15 16:41:52 +000053 }
Justin Bogner388e8b92016-09-02 01:21:37 +000054
Patrick Jenkins032091d2006-08-15 16:41:52 +000055 std::string Filename;
Rafael Espindolacdac12c2018-02-14 21:44:34 +000056 if (runPasses(*Program, PassesToRun, Filename, false)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000057 outs() << "\n";
58 outs() << "Optimizer passes caused failure!\n\n";
Justin Bognerd8090ae2016-09-06 17:18:22 +000059 return debugOptimizerCrash();
Chris Lattner7f275702006-08-17 18:49:52 +000060 } else {
Dan Gohmanac95cc72009-07-16 15:30:09 +000061 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000062 }
Justin Bogner388e8b92016-09-02 01:21:37 +000063
Patrick Jenkins032091d2006-08-15 16:41:52 +000064 //
65 // Step 3: Compile the optimized code.
66 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000067 outs() << "Running the code generator to test for a crash: ";
Rafael Espindolacdac12c2018-02-14 21:44:34 +000068 if (Error E = compileProgram(*Program)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +000069 outs() << "\n*** compileProgram threw an exception: ";
Justin Bognerd8090ae2016-09-06 17:18:22 +000070 outs() << toString(std::move(E));
71 return debugCodeGeneratorCrash();
Patrick Jenkins032091d2006-08-15 16:41:52 +000072 }
Nick Lewycky22ff7482010-04-12 05:08:25 +000073 outs() << '\n';
Justin Bogner388e8b92016-09-02 01:21:37 +000074
Patrick Jenkins032091d2006-08-15 16:41:52 +000075 //
Justin Bogner388e8b92016-09-02 01:21:37 +000076 // Step 4: Run the program and compare its output to the reference
Patrick Jenkins032091d2006-08-15 16:41:52 +000077 // output (created above).
78 //
Dan Gohmanac95cc72009-07-16 15:30:09 +000079 outs() << "*** Checking if passes caused miscompliation:\n";
Rafael Espindolacdac12c2018-02-14 21:44:34 +000080 Expected<bool> Diff = diffProgram(*Program, Filename, "", false);
Justin Bognerd8090ae2016-09-06 17:18:22 +000081 if (Error E = Diff.takeError()) {
82 errs() << toString(std::move(E));
83 return debugCodeGeneratorCrash();
Nick Lewycky22ff7482010-04-12 05:08:25 +000084 }
Justin Bognerd8090ae2016-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 Jenkins032091d2006-08-15 16:41:52 +000090 }
Nick Lewycky22ff7482010-04-12 05:08:25 +000091 outs() << "\n*** diff'd output matches!\n";
Justin Bogner388e8b92016-09-02 01:21:37 +000092
Rafael Espindolacacfeed2013-06-17 20:48:36 +000093 sys::fs::remove(Filename);
Justin Bogner388e8b92016-09-02 01:21:37 +000094
Dan Gohmanac95cc72009-07-16 15:30:09 +000095 outs() << "\n\n";
Patrick Jenkins032091d2006-08-15 16:41:52 +000096 num++;
Justin Bogner388e8b92016-09-02 01:21:37 +000097 } // end while
98
Chris Lattner7f275702006-08-17 18:49:52 +000099 // Unreachable.
Patrick Jenkins032091d2006-08-15 16:41:52 +0000100}