blob: 4c60f52cb5ee0b18bc65f07263c044e3d328dbe6 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- 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"
Chris Lattner218e26e2002-12-23 23:49:59 +000012#include "SystemUtils.h"
Chris Lattnerafade922002-11-20 22:28:10 +000013#include "llvm/PassManager.h"
14#include "llvm/Analysis/Verifier.h"
15#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerf2bcccf2003-01-22 23:24:11 +000016#include "llvm/Target/TargetData.h"
Chris Lattnerafade922002-11-20 22:28:10 +000017#include <sys/types.h>
18#include <sys/wait.h>
19#include <unistd.h>
20#include <stdlib.h>
21#include <fstream>
22
Chris Lattnerafade922002-11-20 22:28:10 +000023/// writeProgramToFile - This writes the current "Program" to the named bytecode
24/// file. If an error occurs, true is returned.
25///
Chris Lattner218e26e2002-12-23 23:49:59 +000026bool BugDriver::writeProgramToFile(const std::string &Filename,
27 Module *M) const {
Chris Lattnerafade922002-11-20 22:28:10 +000028 std::ofstream Out(Filename.c_str());
29 if (!Out.good()) return true;
Chris Lattner218e26e2002-12-23 23:49:59 +000030 WriteBytecodeToFile(M ? M : Program, Out);
Chris Lattnerafade922002-11-20 22:28:10 +000031 return false;
32}
33
34
35/// EmitProgressBytecode - This function is used to output the current Program
Misha Brukman265789f2003-07-21 21:58:16 +000036/// to a file named "bugpoint-ID.bc".
Chris Lattnerafade922002-11-20 22:28:10 +000037///
Chris Lattner640f22e2003-04-24 17:02:17 +000038void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
Chris Lattnerafade922002-11-20 22:28:10 +000039 // 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
Chris Lattner218e26e2002-12-23 23:49:59 +000048 std::cout << "Emitted bytecode to '" << Filename << "'\n";
Chris Lattner640f22e2003-04-24 17:02:17 +000049 if (NoFlyer) return;
Chris Lattnerafade922002-11-20 22:28:10 +000050 std::cout << "\n*** You can reproduce the problem with: ";
51
Chris Lattner640f22e2003-04-24 17:02:17 +000052 unsigned PassType = PassesToRun[0]->getPassType();
53 for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i)
54 PassType &= PassesToRun[i]->getPassType();
55
Chris Lattnerafade922002-11-20 22:28:10 +000056 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";
Chris Lattner640f22e2003-04-24 17:02:17 +000064 std::cout << " " << Filename << " ";
65 std::cout << getPassesString(PassesToRun) << "\n";
Chris Lattnerafade922002-11-20 22:28:10 +000066}
67
Chris Lattnerafade922002-11-20 22:28:10 +000068static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
69 const std::string &OutFilename) {
70 std::ofstream OutFile(OutFilename.c_str());
71 if (!OutFile.good()) {
72 std::cerr << "Error opening bytecode file: " << OutFilename << "\n";
73 exit(1);
74 }
75
76 PassManager PM;
Chris Lattner9c3b55e2003-04-24 19:13:02 +000077 // Make sure that the appropriate target data is always used...
78 PM.add(new TargetData("bugpoint", Program));
79
Chris Lattnerafade922002-11-20 22:28:10 +000080 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
81 if (Passes[i]->getNormalCtor())
82 PM.add(Passes[i]->getNormalCtor()());
83 else
84 std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
85 << "\n";
86 }
87 // Check that the module is well formed on completion of optimization
88 PM.add(createVerifierPass());
89
90 // Write bytecode out to disk as the last step...
91 PM.add(new WriteBytecodePass(&OutFile));
92
93 // Run all queued passes.
94 PM.run(*Program);
95}
96
97/// runPasses - Run the specified passes on Program, outputting a bytecode file
98/// and writting the filename into OutputFile if successful. If the
99/// optimizations fail for some reason (optimizer crashes), return true,
100/// otherwise return false. If DeleteOutput is set to true, the bytecode is
101/// deleted on success, and the filename string is undefined. This prints to
102/// cout a single line message indicating whether compilation was successful or
103/// failed.
104///
105bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
Chris Lattner218e26e2002-12-23 23:49:59 +0000106 std::string &OutputFilename, bool DeleteOutput,
107 bool Quiet) const{
Chris Lattnerafade922002-11-20 22:28:10 +0000108 std::cout << std::flush;
Chris Lattner218e26e2002-12-23 23:49:59 +0000109 OutputFilename = getUniqueFilename("bugpoint-output.bc");
Chris Lattnerafade922002-11-20 22:28:10 +0000110
Chris Lattnerafade922002-11-20 22:28:10 +0000111 pid_t child_pid;
112 switch (child_pid = fork()) {
113 case -1: // Error occurred
114 std::cerr << ToolName << ": Error forking!\n";
115 exit(1);
116 case 0: // Child process runs passes.
117 RunChild(Program, Passes, OutputFilename);
118 exit(0); // If we finish successfully, return 0!
119 default: // Parent continues...
120 break;
121 }
122
123 // Wait for the child process to get done.
124 int Status;
125 if (wait(&Status) != child_pid) {
126 std::cerr << "Error waiting for child process!\n";
127 exit(1);
128 }
129
130 // If we are supposed to delete the bytecode file, remove it now
131 // unconditionally... this may fail if the file was never created, but that's
132 // ok.
133 if (DeleteOutput)
134 removeFile(OutputFilename);
135
Chris Lattner24271cf2003-06-02 04:54:16 +0000136 bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
137
138 if (!Quiet) {
139 if (ExitedOK)
140 std::cout << "Success!\n";
141 else if (WIFEXITED(Status))
142 std::cout << "Exited with error code '" << WEXITSTATUS(Status) << "'\n";
143 else if (WIFSIGNALED(Status))
144 std::cout << "Crashed with signal #" << WTERMSIG(Status) << "\n";
145#ifdef WCOREDUMP
146 else if (WCOREDUMP(Status))
147 std::cout << "Dumped core\n";
148#endif
149 else
150 std::cout << "Failed for unknown reason!\n";
151 }
Chris Lattnerafade922002-11-20 22:28:10 +0000152
153 // Was the child successful?
Chris Lattner24271cf2003-06-02 04:54:16 +0000154 return !ExitedOK;
Chris Lattnerafade922002-11-20 22:28:10 +0000155}