2002-11-20 23:28:10 +01:00
|
|
|
//===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:44:31 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 02:00:37 +02:00
|
|
|
//
|
2003-10-20 19:47:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-20 23:28:10 +01:00
|
|
|
//
|
|
|
|
// This file defines an interface that allows bugpoint to run various passes
|
2003-10-10 19:57:28 +02:00
|
|
|
// without the threat of a buggy pass corrupting bugpoint (of course, bugpoint
|
|
|
|
// may have its own bugs, but that's another story...). It achieves this by
|
2002-11-20 23:28:10 +01:00
|
|
|
// forking a copy of itself and having the child process do the optimizations.
|
|
|
|
// If this client dies, we can always fork a new one. :)
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BugDriver.h"
|
2016-11-11 06:34:58 +01:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2005-12-22 21:02:55 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-08-08 01:03:21 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2006-01-26 19:37:21 +01:00
|
|
|
|
|
|
|
#define DONT_GET_PLUGIN_LOADER_OPTION
|
|
|
|
#include "llvm/Support/PluginLoader.h"
|
|
|
|
|
2013-06-18 17:54:13 +02:00
|
|
|
|
2004-01-14 04:38:37 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2014-04-22 00:55:11 +02:00
|
|
|
#define DEBUG_TYPE "bugpoint"
|
|
|
|
|
2009-09-07 21:26:11 +02:00
|
|
|
namespace llvm {
|
2016-09-02 03:21:37 +02:00
|
|
|
extern cl::opt<std::string> OutputPrefix;
|
2009-09-07 21:26:11 +02:00
|
|
|
}
|
2007-05-06 07:47:06 +02:00
|
|
|
|
2015-04-15 05:14:06 +02:00
|
|
|
static cl::opt<bool> PreserveBitcodeUseListOrder(
|
|
|
|
"preserve-bc-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM bitcode."),
|
|
|
|
cl::init(true), cl::Hidden);
|
|
|
|
|
2017-06-01 21:20:26 +02:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OptCmd("opt-command", cl::init(""),
|
|
|
|
cl::desc("Path to opt. (default: search path "
|
|
|
|
"for 'opt'.)"));
|
2005-12-22 21:02:55 +01:00
|
|
|
|
2018-02-14 22:44:34 +01:00
|
|
|
/// This writes the current "Program" to the named bitcode file. If an error
|
|
|
|
/// occurs, true is returned.
|
|
|
|
static bool writeProgramToFileAux(ToolOutputFile &Out, const Module &M) {
|
|
|
|
WriteBitcodeToFile(M, Out.os(), PreserveBitcodeUseListOrder);
|
2013-06-18 17:29:32 +02:00
|
|
|
Out.os().close();
|
|
|
|
if (!Out.os().has_error()) {
|
|
|
|
Out.keep();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BugDriver::writeProgramToFile(const std::string &Filename, int FD,
|
2018-02-14 22:44:34 +01:00
|
|
|
const Module &M) const {
|
2017-09-23 03:03:17 +02:00
|
|
|
ToolOutputFile Out(Filename, FD);
|
2013-06-18 17:29:32 +02:00
|
|
|
return writeProgramToFileAux(Out, M);
|
|
|
|
}
|
|
|
|
|
2018-02-14 22:44:34 +01:00
|
|
|
bool BugDriver::writeProgramToFile(int FD, const Module &M) const {
|
2017-11-16 22:40:10 +01:00
|
|
|
raw_fd_ostream OS(FD, /*shouldClose*/ false);
|
2018-02-14 22:44:34 +01:00
|
|
|
WriteBitcodeToFile(M, OS, PreserveBitcodeUseListOrder);
|
2017-11-16 22:40:10 +01:00
|
|
|
OS.flush();
|
|
|
|
if (!OS.has_error())
|
|
|
|
return false;
|
|
|
|
OS.clear_error();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-12-24 00:49:59 +01:00
|
|
|
bool BugDriver::writeProgramToFile(const std::string &Filename,
|
2018-02-14 22:44:34 +01:00
|
|
|
const Module &M) const {
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
2017-09-23 03:03:17 +02:00
|
|
|
ToolOutputFile Out(Filename, EC, sys::fs::F_None);
|
2014-08-25 20:16:47 +02:00
|
|
|
if (!EC)
|
2013-06-18 17:29:32 +02:00
|
|
|
return writeProgramToFileAux(Out, M);
|
2010-08-20 18:59:15 +02:00
|
|
|
return true;
|
2002-11-20 23:28:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-14 22:44:34 +01:00
|
|
|
/// This function is used to output the current Program to a file named
|
|
|
|
/// "bugpoint-ID.bc".
|
|
|
|
void BugDriver::EmitProgressBitcode(const Module &M, const std::string &ID,
|
2016-09-02 03:21:37 +02:00
|
|
|
bool NoFlyer) const {
|
2007-07-04 23:55:50 +02:00
|
|
|
// Output the input to the current pass to a bitcode file, emit a message
|
2002-11-20 23:28:10 +01:00
|
|
|
// telling the user how to reproduce it: opt -foo blah.bc
|
|
|
|
//
|
2009-09-07 21:26:11 +02:00
|
|
|
std::string Filename = OutputPrefix + "-" + ID + ".bc";
|
2010-07-28 20:12:30 +02:00
|
|
|
if (writeProgramToFile(Filename, M)) {
|
2016-09-02 03:21:37 +02:00
|
|
|
errs() << "Error opening file '" << Filename << "' for writing!\n";
|
2002-11-20 23:28:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-16 17:30:09 +02:00
|
|
|
outs() << "Emitted bitcode to '" << Filename << "'\n";
|
2016-09-02 03:21:37 +02:00
|
|
|
if (NoFlyer || PassesToRun.empty())
|
|
|
|
return;
|
2009-07-16 17:30:09 +02:00
|
|
|
outs() << "\n*** You can reproduce the problem with: ";
|
2016-09-02 03:21:37 +02:00
|
|
|
if (UseValgrind)
|
|
|
|
outs() << "valgrind ";
|
2012-03-20 00:42:11 +01:00
|
|
|
outs() << "opt " << Filename;
|
|
|
|
for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
|
|
|
|
outs() << " -load " << PluginLoader::getPlugin(i);
|
|
|
|
}
|
|
|
|
outs() << " " << getPassesString(PassesToRun) << "\n";
|
2002-11-20 23:28:10 +01:00
|
|
|
}
|
|
|
|
|
2016-09-02 03:21:37 +02:00
|
|
|
cl::opt<bool> SilencePasses(
|
|
|
|
"silence-passes",
|
|
|
|
cl::desc("Suppress output of running passes (both stdout and stderr)"));
|
2008-06-12 15:02:26 +02:00
|
|
|
|
2010-08-09 00:14:20 +02:00
|
|
|
static cl::list<std::string> OptArgs("opt-args", cl::Positional,
|
|
|
|
cl::desc("<opt arguments>..."),
|
|
|
|
cl::ZeroOrMore, cl::PositionalEatsArgs);
|
|
|
|
|
2007-07-04 23:55:50 +02:00
|
|
|
/// runPasses - Run the specified passes on Program, outputting a bitcode file
|
2003-10-10 19:57:28 +02:00
|
|
|
/// and writing the filename into OutputFile if successful. If the
|
2002-11-20 23:28:10 +01:00
|
|
|
/// optimizations fail for some reason (optimizer crashes), return true,
|
2007-07-04 23:55:50 +02:00
|
|
|
/// otherwise return false. If DeleteOutput is set to true, the bitcode is
|
2002-11-20 23:28:10 +01:00
|
|
|
/// deleted on success, and the filename string is undefined. This prints to
|
2009-07-16 17:30:09 +02:00
|
|
|
/// outs() a single line message indicating whether compilation was successful
|
|
|
|
/// or failed.
|
2002-11-20 23:28:10 +01:00
|
|
|
///
|
2018-02-14 22:44:34 +01:00
|
|
|
bool BugDriver::runPasses(Module &Program,
|
2010-08-08 05:55:08 +02:00
|
|
|
const std::vector<std::string> &Passes,
|
2002-12-24 00:49:59 +01:00
|
|
|
std::string &OutputFilename, bool DeleteOutput,
|
2007-11-14 07:47:06 +01:00
|
|
|
bool Quiet, unsigned NumExtraArgs,
|
2016-09-02 03:21:37 +02:00
|
|
|
const char *const *ExtraArgs) const {
|
2005-12-22 21:02:55 +01:00
|
|
|
// setup the output file name
|
2009-07-16 17:30:09 +02:00
|
|
|
outs().flush();
|
2013-06-18 17:54:13 +02:00
|
|
|
SmallString<128> UniqueFilename;
|
2014-06-13 05:07:50 +02:00
|
|
|
std::error_code EC = sys::fs::createUniqueFile(
|
2013-07-05 23:01:08 +02:00
|
|
|
OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);
|
2013-06-18 17:54:13 +02:00
|
|
|
if (EC) {
|
2016-09-02 03:21:37 +02:00
|
|
|
errs() << getToolName()
|
|
|
|
<< ": Error making unique filename: " << EC.message() << "\n";
|
2013-06-18 17:54:13 +02:00
|
|
|
return 1;
|
2006-08-23 22:34:57 +02:00
|
|
|
}
|
2013-06-18 17:54:13 +02:00
|
|
|
OutputFilename = UniqueFilename.str();
|
2002-11-20 23:28:10 +01:00
|
|
|
|
2005-12-22 21:02:55 +01:00
|
|
|
// set up the input file name
|
2017-11-16 02:06:36 +01:00
|
|
|
Expected<sys::fs::TempFile> Temp =
|
|
|
|
sys::fs::TempFile::create(OutputPrefix + "-input-%%%%%%%.bc");
|
|
|
|
if (!Temp) {
|
2016-09-02 03:21:37 +02:00
|
|
|
errs() << getToolName()
|
2017-11-16 02:06:36 +01:00
|
|
|
<< ": Error making unique filename: " << toString(Temp.takeError())
|
|
|
|
<< "\n";
|
2013-06-18 17:54:13 +02:00
|
|
|
return 1;
|
2006-08-23 22:34:57 +02:00
|
|
|
}
|
2017-11-16 02:06:36 +01:00
|
|
|
DiscardTemp Discard{*Temp};
|
|
|
|
raw_fd_ostream OS(Temp->FD, /*shouldClose*/ false);
|
|
|
|
|
2018-02-14 22:44:34 +01:00
|
|
|
WriteBitcodeToFile(Program, OS, PreserveBitcodeUseListOrder);
|
2017-11-16 02:06:36 +01:00
|
|
|
OS.flush();
|
|
|
|
if (OS.has_error()) {
|
|
|
|
errs() << "Error writing bitcode file: " << Temp->TmpName << "\n";
|
|
|
|
OS.clear_error();
|
2010-08-20 18:59:15 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-29 18:18:26 +02:00
|
|
|
|
2014-11-04 02:29:59 +01:00
|
|
|
std::string tool = OptCmd;
|
|
|
|
if (OptCmd.empty()) {
|
2014-11-07 22:30:36 +01:00
|
|
|
if (ErrorOr<std::string> Path = sys::findProgramByName("opt"))
|
2014-11-04 02:29:59 +01:00
|
|
|
tool = *Path;
|
2014-11-07 22:30:36 +01:00
|
|
|
else
|
|
|
|
errs() << Path.getError().message() << "\n";
|
2014-11-04 02:29:59 +01:00
|
|
|
}
|
2010-10-29 18:18:26 +02:00
|
|
|
if (tool.empty()) {
|
2012-03-20 00:42:11 +01:00
|
|
|
errs() << "Cannot find `opt' in PATH!\n";
|
2010-10-29 18:18:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-02-09 07:09:15 +01:00
|
|
|
if (!sys::fs::exists(tool)) {
|
|
|
|
errs() << "Specified `opt' binary does not exist: " << tool << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2010-10-29 18:18:26 +02:00
|
|
|
|
2014-11-04 02:29:59 +01:00
|
|
|
std::string Prog;
|
|
|
|
if (UseValgrind) {
|
2014-11-07 22:30:36 +01:00
|
|
|
if (ErrorOr<std::string> Path = sys::findProgramByName("valgrind"))
|
2014-11-04 02:29:59 +01:00
|
|
|
Prog = *Path;
|
2014-11-07 22:30:36 +01:00
|
|
|
else
|
|
|
|
errs() << Path.getError().message() << "\n";
|
2014-11-04 02:29:59 +01:00
|
|
|
} else
|
|
|
|
Prog = tool;
|
|
|
|
if (Prog.empty()) {
|
|
|
|
errs() << "Cannot find `valgrind' in PATH!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-12-22 21:02:55 +01:00
|
|
|
// setup the child process' arguments
|
2018-06-12 19:43:52 +02:00
|
|
|
SmallVector<StringRef, 8> Args;
|
2006-09-14 05:49:54 +02:00
|
|
|
if (UseValgrind) {
|
2009-08-24 00:45:37 +02:00
|
|
|
Args.push_back("valgrind");
|
|
|
|
Args.push_back("--error-exitcode=1");
|
|
|
|
Args.push_back("-q");
|
2018-06-12 19:43:52 +02:00
|
|
|
Args.push_back(tool);
|
2006-09-14 05:49:54 +02:00
|
|
|
} else
|
2018-06-12 19:43:52 +02:00
|
|
|
Args.push_back(tool);
|
2006-09-14 05:49:54 +02:00
|
|
|
|
2010-08-09 00:14:20 +02:00
|
|
|
for (unsigned i = 0, e = OptArgs.size(); i != e; ++i)
|
2018-06-12 19:43:52 +02:00
|
|
|
Args.push_back(OptArgs[i]);
|
bugpoint: disabling symbolication of bugpoint-executed programs
Initial implementation - needs similar work/testing for other tools
bugpoint invokes (llc, lli I think, maybe more).
Alternatively (as suggested by chandlerc@) an environment variable could
be used. This would allow the option to pass transparently through user
scripts, pass to compilers if they happened to be LLVM-ish, etc.
I worry a bit about using cl::opt in the crash handling code - LLVM
might crash early, perhaps before the cl::opt is properly initialized?
Or at least before arguments have been parsed?
- should be OK since it defaults to "pretty", so if the crash is very
early in opt parsing, etc, then crash reports will still be symbolized.
I shyed away from doing this with an environment variable when I
realized that would require copying the existing environment and
appending the env variable of interest. But it seems there's no existing
LLVM API for accessing the environment (even the Support tests for
process launching have their own ifdefs for getting the environment). It
could be added, but seemed like a higher bar/untested codepath to
actually add environment variables.
Most importantly, this reduces the runtime of test/BugPoint/metadata.ll
in a split-dwarf Debug build from 1m34s to 6.5s by avoiding a lot of
symbolication. (this wasn't a problem for non-split-dwarf builds only
because the executable was too large to map into memory (due to bugpoint
setting a 400MB memory (including address space - not sure why? Going to
remove that) limit on the child process) so symbolication would fail
fast & wouldn't spend all that time parsing DWARF, etc)
Reviewers: chandlerc, dannyb
Differential Revision: https://reviews.llvm.org/D33804
llvm-svn: 305056
2017-06-09 09:29:03 +02:00
|
|
|
Args.push_back("-disable-symbolication");
|
|
|
|
Args.push_back("-o");
|
2018-06-12 19:43:52 +02:00
|
|
|
Args.push_back(OutputFilename);
|
2005-12-22 21:02:55 +01:00
|
|
|
std::vector<std::string> pass_args;
|
2006-01-26 19:37:21 +01:00
|
|
|
for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
|
2016-09-02 03:21:37 +02:00
|
|
|
pass_args.push_back(std::string("-load"));
|
|
|
|
pass_args.push_back(PluginLoader::getPlugin(i));
|
2006-01-26 19:37:21 +01:00
|
|
|
}
|
2010-08-08 05:55:08 +02:00
|
|
|
for (std::vector<std::string>::const_iterator I = Passes.begin(),
|
2016-09-02 03:21:37 +02:00
|
|
|
E = Passes.end();
|
|
|
|
I != E; ++I)
|
|
|
|
pass_args.push_back(std::string("-") + (*I));
|
2005-12-22 21:02:55 +01:00
|
|
|
for (std::vector<std::string>::const_iterator I = pass_args.begin(),
|
2016-09-02 03:21:37 +02:00
|
|
|
E = pass_args.end();
|
|
|
|
I != E; ++I)
|
2009-08-24 00:45:37 +02:00
|
|
|
Args.push_back(I->c_str());
|
2017-11-16 02:06:36 +01:00
|
|
|
Args.push_back(Temp->TmpName.c_str());
|
2007-11-14 07:47:06 +01:00
|
|
|
for (unsigned i = 0; i < NumExtraArgs; ++i)
|
2009-08-24 00:45:37 +02:00
|
|
|
Args.push_back(*ExtraArgs);
|
2005-12-22 21:02:55 +01:00
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(errs() << "\nAbout to run:\t";
|
|
|
|
for (unsigned i = 0, e = Args.size() - 1; i != e; ++i) errs()
|
|
|
|
<< " " << Args[i];
|
|
|
|
errs() << "\n";);
|
2010-08-08 01:03:21 +02:00
|
|
|
|
2017-09-13 19:03:37 +02:00
|
|
|
Optional<StringRef> Redirects[3] = {None, None, None};
|
|
|
|
// Redirect stdout and stderr to nowhere if SilencePasses is given.
|
|
|
|
if (SilencePasses) {
|
|
|
|
Redirects[1] = "";
|
|
|
|
Redirects[2] = "";
|
|
|
|
}
|
2008-06-12 15:02:26 +02:00
|
|
|
|
2013-06-18 17:54:13 +02:00
|
|
|
std::string ErrMsg;
|
2018-06-12 19:43:52 +02:00
|
|
|
int result = sys::ExecuteAndWait(Prog, Args, None, Redirects, Timeout,
|
|
|
|
MemoryLimit, &ErrMsg);
|
2004-05-12 04:55:45 +02:00
|
|
|
|
2007-07-04 23:55:50 +02:00
|
|
|
// If we are supposed to delete the bitcode file or if the passes crashed,
|
2004-05-12 04:55:45 +02:00
|
|
|
// remove it now. This may fail if the file was never created, but that's ok.
|
2005-12-22 21:02:55 +01:00
|
|
|
if (DeleteOutput || result != 0)
|
2013-06-18 17:33:18 +02:00
|
|
|
sys::fs::remove(OutputFilename);
|
2005-01-22 18:36:17 +01:00
|
|
|
|
2003-06-02 06:54:16 +02:00
|
|
|
if (!Quiet) {
|
2005-12-22 21:02:55 +01:00
|
|
|
if (result == 0)
|
2009-07-16 17:30:09 +02:00
|
|
|
outs() << "Success!\n";
|
2005-12-22 21:02:55 +01:00
|
|
|
else if (result > 0)
|
2009-07-16 17:30:09 +02:00
|
|
|
outs() << "Exited with error code '" << result << "'\n";
|
2006-08-21 08:04:45 +02:00
|
|
|
else if (result < 0) {
|
|
|
|
if (result == -1)
|
2009-07-16 17:30:09 +02:00
|
|
|
outs() << "Execute failed: " << ErrMsg << "\n";
|
2006-08-21 08:04:45 +02:00
|
|
|
else
|
2011-05-21 02:56:46 +02:00
|
|
|
outs() << "Crashed: " << ErrMsg << "\n";
|
2006-08-21 08:04:45 +02:00
|
|
|
}
|
2005-12-22 21:02:55 +01:00
|
|
|
if (result & 0x01000000)
|
2009-07-16 17:30:09 +02:00
|
|
|
outs() << "Dumped core\n";
|
2003-06-02 06:54:16 +02:00
|
|
|
}
|
2002-11-20 23:28:10 +01:00
|
|
|
|
|
|
|
// Was the child successful?
|
2005-12-22 21:02:55 +01:00
|
|
|
return result != 0;
|
2002-11-20 23:28:10 +01:00
|
|
|
}
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2014-08-26 19:19:03 +02:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
BugDriver::runPassesOn(Module *M, const std::vector<std::string> &Passes,
|
2016-09-02 03:21:37 +02:00
|
|
|
unsigned NumExtraArgs, const char *const *ExtraArgs) {
|
2007-07-04 23:55:50 +02:00
|
|
|
std::string BitcodeResult;
|
2018-02-14 22:44:34 +01:00
|
|
|
if (runPasses(*M, Passes, BitcodeResult, false /*delete*/, true /*quiet*/,
|
2007-11-14 07:47:06 +01:00
|
|
|
NumExtraArgs, ExtraArgs)) {
|
2014-04-25 06:24:47 +02:00
|
|
|
return nullptr;
|
2004-03-14 22:21:57 +01:00
|
|
|
}
|
2004-03-14 22:17:03 +01:00
|
|
|
|
2014-08-26 19:19:03 +02:00
|
|
|
std::unique_ptr<Module> Ret = parseInputFile(BitcodeResult, Context);
|
2014-04-25 06:24:47 +02:00
|
|
|
if (!Ret) {
|
2016-09-02 03:21:37 +02:00
|
|
|
errs() << getToolName() << ": Error reading bitcode file '" << BitcodeResult
|
|
|
|
<< "'!\n";
|
2004-03-14 22:17:03 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-06-18 17:33:18 +02:00
|
|
|
sys::fs::remove(BitcodeResult);
|
2004-03-14 22:17:03 +01:00
|
|
|
return Ret;
|
|
|
|
}
|