1 | //===-- FindBugs.cpp - Run Many Different Optimizations -------------------===// |
---|---|
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines an interface that allows bugpoint to choose different |
10 | // combinations of optimizations to run on the selected input. Bugpoint will |
11 | // run these optimizations and record the success/failure of each. This way |
12 | // we can hopefully spot bugs in the optimizations. |
13 | // |
14 | //===----------------------------------------------------------------------===// |
15 | |
16 | #include "BugDriver.h" |
17 | #include "llvm/Support/FileSystem.h" |
18 | #include "llvm/Support/raw_ostream.h" |
19 | #include <random> |
20 | using namespace llvm; |
21 | |
22 | Error |
23 | BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) { |
24 | setPassesToRun(AllPasses); |
25 | outs() << "Starting bug finding procedure...\n\n"; |
26 | |
27 | // Creating a reference output if necessary |
28 | if (Error E = initializeExecutionEnvironment()) |
29 | return E; |
30 | |
31 | outs() << "\n"; |
32 | if (ReferenceOutputFile.empty()) { |
33 | outs() << "Generating reference output from raw program: \n"; |
34 | if (Error E = createReferenceFile(M&: *Program)) |
35 | return E; |
36 | } |
37 | |
38 | std::mt19937 randomness(std::random_device{}()); |
39 | unsigned num = 1; |
40 | while (true) { |
41 | // |
42 | // Step 1: Randomize the order of the optimizer passes. |
43 | // |
44 | llvm::shuffle(first: PassesToRun.begin(), last: PassesToRun.end(), g&: randomness); |
45 | |
46 | // |
47 | // Step 2: Run optimizer passes on the program and check for success. |
48 | // |
49 | outs() << "Running selected passes on program to test for crash: "; |
50 | for (const std::string &Pass : PassesToRun) |
51 | outs() << "-"<< Pass << " "; |
52 | |
53 | std::string Filename; |
54 | if (runPasses(Program&: *Program, PassesToRun, OutputFilename&: Filename, DeleteOutput: false)) { |
55 | outs() << "\n"; |
56 | outs() << "Optimizer passes caused failure!\n\n"; |
57 | return debugOptimizerCrash(); |
58 | } else { |
59 | outs() << "Combination "<< num << " optimized successfully!\n"; |
60 | } |
61 | |
62 | // |
63 | // Step 3: Compile the optimized code. |
64 | // |
65 | outs() << "Running the code generator to test for a crash: "; |
66 | if (Error E = compileProgram(M&: *Program)) { |
67 | outs() << "\n*** compileProgram threw an exception: "; |
68 | outs() << toString(E: std::move(E)); |
69 | return debugCodeGeneratorCrash(); |
70 | } |
71 | outs() << '\n'; |
72 | |
73 | // |
74 | // Step 4: Run the program and compare its output to the reference |
75 | // output (created above). |
76 | // |
77 | outs() << "*** Checking if passes caused miscompliation:\n"; |
78 | Expected<bool> Diff = diffProgram(Program: *Program, BitcodeFile: Filename, SharedObj: "", RemoveBitcode: false); |
79 | if (Error E = Diff.takeError()) { |
80 | errs() << toString(E: std::move(E)); |
81 | return debugCodeGeneratorCrash(); |
82 | } |
83 | if (*Diff) { |
84 | outs() << "\n*** diffProgram returned true!\n"; |
85 | Error E = debugMiscompilation(); |
86 | if (!E) |
87 | return Error::success(); |
88 | } |
89 | outs() << "\n*** diff'd output matches!\n"; |
90 | |
91 | sys::fs::remove(path: Filename); |
92 | |
93 | outs() << "\n\n"; |
94 | num++; |
95 | } // end while |
96 | |
97 | // Unreachable. |
98 | } |
99 |