Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 1 | //===-- FindBugs.cpp - Run Many Different Optimizations -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 9 | // This file defines an interface that allows bugpoint to choose different |
| 10 | // combinations of optimizations to run on the selected input. Bugpoint will |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 11 | // run these optimizations and record the success/failure of each. This way |
| 12 | // we can hopefully spot bugs in the optimizations. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6e81c96 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 15 | |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 16 | #include "BugDriver.h" |
Rafael Espindola | 6ce17a4 | 2013-06-17 20:48:36 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 19 | #include <random> |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 22 | Error |
| 23 | BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) { |
Nick Lewycky | 8102d47 | 2008-02-14 05:01:46 +0000 | [diff] [blame] | 24 | setPassesToRun(AllPasses); |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 25 | outs() << "Starting bug finding procedure...\n\n"; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 26 | |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 27 | // Creating a reference output if necessary |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 28 | if (Error E = initializeExecutionEnvironment()) |
| 29 | return E; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 30 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 31 | outs() << "\n"; |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 32 | if (ReferenceOutputFile.empty()) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 33 | outs() << "Generating reference output from raw program: \n"; |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 34 | if (Error E = createReferenceFile(*Program)) |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 35 | return E; |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 36 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 37 | |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 38 | std::mt19937 randomness(std::random_device{}()); |
Chris Lattner | 6e81c96 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 39 | unsigned num = 1; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 40 | while (1) { |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 41 | // |
| 42 | // Step 1: Randomize the order of the optimizer passes. |
| 43 | // |
Marshall Clow | e9110d7 | 2017-02-16 14:37:03 +0000 | [diff] [blame] | 44 | std::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 45 | |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 46 | // |
| 47 | // Step 2: Run optimizer passes on the program and check for success. |
| 48 | // |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 49 | outs() << "Running selected passes on program to test for crash: "; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 50 | for (int i = 0, e = PassesToRun.size(); i != e; i++) { |
Rafael Espindola | 33e81a8 | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 51 | outs() << "-" << PassesToRun[i] << " "; |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 52 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 53 | |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 54 | std::string Filename; |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 55 | if (runPasses(*Program, PassesToRun, Filename, false)) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 56 | outs() << "\n"; |
| 57 | outs() << "Optimizer passes caused failure!\n\n"; |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 58 | return debugOptimizerCrash(); |
Chris Lattner | 6e81c96 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 59 | } else { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 60 | outs() << "Combination " << num << " optimized successfully!\n"; |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 61 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 62 | |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 63 | // |
| 64 | // Step 3: Compile the optimized code. |
| 65 | // |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 66 | outs() << "Running the code generator to test for a crash: "; |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 67 | if (Error E = compileProgram(*Program)) { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 68 | outs() << "\n*** compileProgram threw an exception: "; |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 69 | outs() << toString(std::move(E)); |
| 70 | return debugCodeGeneratorCrash(); |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 71 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 72 | outs() << '\n'; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 73 | |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 74 | // |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 75 | // Step 4: Run the program and compare its output to the reference |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 76 | // output (created above). |
| 77 | // |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 78 | outs() << "*** Checking if passes caused miscompliation:\n"; |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 79 | Expected<bool> Diff = diffProgram(*Program, Filename, "", false); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 80 | if (Error E = Diff.takeError()) { |
| 81 | errs() << toString(std::move(E)); |
| 82 | return debugCodeGeneratorCrash(); |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 83 | } |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 84 | if (*Diff) { |
| 85 | outs() << "\n*** diffProgram returned true!\n"; |
| 86 | Error E = debugMiscompilation(); |
| 87 | if (!E) |
| 88 | return Error::success(); |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 89 | } |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 90 | outs() << "\n*** diff'd output matches!\n"; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 91 | |
Rafael Espindola | 6ce17a4 | 2013-06-17 20:48:36 +0000 | [diff] [blame] | 92 | sys::fs::remove(Filename); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 93 | |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 94 | outs() << "\n\n"; |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 95 | num++; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 96 | } // end while |
| 97 | |
Chris Lattner | 6e81c96 | 2006-08-17 18:49:52 +0000 | [diff] [blame] | 98 | // Unreachable. |
Patrick Jenkins | 38a197a | 2006-08-15 16:41:52 +0000 | [diff] [blame] | 99 | } |