blob: 424a7aad140a96a7827dfdece1bd93906566af7d [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//===----------------------------------------------------------------------===//
9//
Chris Lattnerafade922002-11-20 22:28:10 +000010//
11// This file defines an interface that allows bugpoint to run various passes
Misha Brukmancf00c4a2003-10-10 17:57:28 +000012// 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
Chris Lattnerafade922002-11-20 22:28:10 +000014// 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. :)
16//
17//===----------------------------------------------------------------------===//
18
19#include "BugDriver.h"
20#include "llvm/PassManager.h"
21#include "llvm/Analysis/Verifier.h"
22#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerf2bcccf2003-01-22 23:24:11 +000023#include "llvm/Target/TargetData.h"
Misha Brukman3d9cafa2003-08-07 21:42:28 +000024#include "Support/FileUtilities.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000025#include <fstream>
Misha Brukmane49603d2003-08-07 21:19:30 +000026#include <unistd.h>
Chris Lattnerafade922002-11-20 22:28:10 +000027#include <sys/types.h>
28#include <sys/wait.h>
Chris Lattnerafade922002-11-20 22:28:10 +000029
Chris Lattnerafade922002-11-20 22:28:10 +000030/// writeProgramToFile - This writes the current "Program" to the named bytecode
31/// file. If an error occurs, true is returned.
32///
Chris Lattner218e26e2002-12-23 23:49:59 +000033bool BugDriver::writeProgramToFile(const std::string &Filename,
34 Module *M) const {
Chris Lattnerafade922002-11-20 22:28:10 +000035 std::ofstream Out(Filename.c_str());
36 if (!Out.good()) return true;
Chris Lattner218e26e2002-12-23 23:49:59 +000037 WriteBytecodeToFile(M ? M : Program, Out);
Chris Lattnerafade922002-11-20 22:28:10 +000038 return false;
39}
40
41
42/// EmitProgressBytecode - This function is used to output the current Program
Misha Brukman265789f2003-07-21 21:58:16 +000043/// to a file named "bugpoint-ID.bc".
Chris Lattnerafade922002-11-20 22:28:10 +000044///
Chris Lattner640f22e2003-04-24 17:02:17 +000045void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
Chris Lattnerafade922002-11-20 22:28:10 +000046 // 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
48 //
49 std::string Filename = "bugpoint-" + ID + ".bc";
50 if (writeProgramToFile(Filename)) {
51 std::cerr << "Error opening file '" << Filename << "' for writing!\n";
52 return;
53 }
54
Chris Lattner218e26e2002-12-23 23:49:59 +000055 std::cout << "Emitted bytecode to '" << Filename << "'\n";
Chris Lattner640f22e2003-04-24 17:02:17 +000056 if (NoFlyer) return;
Chris Lattnerafade922002-11-20 22:28:10 +000057 std::cout << "\n*** You can reproduce the problem with: ";
58
Chris Lattner640f22e2003-04-24 17:02:17 +000059 unsigned PassType = PassesToRun[0]->getPassType();
60 for (unsigned i = 1, e = PassesToRun.size(); i != e; ++i)
61 PassType &= PassesToRun[i]->getPassType();
62
Chris Lattnerafade922002-11-20 22:28:10 +000063 if (PassType & PassInfo::Analysis)
64 std::cout << "analyze";
65 else if (PassType & PassInfo::Optimization)
66 std::cout << "opt";
67 else if (PassType & PassInfo::LLC)
68 std::cout << "llc";
69 else
70 std::cout << "bugpoint";
Chris Lattner640f22e2003-04-24 17:02:17 +000071 std::cout << " " << Filename << " ";
72 std::cout << getPassesString(PassesToRun) << "\n";
Chris Lattnerafade922002-11-20 22:28:10 +000073}
74
Chris Lattnerafade922002-11-20 22:28:10 +000075static 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";
80 exit(1);
81 }
82
83 PassManager PM;
Chris Lattner9c3b55e2003-04-24 19:13:02 +000084 // Make sure that the appropriate target data is always used...
85 PM.add(new TargetData("bugpoint", Program));
86
Chris Lattnerafade922002-11-20 22:28:10 +000087 for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
88 if (Passes[i]->getNormalCtor())
89 PM.add(Passes[i]->getNormalCtor()());
90 else
91 std::cerr << "Cannot create pass yet: " << Passes[i]->getPassName()
92 << "\n";
93 }
94 // Check that the module is well formed on completion of optimization
95 PM.add(createVerifierPass());
96
97 // Write bytecode out to disk as the last step...
98 PM.add(new WriteBytecodePass(&OutFile));
99
100 // Run all queued passes.
101 PM.run(*Program);
102}
103
104/// runPasses - Run the specified passes on Program, outputting a bytecode file
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000105/// and writing the filename into OutputFile if successful. If the
Chris Lattnerafade922002-11-20 22:28:10 +0000106/// 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
110/// failed.
111///
112bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
Chris Lattner218e26e2002-12-23 23:49:59 +0000113 std::string &OutputFilename, bool DeleteOutput,
114 bool Quiet) const{
Chris Lattnerafade922002-11-20 22:28:10 +0000115 std::cout << std::flush;
Chris Lattner218e26e2002-12-23 23:49:59 +0000116 OutputFilename = getUniqueFilename("bugpoint-output.bc");
Chris Lattnerafade922002-11-20 22:28:10 +0000117
Chris Lattnerafade922002-11-20 22:28:10 +0000118 pid_t child_pid;
119 switch (child_pid = fork()) {
120 case -1: // Error occurred
121 std::cerr << ToolName << ": Error forking!\n";
122 exit(1);
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...
127 break;
128 }
129
130 // Wait for the child process to get done.
131 int Status;
132 if (wait(&Status) != child_pid) {
133 std::cerr << "Error waiting for child process!\n";
134 exit(1);
135 }
136
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
139 // ok.
140 if (DeleteOutput)
141 removeFile(OutputFilename);
142
Chris Lattner24271cf2003-06-02 04:54:16 +0000143 bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
144
145 if (!Quiet) {
146 if (ExitedOK)
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";
152#ifdef WCOREDUMP
153 else if (WCOREDUMP(Status))
154 std::cout << "Dumped core\n";
155#endif
156 else
157 std::cout << "Failed for unknown reason!\n";
158 }
Chris Lattnerafade922002-11-20 22:28:10 +0000159
160 // Was the child successful?
Chris Lattner24271cf2003-06-02 04:54:16 +0000161 return !ExitedOK;
Chris Lattnerafade922002-11-20 22:28:10 +0000162}