Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===// |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines an interface that allows bugpoint to run various passes |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 11 | // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint |
| 12 | // may have its own bugs, but that's another story...). It achieves this by |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 13 | // forking a copy of itself and having the child process do the optimizations. |
| 14 | // If this client dies, we can always fork a new one. :) |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 18 | // Note: as a short term hack, the old Unix-specific code and platform- |
| 19 | // independent code co-exist via conditional compilation until it is verified |
| 20 | // that the new code works correctly on Unix. |
| 21 | |
Jeff Cohen | 741c118 | 2005-01-22 17:37:13 +0000 | [diff] [blame^] | 22 | //#define PLATFORMINDEPENDENT |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 23 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 24 | #include "BugDriver.h" |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 26 | #include "llvm/PassManager.h" |
| 27 | #include "llvm/Analysis/Verifier.h" |
| 28 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | f2bcccf | 2003-01-22 23:24:11 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/FileUtilities.h" |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 31 | #include "llvm/System/Path.h" |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 32 | #include <fstream> |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 33 | #ifndef PLATFORMINDEPENDENT |
Misha Brukman | e49603d | 2003-08-07 21:19:30 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
| 36 | #include <sys/wait.h> |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 37 | #endif |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 40 | /// writeProgramToFile - This writes the current "Program" to the named bytecode |
| 41 | /// file. If an error occurs, true is returned. |
| 42 | /// |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 43 | bool BugDriver::writeProgramToFile(const std::string &Filename, |
| 44 | Module *M) const { |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 45 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 46 | std::ios::binary; |
| 47 | std::ofstream Out(Filename.c_str(), io_mode); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 48 | if (!Out.good()) return true; |
Reid Spencer | 34b9071 | 2004-11-07 05:43:51 +0000 | [diff] [blame] | 49 | WriteBytecodeToFile(M ? M : Program, Out, /*compression=*/true); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /// EmitProgressBytecode - This function is used to output the current Program |
Misha Brukman | 265789f | 2003-07-21 21:58:16 +0000 | [diff] [blame] | 55 | /// to a file named "bugpoint-ID.bc". |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 56 | /// |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 57 | void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) { |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 58 | // Output the input to the current pass to a bytecode file, emit a message |
| 59 | // telling the user how to reproduce it: opt -foo blah.bc |
| 60 | // |
| 61 | std::string Filename = "bugpoint-" + ID + ".bc"; |
| 62 | if (writeProgramToFile(Filename)) { |
| 63 | std::cerr << "Error opening file '" << Filename << "' for writing!\n"; |
| 64 | return; |
| 65 | } |
| 66 | |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 67 | std::cout << "Emitted bytecode to '" << Filename << "'\n"; |
Chris Lattner | ca00512 | 2004-02-18 23:24:56 +0000 | [diff] [blame] | 68 | if (NoFlyer || PassesToRun.empty()) return; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 69 | std::cout << "\n*** You can reproduce the problem with: "; |
| 70 | |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 71 | unsigned PassType = PassesToRun[0]->getPassType(); |
| 72 | for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i) |
| 73 | PassType &= PassesToRun[i]->getPassType(); |
| 74 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 75 | if (PassType & PassInfo::Analysis) |
| 76 | std::cout << "analyze"; |
| 77 | else if (PassType & PassInfo::Optimization) |
| 78 | std::cout << "opt"; |
| 79 | else if (PassType & PassInfo::LLC) |
| 80 | std::cout << "llc"; |
| 81 | else |
| 82 | std::cout << "bugpoint"; |
Chris Lattner | 640f22e | 2003-04-24 17:02:17 +0000 | [diff] [blame] | 83 | std::cout << " " << Filename << " "; |
| 84 | std::cout << getPassesString(PassesToRun) << "\n"; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 87 | static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes, |
| 88 | const std::string &OutFilename) { |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 89 | std::ios::openmode io_mode = std::ios::out | std::ios::trunc | |
| 90 | std::ios::binary; |
| 91 | std::ofstream OutFile(OutFilename.c_str(), io_mode); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 92 | if (!OutFile.good()) { |
| 93 | std::cerr << "Error opening bytecode file: " << OutFilename << "\n"; |
| 94 | exit(1); |
| 95 | } |
| 96 | |
| 97 | PassManager PM; |
Chris Lattner | 9c3b55e | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 98 | // Make sure that the appropriate target data is always used... |
| 99 | PM.add(new TargetData("bugpoint", Program)); |
| 100 | |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 101 | for (unsigned i = 0, e = Passes.size(); i != e; ++i) { |
| 102 | if (Passes[i]->getNormalCtor()) |
| 103 | PM.add(Passes[i]->getNormalCtor()()); |
| 104 | else |
| 105 | std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName() |
| 106 | << "\n"; |
| 107 | } |
| 108 | // Check that the module is well formed on completion of optimization |
| 109 | PM.add(createVerifierPass()); |
| 110 | |
| 111 | // Write bytecode out to disk as the last step... |
| 112 | PM.add(new WriteBytecodePass(&OutFile)); |
| 113 | |
| 114 | // Run all queued passes. |
| 115 | PM.run(*Program); |
| 116 | } |
| 117 | |
| 118 | /// runPasses - Run the specified passes on Program, outputting a bytecode file |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 119 | /// and writing the filename into OutputFile if successful. If the |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 120 | /// optimizations fail for some reason (optimizer crashes), return true, |
| 121 | /// otherwise return false. If DeleteOutput is set to true, the bytecode is |
| 122 | /// deleted on success, and the filename string is undefined. This prints to |
| 123 | /// cout a single line message indicating whether compilation was successful or |
| 124 | /// failed. |
| 125 | /// |
| 126 | bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes, |
Chris Lattner | 218e26e | 2002-12-23 23:49:59 +0000 | [diff] [blame] | 127 | std::string &OutputFilename, bool DeleteOutput, |
| 128 | bool Quiet) const{ |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 129 | std::cout << std::flush; |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 130 | sys::Path uniqueFilename("bugpoint-output.bc"); |
| 131 | uniqueFilename.makeUnique(); |
| 132 | OutputFilename = uniqueFilename.toString(); |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 133 | |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 134 | #ifndef PLATFORMINDEPENDENT |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 135 | pid_t child_pid; |
| 136 | switch (child_pid = fork()) { |
| 137 | case -1: // Error occurred |
| 138 | std::cerr << ToolName << ": Error forking!\n"; |
| 139 | exit(1); |
| 140 | case 0: // Child process runs passes. |
| 141 | RunChild(Program, Passes, OutputFilename); |
| 142 | exit(0); // If we finish successfully, return 0! |
| 143 | default: // Parent continues... |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | // Wait for the child process to get done. |
| 148 | int Status; |
| 149 | if (wait(&Status) != child_pid) { |
| 150 | std::cerr << "Error waiting for child process!\n"; |
| 151 | exit(1); |
| 152 | } |
| 153 | |
Chris Lattner | 24271cf | 2003-06-02 04:54:16 +0000 | [diff] [blame] | 154 | bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0; |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 155 | #else |
| 156 | bool ExitedOK = false; |
| 157 | #endif |
Chris Lattner | b83c0f3 | 2004-05-12 02:55:45 +0000 | [diff] [blame] | 158 | |
| 159 | // If we are supposed to delete the bytecode file or if the passes crashed, |
| 160 | // remove it now. This may fail if the file was never created, but that's ok. |
| 161 | if (DeleteOutput || !ExitedOK) |
Reid Spencer | 5f76760 | 2004-12-16 23:04:20 +0000 | [diff] [blame] | 162 | sys::Path(OutputFilename).destroyFile(); |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 163 | |
| 164 | #ifndef PLATFORMINDEPENDENT |
Chris Lattner | 24271cf | 2003-06-02 04:54:16 +0000 | [diff] [blame] | 165 | if (!Quiet) { |
| 166 | if (ExitedOK) |
| 167 | std::cout << "Success!\n"; |
| 168 | else if (WIFEXITED(Status)) |
| 169 | std::cout << "Exited with error code '" << WEXITSTATUS(Status) << "'\n"; |
| 170 | else if (WIFSIGNALED(Status)) |
| 171 | std::cout << "Crashed with signal #" << WTERMSIG(Status) << "\n"; |
| 172 | #ifdef WCOREDUMP |
| 173 | else if (WCOREDUMP(Status)) |
| 174 | std::cout << "Dumped core\n"; |
| 175 | #endif |
| 176 | else |
| 177 | std::cout << "Failed for unknown reason!\n"; |
| 178 | } |
Jeff Cohen | 5fb6ed4 | 2005-01-22 17:36:17 +0000 | [diff] [blame] | 179 | #endif |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 180 | |
| 181 | // Was the child successful? |
Chris Lattner | 24271cf | 2003-06-02 04:54:16 +0000 | [diff] [blame] | 182 | return !ExitedOK; |
Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 183 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 185 | |
| 186 | /// runPassesOn - Carefully run the specified set of pass on the specified |
| 187 | /// module, returning the transformed module on success, or a null pointer on |
| 188 | /// failure. |
| 189 | Module *BugDriver::runPassesOn(Module *M, |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 190 | const std::vector<const PassInfo*> &Passes, |
| 191 | bool AutoDebugCrashes) { |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 192 | Module *OldProgram = swapProgramIn(M); |
| 193 | std::string BytecodeResult; |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 194 | if (runPasses(Passes, BytecodeResult, false/*delete*/, true/*quiet*/)) { |
| 195 | if (AutoDebugCrashes) { |
| 196 | std::cerr << " Error running this sequence of passes" |
| 197 | << " on the input program!\n"; |
| 198 | delete OldProgram; |
| 199 | EmitProgressBytecode("pass-error", false); |
| 200 | exit(debugOptimizerCrash()); |
| 201 | } |
Chris Lattner | 44a64bc | 2004-03-14 21:37:41 +0000 | [diff] [blame] | 202 | swapProgramIn(OldProgram); |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 203 | return 0; |
Chris Lattner | 0a00256 | 2004-03-14 21:21:57 +0000 | [diff] [blame] | 204 | } |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 205 | |
| 206 | // Restore the current program. |
| 207 | swapProgramIn(OldProgram); |
| 208 | |
| 209 | Module *Ret = ParseInputFile(BytecodeResult); |
| 210 | if (Ret == 0) { |
| 211 | std::cerr << getToolName() << ": Error reading bytecode file '" |
| 212 | << BytecodeResult << "'!\n"; |
| 213 | exit(1); |
| 214 | } |
Reid Spencer | 5f76760 | 2004-12-16 23:04:20 +0000 | [diff] [blame] | 215 | sys::Path(BytecodeResult).destroyFile(); // No longer need the file on disk |
Chris Lattner | 3b6441e | 2004-03-14 21:17:03 +0000 | [diff] [blame] | 216 | return Ret; |
| 217 | } |