Chris Lattner | afade92 | 2002-11-20 22:28:10 +0000 | [diff] [blame] | 1 | //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===// |
| 2 | // |
| 3 | // This file defines an interface that allows bugpoint to run various passes |
| 4 | // without the threat of a buggy pass corrupting bugpoint (of course bugpoint |
| 5 | // may have it's own bugs, but that's another story...). It acheives this by |
| 6 | // forking a copy of itself and having the child process do the optimizations. |
| 7 | // If this client dies, we can always fork a new one. :) |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "BugDriver.h" |
| 12 | #include "llvm/PassManager.h" |
| 13 | #include "llvm/Analysis/Verifier.h" |
| 14 | #include "llvm/Bytecode/WriteBytecodePass.h" |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/wait.h> |
| 17 | #include <unistd.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <fstream> |
| 20 | |
| 21 | /// removeFile - Delete the specified file |
| 22 | /// |
| 23 | void BugDriver::removeFile(const std::string &Filename) const { |
| 24 | unlink(Filename.c_str()); |
| 25 | } |
| 26 | |
| 27 | /// writeProgramToFile - This writes the current "Program" to the named bytecode |
| 28 | /// file. If an error occurs, true is returned. |
| 29 | /// |
| 30 | bool BugDriver::writeProgramToFile(const std::string &Filename) const { |
| 31 | std::ofstream Out(Filename.c_str()); |
| 32 | if (!Out.good()) return true; |
| 33 | |
| 34 | WriteBytecodeToFile(Program, Out); |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /// EmitProgressBytecode - This function is used to output the current Program |
| 40 | /// to a file named "bugpoing-ID.bc". |
| 41 | /// |
| 42 | void BugDriver::EmitProgressBytecode(const PassInfo *Pass, |
| 43 | const std::string &ID) { |
| 44 | // Output the input to the current pass to a bytecode file, emit a message |
| 45 | // telling the user how to reproduce it: opt -foo blah.bc |
| 46 | // |
| 47 | std::string Filename = "bugpoint-" + ID + ".bc"; |
| 48 | if (writeProgramToFile(Filename)) { |
| 49 | std::cerr << "Error opening file '" << Filename << "' for writing!\n"; |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | std::cout << "Emitted bytecode to 'bugpoint-" << Filename << ".bc'\n"; |
| 54 | std::cout << "\n*** You can reproduce the problem with: "; |
| 55 | |
| 56 | unsigned PassType = Pass->getPassType(); |
| 57 | if (PassType & PassInfo::Analysis) |
| 58 | std::cout << "analyze"; |
| 59 | else if (PassType & PassInfo::Optimization) |
| 60 | std::cout << "opt"; |
| 61 | else if (PassType & PassInfo::LLC) |
| 62 | std::cout << "llc"; |
| 63 | else |
| 64 | std::cout << "bugpoint"; |
| 65 | std::cout << " " << Filename << " -" << Pass->getPassArgument() << "\n"; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes, |
| 70 | const std::string &OutFilename) { |
| 71 | std::ofstream OutFile(OutFilename.c_str()); |
| 72 | if (!OutFile.good()) { |
| 73 | std::cerr << "Error opening bytecode file: " << OutFilename << "\n"; |
| 74 | exit(1); |
| 75 | } |
| 76 | |
| 77 | PassManager PM; |
| 78 | for (unsigned i = 0, e = Passes.size(); i != e; ++i) { |
| 79 | if (Passes[i]->getNormalCtor()) |
| 80 | PM.add(Passes[i]->getNormalCtor()()); |
| 81 | else |
| 82 | std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName() |
| 83 | << "\n"; |
| 84 | } |
| 85 | // Check that the module is well formed on completion of optimization |
| 86 | PM.add(createVerifierPass()); |
| 87 | |
| 88 | // Write bytecode out to disk as the last step... |
| 89 | PM.add(new WriteBytecodePass(&OutFile)); |
| 90 | |
| 91 | // Run all queued passes. |
| 92 | PM.run(*Program); |
| 93 | } |
| 94 | |
| 95 | /// runPasses - Run the specified passes on Program, outputting a bytecode file |
| 96 | /// and writting the filename into OutputFile if successful. If the |
| 97 | /// optimizations fail for some reason (optimizer crashes), return true, |
| 98 | /// otherwise return false. If DeleteOutput is set to true, the bytecode is |
| 99 | /// deleted on success, and the filename string is undefined. This prints to |
| 100 | /// cout a single line message indicating whether compilation was successful or |
| 101 | /// failed. |
| 102 | /// |
| 103 | bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes, |
| 104 | std::string &OutputFilename, bool DeleteOutput) const{ |
| 105 | std::cout << std::flush; |
| 106 | |
| 107 | // Agree on a temporary file name to use.... |
| 108 | char FNBuffer[] = "bugpoint-output.bc-XXXXXX"; |
| 109 | int TempFD; |
| 110 | if ((TempFD = mkstemp(FNBuffer)) == -1) { |
| 111 | std::cerr << ToolName << ": ERROR: Cannot create temporary" |
| 112 | << " file in the current directory!\n"; |
| 113 | exit(1); |
| 114 | } |
| 115 | OutputFilename = FNBuffer; |
| 116 | |
| 117 | // We don't need to hold the temp file descriptor... we will trust that noone |
| 118 | // will overwrite/delete the file while we are working on it... |
| 119 | close(TempFD); |
| 120 | |
| 121 | pid_t child_pid; |
| 122 | switch (child_pid = fork()) { |
| 123 | case -1: // Error occurred |
| 124 | std::cerr << ToolName << ": Error forking!\n"; |
| 125 | exit(1); |
| 126 | case 0: // Child process runs passes. |
| 127 | RunChild(Program, Passes, OutputFilename); |
| 128 | exit(0); // If we finish successfully, return 0! |
| 129 | default: // Parent continues... |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | // Wait for the child process to get done. |
| 134 | int Status; |
| 135 | if (wait(&Status) != child_pid) { |
| 136 | std::cerr << "Error waiting for child process!\n"; |
| 137 | exit(1); |
| 138 | } |
| 139 | |
| 140 | // If we are supposed to delete the bytecode file, remove it now |
| 141 | // unconditionally... this may fail if the file was never created, but that's |
| 142 | // ok. |
| 143 | if (DeleteOutput) |
| 144 | removeFile(OutputFilename); |
| 145 | |
| 146 | std::cout << (Status ? "Crashed!\n" : "Success!\n"); |
| 147 | |
| 148 | // Was the child successful? |
| 149 | return Status != 0; |
| 150 | } |