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