blob: 2b1146da9680d9e76b382b28449af6843fed85f5 [file] [log] [blame]
Patrick Jenkins38a197a2006-08-15 16:41:52 +00001//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Patrick Jenkins38a197a2006-08-15 16:41:52 +00006//
7//===----------------------------------------------------------------------===//
8//
Justin Bogner8d0a0812016-09-02 01:21:37 +00009// This file defines an interface that allows bugpoint to choose different
10// combinations of optimizations to run on the selected input. Bugpoint will
Patrick Jenkins38a197a2006-08-15 16:41:52 +000011// run these optimizations and record the success/failure of each. This way
12// we can hopefully spot bugs in the optimizations.
13//
14//===----------------------------------------------------------------------===//
Chris Lattner6e81c962006-08-17 18:49:52 +000015
Patrick Jenkins38a197a2006-08-15 16:41:52 +000016#include "BugDriver.h"
Rafael Espindola6ce17a42013-06-17 20:48:36 +000017#include "llvm/Support/FileSystem.h"
Chris Lattnerc521f542009-08-23 22:45:37 +000018#include "llvm/Support/raw_ostream.h"
Marshall Clowe9110d72017-02-16 14:37:03 +000019#include <random>
Patrick Jenkins38a197a2006-08-15 16:41:52 +000020using namespace llvm;
21
Justin Bogner1c039152016-09-06 17:18:22 +000022Error
23BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {
Nick Lewycky8102d472008-02-14 05:01:46 +000024 setPassesToRun(AllPasses);
Dan Gohmanee051522009-07-16 15:30:09 +000025 outs() << "Starting bug finding procedure...\n\n";
Justin Bogner8d0a0812016-09-02 01:21:37 +000026
Patrick Jenkins38a197a2006-08-15 16:41:52 +000027 // Creating a reference output if necessary
Justin Bogner1c039152016-09-06 17:18:22 +000028 if (Error E = initializeExecutionEnvironment())
29 return E;
Justin Bogner8d0a0812016-09-02 01:21:37 +000030
Dan Gohmanee051522009-07-16 15:30:09 +000031 outs() << "\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000032 if (ReferenceOutputFile.empty()) {
Dan Gohmanee051522009-07-16 15:30:09 +000033 outs() << "Generating reference output from raw program: \n";
Rafael Espindolaf6074ed2018-02-14 21:44:34 +000034 if (Error E = createReferenceFile(*Program))
Justin Bogner1c039152016-09-06 17:18:22 +000035 return E;
Patrick Jenkins38a197a2006-08-15 16:41:52 +000036 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000037
Marshall Clowe9110d72017-02-16 14:37:03 +000038 std::mt19937 randomness(std::random_device{}());
Chris Lattner6e81c962006-08-17 18:49:52 +000039 unsigned num = 1;
Justin Bogner8d0a0812016-09-02 01:21:37 +000040 while (1) {
Patrick Jenkins38a197a2006-08-15 16:41:52 +000041 //
42 // Step 1: Randomize the order of the optimizer passes.
43 //
Marshall Clowe9110d72017-02-16 14:37:03 +000044 std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
Justin Bogner8d0a0812016-09-02 01:21:37 +000045
Patrick Jenkins38a197a2006-08-15 16:41:52 +000046 //
47 // Step 2: Run optimizer passes on the program and check for success.
48 //
Dan Gohmanee051522009-07-16 15:30:09 +000049 outs() << "Running selected passes on program to test for crash: ";
Justin Bogner8d0a0812016-09-02 01:21:37 +000050 for (int i = 0, e = PassesToRun.size(); i != e; i++) {
Rafael Espindola33e81a82010-08-08 03:55:08 +000051 outs() << "-" << PassesToRun[i] << " ";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000052 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000053
Patrick Jenkins38a197a2006-08-15 16:41:52 +000054 std::string Filename;
Rafael Espindolaf6074ed2018-02-14 21:44:34 +000055 if (runPasses(*Program, PassesToRun, Filename, false)) {
Dan Gohmanee051522009-07-16 15:30:09 +000056 outs() << "\n";
57 outs() << "Optimizer passes caused failure!\n\n";
Justin Bogner1c039152016-09-06 17:18:22 +000058 return debugOptimizerCrash();
Chris Lattner6e81c962006-08-17 18:49:52 +000059 } else {
Dan Gohmanee051522009-07-16 15:30:09 +000060 outs() << "Combination " << num << " optimized successfully!\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000061 }
Justin Bogner8d0a0812016-09-02 01:21:37 +000062
Patrick Jenkins38a197a2006-08-15 16:41:52 +000063 //
64 // Step 3: Compile the optimized code.
65 //
Dan Gohmanee051522009-07-16 15:30:09 +000066 outs() << "Running the code generator to test for a crash: ";
Rafael Espindolaf6074ed2018-02-14 21:44:34 +000067 if (Error E = compileProgram(*Program)) {
Dan Gohmanee051522009-07-16 15:30:09 +000068 outs() << "\n*** compileProgram threw an exception: ";
Justin Bogner1c039152016-09-06 17:18:22 +000069 outs() << toString(std::move(E));
70 return debugCodeGeneratorCrash();
Patrick Jenkins38a197a2006-08-15 16:41:52 +000071 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +000072 outs() << '\n';
Justin Bogner8d0a0812016-09-02 01:21:37 +000073
Patrick Jenkins38a197a2006-08-15 16:41:52 +000074 //
Justin Bogner8d0a0812016-09-02 01:21:37 +000075 // Step 4: Run the program and compare its output to the reference
Patrick Jenkins38a197a2006-08-15 16:41:52 +000076 // output (created above).
77 //
Dan Gohmanee051522009-07-16 15:30:09 +000078 outs() << "*** Checking if passes caused miscompliation:\n";
Rafael Espindolaf6074ed2018-02-14 21:44:34 +000079 Expected<bool> Diff = diffProgram(*Program, Filename, "", false);
Justin Bogner1c039152016-09-06 17:18:22 +000080 if (Error E = Diff.takeError()) {
81 errs() << toString(std::move(E));
82 return debugCodeGeneratorCrash();
Nick Lewycky6ba630b2010-04-12 05:08:25 +000083 }
Justin Bogner1c039152016-09-06 17:18:22 +000084 if (*Diff) {
85 outs() << "\n*** diffProgram returned true!\n";
86 Error E = debugMiscompilation();
87 if (!E)
88 return Error::success();
Patrick Jenkins38a197a2006-08-15 16:41:52 +000089 }
Nick Lewycky6ba630b2010-04-12 05:08:25 +000090 outs() << "\n*** diff'd output matches!\n";
Justin Bogner8d0a0812016-09-02 01:21:37 +000091
Rafael Espindola6ce17a42013-06-17 20:48:36 +000092 sys::fs::remove(Filename);
Justin Bogner8d0a0812016-09-02 01:21:37 +000093
Dan Gohmanee051522009-07-16 15:30:09 +000094 outs() << "\n\n";
Patrick Jenkins38a197a2006-08-15 16:41:52 +000095 num++;
Justin Bogner8d0a0812016-09-02 01:21:37 +000096 } // end while
97
Chris Lattner6e81c962006-08-17 18:49:52 +000098 // Unreachable.
Patrick Jenkins38a197a2006-08-15 16:41:52 +000099}