blob: 7952ab1ac73fedacb7105a43adc8bc569e99990d [file] [log] [blame]
Chris Lattner73a6bdd2002-11-20 22:28:10 +00001//===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswell09344dc2003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswell09344dc2003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner73a6bdd2002-11-20 22:28:10 +00009//
10// This file defines an interface that allows bugpoint to run various passes
Misha Brukman8b2bd4e2003-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 Lattner73a6bdd2002-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"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000019#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000021#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000023#include "llvm/IR/Verifier.h"
Reid Spencer842118c2005-12-22 20:02:55 +000024#include "llvm/Support/CommandLine.h"
Rafael Espindolabbdce492010-08-07 23:03:21 +000025#include "llvm/Support/Debug.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000026#include "llvm/Support/FileUtilities.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000027#include "llvm/Support/Path.h"
28#include "llvm/Support/Program.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Support/SystemUtils.h"
30#include "llvm/Support/ToolOutputFile.h"
Andrew Lenharth67e01892006-01-26 18:37:21 +000031
32#define DONT_GET_PLUGIN_LOADER_OPTION
33#include "llvm/Support/PluginLoader.h"
34
Misha Brukman0c2305b2003-08-07 21:19:30 +000035#include <fstream>
Rafael Espindola7b349ce2013-06-18 15:54:13 +000036
Chris Lattner2f1aa112004-01-14 03:38:37 +000037using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000038
Chandler Carruthe96dd892014-04-21 22:55:11 +000039#define DEBUG_TYPE "bugpoint"
40
Daniel Dunbara53337f2009-09-07 19:26:11 +000041namespace llvm {
Justin Bogner8d0a0812016-09-02 01:21:37 +000042extern cl::opt<std::string> OutputPrefix;
Daniel Dunbara53337f2009-09-07 19:26:11 +000043}
Chris Lattnerf6dd4d72007-05-06 05:47:06 +000044
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +000045static cl::opt<bool> PreserveBitcodeUseListOrder(
46 "preserve-bc-uselistorder",
47 cl::desc("Preserve use-list order when writing LLVM bitcode."),
48 cl::init(true), cl::Hidden);
49
Reid Spencer842118c2005-12-22 20:02:55 +000050namespace {
Justin Bogner8d0a0812016-09-02 01:21:37 +000051// ChildOutput - This option captures the name of the child output file that
52// is set up by the parent bugpoint process
53cl::opt<std::string> ChildOutput("child-output", cl::ReallyHidden);
54cl::opt<std::string> OptCmd("opt-command", cl::init(""),
55 cl::desc("Path to opt. (default: search path "
56 "for 'opt'.)"));
Reid Spencer842118c2005-12-22 20:02:55 +000057}
58
Gabor Greif0e535c3c2007-07-04 21:55:50 +000059/// writeProgramToFile - This writes the current "Program" to the named bitcode
Chris Lattner73a6bdd2002-11-20 22:28:10 +000060/// file. If an error occurs, true is returned.
61///
Rafael Espindola302c0da2013-06-18 15:29:32 +000062static bool writeProgramToFileAux(tool_output_file &Out, const Module *M) {
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +000063 WriteBitcodeToFile(M, Out.os(), PreserveBitcodeUseListOrder);
Rafael Espindola302c0da2013-06-18 15:29:32 +000064 Out.os().close();
65 if (!Out.os().has_error()) {
66 Out.keep();
67 return false;
68 }
69 return true;
70}
71
72bool BugDriver::writeProgramToFile(const std::string &Filename, int FD,
73 const Module *M) const {
Rafael Espindola3fd1e992014-08-25 18:16:47 +000074 tool_output_file Out(Filename, FD);
Rafael Espindola302c0da2013-06-18 15:29:32 +000075 return writeProgramToFileAux(Out, M);
76}
77
Chris Lattnerd4e04742002-12-23 23:49:59 +000078bool BugDriver::writeProgramToFile(const std::string &Filename,
Rafael Espindola594994a2010-07-28 18:12:30 +000079 const Module *M) const {
Rafael Espindola3fd1e992014-08-25 18:16:47 +000080 std::error_code EC;
81 tool_output_file Out(Filename, EC, sys::fs::F_None);
82 if (!EC)
Rafael Espindola302c0da2013-06-18 15:29:32 +000083 return writeProgramToFileAux(Out, M);
Dan Gohman8525fe72010-08-20 16:59:15 +000084 return true;
Chris Lattner73a6bdd2002-11-20 22:28:10 +000085}
86
Gabor Greif0e535c3c2007-07-04 21:55:50 +000087/// EmitProgressBitcode - This function is used to output the current Program
Misha Brukman6a6587a2003-07-21 21:58:16 +000088/// to a file named "bugpoint-ID.bc".
Chris Lattner73a6bdd2002-11-20 22:28:10 +000089///
Justin Bogner8d0a0812016-09-02 01:21:37 +000090void BugDriver::EmitProgressBitcode(const Module *M, const std::string &ID,
91 bool NoFlyer) const {
Gabor Greif0e535c3c2007-07-04 21:55:50 +000092 // Output the input to the current pass to a bitcode file, emit a message
Chris Lattner73a6bdd2002-11-20 22:28:10 +000093 // telling the user how to reproduce it: opt -foo blah.bc
94 //
Daniel Dunbara53337f2009-09-07 19:26:11 +000095 std::string Filename = OutputPrefix + "-" + ID + ".bc";
Rafael Espindola594994a2010-07-28 18:12:30 +000096 if (writeProgramToFile(Filename, M)) {
Justin Bogner8d0a0812016-09-02 01:21:37 +000097 errs() << "Error opening file '" << Filename << "' for writing!\n";
Chris Lattner73a6bdd2002-11-20 22:28:10 +000098 return;
99 }
100
Dan Gohmanee051522009-07-16 15:30:09 +0000101 outs() << "Emitted bitcode to '" << Filename << "'\n";
Justin Bogner8d0a0812016-09-02 01:21:37 +0000102 if (NoFlyer || PassesToRun.empty())
103 return;
Dan Gohmanee051522009-07-16 15:30:09 +0000104 outs() << "\n*** You can reproduce the problem with: ";
Justin Bogner8d0a0812016-09-02 01:21:37 +0000105 if (UseValgrind)
106 outs() << "valgrind ";
Chris Lattner0a2ac902012-03-19 23:42:11 +0000107 outs() << "opt " << Filename;
108 for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
109 outs() << " -load " << PluginLoader::getPlugin(i);
110 }
111 outs() << " " << getPassesString(PassesToRun) << "\n";
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000112}
113
Justin Bogner8d0a0812016-09-02 01:21:37 +0000114cl::opt<bool> SilencePasses(
115 "silence-passes",
116 cl::desc("Suppress output of running passes (both stdout and stderr)"));
Matthijs Kooijman4ba5df82008-06-12 13:02:26 +0000117
Rafael Espindolafb1f29a2010-08-08 22:14:20 +0000118static cl::list<std::string> OptArgs("opt-args", cl::Positional,
119 cl::desc("<opt arguments>..."),
120 cl::ZeroOrMore, cl::PositionalEatsArgs);
121
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000122/// runPasses - Run the specified passes on Program, outputting a bitcode file
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000123/// and writing the filename into OutputFile if successful. If the
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000124/// optimizations fail for some reason (optimizer crashes), return true,
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000125/// otherwise return false. If DeleteOutput is set to true, the bitcode is
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000126/// deleted on success, and the filename string is undefined. This prints to
Dan Gohmanee051522009-07-16 15:30:09 +0000127/// outs() a single line message indicating whether compilation was successful
128/// or failed.
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000129///
Rafael Espindola315190b2010-08-05 00:29:04 +0000130bool BugDriver::runPasses(Module *Program,
Rafael Espindola33e81a82010-08-08 03:55:08 +0000131 const std::vector<std::string> &Passes,
Chris Lattnerd4e04742002-12-23 23:49:59 +0000132 std::string &OutputFilename, bool DeleteOutput,
Nick Lewyckyc6243022007-11-14 06:47:06 +0000133 bool Quiet, unsigned NumExtraArgs,
Justin Bogner8d0a0812016-09-02 01:21:37 +0000134 const char *const *ExtraArgs) const {
Reid Spencer842118c2005-12-22 20:02:55 +0000135 // setup the output file name
Dan Gohmanee051522009-07-16 15:30:09 +0000136 outs().flush();
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000137 SmallString<128> UniqueFilename;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000138 std::error_code EC = sys::fs::createUniqueFile(
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000139 OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000140 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000141 errs() << getToolName()
142 << ": Error making unique filename: " << EC.message() << "\n";
Chris Lattner69733952009-08-23 07:49:08 +0000143 return 1;
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000144 }
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000145 OutputFilename = UniqueFilename.str();
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000146
147 // set up the input file name
148 SmallString<128> InputFilename;
149 int InputFD;
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000150 EC = sys::fs::createUniqueFile(OutputPrefix + "-input-%%%%%%%.bc", InputFD,
151 InputFilename);
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000152 if (EC) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000153 errs() << getToolName()
154 << ": Error making unique filename: " << EC.message() << "\n";
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000155 return 1;
156 }
157
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000158 tool_output_file InFile(InputFilename, InputFD);
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000159
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000160 WriteBitcodeToFile(Program, InFile.os(), PreserveBitcodeUseListOrder);
Dan Gohmana2233f22010-09-01 14:20:41 +0000161 InFile.os().close();
162 if (InFile.os().has_error()) {
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000163 errs() << "Error writing bitcode file: " << InputFilename << "\n";
Dan Gohmana2233f22010-09-01 14:20:41 +0000164 InFile.os().clear_error();
Dan Gohman8525fe72010-08-20 16:59:15 +0000165 return 1;
166 }
Dan Gohman094da1d2010-10-29 16:18:26 +0000167
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000168 std::string tool = OptCmd;
169 if (OptCmd.empty()) {
Michael J. Spencerd48829b2014-11-07 21:30:36 +0000170 if (ErrorOr<std::string> Path = sys::findProgramByName("opt"))
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000171 tool = *Path;
Michael J. Spencerd48829b2014-11-07 21:30:36 +0000172 else
173 errs() << Path.getError().message() << "\n";
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000174 }
Dan Gohman094da1d2010-10-29 16:18:26 +0000175 if (tool.empty()) {
Chris Lattner0a2ac902012-03-19 23:42:11 +0000176 errs() << "Cannot find `opt' in PATH!\n";
Dan Gohman094da1d2010-10-29 16:18:26 +0000177 return 1;
178 }
179
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000180 std::string Prog;
181 if (UseValgrind) {
Michael J. Spencerd48829b2014-11-07 21:30:36 +0000182 if (ErrorOr<std::string> Path = sys::findProgramByName("valgrind"))
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000183 Prog = *Path;
Michael J. Spencerd48829b2014-11-07 21:30:36 +0000184 else
185 errs() << Path.getError().message() << "\n";
Michael J. Spencerf9074b52014-11-04 01:29:59 +0000186 } else
187 Prog = tool;
188 if (Prog.empty()) {
189 errs() << "Cannot find `valgrind' in PATH!\n";
190 return 1;
191 }
192
Dan Gohman094da1d2010-10-29 16:18:26 +0000193 // Ok, everything that could go wrong before running opt is done.
Dan Gohman8525fe72010-08-20 16:59:15 +0000194 InFile.keep();
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000195
Reid Spencer842118c2005-12-22 20:02:55 +0000196 // setup the child process' arguments
Justin Bogner8d0a0812016-09-02 01:21:37 +0000197 SmallVector<const char *, 8> Args;
Nick Lewycky7faef232006-09-14 03:49:54 +0000198 if (UseValgrind) {
Chris Lattnerc521f542009-08-23 22:45:37 +0000199 Args.push_back("valgrind");
200 Args.push_back("--error-exitcode=1");
201 Args.push_back("-q");
202 Args.push_back(tool.c_str());
Nick Lewycky7faef232006-09-14 03:49:54 +0000203 } else
Rafael Espindola4c7ad8f2013-06-13 19:25:37 +0000204 Args.push_back(tool.c_str());
Nick Lewycky7faef232006-09-14 03:49:54 +0000205
Rafael Espindolabbdce492010-08-07 23:03:21 +0000206 Args.push_back("-o");
Chris Lattnerc521f542009-08-23 22:45:37 +0000207 Args.push_back(OutputFilename.c_str());
Rafael Espindolafb1f29a2010-08-08 22:14:20 +0000208 for (unsigned i = 0, e = OptArgs.size(); i != e; ++i)
209 Args.push_back(OptArgs[i].c_str());
Reid Spencer842118c2005-12-22 20:02:55 +0000210 std::vector<std::string> pass_args;
Andrew Lenharth67e01892006-01-26 18:37:21 +0000211 for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000212 pass_args.push_back(std::string("-load"));
213 pass_args.push_back(PluginLoader::getPlugin(i));
Andrew Lenharth67e01892006-01-26 18:37:21 +0000214 }
Rafael Espindola33e81a82010-08-08 03:55:08 +0000215 for (std::vector<std::string>::const_iterator I = Passes.begin(),
Justin Bogner8d0a0812016-09-02 01:21:37 +0000216 E = Passes.end();
217 I != E; ++I)
218 pass_args.push_back(std::string("-") + (*I));
Reid Spencer842118c2005-12-22 20:02:55 +0000219 for (std::vector<std::string>::const_iterator I = pass_args.begin(),
Justin Bogner8d0a0812016-09-02 01:21:37 +0000220 E = pass_args.end();
221 I != E; ++I)
Chris Lattnerc521f542009-08-23 22:45:37 +0000222 Args.push_back(I->c_str());
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000223 Args.push_back(InputFilename.c_str());
Nick Lewyckyc6243022007-11-14 06:47:06 +0000224 for (unsigned i = 0; i < NumExtraArgs; ++i)
Chris Lattnerc521f542009-08-23 22:45:37 +0000225 Args.push_back(*ExtraArgs);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000226 Args.push_back(nullptr);
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000227
Rafael Espindolabbdce492010-08-07 23:03:21 +0000228 DEBUG(errs() << "\nAbout to run:\t";
Justin Bogner8d0a0812016-09-02 01:21:37 +0000229 for (unsigned i = 0, e = Args.size() - 1; i != e; ++i) errs()
230 << " " << Args[i];
231 errs() << "\n";);
Rafael Espindolabbdce492010-08-07 23:03:21 +0000232
Matthijs Kooijman4ba5df82008-06-12 13:02:26 +0000233 // Redirect stdout and stderr to nowhere if SilencePasses is given
Rafael Espindola7c1023a2013-06-13 20:25:38 +0000234 StringRef Nowhere;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000235 const StringRef *Redirects[3] = {nullptr, &Nowhere, &Nowhere};
Matthijs Kooijman4ba5df82008-06-12 13:02:26 +0000236
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000237 std::string ErrMsg;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000238 int result = sys::ExecuteAndWait(Prog, Args.data(), nullptr,
239 (SilencePasses ? Redirects : nullptr),
240 Timeout, MemoryLimit, &ErrMsg);
Chris Lattneraf24e902004-05-12 02:55:45 +0000241
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000242 // If we are supposed to delete the bitcode file or if the passes crashed,
Chris Lattneraf24e902004-05-12 02:55:45 +0000243 // remove it now. This may fail if the file was never created, but that's ok.
Reid Spencer842118c2005-12-22 20:02:55 +0000244 if (DeleteOutput || result != 0)
Rafael Espindola75d8fa52013-06-18 15:33:18 +0000245 sys::fs::remove(OutputFilename);
Jeff Cohenc8f1f4b2005-01-22 17:36:17 +0000246
Reid Spencer842118c2005-12-22 20:02:55 +0000247 // Remove the temporary input file as well
Rafael Espindola7b349ce2013-06-18 15:54:13 +0000248 sys::fs::remove(InputFilename.c_str());
Reid Spencer842118c2005-12-22 20:02:55 +0000249
Chris Lattnerb165ea32003-06-02 04:54:16 +0000250 if (!Quiet) {
Reid Spencer842118c2005-12-22 20:02:55 +0000251 if (result == 0)
Dan Gohmanee051522009-07-16 15:30:09 +0000252 outs() << "Success!\n";
Reid Spencer842118c2005-12-22 20:02:55 +0000253 else if (result > 0)
Dan Gohmanee051522009-07-16 15:30:09 +0000254 outs() << "Exited with error code '" << result << "'\n";
Reid Spencer944645a2006-08-21 06:04:45 +0000255 else if (result < 0) {
256 if (result == -1)
Dan Gohmanee051522009-07-16 15:30:09 +0000257 outs() << "Execute failed: " << ErrMsg << "\n";
Reid Spencer944645a2006-08-21 06:04:45 +0000258 else
Andrew Trickd5d07642011-05-21 00:56:46 +0000259 outs() << "Crashed: " << ErrMsg << "\n";
Reid Spencer944645a2006-08-21 06:04:45 +0000260 }
Reid Spencer842118c2005-12-22 20:02:55 +0000261 if (result & 0x01000000)
Dan Gohmanee051522009-07-16 15:30:09 +0000262 outs() << "Dumped core\n";
Chris Lattnerb165ea32003-06-02 04:54:16 +0000263 }
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000264
265 // Was the child successful?
Reid Spencer842118c2005-12-22 20:02:55 +0000266 return result != 0;
Chris Lattner73a6bdd2002-11-20 22:28:10 +0000267}
Brian Gaeke960707c2003-11-11 22:41:34 +0000268
Rafael Espindola28b351a2014-08-26 17:19:03 +0000269std::unique_ptr<Module>
270BugDriver::runPassesOn(Module *M, const std::vector<std::string> &Passes,
Justin Bogner8d0a0812016-09-02 01:21:37 +0000271 unsigned NumExtraArgs, const char *const *ExtraArgs) {
Gabor Greif0e535c3c2007-07-04 21:55:50 +0000272 std::string BitcodeResult;
Justin Bogner8d0a0812016-09-02 01:21:37 +0000273 if (runPasses(M, Passes, BitcodeResult, false /*delete*/, true /*quiet*/,
Nick Lewyckyc6243022007-11-14 06:47:06 +0000274 NumExtraArgs, ExtraArgs)) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000275 return nullptr;
Chris Lattner37117a02004-03-14 21:21:57 +0000276 }
Chris Lattner1a5c5402004-03-14 21:17:03 +0000277
Rafael Espindola28b351a2014-08-26 17:19:03 +0000278 std::unique_ptr<Module> Ret = parseInputFile(BitcodeResult, Context);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000279 if (!Ret) {
Justin Bogner8d0a0812016-09-02 01:21:37 +0000280 errs() << getToolName() << ": Error reading bitcode file '" << BitcodeResult
281 << "'!\n";
Chris Lattner1a5c5402004-03-14 21:17:03 +0000282 exit(1);
283 }
Rafael Espindola75d8fa52013-06-18 15:33:18 +0000284 sys::fs::remove(BitcodeResult);
Chris Lattner1a5c5402004-03-14 21:17:03 +0000285 return Ret;
286}