blob: af9d1e5a9453a5680a1cf982874f6a86f524d718 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
10// This file defines an interface that allows bugpoint to run various passes
Misha Brukmancf00c4a2003-10-10 17:57:28 +000011// 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
Chris Lattnerafade922002-11-20 22:28:10 +000013// 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. :)
15//
16//===----------------------------------------------------------------------===//
17
18#include "BugDriver.h"
19#include "llvm/PassManager.h"
20#include "llvm/Analysis/Verifier.h"
21#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerf2bcccf2003-01-22 23:24:11 +000022#include "llvm/Target/TargetData.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000023#include "Support/FileUtilities.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000024#include <fstream>
Misha Brukmane49603d2003-08-07 21:19:30 +000025#include <unistd.h>
Chris Lattnerafade922002-11-20 22:28:10 +000026#include <sys/types.h>
27#include <sys/wait.h>
Chris Lattnerafade922002-11-20 22:28:10 +000028
Brian Gaeked0fde302003-11-11 22:41:34 +000029namespace llvm {
30
Chris Lattnerafade922002-11-20 22:28:10 +000031/// writeProgramToFile - This writes the current "Program" to the named bytecode
32/// file. If an error occurs, true is returned.
33///
Chris Lattner218e26e2002-12-23 23:49:59 +000034bool BugDriver::writeProgramToFile(const std::string &Filename,
35 Module *M) const {
Chris Lattnerafade922002-11-20 22:28:10 +000036 std::ofstream Out(Filename.c_str());
37 if (!Out.good()) return true;
Chris Lattner218e26e2002-12-23 23:49:59 +000038 WriteBytecodeToFile(M ? M : Program, Out);
Chris Lattnerafade922002-11-20 22:28:10 +000039 return false;
40}
41
42
43/// EmitProgressBytecode - This function is used to output the current Program
Misha Brukman265789f2003-07-21 21:58:16 +000044/// to a file named "bugpoint-ID.bc".
Chris Lattnerafade922002-11-20 22:28:10 +000045///
Chris Lattner640f22e2003-04-24 17:02:17 +000046void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
Chris Lattnerafade922002-11-20 22:28:10 +000047 // 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
49 //
50 std::string Filename = "bugpoint-" + ID + ".bc";
51 if (writeProgramToFile(Filename)) {
52 std::cerr << "Error opening file '" << Filename << "' for writing!\n";
53 return;
54 }
55
Chris Lattner218e26e2002-12-23 23:49:59 +000056 std::cout << "Emitted bytecode to '" << Filename << "'\n";
Chris Lattner640f22e2003-04-24 17:02:17 +000057 if (NoFlyer) return;
Chris Lattnerafade922002-11-20 22:28:10 +000058 std::cout << "\n*** You can reproduce the problem with: ";
59
Chris Lattner640f22e2003-04-24 17:02:17 +000060 unsigned PassType = PassesToRun[0]->getPassType();
61 for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i)
62 PassType &= PassesToRun[i]->getPassType();
63
Chris Lattnerafade922002-11-20 22:28:10 +000064 if (PassType & PassInfo::Analysis)
65 std::cout << "analyze";
66 else if (PassType & PassInfo::Optimization)
67 std::cout << "opt";
68 else if (PassType & PassInfo::LLC)
69 std::cout << "llc";
70 else
71 std::cout << "bugpoint";
Chris Lattner640f22e2003-04-24 17:02:17 +000072 std::cout << " " << Filename << " ";
73 std::cout << getPassesString(PassesToRun) << "\n";
Chris Lattnerafade922002-11-20 22:28:10 +000074}
75
Chris Lattnerafade922002-11-20 22:28:10 +000076static 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";
81 exit(1);
82 }
83
84 PassManager PM;
Chris Lattner9c3b55e2003-04-24 19:13:02 +000085 // Make sure that the appropriate target data is always used...
86 PM.add(new TargetData("bugpoint", Program));
87
Chris Lattnerafade922002-11-20 22:28:10 +000088 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
89 if (Passes[i]->getNormalCtor())
90 PM.add(Passes[i]->getNormalCtor()());
91 else
92 std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
93 << "\n";
94 }
95 // Check that the module is well formed on completion of optimization
96 PM.add(createVerifierPass());
97
98 // Write bytecode out to disk as the last step...
99 PM.add(new WriteBytecodePass(&OutFile));
100
101 // Run all queued passes.
102 PM.run(*Program);
103}
104
105/// runPasses - Run the specified passes on Program, outputting a bytecode file
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000106/// and writing the filename into OutputFile if successful. If the
Chris Lattnerafade922002-11-20 22:28:10 +0000107/// 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
111/// failed.
112///
113bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
Chris Lattner218e26e2002-12-23 23:49:59 +0000114 std::string &OutputFilename, bool DeleteOutput,
115 bool Quiet) const{
Chris Lattnerafade922002-11-20 22:28:10 +0000116 std::cout << std::flush;
Chris Lattner218e26e2002-12-23 23:49:59 +0000117 OutputFilename = getUniqueFilename("bugpoint-output.bc");
Chris Lattnerafade922002-11-20 22:28:10 +0000118
Chris Lattnerafade922002-11-20 22:28:10 +0000119 pid_t child_pid;
120 switch (child_pid = fork()) {
121 case -1: // Error occurred
122 std::cerr << ToolName << ": Error forking!\n";
123 exit(1);
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...
128 break;
129 }
130
131 // Wait for the child process to get done.
132 int Status;
133 if (wait(&Status) != child_pid) {
134 std::cerr << "Error waiting for child process!\n";
135 exit(1);
136 }
137
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
140 // ok.
141 if (DeleteOutput)
142 removeFile(OutputFilename);
143
Chris Lattner24271cf2003-06-02 04:54:16 +0000144 bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
145
146 if (!Quiet) {
147 if (ExitedOK)
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";
153#ifdef WCOREDUMP
154 else if (WCOREDUMP(Status))
155 std::cout << "Dumped core\n";
156#endif
157 else
158 std::cout << "Failed for unknown reason!\n";
159 }
Chris Lattnerafade922002-11-20 22:28:10 +0000160
161 // Was the child successful?
Chris Lattner24271cf2003-06-02 04:54:16 +0000162 return !ExitedOK;
Chris Lattnerafade922002-11-20 22:28:10 +0000163}
Brian Gaeked0fde302003-11-11 22:41:34 +0000164
165} // End llvm namespace