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 //===----------------------------------------------------------------------===//
10 // This file defines an interface that allows bugpoint to run various passes
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
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. :)
16 //===----------------------------------------------------------------------===//
18 #include "BugDriver.h"
19 #include "llvm/Module.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>
31 /// writeProgramToFile - This writes the current "Program" to the named bytecode
32 /// file. If an error occurs, true is returned.
34 bool BugDriver::writeProgramToFile(const std::string &Filename,
36 std::ofstream Out(Filename.c_str());
37 if (!Out.good()) return true;
38 WriteBytecodeToFile(M ? M : Program, Out);
43 /// EmitProgressBytecode - This function is used to output the current Program
44 /// to a file named "bugpoint-ID.bc".
46 void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
47 // Output the input to the current pass to a bytecode file, emit a message
48 // telling the user how to reproduce it: opt -foo blah.bc
50 std::string Filename = "bugpoint-" + ID + ".bc";
51 if (writeProgramToFile(Filename)) {
52 std::cerr << "Error opening file '" << Filename << "' for writing!\n";
56 std::cout << "Emitted bytecode to '" << Filename << "'\n";
57 if (NoFlyer || PassesToRun.empty()) return;
58 std::cout << "\n*** You can reproduce the problem with: ";
60 unsigned PassType = PassesToRun[0]->getPassType();
61 for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i)
62 PassType &= PassesToRun[i]->getPassType();
64 if (PassType & PassInfo::Analysis)
65 std::cout << "analyze";
66 else if (PassType & PassInfo::Optimization)
68 else if (PassType & PassInfo::LLC)
71 std::cout << "bugpoint";
72 std::cout << " " << Filename << " ";
73 std::cout << getPassesString(PassesToRun) << "\n";
76 static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
77 const std::string &OutFilename) {
78 std::ofstream OutFile(OutFilename.c_str());
79 if (!OutFile.good()) {
80 std::cerr << "Error opening bytecode file: " << OutFilename << "\n";
85 // Make sure that the appropriate target data is always used...
86 PM.add(new TargetData("bugpoint", Program));
88 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
89 if (Passes[i]->getNormalCtor())
90 PM.add(Passes[i]->getNormalCtor()());
92 std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
95 // Check that the module is well formed on completion of optimization
96 PM.add(createVerifierPass());
98 // Write bytecode out to disk as the last step...
99 PM.add(new WriteBytecodePass(&OutFile));
101 // Run all queued passes.
105 /// runPasses - Run the specified passes on Program, outputting a bytecode file
106 /// and writing the filename into OutputFile if successful. If the
107 /// optimizations fail for some reason (optimizer crashes), return true,
108 /// otherwise return false. If DeleteOutput is set to true, the bytecode is
109 /// deleted on success, and the filename string is undefined. This prints to
110 /// cout a single line message indicating whether compilation was successful or
113 bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
114 std::string &OutputFilename, bool DeleteOutput,
116 std::cout << std::flush;
117 OutputFilename = getUniqueFilename("bugpoint-output.bc");
120 switch (child_pid = fork()) {
121 case -1: // Error occurred
122 std::cerr << ToolName << ": Error forking!\n";
124 case 0: // Child process runs passes.
125 RunChild(Program, Passes, OutputFilename);
126 exit(0); // If we finish successfully, return 0!
127 default: // Parent continues...
131 // Wait for the child process to get done.
133 if (wait(&Status) != child_pid) {
134 std::cerr << "Error waiting for child process!\n";
138 // If we are supposed to delete the bytecode file, remove it now
139 // unconditionally... this may fail if the file was never created, but that's
142 removeFile(OutputFilename);
144 bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
148 std::cout << "Success!\n";
149 else if (WIFEXITED(Status))
150 std::cout << "Exited with error code '" << WEXITSTATUS(Status) << "'\n";
151 else if (WIFSIGNALED(Status))
152 std::cout << "Crashed with signal #" << WTERMSIG(Status) << "\n";
154 else if (WCOREDUMP(Status))
155 std::cout << "Dumped core\n";
158 std::cout << "Failed for unknown reason!\n";
161 // Was the child successful?
166 /// runPassesOn - Carefully run the specified set of pass on the specified
167 /// module, returning the transformed module on success, or a null pointer on
169 Module *BugDriver::runPassesOn(Module *M,
170 const std::vector<const PassInfo*> &Passes,
171 bool AutoDebugCrashes) {
172 Module *OldProgram = swapProgramIn(M);
173 std::string BytecodeResult;
174 if (runPasses(Passes, BytecodeResult, false/*delete*/, true/*quiet*/)) {
175 if (AutoDebugCrashes) {
176 std::cerr << " Error running this sequence of passes"
177 << " on the input program!\n";
179 EmitProgressBytecode("pass-error", false);
180 exit(debugOptimizerCrash());
182 swapProgramIn(OldProgram);
186 // Restore the current program.
187 swapProgramIn(OldProgram);
189 Module *Ret = ParseInputFile(BytecodeResult);
191 std::cerr << getToolName() << ": Error reading bytecode file '"
192 << BytecodeResult << "'!\n";
195 removeFile(BytecodeResult); // No longer need the file on disk