Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- Miscompilation.cpp - Debug program miscompilations -----------------===// |
| 2 | // |
| 3 | // This file implements program miscompilation debugging support. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "BugDriver.h" |
| 8 | #include "SystemUtils.h" |
Chris Lattner | 126840f | 2003-04-24 20:16:29 +0000 | [diff] [blame^] | 9 | #include "ListReducer.h" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 10 | #include "llvm/Pass.h" |
| 11 | #include "llvm/Module.h" |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 12 | #include "llvm/Transforms/Utils/Cloning.h" |
| 13 | #include "llvm/Transforms/Utils/Linker.h" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 14 | #include "Support/CommandLine.h" |
| 15 | |
| 16 | // Anonymous namespace to define command line options for miscompilation |
| 17 | // debugging. |
| 18 | // |
| 19 | namespace { |
| 20 | // Output - The user can specify a file containing the expected output of the |
| 21 | // program. If this filename is set, it is used as the reference diff source, |
| 22 | // otherwise the raw input run through an interpreter is used as the reference |
| 23 | // source. |
| 24 | // |
| 25 | cl::opt<std::string> |
| 26 | Output("output", cl::desc("Specify a reference program output " |
| 27 | "(for miscompilation detection)")); |
| 28 | } |
| 29 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 30 | class ReduceMiscompilingPasses : public ListReducer<const PassInfo*> { |
| 31 | BugDriver &BD; |
| 32 | public: |
| 33 | ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} |
| 34 | |
| 35 | virtual TestResult doTest(const std::vector<const PassInfo*> &Prefix, |
| 36 | const std::vector<const PassInfo*> &Kept); |
| 37 | }; |
| 38 | |
| 39 | ReduceMiscompilingPasses::TestResult |
| 40 | ReduceMiscompilingPasses::doTest(const std::vector<const PassInfo*> &Prefix, |
| 41 | const std::vector<const PassInfo*> &Kept) { |
| 42 | // First, run the program with just the Kept passes. If it is still broken |
| 43 | // with JUST the kept passes, discard the prefix passes. |
| 44 | std::cout << "Checking to see if '" << getPassesString(Kept) |
| 45 | << "' compile correctly: "; |
| 46 | |
| 47 | std::string BytecodeResult; |
| 48 | if (BD.runPasses(Kept, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
| 49 | std::cerr << BD.getToolName() << ": Error running this sequence of passes" |
| 50 | << " on the input program!\n"; |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | // Check to see if the finished program matches the reference output... |
| 55 | if (BD.diffProgram(Output, BytecodeResult, true /*delete bytecode*/)) { |
| 56 | std::cout << "nope.\n"; |
| 57 | return KeepSuffix; // Miscompilation detected! |
| 58 | } |
| 59 | std::cout << "yup.\n"; // No miscompilation! |
| 60 | |
| 61 | if (Prefix.empty()) return NoFailure; |
| 62 | |
| 63 | // First, run the program with just the Kept passes. If it is still broken |
| 64 | // with JUST the kept passes, discard the prefix passes. |
| 65 | std::cout << "Checking to see if '" << getPassesString(Prefix) |
| 66 | << "' compile correctly: "; |
| 67 | |
| 68 | // If it is not broken with the kept passes, it's possible that the prefix |
| 69 | // passes must be run before the kept passes to break it. If the program |
| 70 | // WORKS after the prefix passes, but then fails if running the prefix AND |
| 71 | // kept passes, we can update our bytecode file to include the result of the |
| 72 | // prefix passes, then discard the prefix passes. |
| 73 | // |
| 74 | if (BD.runPasses(Prefix, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
| 75 | std::cerr << BD.getToolName() << ": Error running this sequence of passes" |
| 76 | << " on the input program!\n"; |
| 77 | exit(1); |
| 78 | } |
| 79 | |
| 80 | // If the prefix maintains the predicate by itself, only keep the prefix! |
| 81 | if (BD.diffProgram(Output, BytecodeResult)) { |
| 82 | std::cout << "nope.\n"; |
| 83 | removeFile(BytecodeResult); |
| 84 | return KeepPrefix; |
| 85 | } |
| 86 | std::cout << "yup.\n"; // No miscompilation! |
| 87 | |
| 88 | // Ok, so now we know that the prefix passes work, try running the suffix |
| 89 | // passes on the result of the prefix passes. |
| 90 | // |
| 91 | Module *PrefixOutput = BD.ParseInputFile(BytecodeResult); |
| 92 | if (PrefixOutput == 0) { |
| 93 | std::cerr << BD.getToolName() << ": Error reading bytecode file '" |
| 94 | << BytecodeResult << "'!\n"; |
| 95 | exit(1); |
| 96 | } |
| 97 | removeFile(BytecodeResult); // No longer need the file on disk |
| 98 | |
| 99 | std::cout << "Checking to see if '" << getPassesString(Kept) |
| 100 | << "' passes compile correctly after the '" |
| 101 | << getPassesString(Prefix) << "' passes: "; |
| 102 | |
| 103 | Module *OriginalInput = BD.Program; |
| 104 | BD.Program = PrefixOutput; |
| 105 | if (BD.runPasses(Kept, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
| 106 | std::cerr << BD.getToolName() << ": Error running this sequence of passes" |
| 107 | << " on the input program!\n"; |
| 108 | exit(1); |
| 109 | } |
| 110 | |
| 111 | // Run the result... |
| 112 | if (BD.diffProgram(Output, BytecodeResult, true/*delete bytecode*/)) { |
| 113 | std::cout << "nope.\n"; |
| 114 | delete OriginalInput; // We pruned down the original input... |
| 115 | return KeepPrefix; |
| 116 | } |
| 117 | |
| 118 | // Otherwise, we must not be running the bad pass anymore. |
| 119 | std::cout << "yup.\n"; // No miscompilation! |
| 120 | BD.Program = OriginalInput; // Restore original program |
| 121 | delete PrefixOutput; // Free experiment |
| 122 | return NoFailure; |
| 123 | } |
| 124 | |
| 125 | static void PrintFunctionList(const std::vector<Function*> &Funcs) { |
| 126 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 127 | if (i) std::cout << ", "; |
| 128 | std::cout << Funcs[i]->getName(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | class ReduceMiscompilingFunctions : public ListReducer<Function*> { |
| 134 | BugDriver &BD; |
| 135 | public: |
| 136 | ReduceMiscompilingFunctions(BugDriver &bd) : BD(bd) {} |
| 137 | |
| 138 | virtual TestResult doTest(const std::vector<Function*> &Prefix, |
| 139 | const std::vector<Function*> &Kept) { |
| 140 | if (TestFuncs(Kept, false)) |
| 141 | return KeepSuffix; |
Chris Lattner | a148ccb | 2003-04-24 19:32:42 +0000 | [diff] [blame] | 142 | if (!Prefix.empty() && TestFuncs(Prefix, false)) |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 143 | return KeepPrefix; |
| 144 | return NoFailure; |
| 145 | } |
| 146 | |
| 147 | bool TestFuncs(const std::vector<Function*> &Prefix, bool EmitBytecode); |
| 148 | }; |
| 149 | |
| 150 | // DeleteFunctionBody - "Remove" the function by deleting all of it's basic |
| 151 | // blocks, making it external. |
| 152 | // |
| 153 | static void DeleteFunctionBody(Function *F) { |
| 154 | // First, break circular use/def chain references... |
| 155 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 156 | I->dropAllReferences(); |
| 157 | |
| 158 | // Next, delete all of the basic blocks. |
| 159 | F->getBasicBlockList().clear(); |
| 160 | |
| 161 | assert(F->isExternal() && "This didn't make the function external!"); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs, |
| 166 | bool EmitBytecode) { |
| 167 | // Test to see if the function is misoptimized if we ONLY run it on the |
| 168 | // functions listed in Funcs. |
| 169 | if (!EmitBytecode) { |
| 170 | std::cout << "Checking to see if the program is misoptimized when these " |
| 171 | << "functions are run\nthrough the passes: "; |
| 172 | PrintFunctionList(Funcs); |
| 173 | std::cout << "\n"; |
| 174 | } else { |
| 175 | std::cout <<"Outputting reduced bytecode files which expose the problem:\n"; |
| 176 | } |
| 177 | |
| 178 | // First step: clone the module for the two halves of the program we want. |
| 179 | Module *ToOptimize = CloneModule(BD.Program); |
| 180 | |
| 181 | // Second step: Make sure functions & globals are all external so that linkage |
| 182 | // between the two modules will work. |
| 183 | for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I) |
| 184 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 185 | for (Module::giterator I = ToOptimize->gbegin(), E = ToOptimize->gend(); |
| 186 | I != E; ++I) |
| 187 | I->setLinkage(GlobalValue::ExternalLinkage); |
| 188 | |
| 189 | // Third step: make a clone of the externalized program for the non-optimized |
| 190 | // part. |
| 191 | Module *ToNotOptimize = CloneModule(ToOptimize); |
| 192 | |
| 193 | // Fourth step: Remove the test functions from the ToNotOptimize module, and |
| 194 | // all of the global variables. |
| 195 | for (unsigned i = 0, e = Funcs.size(); i != e; ++i) { |
| 196 | Function *TNOF = ToNotOptimize->getFunction(Funcs[i]->getName(), |
| 197 | Funcs[i]->getFunctionType()); |
| 198 | assert(TNOF && "Function doesn't exist in module!"); |
| 199 | DeleteFunctionBody(TNOF); // Function is now external in this module! |
| 200 | } |
| 201 | for (Module::giterator I = ToNotOptimize->gbegin(), E = ToNotOptimize->gend(); |
| 202 | I != E; ++I) |
| 203 | I->setInitializer(0); // Delete the initializer to make it external |
| 204 | |
| 205 | if (EmitBytecode) { |
| 206 | std::cout << " Non-optimized portion: "; |
| 207 | std::swap(BD.Program, ToNotOptimize); |
| 208 | BD.EmitProgressBytecode("tonotoptimize", true); |
| 209 | std::swap(BD.Program, ToNotOptimize); |
| 210 | } |
| 211 | |
| 212 | // Fifth step: Remove all functions from the ToOptimize module EXCEPT for the |
| 213 | // ones specified in Funcs. We know which ones these are because they are |
| 214 | // non-external in ToOptimize, but external in ToNotOptimize. |
| 215 | // |
| 216 | for (Module::iterator I = ToOptimize->begin(), E = ToOptimize->end();I!=E;++I) |
| 217 | if (!I->isExternal()) { |
| 218 | Function *TNOF = ToNotOptimize->getFunction(I->getName(), |
| 219 | I->getFunctionType()); |
| 220 | assert(TNOF && "Function doesn't exist in ToNotOptimize module??"); |
| 221 | if (!TNOF->isExternal()) |
| 222 | DeleteFunctionBody(I); |
| 223 | } |
| 224 | |
| 225 | if (EmitBytecode) { |
| 226 | std::cout << " Portion that is input to optimizer: "; |
| 227 | std::swap(BD.Program, ToOptimize); |
| 228 | BD.EmitProgressBytecode("tooptimize"); |
| 229 | std::swap(BD.Program, ToOptimize); |
| 230 | } |
| 231 | |
| 232 | // Sixth step: Run the optimization passes on ToOptimize, producing a |
| 233 | // transformed version of the functions being tested. |
| 234 | Module *OldProgram = BD.Program; |
| 235 | BD.Program = ToOptimize; |
| 236 | |
| 237 | if (!EmitBytecode) |
| 238 | std::cout << " Optimizing functions being tested: "; |
| 239 | std::string BytecodeResult; |
| 240 | if (BD.runPasses(BD.PassesToRun, BytecodeResult, false/*delete*/, |
| 241 | true/*quiet*/)) { |
| 242 | std::cerr << BD.getToolName() << ": Error running this sequence of passes" |
| 243 | << " on the input program!\n"; |
| 244 | exit(1); |
| 245 | } |
| 246 | |
| 247 | if (!EmitBytecode) |
| 248 | std::cout << "done.\n"; |
| 249 | |
| 250 | delete BD.Program; // Delete the old "ToOptimize" module |
| 251 | BD.Program = BD.ParseInputFile(BytecodeResult); |
| 252 | |
| 253 | if (EmitBytecode) { |
| 254 | std::cout << " 'tooptimize' after being optimized: "; |
| 255 | BD.EmitProgressBytecode("optimized", true); |
| 256 | } |
| 257 | |
| 258 | if (BD.Program == 0) { |
| 259 | std::cerr << BD.getToolName() << ": Error reading bytecode file '" |
| 260 | << BytecodeResult << "'!\n"; |
| 261 | exit(1); |
| 262 | } |
| 263 | removeFile(BytecodeResult); // No longer need the file on disk |
| 264 | |
| 265 | // Seventh step: Link the optimized part of the program back to the |
| 266 | // unoptimized part of the program. |
| 267 | // |
| 268 | if (LinkModules(BD.Program, ToNotOptimize, &BytecodeResult)) { |
| 269 | std::cerr << BD.getToolName() << ": Error linking modules together:" |
| 270 | << BytecodeResult << "\n"; |
| 271 | exit(1); |
| 272 | } |
| 273 | delete ToNotOptimize; // We are done with this module... |
| 274 | |
| 275 | if (EmitBytecode) { |
| 276 | std::cout << " Program as tested: "; |
| 277 | BD.EmitProgressBytecode("linked", true); |
| 278 | delete BD.Program; |
| 279 | BD.Program = OldProgram; |
| 280 | return false; // We don't need to actually execute the program here. |
| 281 | } |
| 282 | |
| 283 | std::cout << " Checking to see if the merged program executes correctly: "; |
| 284 | |
| 285 | // Eighth step: Execute the program. If it does not match the expected |
| 286 | // output, then 'Funcs' are being misoptimized! |
| 287 | bool Broken = BD.diffProgram(Output); |
| 288 | |
| 289 | delete BD.Program; // Delete the hacked up program |
| 290 | BD.Program = OldProgram; // Restore the original |
| 291 | |
| 292 | std::cout << (Broken ? "nope.\n" : "yup.\n"); |
| 293 | return Broken; |
| 294 | } |
| 295 | |
| 296 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 297 | /// debugMiscompilation - This method is used when the passes selected are not |
| 298 | /// crashing, but the generated output is semantically different from the |
| 299 | /// input. |
| 300 | /// |
| 301 | bool BugDriver::debugMiscompilation() { |
| 302 | std::cout << "*** Debugging miscompilation!\n"; |
| 303 | |
| 304 | // Set up the execution environment, selecting a method to run LLVM bytecode. |
| 305 | if (initializeExecutionEnvironment()) return true; |
| 306 | |
| 307 | // Run the raw input to see where we are coming from. If a reference output |
| 308 | // was specified, make sure that the raw output matches it. If not, it's a |
| 309 | // problem in the front-end or whatever produced the input code. |
| 310 | // |
| 311 | bool CreatedOutput = false; |
| 312 | if (Output.empty()) { |
| 313 | std::cout << "Generating reference output from raw program..."; |
| 314 | Output = executeProgram("bugpoint.reference.out"); |
| 315 | CreatedOutput = true; |
Chris Lattner | eea21dd | 2003-04-23 20:41:18 +0000 | [diff] [blame] | 316 | std::cout << " done! Reference output is: bugpoint.reference.out.\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 317 | } else if (diffProgram(Output)) { |
| 318 | std::cout << "\n*** Input program does not match reference diff!\n" |
| 319 | << " Must be problem with input source!\n"; |
| 320 | return false; // Problem found |
| 321 | } |
| 322 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 323 | // Figure out which transformations miscompile the input program. |
| 324 | unsigned OldSize = PassesToRun.size(); |
| 325 | ReduceMiscompilingPasses(*this).reduceList(PassesToRun); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 326 | |
| 327 | // Make sure something was miscompiled... |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 328 | if (PassesToRun.size() == OldSize) { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 329 | std::cerr << "*** Optimized program matches reference output! No problem " |
| 330 | << "detected...\nbugpoint can't help you with your problem!\n"; |
| 331 | return false; |
| 332 | } |
| 333 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 334 | std::cout << "\n*** Found miscompiling pass" |
| 335 | << (PassesToRun.size() == 1 ? "" : "es") << ": " |
| 336 | << getPassesString(PassesToRun) << "\n"; |
| 337 | EmitProgressBytecode("passinput"); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 339 | |
| 340 | // Okay, now that we have reduced the list of passes which are causing the |
| 341 | // failure, see if we can pin down which functions are being |
| 342 | // miscompiled... first build a list of all of the non-external functions in |
| 343 | // the program. |
| 344 | std::vector<Function*> MiscompiledFunctions; |
| 345 | for (Module::iterator I = Program->begin(), E = Program->end(); I != E; ++I) |
| 346 | if (!I->isExternal()) |
| 347 | MiscompiledFunctions.push_back(I); |
| 348 | |
| 349 | // Do the reduction... |
| 350 | ReduceMiscompilingFunctions(*this).reduceList(MiscompiledFunctions); |
| 351 | |
| 352 | std::cout << "\n*** The following functions are being miscompiled: "; |
| 353 | PrintFunctionList(MiscompiledFunctions); |
| 354 | std::cout << "\n"; |
| 355 | |
| 356 | // Output a bunch of bytecode files for the user... |
| 357 | ReduceMiscompilingFunctions(*this).TestFuncs(MiscompiledFunctions, true); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 358 | |
| 359 | if (CreatedOutput) removeFile(Output); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 360 | return false; |
| 361 | } |