1 //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
11 // This file defines an interface that allows bugpoint to run various passes
12 // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint
13 // may have its own bugs, but that's another story...). It achieves this by
14 // forking a copy of itself and having the child process do the optimizations.
15 // If this client dies, we can always fork a new one. :)
17 //===----------------------------------------------------------------------===//
19 #include "BugDriver.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "llvm/Bytecode/WriteBytecodePass.h"
23 #include "llvm/Target/TargetData.h"
24 #include "Support/FileUtilities.h"
27 #include <sys/types.h>
30 /// writeProgramToFile - This writes the current "Program" to the named bytecode
31 /// file. If an error occurs, true is returned.
33 bool BugDriver::writeProgramToFile(const std::string &Filename,
35 std::ofstream Out(Filename.c_str());
36 if (!Out.good()) return true;
37 WriteBytecodeToFile(M ? M : Program, Out);
42 /// EmitProgressBytecode - This function is used to output the current Program
43 /// to a file named "bugpoint-ID.bc".
45 void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
46 // Output the input to the current pass to a bytecode file, emit a message
47 // telling the user how to reproduce it: opt -foo blah.bc
49 std::string Filename = "bugpoint-" + ID + ".bc";
50 if (writeProgramToFile(Filename)) {
51 std::cerr << "Error opening file '" << Filename << "' for writing!\n";
55 std::cout << "Emitted bytecode to '" << Filename << "'\n";
57 std::cout << "\n*** You can reproduce the problem with: ";
59 unsigned PassType = PassesToRun[0]->getPassType();
60 for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i)
61 PassType &= PassesToRun[i]->getPassType();
63 if (PassType & PassInfo::Analysis)
64 std::cout << "analyze";
65 else if (PassType & PassInfo::Optimization)
67 else if (PassType & PassInfo::LLC)
70 std::cout << "bugpoint";
71 std::cout << " " << Filename << " ";
72 std::cout << getPassesString(PassesToRun) << "\n";
75 static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
76 const std::string &OutFilename) {
77 std::ofstream OutFile(OutFilename.c_str());
78 if (!OutFile.good()) {
79 std::cerr << "Error opening bytecode file: " << OutFilename << "\n";
84 // Make sure that the appropriate target data is always used...
85 PM.add(new TargetData("bugpoint", Program));
87 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
88 if (Passes[i]->getNormalCtor())
89 PM.add(Passes[i]->getNormalCtor()());
91 std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
94 // Check that the module is well formed on completion of optimization
95 PM.add(createVerifierPass());
97 // Write bytecode out to disk as the last step...
98 PM.add(new WriteBytecodePass(&OutFile));
100 // Run all queued passes.
104 /// runPasses - Run the specified passes on Program, outputting a bytecode file
105 /// and writing the filename into OutputFile if successful. If the
106 /// optimizations fail for some reason (optimizer crashes), return true,
107 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
108 /// deleted on success, and the filename string is undefined. This prints to
109 /// cout a single line message indicating whether compilation was successful or
112 bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
113 std::string &OutputFilename, bool DeleteOutput,
115 std::cout << std::flush;
116 OutputFilename = getUniqueFilename("bugpoint-output.bc");
119 switch (child_pid = fork()) {
120 case -1: // Error occurred
121 std::cerr << ToolName << ": Error forking!\n";
123 case 0: // Child process runs passes.
124 RunChild(Program, Passes, OutputFilename);
125 exit(0); // If we finish successfully, return 0!
126 default: // Parent continues...
130 // Wait for the child process to get done.
132 if (wait(&Status) != child_pid) {
133 std::cerr << "Error waiting for child process!\n";
137 // If we are supposed to delete the bytecode file, remove it now
138 // unconditionally... this may fail if the file was never created, but that's
141 removeFile(OutputFilename);
143 bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
147 std::cout << "Success!\n";
148 else if (WIFEXITED(Status))
149 std::cout << "Exited with error code '" << WEXITSTATUS(Status) << "'\n";
150 else if (WIFSIGNALED(Status))
151 std::cout << "Crashed with signal #" << WTERMSIG(Status) << "\n";
153 else if (WCOREDUMP(Status))
154 std::cout << "Dumped core\n";
157 std::cout << "Failed for unknown reason!\n";
160 // Was the child successful?