Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- Miscompilation.cpp - Debug program miscompilations -----------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 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. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 10 | // This file implements optimizer and code generation miscompilation debugging |
| 11 | // support. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "BugDriver.h" |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame] | 16 | #include "ListReducer.h" |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 17 | #include "ToolRunner.h" |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 18 | #include "llvm/Config/config.h" // for HAVE_LINK_R |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DerivedTypes.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/Module.h" |
Chandler Carruth | 56e1394 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 8a67f12 | 2014-03-06 03:42:23 +0000 | [diff] [blame] | 24 | #include "llvm/Linker/Linker.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/FileUtilities.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/Cloning.h" |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 29 | |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 31 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 32 | namespace llvm { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 33 | extern cl::opt<std::string> OutputPrefix; |
| 34 | extern cl::list<std::string> InputArgv; |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 35 | } // end namespace llvm |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 36 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 37 | namespace { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 38 | static llvm::cl::opt<bool> DisableLoopExtraction( |
| 39 | "disable-loop-extraction", |
| 40 | cl::desc("Don't extract loops when searching for miscompilations"), |
| 41 | cl::init(false)); |
| 42 | static llvm::cl::opt<bool> DisableBlockExtraction( |
| 43 | "disable-block-extraction", |
| 44 | cl::desc("Don't extract blocks when searching for miscompilations"), |
| 45 | cl::init(false)); |
Reid Spencer | dc31a8a | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 46 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 47 | class ReduceMiscompilingPasses : public ListReducer<std::string> { |
| 48 | BugDriver &BD; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 49 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 50 | public: |
Justin Bogner | 79a93a6 | 2016-09-06 04:45:37 +0000 | [diff] [blame] | 51 | ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 52 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 53 | Expected<TestResult> doTest(std::vector<std::string> &Prefix, |
| 54 | std::vector<std::string> &Suffix) override; |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 55 | }; |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 56 | } // end anonymous namespace |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 57 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 58 | /// TestResult - After passes have been split into a test group and a control |
| 59 | /// group, see if they still break the program. |
| 60 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 61 | Expected<ReduceMiscompilingPasses::TestResult> |
Rafael Espindola | 8261dfe | 2010-08-08 03:55:08 +0000 | [diff] [blame] | 62 | ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix, |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 63 | std::vector<std::string> &Suffix) { |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 64 | // First, run the program with just the Suffix passes. If it is still broken |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 65 | // with JUST the kept passes, discard the prefix passes. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 66 | outs() << "Checking to see if '" << getPassesString(Suffix) |
| 67 | << "' compiles correctly: "; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 68 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 69 | std::string BitcodeResult; |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 70 | if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false /*delete*/, |
| 71 | true /*quiet*/)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 72 | errs() << " Error running this sequence of passes" |
| 73 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 74 | BD.setPassesToRun(Suffix); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 75 | BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 76 | // TODO: This should propagate the error instead of exiting. |
| 77 | if (Error E = BD.debugOptimizerCrash()) |
| 78 | exit(1); |
| 79 | exit(0); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 80 | } |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 82 | // Check to see if the finished program matches the reference output... |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 83 | Expected<bool> Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", |
| 84 | true /*delete bitcode*/); |
| 85 | if (Error E = Diff.takeError()) |
| 86 | return std::move(E); |
| 87 | if (*Diff) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 88 | outs() << " nope.\n"; |
Chris Lattner | 59615f0 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 89 | if (Suffix.empty()) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 90 | errs() << BD.getToolName() << ": I'm confused: the test fails when " |
| 91 | << "no passes are run, nondeterministic program?\n"; |
Chris Lattner | 59615f0 | 2005-01-15 00:07:19 +0000 | [diff] [blame] | 92 | exit(1); |
| 93 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 94 | return KeepSuffix; // Miscompilation detected! |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 95 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 96 | outs() << " yup.\n"; // No miscompilation! |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 97 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 98 | if (Prefix.empty()) |
| 99 | return NoFailure; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 101 | // Next, see if the program is broken if we run the "prefix" passes first, |
Misha Brukman | bc0e998 | 2003-07-14 17:20:40 +0000 | [diff] [blame] | 102 | // then separately run the "kept" passes. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 103 | outs() << "Checking to see if '" << getPassesString(Prefix) |
| 104 | << "' compiles correctly: "; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 105 | |
| 106 | // If it is not broken with the kept passes, it's possible that the prefix |
| 107 | // passes must be run before the kept passes to break it. If the program |
| 108 | // WORKS after the prefix passes, but then fails if running the prefix AND |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 109 | // kept passes, we can update our bitcode file to include the result of the |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 110 | // prefix passes, then discard the prefix passes. |
| 111 | // |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 112 | if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false /*delete*/, |
| 113 | true /*quiet*/)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 114 | errs() << " Error running this sequence of passes" |
| 115 | << " on the input program!\n"; |
Chris Lattner | 9c6cfe1 | 2003-10-17 23:03:16 +0000 | [diff] [blame] | 116 | BD.setPassesToRun(Prefix); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 117 | BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 118 | // TODO: This should propagate the error instead of exiting. |
| 119 | if (Error E = BD.debugOptimizerCrash()) |
| 120 | exit(1); |
| 121 | exit(0); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // If the prefix maintains the predicate by itself, only keep the prefix! |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 125 | Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false); |
| 126 | if (Error E = Diff.takeError()) |
| 127 | return std::move(E); |
| 128 | if (*Diff) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 129 | outs() << " nope.\n"; |
Rafael Espindola | 74aec35 | 2013-06-17 21:50:28 +0000 | [diff] [blame] | 130 | sys::fs::remove(BitcodeResult); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 131 | return KeepPrefix; |
| 132 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 133 | outs() << " yup.\n"; // No miscompilation! |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 134 | |
| 135 | // Ok, so now we know that the prefix passes work, try running the suffix |
| 136 | // passes on the result of the prefix passes. |
| 137 | // |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 138 | std::unique_ptr<Module> PrefixOutput = |
| 139 | parseInputFile(BitcodeResult, BD.getContext()); |
David Blaikie | 453f4f0 | 2013-05-15 07:36:59 +0000 | [diff] [blame] | 140 | if (!PrefixOutput) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 141 | errs() << BD.getToolName() << ": Error reading bitcode file '" |
| 142 | << BitcodeResult << "'!\n"; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 143 | exit(1); |
| 144 | } |
Rafael Espindola | 74aec35 | 2013-06-17 21:50:28 +0000 | [diff] [blame] | 145 | sys::fs::remove(BitcodeResult); |
Chris Lattner | f4789e6 | 2004-04-23 20:36:51 +0000 | [diff] [blame] | 146 | |
| 147 | // Don't check if there are no passes in the suffix. |
| 148 | if (Suffix.empty()) |
| 149 | return NoFailure; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 150 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 151 | outs() << "Checking to see if '" << getPassesString(Suffix) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 152 | << "' passes compile correctly after the '" << getPassesString(Prefix) |
| 153 | << "' passes: "; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 155 | std::unique_ptr<Module> OriginalInput = |
| 156 | BD.swapProgramIn(std::move(PrefixOutput)); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 157 | if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false /*delete*/, |
| 158 | true /*quiet*/)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 159 | errs() << " Error running this sequence of passes" |
| 160 | << " on the input program!\n"; |
Chris Lattner | 5ef681c | 2003-10-17 23:07:47 +0000 | [diff] [blame] | 161 | BD.setPassesToRun(Suffix); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 162 | BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 163 | // TODO: This should propagate the error instead of exiting. |
| 164 | if (Error E = BD.debugOptimizerCrash()) |
| 165 | exit(1); |
| 166 | exit(0); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Run the result... |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 170 | Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 171 | true /*delete bitcode*/); |
| 172 | if (Error E = Diff.takeError()) |
| 173 | return std::move(E); |
| 174 | if (*Diff) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 175 | outs() << " nope.\n"; |
Chris Lattner | 06943ad | 2003-04-25 03:16:05 +0000 | [diff] [blame] | 176 | return KeepSuffix; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Otherwise, we must not be running the bad pass anymore. |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 180 | outs() << " yup.\n"; // No miscompilation! |
Jeffrey Yasskin | 6865f29 | 2010-05-11 23:25:16 +0000 | [diff] [blame] | 181 | // Restore orig program & free test. |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 182 | BD.setNewProgram(std::move(OriginalInput)); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 183 | return NoFailure; |
| 184 | } |
| 185 | |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 186 | namespace { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 187 | class ReduceMiscompilingFunctions : public ListReducer<Function *> { |
| 188 | BugDriver &BD; |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 189 | Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 190 | std::unique_ptr<Module>); |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 191 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 192 | public: |
| 193 | ReduceMiscompilingFunctions(BugDriver &bd, |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 194 | Expected<bool> (*F)(BugDriver &, |
| 195 | std::unique_ptr<Module>, |
| 196 | std::unique_ptr<Module>)) |
Justin Bogner | 79a93a6 | 2016-09-06 04:45:37 +0000 | [diff] [blame] | 197 | : BD(bd), TestFn(F) {} |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 198 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 199 | Expected<TestResult> doTest(std::vector<Function *> &Prefix, |
| 200 | std::vector<Function *> &Suffix) override { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 201 | if (!Suffix.empty()) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 202 | Expected<bool> Ret = TestFuncs(Suffix); |
| 203 | if (Error E = Ret.takeError()) |
| 204 | return std::move(E); |
| 205 | if (*Ret) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 206 | return KeepSuffix; |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 207 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 208 | if (!Prefix.empty()) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 209 | Expected<bool> Ret = TestFuncs(Prefix); |
| 210 | if (Error E = Ret.takeError()) |
| 211 | return std::move(E); |
| 212 | if (*Ret) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 213 | return KeepPrefix; |
| 214 | } |
| 215 | return NoFailure; |
| 216 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 217 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 218 | Expected<bool> TestFuncs(const std::vector<Function *> &Prefix); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 219 | }; |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 220 | } // end anonymous namespace |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 221 | |
Rafael Espindola | bdc3e4d | 2015-12-09 00:55:05 +0000 | [diff] [blame] | 222 | /// Given two modules, link them together and run the program, checking to see |
| 223 | /// if the program matches the diff. If there is an error, return NULL. If not, |
| 224 | /// return the merged module. The Broken argument will be set to true if the |
| 225 | /// output is different. If the DeleteInputs argument is set to true then this |
| 226 | /// function deletes both input modules before it returns. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 227 | /// |
Bryant Wong | 062224c | 2017-04-05 22:23:48 +0000 | [diff] [blame] | 228 | static Expected<std::unique_ptr<Module>> testMergedProgram(const BugDriver &BD, |
| 229 | const Module &M1, |
| 230 | const Module &M2, |
| 231 | bool &Broken) { |
| 232 | // Resulting merge of M1 and M2. |
Rafael Espindola | 22b7724 | 2018-02-14 19:50:40 +0000 | [diff] [blame] | 233 | auto Merged = CloneModule(M1); |
| 234 | if (Linker::linkModules(*Merged, CloneModule(M2))) |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 235 | // TODO: Shouldn't we thread the error up instead of exiting? |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 236 | exit(1); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 237 | |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 238 | // Execute the program. |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 239 | Expected<bool> Diff = BD.diffProgram(*Merged, "", "", false); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 240 | if (Error E = Diff.takeError()) |
| 241 | return std::move(E); |
| 242 | Broken = *Diff; |
Bryant Wong | 062224c | 2017-04-05 22:23:48 +0000 | [diff] [blame] | 243 | return std::move(Merged); |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Rafael Espindola | d86697c | 2018-02-14 21:10:29 +0000 | [diff] [blame] | 246 | /// split functions in a Module into two groups: those that are under |
| 247 | /// consideration for miscompilation vs. those that are not, and test |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 248 | /// accordingly. Each group of functions becomes a separate Module. |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 249 | Expected<bool> |
| 250 | ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function *> &Funcs) { |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 251 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 252 | // functions listed in Funcs. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 253 | outs() << "Checking to see if the program is misoptimized when " |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 254 | << (Funcs.size() == 1 ? "this function is" : "these functions are") |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 255 | << " run through the pass" |
| 256 | << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":"; |
Chris Lattner | efdc0b5 | 2004-03-14 20:50:42 +0000 | [diff] [blame] | 257 | PrintFunctionList(Funcs); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 258 | outs() << '\n'; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 259 | |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 260 | // Create a clone for two reasons: |
| 261 | // * If the optimization passes delete any function, the deleted function |
| 262 | // will be in the clone and Funcs will still point to valid memory |
| 263 | // * If the optimization passes use interprocedural information to break |
| 264 | // a function, we want to continue with the original function. Otherwise |
| 265 | // we can conclude that a function triggers the bug when in fact one |
| 266 | // needs a larger set of original functions to do so. |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 267 | ValueToValueMapTy VMap; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 268 | std::unique_ptr<Module> Clone = CloneModule(BD.getProgram(), VMap); |
| 269 | std::unique_ptr<Module> Orig = BD.swapProgramIn(std::move(Clone)); |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 270 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 271 | std::vector<Function *> FuncsOnClone; |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 272 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 273 | Function *F = cast<Function>(VMap[Funcs[i]]); |
| 274 | FuncsOnClone.push_back(F); |
| 275 | } |
| 276 | |
| 277 | // Split the module into the two halves of the program we want. |
| 278 | VMap.clear(); |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 279 | std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 280 | std::unique_ptr<Module> ToOptimize = |
| 281 | SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap); |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 282 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 283 | Expected<bool> Broken = |
| 284 | TestFn(BD, std::move(ToOptimize), std::move(ToNotOptimize)); |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 285 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 286 | BD.setNewProgram(std::move(Orig)); |
Rafael Espindola | 84ae206 | 2010-07-26 00:07:51 +0000 | [diff] [blame] | 287 | |
| 288 | return Broken; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 291 | /// Give anonymous global values names. |
| 292 | static void DisambiguateGlobalSymbols(Module &M) { |
| 293 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; |
| 294 | ++I) |
Chris Lattner | 8abfb8a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 295 | if (!I->hasName()) |
| 296 | I->setName("anon_global"); |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 297 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 8abfb8a | 2010-01-16 21:34:01 +0000 | [diff] [blame] | 298 | if (!I->hasName()) |
| 299 | I->setName("anon_fn"); |
Chris Lattner | 36ee07f | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 302 | /// Given a reduced list of functions that still exposed the bug, check to see |
| 303 | /// if we can extract the loops in the region without obscuring the bug. If so, |
| 304 | /// it reduces the amount of code identified. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 305 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 306 | static Expected<bool> |
| 307 | ExtractLoops(BugDriver &BD, |
| 308 | Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 309 | std::unique_ptr<Module>), |
| 310 | std::vector<Function *> &MiscompiledFunctions) { |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 311 | bool MadeChange = false; |
| 312 | while (1) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 313 | if (BugpointIsInterrupted) |
| 314 | return MadeChange; |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 315 | |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 316 | ValueToValueMapTy VMap; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 317 | std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
Rafael Espindola | f96e649 | 2018-02-14 21:17:36 +0000 | [diff] [blame] | 318 | std::unique_ptr<Module> ToOptimize = SplitFunctionsOutOfModule( |
| 319 | ToNotOptimize.get(), MiscompiledFunctions, VMap); |
Rafael Espindola | 5d88a3e | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 320 | std::unique_ptr<Module> ToOptimizeLoopExtracted = |
Rafael Espindola | f96e649 | 2018-02-14 21:17:36 +0000 | [diff] [blame] | 321 | BD.extractLoop(ToOptimize.get()); |
| 322 | if (!ToOptimizeLoopExtracted) |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 323 | // If the loop extractor crashed or if there were no extractible loops, |
| 324 | // then this chapter of our odyssey is over with. |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 325 | return MadeChange; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 326 | |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 327 | errs() << "Extracted a loop from the breaking portion of the program.\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 328 | |
| 329 | // Bugpoint is intentionally not very trusting of LLVM transformations. In |
| 330 | // particular, we're not going to assume that the loop extractor works, so |
| 331 | // we're going to test the newly loop extracted program to make sure nothing |
| 332 | // has broken. If something broke, then we'll inform the user and stop |
| 333 | // extraction. |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 334 | AbstractInterpreter *AI = BD.switchToSafeInterpreter(); |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 335 | bool Failure; |
Bryant Wong | 062224c | 2017-04-05 22:23:48 +0000 | [diff] [blame] | 336 | Expected<std::unique_ptr<Module>> New = testMergedProgram( |
| 337 | BD, *ToOptimizeLoopExtracted, *ToNotOptimize, Failure); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 338 | if (Error E = New.takeError()) |
| 339 | return std::move(E); |
| 340 | if (!*New) |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 341 | return false; |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 342 | |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 343 | // Delete the original and set the new program. |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 344 | std::unique_ptr<Module> Old = BD.swapProgramIn(std::move(*New)); |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 345 | for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) |
| 346 | MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]); |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 347 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 348 | if (Failure) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 349 | BD.switchToInterpreter(AI); |
| 350 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 351 | // Merged program doesn't work anymore! |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 352 | errs() << " *** ERROR: Loop extraction broke the program. :(" |
| 353 | << " Please report a bug!\n"; |
| 354 | errs() << " Continuing on with un-loop-extracted version.\n"; |
Chris Lattner | 56c4186 | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 355 | |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 356 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc", |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 357 | *ToNotOptimize); |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 358 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc", |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 359 | *ToOptimize); |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 360 | BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc", |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 361 | *ToOptimizeLoopExtracted); |
Chris Lattner | 56c4186 | 2005-05-08 21:54:56 +0000 | [diff] [blame] | 362 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 363 | errs() << "Please submit the " << OutputPrefix |
| 364 | << "-loop-extract-fail-*.bc files.\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 365 | return MadeChange; |
| 366 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 367 | BD.switchToInterpreter(AI); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 368 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 369 | outs() << " Testing after loop extraction:\n"; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 370 | // Clone modules, the tester function will free them. |
Rafael Espindola | 5d88a3e | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 371 | std::unique_ptr<Module> TOLEBackup = |
Rafael Espindola | 22b7724 | 2018-02-14 19:50:40 +0000 | [diff] [blame] | 372 | CloneModule(*ToOptimizeLoopExtracted, VMap); |
| 373 | std::unique_ptr<Module> TNOBackup = CloneModule(*ToNotOptimize, VMap); |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 374 | |
| 375 | for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) |
| 376 | MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]); |
| 377 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 378 | Expected<bool> Result = TestFn(BD, std::move(ToOptimizeLoopExtracted), |
| 379 | std::move(ToNotOptimize)); |
| 380 | if (Error E = Result.takeError()) |
| 381 | return std::move(E); |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 382 | |
Rafael Espindola | 5d88a3e | 2015-12-09 00:08:22 +0000 | [diff] [blame] | 383 | ToOptimizeLoopExtracted = std::move(TOLEBackup); |
Rafael Espindola | 4ed1f8c | 2015-12-09 00:18:41 +0000 | [diff] [blame] | 384 | ToNotOptimize = std::move(TNOBackup); |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 385 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 386 | if (!*Result) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 387 | outs() << "*** Loop extraction masked the problem. Undoing.\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 388 | // If the program is not still broken, then loop extraction did something |
| 389 | // that masked the error. Stop loop extraction now. |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 390 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 391 | std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions; |
Benjamin Kramer | 9589ff8 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 392 | for (Function *F : MiscompiledFunctions) { |
| 393 | MisCompFunctions.emplace_back(F->getName(), F->getFunctionType()); |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 396 | if (Linker::linkModules(*ToNotOptimize, |
| 397 | std::move(ToOptimizeLoopExtracted))) |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 398 | exit(1); |
Hal Finkel | e8bc700 | 2013-08-02 21:13:42 +0000 | [diff] [blame] | 399 | |
| 400 | MiscompiledFunctions.clear(); |
| 401 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
| 402 | Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); |
| 403 | |
| 404 | assert(NewF && "Function not found??"); |
| 405 | MiscompiledFunctions.push_back(NewF); |
| 406 | } |
| 407 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 408 | BD.setNewProgram(std::move(ToNotOptimize)); |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 409 | return MadeChange; |
| 410 | } |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 411 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 412 | outs() << "*** Loop extraction successful!\n"; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 413 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 414 | std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions; |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 415 | for (Module::iterator I = ToOptimizeLoopExtracted->begin(), |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 416 | E = ToOptimizeLoopExtracted->end(); |
| 417 | I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 418 | if (!I->isDeclaration()) |
Benjamin Kramer | 9589ff8 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 419 | MisCompFunctions.emplace_back(I->getName(), I->getFunctionType()); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 420 | |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 421 | // Okay, great! Now we know that we extracted a loop and that loop |
| 422 | // extraction both didn't break the program, and didn't mask the problem. |
| 423 | // Replace the current program with the loop extracted version, and try to |
| 424 | // extract another loop. |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 425 | if (Linker::linkModules(*ToNotOptimize, std::move(ToOptimizeLoopExtracted))) |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 426 | exit(1); |
Rafael Espindola | 72478e5 | 2014-10-25 04:06:10 +0000 | [diff] [blame] | 427 | |
Chris Lattner | d3a533d | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 428 | // All of the Function*'s in the MiscompiledFunctions list are in the old |
Chris Lattner | 5313f23 | 2004-04-02 06:32:17 +0000 | [diff] [blame] | 429 | // module. Update this list to include all of the functions in the |
| 430 | // optimized and loop extracted module. |
| 431 | MiscompiledFunctions.clear(); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 432 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 433 | Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first); |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 434 | |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 435 | assert(NewF && "Function not found??"); |
| 436 | MiscompiledFunctions.push_back(NewF); |
Chris Lattner | d3a533d | 2004-03-17 17:42:09 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 439 | BD.setNewProgram(std::move(ToNotOptimize)); |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 440 | MadeChange = true; |
| 441 | } |
| 442 | } |
| 443 | |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 444 | namespace { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 445 | class ReduceMiscompiledBlocks : public ListReducer<BasicBlock *> { |
| 446 | BugDriver &BD; |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 447 | Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 448 | std::unique_ptr<Module>); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 449 | std::vector<Function *> FunctionsBeingTested; |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 450 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 451 | public: |
| 452 | ReduceMiscompiledBlocks(BugDriver &bd, |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 453 | Expected<bool> (*F)(BugDriver &, |
| 454 | std::unique_ptr<Module>, |
| 455 | std::unique_ptr<Module>), |
Justin Bogner | 79a93a6 | 2016-09-06 04:45:37 +0000 | [diff] [blame] | 456 | const std::vector<Function *> &Fns) |
| 457 | : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 458 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 459 | Expected<TestResult> doTest(std::vector<BasicBlock *> &Prefix, |
| 460 | std::vector<BasicBlock *> &Suffix) override { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 461 | if (!Suffix.empty()) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 462 | Expected<bool> Ret = TestFuncs(Suffix); |
| 463 | if (Error E = Ret.takeError()) |
| 464 | return std::move(E); |
| 465 | if (*Ret) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 466 | return KeepSuffix; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 467 | } |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 468 | if (!Prefix.empty()) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 469 | Expected<bool> Ret = TestFuncs(Prefix); |
| 470 | if (Error E = Ret.takeError()) |
| 471 | return std::move(E); |
| 472 | if (*Ret) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 473 | return KeepPrefix; |
| 474 | } |
| 475 | return NoFailure; |
| 476 | } |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 477 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 478 | Expected<bool> TestFuncs(const std::vector<BasicBlock *> &BBs); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 479 | }; |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 480 | } // end anonymous namespace |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 481 | |
| 482 | /// TestFuncs - Extract all blocks for the miscompiled functions except for the |
| 483 | /// specified blocks. If the problem still exists, return true. |
| 484 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 485 | Expected<bool> |
| 486 | ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock *> &BBs) { |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 487 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 488 | // functions listed in Funcs. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 489 | outs() << "Checking to see if the program is misoptimized when all "; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 490 | if (!BBs.empty()) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 491 | outs() << "but these " << BBs.size() << " blocks are extracted: "; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 492 | for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 493 | outs() << BBs[i]->getName() << " "; |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 494 | if (BBs.size() > 10) |
| 495 | outs() << "..."; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 496 | } else { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 497 | outs() << "blocks are extracted."; |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 498 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 499 | outs() << '\n'; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 500 | |
| 501 | // Split the module into the two halves of the program we want. |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 502 | ValueToValueMapTy VMap; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 503 | std::unique_ptr<Module> Clone = CloneModule(BD.getProgram(), VMap); |
| 504 | std::unique_ptr<Module> Orig = BD.swapProgramIn(std::move(Clone)); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 505 | std::vector<Function *> FuncsOnClone; |
| 506 | std::vector<BasicBlock *> BBsOnClone; |
Rafael Espindola | 115a932 | 2010-07-29 14:20:59 +0000 | [diff] [blame] | 507 | for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) { |
| 508 | Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]); |
| 509 | FuncsOnClone.push_back(F); |
| 510 | } |
| 511 | for (unsigned i = 0, e = BBs.size(); i != e; ++i) { |
| 512 | BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]); |
| 513 | BBsOnClone.push_back(BB); |
| 514 | } |
| 515 | VMap.clear(); |
| 516 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 517 | std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap); |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 518 | std::unique_ptr<Module> ToOptimize = |
| 519 | SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 520 | |
| 521 | // Try the extraction. If it doesn't work, then the block extractor crashed |
| 522 | // or something, in which case bugpoint can't chase down this possibility. |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 523 | if (std::unique_ptr<Module> New = |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 524 | BD.extractMappedBlocksFromModule(BBsOnClone, ToOptimize.get())) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 525 | Expected<bool> Ret = TestFn(BD, std::move(New), std::move(ToNotOptimize)); |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 526 | BD.setNewProgram(std::move(Orig)); |
Rafael Espindola | 115a932 | 2010-07-29 14:20:59 +0000 | [diff] [blame] | 527 | return Ret; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 528 | } |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 529 | BD.setNewProgram(std::move(Orig)); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 533 | /// Given a reduced list of functions that still expose the bug, extract as many |
| 534 | /// basic blocks from the region as possible without obscuring the bug. |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 535 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 536 | static Expected<bool> |
| 537 | ExtractBlocks(BugDriver &BD, |
| 538 | Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 539 | std::unique_ptr<Module>), |
| 540 | std::vector<Function *> &MiscompiledFunctions) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 541 | if (BugpointIsInterrupted) |
| 542 | return false; |
Michael J. Spencer | 56584fc | 2011-03-31 13:06:39 +0000 | [diff] [blame] | 543 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 544 | std::vector<BasicBlock *> Blocks; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 545 | for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i) |
Duncan P. N. Exon Smith | 1e221d5 | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 546 | for (BasicBlock &BB : *MiscompiledFunctions[i]) |
| 547 | Blocks.push_back(&BB); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 548 | |
| 549 | // Use the list reducer to identify blocks that can be extracted without |
| 550 | // obscuring the bug. The Blocks list will end up containing blocks that must |
| 551 | // be retained from the original program. |
| 552 | unsigned OldSize = Blocks.size(); |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 553 | |
| 554 | // Check to see if all blocks are extractible first. |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 555 | Expected<bool> Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions) |
| 556 | .TestFuncs(std::vector<BasicBlock *>()); |
| 557 | if (Error E = Ret.takeError()) |
| 558 | return std::move(E); |
| 559 | if (*Ret) { |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 560 | Blocks.clear(); |
| 561 | } else { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 562 | Expected<bool> Ret = |
| 563 | ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions) |
| 564 | .reduceList(Blocks); |
| 565 | if (Error E = Ret.takeError()) |
| 566 | return std::move(E); |
Chris Lattner | 68bee93 | 2004-05-12 16:08:01 +0000 | [diff] [blame] | 567 | if (Blocks.size() == OldSize) |
| 568 | return false; |
| 569 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 570 | |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 571 | ValueToValueMapTy VMap; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 572 | std::unique_ptr<Module> ProgClone = CloneModule(BD.getProgram(), VMap); |
Rafael Espindola | c80da49 | 2018-02-14 20:13:01 +0000 | [diff] [blame] | 573 | std::unique_ptr<Module> ToExtract = |
| 574 | SplitFunctionsOutOfModule(ProgClone.get(), MiscompiledFunctions, VMap); |
Rafael Espindola | 1bfd87a | 2014-08-26 17:19:03 +0000 | [diff] [blame] | 575 | std::unique_ptr<Module> Extracted = |
Rafael Espindola | c80da49 | 2018-02-14 20:13:01 +0000 | [diff] [blame] | 576 | BD.extractMappedBlocksFromModule(Blocks, ToExtract.get()); |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 577 | if (!Extracted) { |
Chris Lattner | da895d6 | 2005-02-27 06:18:25 +0000 | [diff] [blame] | 578 | // Weird, extraction should have worked. |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 579 | errs() << "Nondeterministic problem extracting blocks??\n"; |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 580 | return false; |
| 581 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 583 | // Otherwise, block extraction succeeded. Link the two program fragments back |
| 584 | // together. |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 585 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 586 | std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions; |
| 587 | for (Module::iterator I = Extracted->begin(), E = Extracted->end(); I != E; |
| 588 | ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 589 | if (!I->isDeclaration()) |
Benjamin Kramer | 9589ff8 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 590 | MisCompFunctions.emplace_back(I->getName(), I->getFunctionType()); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 591 | |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 592 | if (Linker::linkModules(*ProgClone, std::move(Extracted))) |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 593 | exit(1); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 595 | // Set the new program and delete the old one. |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 596 | BD.setNewProgram(std::move(ProgClone)); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 598 | // Update the list of miscompiled functions. |
| 599 | MiscompiledFunctions.clear(); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 601 | for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 602 | Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first); |
Chris Lattner | 90c18c5 | 2004-11-16 06:31:38 +0000 | [diff] [blame] | 603 | assert(NewF && "Function not found??"); |
| 604 | MiscompiledFunctions.push_back(NewF); |
| 605 | } |
Chris Lattner | 2290e75 | 2004-05-12 02:43:24 +0000 | [diff] [blame] | 606 | |
| 607 | return true; |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 610 | /// This is a generic driver to narrow down miscompilations, either in an |
| 611 | /// optimization or a code generator. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 612 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 613 | static Expected<std::vector<Function *>> DebugAMiscompilation( |
| 614 | BugDriver &BD, |
| 615 | Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>, |
| 616 | std::unique_ptr<Module>)) { |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 617 | // Okay, now that we have reduced the list of passes which are causing the |
| 618 | // failure, see if we can pin down which functions are being |
| 619 | // miscompiled... first build a list of all of the non-external functions in |
| 620 | // the program. |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 621 | std::vector<Function *> MiscompiledFunctions; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 622 | Module &Prog = BD.getProgram(); |
| 623 | for (Function &F : Prog) |
Duncan P. N. Exon Smith | 1e221d5 | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 624 | if (!F.isDeclaration()) |
| 625 | MiscompiledFunctions.push_back(&F); |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 626 | |
| 627 | // Do the reduction... |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 628 | if (!BugpointIsInterrupted) { |
| 629 | Expected<bool> Ret = ReduceMiscompilingFunctions(BD, TestFn) |
| 630 | .reduceList(MiscompiledFunctions); |
| 631 | if (Error E = Ret.takeError()) { |
| 632 | errs() << "\n***Cannot reduce functions: "; |
| 633 | return std::move(E); |
| 634 | } |
Andrew Trick | 7c863eb | 2011-05-11 16:31:24 +0000 | [diff] [blame] | 635 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 636 | outs() << "\n*** The following function" |
| 637 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 638 | << " being miscompiled: "; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 639 | PrintFunctionList(MiscompiledFunctions); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 640 | outs() << '\n'; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 641 | |
| 642 | // See if we can rip any loops out of the miscompiled functions and still |
| 643 | // trigger the problem. |
Reid Spencer | dc31a8a | 2006-11-11 19:05:02 +0000 | [diff] [blame] | 644 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 645 | if (!BugpointIsInterrupted && !DisableLoopExtraction) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 646 | Expected<bool> Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions); |
| 647 | if (Error E = Ret.takeError()) |
| 648 | return std::move(E); |
| 649 | if (*Ret) { |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 650 | // Okay, we extracted some loops and the problem still appears. See if |
| 651 | // we can eliminate some of the created functions from being candidates. |
| 652 | DisambiguateGlobalSymbols(BD.getProgram()); |
Chris Lattner | 36ee07f | 2004-04-11 23:52:35 +0000 | [diff] [blame] | 653 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 654 | // Do the reduction... |
| 655 | if (!BugpointIsInterrupted) |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 656 | Ret = ReduceMiscompilingFunctions(BD, TestFn) |
| 657 | .reduceList(MiscompiledFunctions); |
| 658 | if (Error E = Ret.takeError()) |
| 659 | return std::move(E); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 660 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 661 | outs() << "\n*** The following function" |
| 662 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 663 | << " being miscompiled: "; |
| 664 | PrintFunctionList(MiscompiledFunctions); |
| 665 | outs() << '\n'; |
| 666 | } |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 669 | if (!BugpointIsInterrupted && !DisableBlockExtraction) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 670 | Expected<bool> Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions); |
| 671 | if (Error E = Ret.takeError()) |
| 672 | return std::move(E); |
| 673 | if (*Ret) { |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 674 | // Okay, we extracted some blocks and the problem still appears. See if |
| 675 | // we can eliminate some of the created functions from being candidates. |
| 676 | DisambiguateGlobalSymbols(BD.getProgram()); |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 677 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 678 | // Do the reduction... |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 679 | Ret = ReduceMiscompilingFunctions(BD, TestFn) |
| 680 | .reduceList(MiscompiledFunctions); |
| 681 | if (Error E = Ret.takeError()) |
| 682 | return std::move(E); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 683 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 684 | outs() << "\n*** The following function" |
| 685 | << (MiscompiledFunctions.size() == 1 ? " is" : "s are") |
| 686 | << " being miscompiled: "; |
| 687 | PrintFunctionList(MiscompiledFunctions); |
| 688 | outs() << '\n'; |
| 689 | } |
Chris Lattner | 5e783ab | 2004-05-11 21:54:13 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 692 | return MiscompiledFunctions; |
| 693 | } |
| 694 | |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 695 | /// This is the predicate function used to check to see if the "Test" portion of |
| 696 | /// the program is misoptimized. If so, return true. In any case, both module |
| 697 | /// arguments are deleted. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 698 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 699 | static Expected<bool> TestOptimizer(BugDriver &BD, std::unique_ptr<Module> Test, |
| 700 | std::unique_ptr<Module> Safe) { |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 701 | // Run the optimization passes on ToOptimize, producing a transformed version |
| 702 | // of the functions being tested. |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 703 | outs() << " Optimizing functions being tested: "; |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 704 | std::unique_ptr<Module> Optimized = |
Philip Reames | 34618d9 | 2016-06-29 00:26:21 +0000 | [diff] [blame] | 705 | BD.runPassesOn(Test.get(), BD.getPassesToRun()); |
| 706 | if (!Optimized) { |
| 707 | errs() << " Error running this sequence of passes" |
| 708 | << " on the input program!\n"; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 709 | BD.setNewProgram(std::move(Test)); |
| 710 | BD.EmitProgressBitcode(*Test, "pass-error", false); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 711 | if (Error E = BD.debugOptimizerCrash()) |
| 712 | return std::move(E); |
| 713 | return false; |
Philip Reames | 34618d9 | 2016-06-29 00:26:21 +0000 | [diff] [blame] | 714 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 715 | outs() << "done.\n"; |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 716 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 717 | outs() << " Checking to see if the merged program executes correctly: "; |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 718 | bool Broken; |
Bryant Wong | 062224c | 2017-04-05 22:23:48 +0000 | [diff] [blame] | 719 | auto Result = testMergedProgram(BD, *Optimized, *Safe, Broken); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 720 | if (Error E = Result.takeError()) |
| 721 | return std::move(E); |
| 722 | if (auto New = std::move(*Result)) { |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 723 | outs() << (Broken ? " nope.\n" : " yup.\n"); |
| 724 | // Delete the original and set the new program. |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 725 | BD.setNewProgram(std::move(New)); |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 726 | } |
Chris Lattner | b15825b | 2004-04-05 21:37:38 +0000 | [diff] [blame] | 727 | return Broken; |
| 728 | } |
| 729 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 730 | /// debugMiscompilation - This method is used when the passes selected are not |
| 731 | /// crashing, but the generated output is semantically different from the |
| 732 | /// input. |
| 733 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 734 | Error BugDriver::debugMiscompilation() { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 735 | // Make sure something was miscompiled... |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 736 | if (!BugpointIsInterrupted) { |
| 737 | Expected<bool> Result = |
| 738 | ReduceMiscompilingPasses(*this).reduceList(PassesToRun); |
| 739 | if (Error E = Result.takeError()) |
| 740 | return E; |
| 741 | if (!*Result) |
| 742 | return make_error<StringError>( |
| 743 | "*** Optimized program matches reference output! No problem" |
| 744 | " detected...\nbugpoint can't help you with your problem!\n", |
| 745 | inconvertibleErrorCode()); |
| 746 | } |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 747 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 748 | outs() << "\n*** Found miscompiling pass" |
| 749 | << (getPassesToRun().size() == 1 ? "" : "es") << ": " |
| 750 | << getPassesString(getPassesToRun()) << '\n'; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 751 | EmitProgressBitcode(*Program, "passinput"); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 752 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 753 | Expected<std::vector<Function *>> MiscompiledFunctions = |
| 754 | DebugAMiscompilation(*this, TestOptimizer); |
| 755 | if (Error E = MiscompiledFunctions.takeError()) |
| 756 | return E; |
Chris Lattner | a1cf1c8 | 2004-03-14 22:08:00 +0000 | [diff] [blame] | 757 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 758 | // Output a bunch of bitcode files for the user... |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 759 | outs() << "Outputting reduced bitcode files which expose the problem:\n"; |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 760 | ValueToValueMapTy VMap; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 761 | Module *ToNotOptimize = CloneModule(getProgram(), VMap).release(); |
Rafael Espindola | 18d7050 | 2015-12-09 00:34:10 +0000 | [diff] [blame] | 762 | Module *ToOptimize = |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 763 | SplitFunctionsOutOfModule(ToNotOptimize, *MiscompiledFunctions, VMap) |
Rafael Espindola | 18d7050 | 2015-12-09 00:34:10 +0000 | [diff] [blame] | 764 | .release(); |
Chris Lattner | be21ca5 | 2004-03-14 19:27:19 +0000 | [diff] [blame] | 765 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 766 | outs() << " Non-optimized portion: "; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 767 | EmitProgressBitcode(*ToNotOptimize, "tonotoptimize", true); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 768 | delete ToNotOptimize; // Delete hacked module. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 769 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 770 | outs() << " Portion that is input to optimizer: "; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 771 | EmitProgressBitcode(*ToOptimize, "tooptimize"); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 772 | delete ToOptimize; // Delete hacked module. |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 773 | |
| 774 | return Error::success(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 775 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 776 | |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 777 | /// Get the specified modules ready for code generator testing. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 778 | /// |
Rafael Espindola | 46e5502 | 2018-04-24 20:15:27 +0000 | [diff] [blame] | 779 | static std::unique_ptr<Module> |
| 780 | CleanupAndPrepareModules(BugDriver &BD, std::unique_ptr<Module> Test, |
| 781 | Module *Safe) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 782 | // Clean up the modules, removing extra cruft that we don't need anymore... |
Rafael Espindola | 46e5502 | 2018-04-24 20:15:27 +0000 | [diff] [blame] | 783 | Test = BD.performFinalCleanups(std::move(Test)); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 784 | |
| 785 | // If we are executing the JIT, we have several nasty issues to take care of. |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 786 | if (!BD.isExecutingJIT()) |
Rafael Espindola | 46e5502 | 2018-04-24 20:15:27 +0000 | [diff] [blame] | 787 | return Test; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 788 | |
| 789 | // First, if the main function is in the Safe module, we must add a stub to |
| 790 | // the Test module to call into it. Thus, we create a new function `main' |
| 791 | // which just calls the old one. |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 792 | if (Function *oldMain = Safe->getFunction("main")) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 793 | if (!oldMain->isDeclaration()) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 794 | // Rename it |
| 795 | oldMain->setName("llvm_bugpoint_old_main"); |
| 796 | // Create a NEW `main' function with same type in the test module. |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 797 | Function *newMain = |
| 798 | Function::Create(oldMain->getFunctionType(), |
| 799 | GlobalValue::ExternalLinkage, "main", Test.get()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 800 | // Create an `oldmain' prototype in the test module, which will |
| 801 | // corresponds to the real main function in the same module. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 802 | Function *oldMainProto = Function::Create(oldMain->getFunctionType(), |
| 803 | GlobalValue::ExternalLinkage, |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 804 | oldMain->getName(), Test.get()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 805 | // Set up and remember the argument list for the main function. |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 806 | std::vector<Value *> args; |
| 807 | for (Function::arg_iterator I = newMain->arg_begin(), |
| 808 | E = newMain->arg_end(), |
| 809 | OI = oldMain->arg_begin(); |
| 810 | I != E; ++I, ++OI) { |
| 811 | I->setName(OI->getName()); // Copy argument names from oldMain |
Duncan P. N. Exon Smith | 1e221d5 | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 812 | args.push_back(&*I); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | // Call the old main function and return its result |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 816 | BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 817 | CallInst *call = CallInst::Create(oldMainProto, args, "", BB); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 818 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 819 | // If the type of old function wasn't void, return value of call |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 820 | ReturnInst::Create(Safe->getContext(), call, BB); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // The second nasty issue we must deal with in the JIT is that the Safe |
| 824 | // module cannot directly reference any functions defined in the test |
| 825 | // module. Instead, we use a JIT API call to dynamically resolve the |
| 826 | // symbol. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 827 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 828 | // Add the resolver to the Safe module. |
| 829 | // Prototype: void *getPointerToNamedFunction(const char* Name) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 830 | Constant *resolverFunc = Safe->getOrInsertFunction( |
| 831 | "getPointerToNamedFunction", Type::getInt8PtrTy(Safe->getContext()), |
Serge Guelton | 9d54400 | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 832 | Type::getInt8PtrTy(Safe->getContext())); |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 833 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 834 | // Use the function we just added to get addresses of functions we need. |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 835 | for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 836 | if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc && |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 837 | !F->isIntrinsic() /* ignore intrinsics */) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 838 | Function *TestFn = Test->getFunction(F->getName()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 839 | |
| 840 | // Don't forward functions which are external in the test module too. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 841 | if (TestFn && !TestFn->isDeclaration()) { |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 842 | // 1. Add a string constant with its name to the global file |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 843 | Constant *InitArray = |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 844 | ConstantDataArray::getString(F->getContext(), F->getName()); |
| 845 | GlobalVariable *funcName = new GlobalVariable( |
| 846 | *Safe, InitArray->getType(), true /*isConstant*/, |
| 847 | GlobalValue::InternalLinkage, InitArray, F->getName() + "_name"); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 848 | |
| 849 | // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an |
| 850 | // sbyte* so it matches the signature of the resolver function. |
| 851 | |
| 852 | // GetElementPtr *funcName, ulong 0, ulong 0 |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 853 | std::vector<Constant *> GEPargs( |
| 854 | 2, Constant::getNullValue(Type::getInt32Ty(F->getContext()))); |
David Blaikie | 19443c1 | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 855 | Value *GEP = ConstantExpr::getGetElementPtr(InitArray->getType(), |
| 856 | funcName, GEPargs); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 857 | std::vector<Value *> ResolverArgs; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 858 | ResolverArgs.push_back(GEP); |
| 859 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 860 | // Rewrite uses of F in global initializers, etc. to uses of a wrapper |
| 861 | // function that dynamically resolves the calls to F via our JIT API |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 862 | if (!F->use_empty()) { |
| 863 | // Create a new global to hold the cached function pointer. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 864 | Constant *NullPtr = ConstantPointerNull::get(F->getType()); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 865 | GlobalVariable *Cache = new GlobalVariable( |
| 866 | *F->getParent(), F->getType(), false, |
| 867 | GlobalValue::InternalLinkage, NullPtr, F->getName() + ".fpcache"); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 868 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 869 | // Construct a new stub function that will re-route calls to F |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 870 | FunctionType *FuncTy = F->getFunctionType(); |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 871 | Function *FuncWrapper = |
| 872 | Function::Create(FuncTy, GlobalValue::InternalLinkage, |
| 873 | F->getName() + "_wrapper", F->getParent()); |
| 874 | BasicBlock *EntryBB = |
| 875 | BasicBlock::Create(F->getContext(), "entry", FuncWrapper); |
| 876 | BasicBlock *DoCallBB = |
| 877 | BasicBlock::Create(F->getContext(), "usecache", FuncWrapper); |
| 878 | BasicBlock *LookupBB = |
| 879 | BasicBlock::Create(F->getContext(), "lookupfp", FuncWrapper); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 880 | |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 881 | // Check to see if we already looked up the value. |
| 882 | Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 883 | Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal, |
| 884 | NullPtr, "isNull"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 885 | BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 886 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 887 | // Resolve the call to function F via the JIT API: |
| 888 | // |
| 889 | // call resolver(GetElementPtr...) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 890 | CallInst *Resolver = CallInst::Create(resolverFunc, ResolverArgs, |
| 891 | "resolver", LookupBB); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 892 | |
| 893 | // Cast the result from the resolver to correctly-typed function. |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 894 | CastInst *CastedResolver = new BitCastInst( |
| 895 | Resolver, PointerType::getUnqual(F->getFunctionType()), |
| 896 | "resolverCast", LookupBB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 897 | |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 898 | // Save the value in our cache. |
| 899 | new StoreInst(CastedResolver, Cache, LookupBB); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 900 | BranchInst::Create(DoCallBB, LookupBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 901 | |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 902 | PHINode *FuncPtr = |
| 903 | PHINode::Create(NullPtr->getType(), 2, "fp", DoCallBB); |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 904 | FuncPtr->addIncoming(CastedResolver, LookupBB); |
| 905 | FuncPtr->addIncoming(CachedVal, EntryBB); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 906 | |
Chris Lattner | a3efca1 | 2005-07-12 01:00:32 +0000 | [diff] [blame] | 907 | // Save the argument list. |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 908 | std::vector<Value *> Args; |
Duncan P. N. Exon Smith | 1e221d5 | 2015-10-20 19:36:39 +0000 | [diff] [blame] | 909 | for (Argument &A : FuncWrapper->args()) |
| 910 | Args.push_back(&A); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 911 | |
| 912 | // Pass on the arguments to the real function, return its result |
Dan Gohman | e49a13e | 2010-06-07 20:19:26 +0000 | [diff] [blame] | 913 | if (F->getReturnType()->isVoidTy()) { |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 914 | CallInst::Create(FuncPtr, Args, "", DoCallBB); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 915 | ReturnInst::Create(F->getContext(), DoCallBB); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 916 | } else { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 917 | CallInst *Call = |
| 918 | CallInst::Create(FuncPtr, Args, "retval", DoCallBB); |
| 919 | ReturnInst::Create(F->getContext(), Call, DoCallBB); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 920 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 921 | |
Misha Brukman | de4803d | 2004-04-19 03:36:47 +0000 | [diff] [blame] | 922 | // Use the wrapper function instead of the old function |
| 923 | F->replaceAllUsesWith(FuncWrapper); |
Misha Brukman | dc7fef8 | 2004-04-19 01:12:01 +0000 | [diff] [blame] | 924 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | if (verifyModule(*Test) || verifyModule(*Safe)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 930 | errs() << "Bugpoint has a bug, which corrupted a module!!\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 931 | abort(); |
| 932 | } |
Rafael Espindola | 46e5502 | 2018-04-24 20:15:27 +0000 | [diff] [blame] | 933 | |
| 934 | return Test; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 937 | /// This is the predicate function used to check to see if the "Test" portion of |
| 938 | /// the program is miscompiled by the code generator under test. If so, return |
| 939 | /// true. In any case, both module arguments are deleted. |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 940 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 941 | static Expected<bool> TestCodeGenerator(BugDriver &BD, |
| 942 | std::unique_ptr<Module> Test, |
| 943 | std::unique_ptr<Module> Safe) { |
Rafael Espindola | 46e5502 | 2018-04-24 20:15:27 +0000 | [diff] [blame] | 944 | Test = CleanupAndPrepareModules(BD, std::move(Test), Safe.get()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 945 | |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 946 | SmallString<128> TestModuleBC; |
| 947 | int TestModuleFD; |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 948 | std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc", |
| 949 | TestModuleFD, TestModuleBC); |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 950 | if (EC) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 951 | errs() << BD.getToolName() |
| 952 | << "Error making unique filename: " << EC.message() << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 953 | exit(1); |
| 954 | } |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 955 | if (BD.writeProgramToFile(TestModuleBC.str(), TestModuleFD, *Test)) { |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 956 | errs() << "Error writing bitcode to `" << TestModuleBC.str() |
| 957 | << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 958 | exit(1); |
| 959 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 960 | |
Michael J. Spencer | c9c08fb | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 961 | FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps); |
Rafael Espindola | bc2ed59 | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 962 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 963 | // Make the shared library |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 964 | SmallString<128> SafeModuleBC; |
| 965 | int SafeModuleFD; |
Rafael Espindola | 1276b39 | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 966 | EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD, |
| 967 | SafeModuleBC); |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 968 | if (EC) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 969 | errs() << BD.getToolName() |
| 970 | << "Error making unique filename: " << EC.message() << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 971 | exit(1); |
| 972 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 973 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 974 | if (BD.writeProgramToFile(SafeModuleBC.str(), SafeModuleFD, *Safe)) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 975 | errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 976 | exit(1); |
| 977 | } |
Rafael Espindola | bc2ed59 | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 978 | |
Michael J. Spencer | c9c08fb | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 979 | FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps); |
Rafael Espindola | bc2ed59 | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 980 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 981 | Expected<std::string> SharedObject = |
| 982 | BD.compileSharedObject(SafeModuleBC.str()); |
| 983 | if (Error E = SharedObject.takeError()) |
| 984 | return std::move(E); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 985 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 986 | FileRemover SharedObjectRemover(*SharedObject, !SaveTemps); |
Rafael Espindola | bc2ed59 | 2010-06-21 02:17:36 +0000 | [diff] [blame] | 987 | |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 988 | // Run the code generator on the `Test' code, loading the shared library. |
| 989 | // The function returns whether or not the new output differs from reference. |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 990 | Expected<bool> Result = |
| 991 | BD.diffProgram(BD.getProgram(), TestModuleBC.str(), *SharedObject, false); |
| 992 | if (Error E = Result.takeError()) |
| 993 | return std::move(E); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 994 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 995 | if (*Result) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 996 | errs() << ": still failing!\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 997 | else |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 998 | errs() << ": didn't fail.\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 999 | |
| 1000 | return Result; |
| 1001 | } |
| 1002 | |
Misha Brukman | 8c194ea | 2004-04-21 18:36:43 +0000 | [diff] [blame] | 1003 | /// debugCodeGenerator - debug errors in LLC, LLI, or CBE. |
| 1004 | /// |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1005 | Error BugDriver::debugCodeGenerator() { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1006 | if ((void *)SafeInterpreter == (void *)Interpreter) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1007 | Expected<std::string> Result = |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 1008 | executeProgramSafely(*Program, "bugpoint.safe.out"); |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1009 | if (Result) { |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1010 | outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match " |
| 1011 | << "the reference diff. This may be due to a\n front-end " |
| 1012 | << "bug or a bug in the original program, but this can also " |
| 1013 | << "happen if bugpoint isn't running the program with the " |
| 1014 | << "right flags or input.\n I left the result of executing " |
| 1015 | << "the program with the \"safe\" backend in this file for " |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1016 | << "you: '" << *Result << "'.\n"; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1017 | } |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1018 | return Error::success(); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 1021 | DisambiguateGlobalSymbols(*Program); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1022 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1023 | Expected<std::vector<Function *>> Funcs = |
| 1024 | DebugAMiscompilation(*this, TestCodeGenerator); |
| 1025 | if (Error E = Funcs.takeError()) |
| 1026 | return E; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1027 | |
| 1028 | // Split the module into the two halves of the program we want. |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 1029 | ValueToValueMapTy VMap; |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 1030 | std::unique_ptr<Module> ToNotCodeGen = CloneModule(getProgram(), VMap); |
Rafael Espindola | 0340a6f | 2015-12-09 00:51:06 +0000 | [diff] [blame] | 1031 | std::unique_ptr<Module> ToCodeGen = |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1032 | SplitFunctionsOutOfModule(ToNotCodeGen.get(), *Funcs, VMap); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Condition the modules |
Rafael Espindola | 46e5502 | 2018-04-24 20:15:27 +0000 | [diff] [blame] | 1035 | ToCodeGen = |
| 1036 | CleanupAndPrepareModules(*this, std::move(ToCodeGen), ToNotCodeGen.get()); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1037 | |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1038 | SmallString<128> TestModuleBC; |
| 1039 | int TestModuleFD; |
Rafael Espindola | 1ad4502 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1040 | std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc", |
| 1041 | TestModuleFD, TestModuleBC); |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1042 | if (EC) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1043 | errs() << getToolName() << "Error making unique filename: " << EC.message() |
| 1044 | << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 1045 | exit(1); |
| 1046 | } |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 1047 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 1048 | if (writeProgramToFile(TestModuleBC.str(), TestModuleFD, *ToCodeGen)) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1049 | errs() << "Error writing bitcode to `" << TestModuleBC << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1050 | exit(1); |
| 1051 | } |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1052 | |
| 1053 | // Make the shared library |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1054 | SmallString<128> SafeModuleBC; |
| 1055 | int SafeModuleFD; |
Rafael Espindola | 1276b39 | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 1056 | EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD, |
| 1057 | SafeModuleBC); |
Rafael Espindola | 88088f4 | 2013-06-18 15:29:32 +0000 | [diff] [blame] | 1058 | if (EC) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1059 | errs() << getToolName() << "Error making unique filename: " << EC.message() |
| 1060 | << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 1061 | exit(1); |
| 1062 | } |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 1063 | |
Rafael Espindola | cdac12c | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 1064 | if (writeProgramToFile(SafeModuleBC.str(), SafeModuleFD, *ToNotCodeGen)) { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1065 | errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting."; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1066 | exit(1); |
| 1067 | } |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1068 | Expected<std::string> SharedObject = compileSharedObject(SafeModuleBC.str()); |
| 1069 | if (Error E = SharedObject.takeError()) |
| 1070 | return E; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1071 | |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1072 | outs() << "You can reproduce the problem with the command line: \n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1073 | if (isExecutingJIT()) { |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1074 | outs() << " lli -load " << *SharedObject << " " << TestModuleBC; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1075 | } else { |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1076 | outs() << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n"; |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1077 | outs() << " cc " << *SharedObject << " " << TestModuleBC.str() << ".s -o " |
Joerg Sonnenberger | a27efcd | 2016-10-01 07:34:18 +0000 | [diff] [blame] | 1078 | << TestModuleBC << ".exe\n"; |
| 1079 | outs() << " ./" << TestModuleBC << ".exe"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1080 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 1081 | for (unsigned i = 0, e = InputArgv.size(); i != e; ++i) |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 1082 | outs() << " " << InputArgv[i]; |
| 1083 | outs() << '\n'; |
| 1084 | outs() << "The shared object was created with:\n llc -march=c " |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 1085 | << SafeModuleBC.str() << " -o temporary.c\n" |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1086 | << " cc -xc temporary.c -O2 -o " << *SharedObject; |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 1087 | if (TargetTriple.getArch() == Triple::sparc) |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1088 | outs() << " -G"; // Compile a shared library, `-G' for Sparc |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 1089 | else |
Justin Bogner | 388e8b9 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 1090 | outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others |
Daniel Dunbar | ca74096 | 2009-08-18 03:35:57 +0000 | [diff] [blame] | 1091 | |
| 1092 | outs() << " -fno-strict-aliasing\n"; |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1093 | |
Justin Bogner | d8090ae | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 1094 | return Error::success(); |
Chris Lattner | a57d86b | 2004-04-05 22:58:16 +0000 | [diff] [blame] | 1095 | } |