OptimizerDriver.cpp revision 640f22e66d90439857a97a83896ee68c4f7128c9
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 "SystemUtils.h" 13#include "llvm/PassManager.h" 14#include "llvm/Analysis/Verifier.h" 15#include "llvm/Bytecode/WriteBytecodePass.h" 16#include "llvm/Target/TargetData.h" 17#include <sys/types.h> 18#include <sys/wait.h> 19#include <unistd.h> 20#include <stdlib.h> 21#include <fstream> 22 23/// writeProgramToFile - This writes the current "Program" to the named bytecode 24/// file. If an error occurs, true is returned. 25/// 26bool BugDriver::writeProgramToFile(const std::string &Filename, 27 Module *M) const { 28 std::ofstream Out(Filename.c_str()); 29 if (!Out.good()) return true; 30 WriteBytecodeToFile(M ? M : Program, Out); 31 return false; 32} 33 34 35/// EmitProgressBytecode - This function is used to output the current Program 36/// to a file named "bugpoing-ID.bc". 37/// 38void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) { 39 // Output the input to the current pass to a bytecode file, emit a message 40 // telling the user how to reproduce it: opt -foo blah.bc 41 // 42 std::string Filename = "bugpoint-" + ID + ".bc"; 43 if (writeProgramToFile(Filename)) { 44 std::cerr << "Error opening file '" << Filename << "' for writing!\n"; 45 return; 46 } 47 48 std::cout << "Emitted bytecode to '" << Filename << "'\n"; 49 if (NoFlyer) return; 50 std::cout << "\n*** You can reproduce the problem with: "; 51 52 unsigned PassType = PassesToRun[0]->getPassType(); 53 for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i) 54 PassType &= PassesToRun[i]->getPassType(); 55 56 if (PassType & PassInfo::Analysis) 57 std::cout << "analyze"; 58 else if (PassType & PassInfo::Optimization) 59 std::cout << "opt"; 60 else if (PassType & PassInfo::LLC) 61 std::cout << "llc"; 62 else 63 std::cout << "bugpoint"; 64 std::cout << " " << Filename << " "; 65 std::cout << getPassesString(PassesToRun) << "\n"; 66} 67 68/// FIXME: This should be parameterizable!! 69static TargetData TD("bugpoint target"); 70 71static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes, 72 const std::string &OutFilename) { 73 std::ofstream OutFile(OutFilename.c_str()); 74 if (!OutFile.good()) { 75 std::cerr << "Error opening bytecode file: " << OutFilename << "\n"; 76 exit(1); 77 } 78 79 PassManager PM; 80 for (unsigned i = 0, e = Passes.size(); i != e; ++i) { 81 if (Passes[i]->getNormalCtor()) 82 PM.add(Passes[i]->getNormalCtor()()); 83 else if (Passes[i]->getDataCtor()) 84 PM.add(Passes[i]->getDataCtor()(TD)); // Provide dummy target data... 85 else 86 std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName() 87 << "\n"; 88 } 89 // Check that the module is well formed on completion of optimization 90 PM.add(createVerifierPass()); 91 92 // Write bytecode out to disk as the last step... 93 PM.add(new WriteBytecodePass(&OutFile)); 94 95 // Run all queued passes. 96 PM.run(*Program); 97} 98 99/// runPasses - Run the specified passes on Program, outputting a bytecode file 100/// and writting the filename into OutputFile if successful. If the 101/// optimizations fail for some reason (optimizer crashes), return true, 102/// otherwise return false. If DeleteOutput is set to true, the bytecode is 103/// deleted on success, and the filename string is undefined. This prints to 104/// cout a single line message indicating whether compilation was successful or 105/// failed. 106/// 107bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes, 108 std::string &OutputFilename, bool DeleteOutput, 109 bool Quiet) const{ 110 std::cout << std::flush; 111 OutputFilename = getUniqueFilename("bugpoint-output.bc"); 112 113 pid_t child_pid; 114 switch (child_pid = fork()) { 115 case -1: // Error occurred 116 std::cerr << ToolName << ": Error forking!\n"; 117 exit(1); 118 case 0: // Child process runs passes. 119 RunChild(Program, Passes, OutputFilename); 120 exit(0); // If we finish successfully, return 0! 121 default: // Parent continues... 122 break; 123 } 124 125 // Wait for the child process to get done. 126 int Status; 127 if (wait(&Status) != child_pid) { 128 std::cerr << "Error waiting for child process!\n"; 129 exit(1); 130 } 131 132 // If we are supposed to delete the bytecode file, remove it now 133 // unconditionally... this may fail if the file was never created, but that's 134 // ok. 135 if (DeleteOutput) 136 removeFile(OutputFilename); 137 138 if (!Quiet) std::cout << (Status ? "Crashed!\n" : "Success!\n"); 139 140 // Was the child successful? 141 return Status != 0; 142} 143