2002-11-20 22:28:10 +00:00
|
|
|
//===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:44:31 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-20 22:28:10 +00:00
|
|
|
//
|
|
|
|
// This file defines an interface that allows bugpoint to run various passes
|
2003-10-10 17:57:28 +00: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 22:28:10 +00: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. :)
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-01-22 17:36:17 +00:00
|
|
|
// Note: as a short term hack, the old Unix-specific code and platform-
|
|
|
|
// independent code co-exist via conditional compilation until it is verified
|
|
|
|
// that the new code works correctly on Unix.
|
|
|
|
|
2002-11-20 22:28:10 +00:00
|
|
|
#include "BugDriver.h"
|
2004-03-14 21:21:57 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-11-20 22:28:10 +00:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2007-05-06 05:47:06 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2003-01-22 23:24:11 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2005-12-22 20:02:55 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-11-29 00:19:40 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2004-12-15 01:53:08 +00:00
|
|
|
#include "llvm/System/Path.h"
|
2005-12-22 20:02:55 +00:00
|
|
|
#include "llvm/System/Program.h"
|
2005-12-23 05:00:38 +00:00
|
|
|
#include "llvm/Config/alloca.h"
|
2006-01-26 18:37:21 +00:00
|
|
|
|
|
|
|
#define DONT_GET_PLUGIN_LOADER_OPTION
|
|
|
|
#include "llvm/Support/PluginLoader.h"
|
|
|
|
|
2003-08-07 21:19:30 +00:00
|
|
|
#include <fstream>
|
2004-01-14 03:38:37 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2007-05-06 05:47:06 +00:00
|
|
|
|
2005-12-22 20:02:55 +00: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);
|
2006-09-14 03:49:54 +00:00
|
|
|
cl::opt<bool> UseValgrind("enable-valgrind",
|
|
|
|
cl::desc("Run optimizations through valgrind"));
|
2005-12-22 20:02:55 +00:00
|
|
|
}
|
|
|
|
|
2007-07-04 21:55:50 +00:00
|
|
|
/// writeProgramToFile - This writes the current "Program" to the named bitcode
|
2002-11-20 22:28:10 +00:00
|
|
|
/// file. If an error occurs, true is returned.
|
|
|
|
///
|
2002-12-23 23:49:59 +00:00
|
|
|
bool BugDriver::writeProgramToFile(const std::string &Filename,
|
2005-04-22 04:13:13 +00:00
|
|
|
Module *M) const {
|
2005-01-22 17:36:17 +00:00
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
|
|
|
std::ofstream Out(Filename.c_str(), io_mode);
|
2002-11-20 22:28:10 +00:00
|
|
|
if (!Out.good()) return true;
|
2007-05-06 09:32:02 +00:00
|
|
|
|
2007-05-06 19:43:09 +00:00
|
|
|
WriteBitcodeToFile(M ? M : Program, Out);
|
2002-11-20 22:28:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-04 21:55:50 +00:00
|
|
|
/// EmitProgressBitcode - This function is used to output the current Program
|
2003-07-21 21:58:16 +00:00
|
|
|
/// to a file named "bugpoint-ID.bc".
|
2002-11-20 22:28:10 +00:00
|
|
|
///
|
2007-07-04 21:55:50 +00:00
|
|
|
void BugDriver::EmitProgressBitcode(const std::string &ID, bool NoFlyer) {
|
|
|
|
// Output the input to the current pass to a bitcode file, emit a message
|
2002-11-20 22:28:10 +00:00
|
|
|
// telling the user how to reproduce it: opt -foo blah.bc
|
|
|
|
//
|
|
|
|
std::string Filename = "bugpoint-" + ID + ".bc";
|
|
|
|
if (writeProgramToFile(Filename)) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Error opening file '" << Filename << "' for writing!\n";
|
2002-11-20 22:28:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-04 21:55:50 +00:00
|
|
|
cout << "Emitted bitcode to '" << Filename << "'\n";
|
2004-02-18 23:24:56 +00:00
|
|
|
if (NoFlyer || PassesToRun.empty()) return;
|
2006-12-07 01:30:32 +00:00
|
|
|
cout << "\n*** You can reproduce the problem with: ";
|
|
|
|
cout << "opt " << Filename << " ";
|
|
|
|
cout << getPassesString(PassesToRun) << "\n";
|
2002-11-20 22:28:10 +00:00
|
|
|
}
|
|
|
|
|
2005-12-22 20:02:55 +00:00
|
|
|
int BugDriver::runPassesAsChild(const std::vector<const PassInfo*> &Passes) {
|
|
|
|
|
2005-01-22 17:36:17 +00:00
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
2005-12-22 20:02:55 +00:00
|
|
|
std::ofstream OutFile(ChildOutput.c_str(), io_mode);
|
2002-11-20 22:28:10 +00:00
|
|
|
if (!OutFile.good()) {
|
2007-07-04 21:55:50 +00:00
|
|
|
cerr << "Error opening bitcode file: " << ChildOutput << "\n";
|
2005-12-22 20:02:55 +00:00
|
|
|
return 1;
|
2002-11-20 22:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PassManager PM;
|
2003-04-24 19:13:02 +00:00
|
|
|
// Make sure that the appropriate target data is always used...
|
2006-06-16 18:23:49 +00:00
|
|
|
PM.add(new TargetData(Program));
|
2003-04-24 19:13:02 +00:00
|
|
|
|
2002-11-20 22:28:10 +00:00
|
|
|
for (unsigned i = 0, e = Passes.size(); i != e; ++i) {
|
|
|
|
if (Passes[i]->getNormalCtor())
|
|
|
|
PM.add(Passes[i]->getNormalCtor()());
|
|
|
|
else
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << "Cannot create pass yet: " << Passes[i]->getPassName() << "\n";
|
2002-11-20 22:28:10 +00:00
|
|
|
}
|
|
|
|
// Check that the module is well formed on completion of optimization
|
|
|
|
PM.add(createVerifierPass());
|
|
|
|
|
2007-07-04 21:55:50 +00:00
|
|
|
// Write bitcode out to disk as the last step...
|
2007-05-06 09:32:02 +00:00
|
|
|
PM.add(CreateBitcodeWriterPass(OutFile));
|
2002-11-20 22:28:10 +00:00
|
|
|
|
|
|
|
// Run all queued passes.
|
|
|
|
PM.run(*Program);
|
2005-12-22 20:02:55 +00:00
|
|
|
|
|
|
|
return 0;
|
2002-11-20 22:28:10 +00:00
|
|
|
}
|
|
|
|
|
2007-07-04 21:55:50 +00:00
|
|
|
/// runPasses - Run the specified passes on Program, outputting a bitcode file
|
2003-10-10 17:57:28 +00:00
|
|
|
/// and writing the filename into OutputFile if successful. If the
|
2002-11-20 22:28:10 +00:00
|
|
|
/// optimizations fail for some reason (optimizer crashes), return true,
|
2007-07-04 21:55:50 +00:00
|
|
|
/// otherwise return false. If DeleteOutput is set to true, the bitcode is
|
2002-11-20 22:28:10 +00:00
|
|
|
/// deleted on success, and the filename string is undefined. This prints to
|
|
|
|
/// cout a single line message indicating whether compilation was successful or
|
|
|
|
/// failed.
|
|
|
|
///
|
|
|
|
bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
|
2002-12-23 23:49:59 +00:00
|
|
|
std::string &OutputFilename, bool DeleteOutput,
|
2007-11-14 06:47:06 +00:00
|
|
|
bool Quiet, unsigned NumExtraArgs,
|
|
|
|
const char * const *ExtraArgs) const {
|
2005-12-22 20:02:55 +00:00
|
|
|
// setup the output file name
|
2006-12-07 01:30:32 +00:00
|
|
|
cout << std::flush;
|
2004-12-15 01:53:08 +00:00
|
|
|
sys::Path uniqueFilename("bugpoint-output.bc");
|
2006-08-23 20:34:57 +00:00
|
|
|
std::string ErrMsg;
|
|
|
|
if (uniqueFilename.makeUnique(true, &ErrMsg)) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << getToolName() << ": Error making unique filename: "
|
|
|
|
<< ErrMsg << "\n";
|
2006-08-23 20:34:57 +00:00
|
|
|
return(1);
|
|
|
|
}
|
2004-12-15 01:53:08 +00:00
|
|
|
OutputFilename = uniqueFilename.toString();
|
2002-11-20 22:28:10 +00:00
|
|
|
|
2005-12-22 20:02:55 +00:00
|
|
|
// set up the input file name
|
|
|
|
sys::Path inputFilename("bugpoint-input.bc");
|
2006-08-23 20:34:57 +00:00
|
|
|
if (inputFilename.makeUnique(true, &ErrMsg)) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << getToolName() << ": Error making unique filename: "
|
|
|
|
<< ErrMsg << "\n";
|
2006-08-23 20:34:57 +00:00
|
|
|
return(1);
|
|
|
|
}
|
2005-12-22 20:02:55 +00:00
|
|
|
std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
|
|
|
|
std::ios::binary;
|
|
|
|
std::ofstream InFile(inputFilename.c_str(), io_mode);
|
|
|
|
if (!InFile.good()) {
|
2007-07-04 21:55:50 +00:00
|
|
|
cerr << "Error opening bitcode file: " << inputFilename << "\n";
|
2005-12-22 20:02:55 +00:00
|
|
|
return(1);
|
2002-11-20 22:28:10 +00:00
|
|
|
}
|
2007-05-06 09:32:02 +00:00
|
|
|
WriteBitcodeToFile(Program, InFile);
|
2005-12-22 20:02:55 +00:00
|
|
|
InFile.close();
|
|
|
|
|
|
|
|
// setup the child process' arguments
|
|
|
|
const char** args = (const char**)
|
2006-01-26 18:37:21 +00:00
|
|
|
alloca(sizeof(const char*) *
|
2008-02-26 10:46:10 +00:00
|
|
|
(Passes.size()+13+2*PluginLoader::getNumPlugins()+NumExtraArgs));
|
2005-12-22 20:02:55 +00:00
|
|
|
int n = 0;
|
2006-09-14 04:20:17 +00:00
|
|
|
sys::Path tool = sys::Program::FindProgramByName(ToolName);
|
2006-09-14 03:49:54 +00:00
|
|
|
if (UseValgrind) {
|
|
|
|
args[n++] = "valgrind";
|
|
|
|
args[n++] = "--error-exitcode=1";
|
|
|
|
args[n++] = "-q";
|
2006-09-14 04:20:17 +00:00
|
|
|
args[n++] = tool.c_str();
|
2006-09-14 03:49:54 +00:00
|
|
|
} else
|
|
|
|
args[n++] = ToolName.c_str();
|
|
|
|
|
2005-12-22 20:02:55 +00:00
|
|
|
args[n++] = "-as-child";
|
|
|
|
args[n++] = "-child-output";
|
|
|
|
args[n++] = OutputFilename.c_str();
|
|
|
|
std::vector<std::string> pass_args;
|
2006-01-26 18:37:21 +00: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));
|
|
|
|
}
|
2005-12-22 20:02:55 +00:00
|
|
|
for (std::vector<const PassInfo*>::const_iterator I = Passes.begin(),
|
|
|
|
E = Passes.end(); I != E; ++I )
|
|
|
|
pass_args.push_back( std::string("-") + (*I)->getPassArgument() );
|
|
|
|
for (std::vector<std::string>::const_iterator I = pass_args.begin(),
|
|
|
|
E = pass_args.end(); I != E; ++I )
|
|
|
|
args[n++] = I->c_str();
|
|
|
|
args[n++] = inputFilename.c_str();
|
2007-11-14 06:47:06 +00:00
|
|
|
for (unsigned i = 0; i < NumExtraArgs; ++i)
|
|
|
|
args[n++] = *ExtraArgs;
|
2005-12-22 20:02:55 +00:00
|
|
|
args[n++] = 0;
|
|
|
|
|
2006-09-14 03:49:54 +00:00
|
|
|
sys::Path prog;
|
|
|
|
if (UseValgrind)
|
|
|
|
prog = sys::Program::FindProgramByName("valgrind");
|
|
|
|
else
|
2006-09-14 04:20:17 +00:00
|
|
|
prog = tool;
|
2007-02-16 19:11:07 +00:00
|
|
|
int result = sys::Program::ExecuteAndWait(prog, args, 0, 0,
|
|
|
|
Timeout, MemoryLimit, &ErrMsg);
|
2004-05-12 02:55:45 +00:00
|
|
|
|
2007-07-04 21:55:50 +00:00
|
|
|
// If we are supposed to delete the bitcode file or if the passes crashed,
|
2004-05-12 02:55:45 +00:00
|
|
|
// remove it now. This may fail if the file was never created, but that's ok.
|
2005-12-22 20:02:55 +00:00
|
|
|
if (DeleteOutput || result != 0)
|
2005-07-08 03:08:58 +00:00
|
|
|
sys::Path(OutputFilename).eraseFromDisk();
|
2005-01-22 17:36:17 +00:00
|
|
|
|
2005-12-22 20:02:55 +00:00
|
|
|
// Remove the temporary input file as well
|
|
|
|
inputFilename.eraseFromDisk();
|
|
|
|
|
2003-06-02 04:54:16 +00:00
|
|
|
if (!Quiet) {
|
2005-12-22 20:02:55 +00:00
|
|
|
if (result == 0)
|
2006-12-07 01:30:32 +00:00
|
|
|
cout << "Success!\n";
|
2005-12-22 20:02:55 +00:00
|
|
|
else if (result > 0)
|
2006-12-07 01:30:32 +00:00
|
|
|
cout << "Exited with error code '" << result << "'\n";
|
2006-08-21 06:04:45 +00:00
|
|
|
else if (result < 0) {
|
|
|
|
if (result == -1)
|
2006-12-07 01:30:32 +00:00
|
|
|
cout << "Execute failed: " << ErrMsg << "\n";
|
2006-08-21 06:04:45 +00:00
|
|
|
else
|
2006-12-07 01:30:32 +00:00
|
|
|
cout << "Crashed with signal #" << abs(result) << "\n";
|
2006-08-21 06:04:45 +00:00
|
|
|
}
|
2005-12-22 20:02:55 +00:00
|
|
|
if (result & 0x01000000)
|
2006-12-07 01:30:32 +00:00
|
|
|
cout << "Dumped core\n";
|
2003-06-02 04:54:16 +00:00
|
|
|
}
|
2002-11-20 22:28:10 +00:00
|
|
|
|
|
|
|
// Was the child successful?
|
2005-12-22 20:02:55 +00:00
|
|
|
return result != 0;
|
2002-11-20 22:28:10 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-03-14 21:17:03 +00:00
|
|
|
|
|
|
|
/// runPassesOn - Carefully run the specified set of pass on the specified
|
|
|
|
/// module, returning the transformed module on success, or a null pointer on
|
|
|
|
/// failure.
|
|
|
|
Module *BugDriver::runPassesOn(Module *M,
|
2004-03-14 21:21:57 +00:00
|
|
|
const std::vector<const PassInfo*> &Passes,
|
2007-11-14 06:47:06 +00:00
|
|
|
bool AutoDebugCrashes, unsigned NumExtraArgs,
|
|
|
|
const char * const *ExtraArgs) {
|
2004-03-14 21:17:03 +00:00
|
|
|
Module *OldProgram = swapProgramIn(M);
|
2007-07-04 21:55:50 +00:00
|
|
|
std::string BitcodeResult;
|
2007-11-14 06:47:06 +00:00
|
|
|
if (runPasses(Passes, BitcodeResult, false/*delete*/, true/*quiet*/,
|
|
|
|
NumExtraArgs, ExtraArgs)) {
|
2004-03-14 21:21:57 +00:00
|
|
|
if (AutoDebugCrashes) {
|
2006-12-07 01:30:32 +00:00
|
|
|
cerr << " Error running this sequence of passes"
|
|
|
|
<< " on the input program!\n";
|
2004-03-14 21:21:57 +00:00
|
|
|
delete OldProgram;
|
2007-07-04 21:55:50 +00:00
|
|
|
EmitProgressBitcode("pass-error", false);
|
2004-03-14 21:21:57 +00:00
|
|
|
exit(debugOptimizerCrash());
|
|
|
|
}
|
2004-03-14 21:37:41 +00:00
|
|
|
swapProgramIn(OldProgram);
|
2004-03-14 21:17:03 +00:00
|
|
|
return 0;
|
2004-03-14 21:21:57 +00:00
|
|
|
}
|
2004-03-14 21:17:03 +00:00
|
|
|
|
|
|
|
// Restore the current program.
|
|
|
|
swapProgramIn(OldProgram);
|
|
|
|
|
2007-07-04 21:55:50 +00:00
|
|
|
Module *Ret = ParseInputFile(BitcodeResult);
|
2004-03-14 21:17:03 +00:00
|
|
|
if (Ret == 0) {
|
2007-07-04 21:55:50 +00:00
|
|
|
cerr << getToolName() << ": Error reading bitcode file '"
|
|
|
|
<< BitcodeResult << "'!\n";
|
2004-03-14 21:17:03 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-07-04 21:55:50 +00:00
|
|
|
sys::Path(BitcodeResult).eraseFromDisk(); // No longer need the file on disk
|
2004-03-14 21:17:03 +00:00
|
|
|
return Ret;
|
|
|
|
}
|