1 //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
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. :)
9 //===----------------------------------------------------------------------===//
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>
21 /// removeFile - Delete the specified file
23 void BugDriver::removeFile(const std::string &Filename) const {
24 unlink(Filename.c_str());
27 /// writeProgramToFile - This writes the current "Program" to the named bytecode
28 /// file. If an error occurs, true is returned.
30 bool BugDriver::writeProgramToFile(const std::string &Filename) const {
31 std::ofstream Out(Filename.c_str());
32 if (!Out.good()) return true;
34 WriteBytecodeToFile(Program, Out);
39 /// EmitProgressBytecode - This function is used to output the current Program
40 /// to a file named "bugpoing-ID.bc".
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
47 std::string Filename = "bugpoint-" + ID + ".bc";
48 if (writeProgramToFile(Filename)) {
49 std::cerr << "Error opening file '" << Filename << "' for writing!\n";
53 std::cout << "Emitted bytecode to 'bugpoint-" << Filename << ".bc'\n";
54 std::cout << "\n*** You can reproduce the problem with: ";
56 unsigned PassType = Pass->getPassType();
57 if (PassType & PassInfo::Analysis)
58 std::cout << "analyze";
59 else if (PassType & PassInfo::Optimization)
61 else if (PassType & PassInfo::LLC)
64 std::cout << "bugpoint";
65 std::cout << " " << Filename << " -" << Pass->getPassArgument() << "\n";
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";
78 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
79 if (Passes[i]->getNormalCtor())
80 PM.add(Passes[i]->getNormalCtor()());
82 std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
85 // Check that the module is well formed on completion of optimization
86 PM.add(createVerifierPass());
88 // Write bytecode out to disk as the last step...
89 PM.add(new WriteBytecodePass(&OutFile));
91 // Run all queued passes.
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
103 bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
104 std::string &OutputFilename, bool DeleteOutput) const{
105 std::cout << std::flush;
107 // Agree on a temporary file name to use....
108 char FNBuffer[] = "bugpoint-output.bc-XXXXXX";
110 if ((TempFD = mkstemp(FNBuffer)) == -1) {
111 std::cerr << ToolName << ": ERROR: Cannot create temporary"
112 << " file in the current directory!\n";
115 OutputFilename = FNBuffer;
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...
122 switch (child_pid = fork()) {
123 case -1: // Error occurred
124 std::cerr << ToolName << ": Error forking!\n";
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...
133 // Wait for the child process to get done.
135 if (wait(&Status) != child_pid) {
136 std::cerr << "Error waiting for child process!\n";
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
144 removeFile(OutputFilename);
146 std::cout << (Status ? "Crashed!\n" : "Success!\n");
148 // Was the child successful?