2002-11-21 06:28:10 +08:00
|
|
|
//===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:44:31 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 08:00:37 +08:00
|
|
|
//
|
2003-10-21 01:47:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-21 06:28:10 +08:00
|
|
|
//
|
|
|
|
// This file defines an interface that allows bugpoint to run various passes
|
2003-10-11 01:57:28 +08: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-21 06:28:10 +08: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"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2005-12-23 04:02:55 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-08-08 07:03:21 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2006-01-27 02:37:21 +08:00
|
|
|
|
|
|
|
#define DONT_GET_PLUGIN_LOADER_OPTION
|
|
|
|
#include "llvm/Support/PluginLoader.h"
|
|
|
|
|
2003-08-08 05:19:30 +08:00
|
|
|
#include <fstream>
|
2013-06-18 23:54:13 +08:00
|
|
|
|
2004-01-14 11:38:37 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "bugpoint"
|
|
|
|
|
2009-09-08 03:26:11 +08:00
|
|
|
namespace llvm {
|
|
|
|
extern cl::opt<std::string> OutputPrefix;
|
|
|
|
}
|
2007-05-06 13:47:06 +08:00
|
|
|
|
2015-04-15 11:14:06 +08:00
|
|
|
static cl::opt<bool> PreserveBitcodeUseListOrder(
|
|
|
|
"preserve-bc-uselistorder",
|
|
|
|
cl::desc("Preserve use-list order when writing LLVM bitcode."),
|
|
|
|
cl::init(true), cl::Hidden);
|
|
|
|
|
2005-12-23 04:02:55 +08:00
|
|
|
namespace {
|
|
|
|
// ChildOutput - This option captures the name of the child output file that
|
|
|
|
// is set up by the parent bugpoint process
|
|
|
|
cl::opt<std::string> ChildOutput("child-output", cl::ReallyHidden);
|
2013-08-06 05:07:07 +08:00
|
|
|
cl::opt<std::string> OptCmd("opt-command", cl::init(""),
|
|
|
|
cl::desc("Path to opt. (default: search path "
|
|
|
|
"for 'opt'.)"));
|
2005-12-23 04:02:55 +08:00
|
|
|
}
|
|
|
|
|
2007-07-05 05:55:50 +08:00
|
|
|
/// writeProgramToFile - This writes the current "Program" to the named bitcode
|
2002-11-21 06:28:10 +08:00
|
|
|
/// file. If an error occurs, true is returned.
|
|
|
|
///
|
2013-06-18 23:29:32 +08:00
|
|
|
static bool writeProgramToFileAux(tool_output_file &Out, const Module *M) {
|
2015-04-15 11:14:06 +08:00
|
|
|
WriteBitcodeToFile(M, Out.os(), PreserveBitcodeUseListOrder);
|
2013-06-18 23:29:32 +08:00
|
|
|
Out.os().close();
|
|
|
|
if (!Out.os().has_error()) {
|
|
|
|
Out.keep();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BugDriver::writeProgramToFile(const std::string &Filename, int FD,
|
|
|
|
const Module *M) const {
|
2014-08-26 02:16:47 +08:00
|
|
|
tool_output_file Out(Filename, FD);
|
2013-06-18 23:29:32 +08:00
|
|
|
return writeProgramToFileAux(Out, M);
|
|
|
|
}
|
|
|
|
|
2002-12-24 07:49:59 +08:00
|
|
|
bool BugDriver::writeProgramToFile(const std::string &Filename,
|
2010-07-29 02:12:30 +08:00
|
|
|
const Module *M) const {
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
|
|
|
tool_output_file Out(Filename, EC, sys::fs::F_None);
|
|
|
|
if (!EC)
|
2013-06-18 23:29:32 +08:00
|
|
|
return writeProgramToFileAux(Out, M);
|
2010-08-21 00:59:15 +08:00
|
|
|
return true;
|
2002-11-21 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-05 05:55:50 +08:00
|
|
|
/// EmitProgressBitcode - This function is used to output the current Program
|
2003-07-22 05:58:16 +08:00
|
|
|
/// to a file named "bugpoint-ID.bc".
|
2002-11-21 06:28:10 +08:00
|
|
|
///
|
2010-07-29 02:12:30 +08:00
|
|
|
void BugDriver::EmitProgressBitcode(const Module *M,
|
2010-08-05 08:29:04 +08:00
|
|
|
const std::string &ID,
|
|
|
|
bool NoFlyer) const {
|
2007-07-05 05:55:50 +08:00
|
|
|
// Output the input to the current pass to a bitcode file, emit a message
|
2002-11-21 06:28:10 +08:00
|
|
|
// telling the user how to reproduce it: opt -foo blah.bc
|
|
|
|
//
|
2009-09-08 03:26:11 +08:00
|
|
|
std::string Filename = OutputPrefix + "-" + ID + ".bc";
|
2010-07-29 02:12:30 +08:00
|
|
|
if (writeProgramToFile(Filename, M)) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << "Error opening file '" << Filename << "' for writing!\n";
|
2002-11-21 06:28:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Emitted bitcode to '" << Filename << "'\n";
|
2004-02-19 07:24:56 +08:00
|
|
|
if (NoFlyer || PassesToRun.empty()) return;
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "\n*** You can reproduce the problem with: ";
|
2009-08-18 14:08:01 +08:00
|
|
|
if (UseValgrind) outs() << "valgrind ";
|
2012-03-20 07:42:11 +08: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-21 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2011-02-25 02:59:38 +08:00
|
|
|
cl::opt<bool> SilencePasses("silence-passes",
|
|
|
|
cl::desc("Suppress output of running passes (both stdout and stderr)"));
|
2008-06-12 21:02:26 +08:00
|
|
|
|
2010-08-09 06:14:20 +08:00
|
|
|
static cl::list<std::string> OptArgs("opt-args", cl::Positional,
|
|
|
|
cl::desc("<opt arguments>..."),
|
|
|
|
cl::ZeroOrMore, cl::PositionalEatsArgs);
|
|
|
|
|
2007-07-05 05:55:50 +08:00
|
|
|
/// runPasses - Run the specified passes on Program, outputting a bitcode file
|
2003-10-11 01:57:28 +08:00
|
|
|
/// and writing the filename into OutputFile if successful. If the
|
2002-11-21 06:28:10 +08:00
|
|
|
/// optimizations fail for some reason (optimizer crashes), return true,
|
2007-07-05 05:55:50 +08:00
|
|
|
/// otherwise return false. If DeleteOutput is set to true, the bitcode is
|
2002-11-21 06:28:10 +08:00
|
|
|
/// deleted on success, and the filename string is undefined. This prints to
|
2009-07-16 23:30:09 +08:00
|
|
|
/// outs() a single line message indicating whether compilation was successful
|
|
|
|
/// or failed.
|
2002-11-21 06:28:10 +08:00
|
|
|
///
|
2010-08-05 08:29:04 +08:00
|
|
|
bool BugDriver::runPasses(Module *Program,
|
2010-08-08 11:55:08 +08:00
|
|
|
const std::vector<std::string> &Passes,
|
2002-12-24 07:49:59 +08:00
|
|
|
std::string &OutputFilename, bool DeleteOutput,
|
2007-11-14 14:47:06 +08:00
|
|
|
bool Quiet, unsigned NumExtraArgs,
|
|
|
|
const char * const *ExtraArgs) const {
|
2005-12-23 04:02:55 +08:00
|
|
|
// setup the output file name
|
2009-07-16 23:30:09 +08:00
|
|
|
outs().flush();
|
2013-06-18 23:54:13 +08:00
|
|
|
SmallString<128> UniqueFilename;
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code EC = sys::fs::createUniqueFile(
|
2013-07-06 05:01:08 +08:00
|
|
|
OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);
|
2013-06-18 23:54:13 +08:00
|
|
|
if (EC) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << getToolName() << ": Error making unique filename: "
|
2013-06-18 23:54:13 +08:00
|
|
|
<< EC.message() << "\n";
|
|
|
|
return 1;
|
2006-08-24 04:34:57 +08:00
|
|
|
}
|
2013-06-18 23:54:13 +08:00
|
|
|
OutputFilename = UniqueFilename.str();
|
2002-11-21 06:28:10 +08:00
|
|
|
|
2005-12-23 04:02:55 +08:00
|
|
|
// set up the input file name
|
2013-06-18 23:54:13 +08:00
|
|
|
SmallString<128> InputFilename;
|
|
|
|
int InputFD;
|
2013-07-06 05:01:08 +08:00
|
|
|
EC = sys::fs::createUniqueFile(OutputPrefix + "-input-%%%%%%%.bc", InputFD,
|
|
|
|
InputFilename);
|
2013-06-18 23:54:13 +08:00
|
|
|
if (EC) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << getToolName() << ": Error making unique filename: "
|
2013-06-18 23:54:13 +08:00
|
|
|
<< EC.message() << "\n";
|
|
|
|
return 1;
|
2006-08-24 04:34:57 +08:00
|
|
|
}
|
2010-11-04 00:14:07 +08:00
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
tool_output_file InFile(InputFilename, InputFD);
|
2010-11-04 00:14:07 +08:00
|
|
|
|
2015-04-15 11:14:06 +08:00
|
|
|
WriteBitcodeToFile(Program, InFile.os(), PreserveBitcodeUseListOrder);
|
2010-09-01 22:20:41 +08:00
|
|
|
InFile.os().close();
|
|
|
|
if (InFile.os().has_error()) {
|
2013-06-18 23:54:13 +08:00
|
|
|
errs() << "Error writing bitcode file: " << InputFilename << "\n";
|
2010-09-01 22:20:41 +08:00
|
|
|
InFile.os().clear_error();
|
2010-08-21 00:59:15 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-30 00:18:26 +08:00
|
|
|
|
2014-11-04 09:29:59 +08:00
|
|
|
std::string tool = OptCmd;
|
|
|
|
if (OptCmd.empty()) {
|
2014-11-08 05:30:36 +08:00
|
|
|
if (ErrorOr<std::string> Path = sys::findProgramByName("opt"))
|
2014-11-04 09:29:59 +08:00
|
|
|
tool = *Path;
|
2014-11-08 05:30:36 +08:00
|
|
|
else
|
|
|
|
errs() << Path.getError().message() << "\n";
|
2014-11-04 09:29:59 +08:00
|
|
|
}
|
2010-10-30 00:18:26 +08:00
|
|
|
if (tool.empty()) {
|
2012-03-20 07:42:11 +08:00
|
|
|
errs() << "Cannot find `opt' in PATH!\n";
|
2010-10-30 00:18:26 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:29:59 +08:00
|
|
|
std::string Prog;
|
|
|
|
if (UseValgrind) {
|
2014-11-08 05:30:36 +08:00
|
|
|
if (ErrorOr<std::string> Path = sys::findProgramByName("valgrind"))
|
2014-11-04 09:29:59 +08:00
|
|
|
Prog = *Path;
|
2014-11-08 05:30:36 +08:00
|
|
|
else
|
|
|
|
errs() << Path.getError().message() << "\n";
|
2014-11-04 09:29:59 +08:00
|
|
|
} else
|
|
|
|
Prog = tool;
|
|
|
|
if (Prog.empty()) {
|
|
|
|
errs() << "Cannot find `valgrind' in PATH!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-10-30 00:18:26 +08:00
|
|
|
// Ok, everything that could go wrong before running opt is done.
|
2010-08-21 00:59:15 +08:00
|
|
|
InFile.keep();
|
2005-12-23 04:02:55 +08:00
|
|
|
|
|
|
|
// setup the child process' arguments
|
2009-08-24 06:45:37 +08:00
|
|
|
SmallVector<const char*, 8> Args;
|
2006-09-14 11:49:54 +08:00
|
|
|
if (UseValgrind) {
|
2009-08-24 06:45:37 +08:00
|
|
|
Args.push_back("valgrind");
|
|
|
|
Args.push_back("--error-exitcode=1");
|
|
|
|
Args.push_back("-q");
|
|
|
|
Args.push_back(tool.c_str());
|
2006-09-14 11:49:54 +08:00
|
|
|
} else
|
2013-06-14 03:25:37 +08:00
|
|
|
Args.push_back(tool.c_str());
|
2006-09-14 11:49:54 +08:00
|
|
|
|
2010-08-08 07:03:21 +08:00
|
|
|
Args.push_back("-o");
|
2009-08-24 06:45:37 +08:00
|
|
|
Args.push_back(OutputFilename.c_str());
|
2010-08-09 06:14:20 +08:00
|
|
|
for (unsigned i = 0, e = OptArgs.size(); i != e; ++i)
|
|
|
|
Args.push_back(OptArgs[i].c_str());
|
2005-12-23 04:02:55 +08:00
|
|
|
std::vector<std::string> pass_args;
|
2006-01-27 02:37:21 +08:00
|
|
|
for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) {
|
|
|
|
pass_args.push_back( std::string("-load"));
|
|
|
|
pass_args.push_back( PluginLoader::getPlugin(i));
|
|
|
|
}
|
2010-08-08 11:55:08 +08:00
|
|
|
for (std::vector<std::string>::const_iterator I = Passes.begin(),
|
2005-12-23 04:02:55 +08:00
|
|
|
E = Passes.end(); I != E; ++I )
|
2010-08-08 11:55:08 +08:00
|
|
|
pass_args.push_back( std::string("-") + (*I) );
|
2005-12-23 04:02:55 +08:00
|
|
|
for (std::vector<std::string>::const_iterator I = pass_args.begin(),
|
|
|
|
E = pass_args.end(); I != E; ++I )
|
2009-08-24 06:45:37 +08:00
|
|
|
Args.push_back(I->c_str());
|
2013-06-18 23:54:13 +08:00
|
|
|
Args.push_back(InputFilename.c_str());
|
2007-11-14 14:47:06 +08:00
|
|
|
for (unsigned i = 0; i < NumExtraArgs; ++i)
|
2009-08-24 06:45:37 +08:00
|
|
|
Args.push_back(*ExtraArgs);
|
2014-04-25 12:24:47 +08:00
|
|
|
Args.push_back(nullptr);
|
2005-12-23 04:02:55 +08:00
|
|
|
|
2010-08-08 07:03:21 +08:00
|
|
|
DEBUG(errs() << "\nAbout to run:\t";
|
|
|
|
for (unsigned i = 0, e = Args.size()-1; i != e; ++i)
|
|
|
|
errs() << " " << Args[i];
|
|
|
|
errs() << "\n";
|
|
|
|
);
|
|
|
|
|
2008-06-12 21:02:26 +08:00
|
|
|
// Redirect stdout and stderr to nowhere if SilencePasses is given
|
2013-06-14 04:25:38 +08:00
|
|
|
StringRef Nowhere;
|
2014-04-25 12:24:47 +08:00
|
|
|
const StringRef *Redirects[3] = {nullptr, &Nowhere, &Nowhere};
|
2008-06-12 21:02:26 +08:00
|
|
|
|
2013-06-18 23:54:13 +08:00
|
|
|
std::string ErrMsg;
|
2014-04-25 12:24:47 +08:00
|
|
|
int result = sys::ExecuteAndWait(Prog, Args.data(), nullptr,
|
|
|
|
(SilencePasses ? Redirects : nullptr),
|
|
|
|
Timeout, MemoryLimit, &ErrMsg);
|
2004-05-12 10:55:45 +08:00
|
|
|
|
2007-07-05 05:55:50 +08:00
|
|
|
// If we are supposed to delete the bitcode file or if the passes crashed,
|
2004-05-12 10:55:45 +08:00
|
|
|
// remove it now. This may fail if the file was never created, but that's ok.
|
2005-12-23 04:02:55 +08:00
|
|
|
if (DeleteOutput || result != 0)
|
2013-06-18 23:33:18 +08:00
|
|
|
sys::fs::remove(OutputFilename);
|
2005-01-23 01:36:17 +08:00
|
|
|
|
2005-12-23 04:02:55 +08:00
|
|
|
// Remove the temporary input file as well
|
2013-06-18 23:54:13 +08:00
|
|
|
sys::fs::remove(InputFilename.c_str());
|
2005-12-23 04:02:55 +08:00
|
|
|
|
2003-06-02 12:54:16 +08:00
|
|
|
if (!Quiet) {
|
2005-12-23 04:02:55 +08:00
|
|
|
if (result == 0)
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Success!\n";
|
2005-12-23 04:02:55 +08:00
|
|
|
else if (result > 0)
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Exited with error code '" << result << "'\n";
|
2006-08-21 14:04:45 +08:00
|
|
|
else if (result < 0) {
|
|
|
|
if (result == -1)
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Execute failed: " << ErrMsg << "\n";
|
2006-08-21 14:04:45 +08:00
|
|
|
else
|
2011-05-21 08:56:46 +08:00
|
|
|
outs() << "Crashed: " << ErrMsg << "\n";
|
2006-08-21 14:04:45 +08:00
|
|
|
}
|
2005-12-23 04:02:55 +08:00
|
|
|
if (result & 0x01000000)
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Dumped core\n";
|
2003-06-02 12:54:16 +08:00
|
|
|
}
|
2002-11-21 06:28:10 +08:00
|
|
|
|
|
|
|
// Was the child successful?
|
2005-12-23 04:02:55 +08:00
|
|
|
return result != 0;
|
2002-11-21 06:28:10 +08:00
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2004-03-15 05:17:03 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
BugDriver::runPassesOn(Module *M, const std::vector<std::string> &Passes,
|
|
|
|
bool AutoDebugCrashes, unsigned NumExtraArgs,
|
|
|
|
const char *const *ExtraArgs) {
|
2007-07-05 05:55:50 +08:00
|
|
|
std::string BitcodeResult;
|
2010-08-05 08:29:04 +08:00
|
|
|
if (runPasses(M, Passes, BitcodeResult, false/*delete*/, true/*quiet*/,
|
2007-11-14 14:47:06 +08:00
|
|
|
NumExtraArgs, ExtraArgs)) {
|
2004-03-15 05:21:57 +08:00
|
|
|
if (AutoDebugCrashes) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << " Error running this sequence of passes"
|
|
|
|
<< " on the input program!\n";
|
2010-08-05 08:29:04 +08:00
|
|
|
delete swapProgramIn(M);
|
|
|
|
EmitProgressBitcode(M, "pass-error", false);
|
2004-03-15 05:21:57 +08:00
|
|
|
exit(debugOptimizerCrash());
|
|
|
|
}
|
2014-04-25 12:24:47 +08:00
|
|
|
return nullptr;
|
2004-03-15 05:21:57 +08:00
|
|
|
}
|
2004-03-15 05:17:03 +08:00
|
|
|
|
2014-08-27 01:19:03 +08:00
|
|
|
std::unique_ptr<Module> Ret = parseInputFile(BitcodeResult, Context);
|
2014-04-25 12:24:47 +08:00
|
|
|
if (!Ret) {
|
2009-07-16 23:30:09 +08:00
|
|
|
errs() << getToolName() << ": Error reading bitcode file '"
|
|
|
|
<< BitcodeResult << "'!\n";
|
2004-03-15 05:17:03 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-06-18 23:33:18 +08:00
|
|
|
sys::fs::remove(BitcodeResult);
|
2004-03-15 05:17:03 +08:00
|
|
|
return Ret;
|
|
|
|
}
|