1193323Sed//===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines an interface that allows bugpoint to run various passes
11193323Sed// without the threat of a buggy pass corrupting bugpoint (of course, bugpoint
12193323Sed// may have its own bugs, but that's another story...).  It achieves this by
13193323Sed// forking a copy of itself and having the child process do the optimizations.
14193323Sed// If this client dies, we can always fork a new one.  :)
15193323Sed//
16193323Sed//===----------------------------------------------------------------------===//
17193323Sed
18193323Sed#include "BugDriver.h"
19193323Sed#include "llvm/Analysis/Verifier.h"
20193323Sed#include "llvm/Bitcode/ReaderWriter.h"
21249423Sdim#include "llvm/IR/DataLayout.h"
22249423Sdim#include "llvm/IR/Module.h"
23249423Sdim#include "llvm/PassManager.h"
24193323Sed#include "llvm/Support/CommandLine.h"
25212793Sdim#include "llvm/Support/Debug.h"
26249423Sdim#include "llvm/Support/FileUtilities.h"
27218885Sdim#include "llvm/Support/Path.h"
28218885Sdim#include "llvm/Support/Program.h"
29249423Sdim#include "llvm/Support/SystemUtils.h"
30249423Sdim#include "llvm/Support/ToolOutputFile.h"
31193323Sed
32193323Sed#define DONT_GET_PLUGIN_LOADER_OPTION
33193323Sed#include "llvm/Support/PluginLoader.h"
34193323Sed
35193323Sed#include <fstream>
36263508Sdim
37193323Sedusing namespace llvm;
38193323Sed
39198090Srdivackynamespace llvm {
40198090Srdivacky  extern cl::opt<std::string> OutputPrefix;
41198090Srdivacky}
42193323Sed
43193323Sednamespace {
44193323Sed  // ChildOutput - This option captures the name of the child output file that
45193323Sed  // is set up by the parent bugpoint process
46193323Sed  cl::opt<std::string> ChildOutput("child-output", cl::ReallyHidden);
47263508Sdim  cl::opt<std::string> OptCmd("opt-command", cl::init(""),
48263508Sdim                              cl::desc("Path to opt. (default: search path "
49263508Sdim                                       "for 'opt'.)"));
50193323Sed}
51193323Sed
52193323Sed/// writeProgramToFile - This writes the current "Program" to the named bitcode
53193323Sed/// file.  If an error occurs, true is returned.
54193323Sed///
55263508Sdimstatic bool writeProgramToFileAux(tool_output_file &Out, const Module *M) {
56263508Sdim  WriteBitcodeToFile(M, Out.os());
57263508Sdim  Out.os().close();
58263508Sdim  if (!Out.os().has_error()) {
59263508Sdim    Out.keep();
60263508Sdim    return false;
61263508Sdim  }
62263508Sdim  return true;
63263508Sdim}
64263508Sdim
65263508Sdimbool BugDriver::writeProgramToFile(const std::string &Filename, int FD,
66263508Sdim                                   const Module *M) const {
67263508Sdim  tool_output_file Out(Filename.c_str(), FD);
68263508Sdim  return writeProgramToFileAux(Out, M);
69263508Sdim}
70263508Sdim
71193323Sedbool BugDriver::writeProgramToFile(const std::string &Filename,
72212793Sdim                                   const Module *M) const {
73198090Srdivacky  std::string ErrInfo;
74263508Sdim  tool_output_file Out(Filename.c_str(), ErrInfo, sys::fs::F_Binary);
75263508Sdim  if (ErrInfo.empty())
76263508Sdim    return writeProgramToFileAux(Out, M);
77212793Sdim  return true;
78193323Sed}
79193323Sed
80193323Sed
81193323Sed/// EmitProgressBitcode - This function is used to output the current Program
82193323Sed/// to a file named "bugpoint-ID.bc".
83193323Sed///
84212793Sdimvoid BugDriver::EmitProgressBitcode(const Module *M,
85212793Sdim                                    const std::string &ID,
86212793Sdim                                    bool NoFlyer)  const {
87193323Sed  // Output the input to the current pass to a bitcode file, emit a message
88193323Sed  // telling the user how to reproduce it: opt -foo blah.bc
89193323Sed  //
90198090Srdivacky  std::string Filename = OutputPrefix + "-" + ID + ".bc";
91212793Sdim  if (writeProgramToFile(Filename, M)) {
92198090Srdivacky    errs() <<  "Error opening file '" << Filename << "' for writing!\n";
93193323Sed    return;
94193323Sed  }
95193323Sed
96198090Srdivacky  outs() << "Emitted bitcode to '" << Filename << "'\n";
97193323Sed  if (NoFlyer || PassesToRun.empty()) return;
98198090Srdivacky  outs() << "\n*** You can reproduce the problem with: ";
99198090Srdivacky  if (UseValgrind) outs() << "valgrind ";
100234353Sdim  outs() << "opt " << Filename;
101234353Sdim  for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
102234353Sdim    outs() << " -load " << PluginLoader::getPlugin(i);
103234353Sdim  }
104234353Sdim  outs() << " " << getPassesString(PassesToRun) << "\n";
105193323Sed}
106193323Sed
107219067Sdimcl::opt<bool> SilencePasses("silence-passes",
108219067Sdim        cl::desc("Suppress output of running passes (both stdout and stderr)"));
109193323Sed
110212793Sdimstatic cl::list<std::string> OptArgs("opt-args", cl::Positional,
111212793Sdim                                     cl::desc("<opt arguments>..."),
112212793Sdim                                     cl::ZeroOrMore, cl::PositionalEatsArgs);
113193323Sed
114193323Sed/// runPasses - Run the specified passes on Program, outputting a bitcode file
115193323Sed/// and writing the filename into OutputFile if successful.  If the
116193323Sed/// optimizations fail for some reason (optimizer crashes), return true,
117193323Sed/// otherwise return false.  If DeleteOutput is set to true, the bitcode is
118193323Sed/// deleted on success, and the filename string is undefined.  This prints to
119198090Srdivacky/// outs() a single line message indicating whether compilation was successful
120198090Srdivacky/// or failed.
121193323Sed///
122212793Sdimbool BugDriver::runPasses(Module *Program,
123212793Sdim                          const std::vector<std::string> &Passes,
124193323Sed                          std::string &OutputFilename, bool DeleteOutput,
125193323Sed                          bool Quiet, unsigned NumExtraArgs,
126193323Sed                          const char * const *ExtraArgs) const {
127193323Sed  // setup the output file name
128198090Srdivacky  outs().flush();
129263508Sdim  SmallString<128> UniqueFilename;
130263508Sdim  error_code EC = sys::fs::createUniqueFile(
131263508Sdim      OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);
132263508Sdim  if (EC) {
133198090Srdivacky    errs() << getToolName() << ": Error making unique filename: "
134263508Sdim           << EC.message() << "\n";
135263508Sdim    return 1;
136193323Sed  }
137263508Sdim  OutputFilename = UniqueFilename.str();
138193323Sed
139193323Sed  // set up the input file name
140263508Sdim  SmallString<128> InputFilename;
141263508Sdim  int InputFD;
142263508Sdim  EC = sys::fs::createUniqueFile(OutputPrefix + "-input-%%%%%%%.bc", InputFD,
143263508Sdim                                 InputFilename);
144263508Sdim  if (EC) {
145198090Srdivacky    errs() << getToolName() << ": Error making unique filename: "
146263508Sdim           << EC.message() << "\n";
147263508Sdim    return 1;
148193323Sed  }
149218885Sdim
150263508Sdim  tool_output_file InFile(InputFilename.c_str(), InputFD);
151218885Sdim
152212793Sdim  WriteBitcodeToFile(Program, InFile.os());
153212793Sdim  InFile.os().close();
154212793Sdim  if (InFile.os().has_error()) {
155263508Sdim    errs() << "Error writing bitcode file: " << InputFilename << "\n";
156212793Sdim    InFile.os().clear_error();
157212793Sdim    return 1;
158212793Sdim  }
159218885Sdim
160263508Sdim  std::string tool = OptCmd.empty()? sys::FindProgramByName("opt") : OptCmd;
161218885Sdim  if (tool.empty()) {
162234353Sdim    errs() << "Cannot find `opt' in PATH!\n";
163218885Sdim    return 1;
164218885Sdim  }
165218885Sdim
166218885Sdim  // Ok, everything that could go wrong before running opt is done.
167212793Sdim  InFile.keep();
168193323Sed
169193323Sed  // setup the child process' arguments
170198090Srdivacky  SmallVector<const char*, 8> Args;
171193323Sed  if (UseValgrind) {
172198090Srdivacky    Args.push_back("valgrind");
173198090Srdivacky    Args.push_back("--error-exitcode=1");
174198090Srdivacky    Args.push_back("-q");
175198090Srdivacky    Args.push_back(tool.c_str());
176193323Sed  } else
177263508Sdim    Args.push_back(tool.c_str());
178193323Sed
179212793Sdim  Args.push_back("-o");
180198090Srdivacky  Args.push_back(OutputFilename.c_str());
181212793Sdim  for (unsigned i = 0, e = OptArgs.size(); i != e; ++i)
182212793Sdim    Args.push_back(OptArgs[i].c_str());
183193323Sed  std::vector<std::string> pass_args;
184193323Sed  for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
185193323Sed    pass_args.push_back( std::string("-load"));
186193323Sed    pass_args.push_back( PluginLoader::getPlugin(i));
187193323Sed  }
188212793Sdim  for (std::vector<std::string>::const_iterator I = Passes.begin(),
189193323Sed       E = Passes.end(); I != E; ++I )
190212793Sdim    pass_args.push_back( std::string("-") + (*I) );
191193323Sed  for (std::vector<std::string>::const_iterator I = pass_args.begin(),
192193323Sed       E = pass_args.end(); I != E; ++I )
193198090Srdivacky    Args.push_back(I->c_str());
194263508Sdim  Args.push_back(InputFilename.c_str());
195193323Sed  for (unsigned i = 0; i < NumExtraArgs; ++i)
196198090Srdivacky    Args.push_back(*ExtraArgs);
197198090Srdivacky  Args.push_back(0);
198193323Sed
199212793Sdim  DEBUG(errs() << "\nAbout to run:\t";
200212793Sdim        for (unsigned i = 0, e = Args.size()-1; i != e; ++i)
201212793Sdim          errs() << " " << Args[i];
202212793Sdim        errs() << "\n";
203212793Sdim        );
204212793Sdim
205263508Sdim  std::string Prog;
206193323Sed  if (UseValgrind)
207263508Sdim    Prog = sys::FindProgramByName("valgrind");
208193323Sed  else
209263508Sdim    Prog = tool;
210218885Sdim
211193323Sed  // Redirect stdout and stderr to nowhere if SilencePasses is given
212263508Sdim  StringRef Nowhere;
213263508Sdim  const StringRef *Redirects[3] = {0, &Nowhere, &Nowhere};
214193323Sed
215263508Sdim  std::string ErrMsg;
216263508Sdim  int result = sys::ExecuteAndWait(Prog, Args.data(), 0,
217263508Sdim                                   (SilencePasses ? Redirects : 0), Timeout,
218263508Sdim                                   MemoryLimit, &ErrMsg);
219193323Sed
220193323Sed  // If we are supposed to delete the bitcode file or if the passes crashed,
221193323Sed  // remove it now.  This may fail if the file was never created, but that's ok.
222193323Sed  if (DeleteOutput || result != 0)
223263508Sdim    sys::fs::remove(OutputFilename);
224193323Sed
225193323Sed  // Remove the temporary input file as well
226263508Sdim  sys::fs::remove(InputFilename.c_str());
227193323Sed
228193323Sed  if (!Quiet) {
229193323Sed    if (result == 0)
230198090Srdivacky      outs() << "Success!\n";
231193323Sed    else if (result > 0)
232198090Srdivacky      outs() << "Exited with error code '" << result << "'\n";
233193323Sed    else if (result < 0) {
234193323Sed      if (result == -1)
235198090Srdivacky        outs() << "Execute failed: " << ErrMsg << "\n";
236193323Sed      else
237223013Sdim        outs() << "Crashed: " << ErrMsg << "\n";
238193323Sed    }
239193323Sed    if (result & 0x01000000)
240198090Srdivacky      outs() << "Dumped core\n";
241193323Sed  }
242193323Sed
243193323Sed  // Was the child successful?
244193323Sed  return result != 0;
245193323Sed}
246193323Sed
247193323Sed
248193323Sed/// runPassesOn - Carefully run the specified set of pass on the specified
249193323Sed/// module, returning the transformed module on success, or a null pointer on
250193323Sed/// failure.
251193323SedModule *BugDriver::runPassesOn(Module *M,
252212793Sdim                               const std::vector<std::string> &Passes,
253193323Sed                               bool AutoDebugCrashes, unsigned NumExtraArgs,
254193323Sed                               const char * const *ExtraArgs) {
255193323Sed  std::string BitcodeResult;
256212793Sdim  if (runPasses(M, Passes, BitcodeResult, false/*delete*/, true/*quiet*/,
257193323Sed                NumExtraArgs, ExtraArgs)) {
258193323Sed    if (AutoDebugCrashes) {
259198090Srdivacky      errs() << " Error running this sequence of passes"
260198090Srdivacky             << " on the input program!\n";
261212793Sdim      delete swapProgramIn(M);
262212793Sdim      EmitProgressBitcode(M, "pass-error",  false);
263193323Sed      exit(debugOptimizerCrash());
264193323Sed    }
265193323Sed    return 0;
266193323Sed  }
267193323Sed
268195340Sed  Module *Ret = ParseInputFile(BitcodeResult, Context);
269193323Sed  if (Ret == 0) {
270198090Srdivacky    errs() << getToolName() << ": Error reading bitcode file '"
271198090Srdivacky           << BitcodeResult << "'!\n";
272193323Sed    exit(1);
273193323Sed  }
274263508Sdim  sys::fs::remove(BitcodeResult);
275193323Sed  return Ret;
276193323Sed}
277