2002-12-24 07:50:16 +08:00
|
|
|
//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
|
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-12-24 07:50:16 +08:00
|
|
|
//
|
|
|
|
// This file contains code used to execute the program utilizing one of the
|
2007-07-05 05:55:50 +08:00
|
|
|
// various ways of running LLVM bitcode.
|
2002-12-24 07:50:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BugDriver.h"
|
2006-06-07 06:30:59 +08:00
|
|
|
#include "ToolRunner.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2015-10-15 03:48:01 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
2009-08-24 06:45:37 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2002-12-24 07:50:16 +08:00
|
|
|
#include <fstream>
|
2006-06-06 08:00:42 +08:00
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2002-12-24 07:50:16 +08:00
|
|
|
namespace {
|
2016-09-02 09:21:37 +08:00
|
|
|
// OutputType - Allow the user to specify the way code should be run, to test
|
|
|
|
// for miscompilation.
|
|
|
|
//
|
|
|
|
enum OutputType {
|
|
|
|
AutoPick,
|
|
|
|
RunLLI,
|
|
|
|
RunJIT,
|
|
|
|
RunLLC,
|
|
|
|
RunLLCIA,
|
|
|
|
LLC_Safe,
|
|
|
|
CompileCustom,
|
|
|
|
Custom
|
|
|
|
};
|
|
|
|
|
|
|
|
cl::opt<double> AbsTolerance("abs-tolerance",
|
|
|
|
cl::desc("Absolute error tolerated"),
|
|
|
|
cl::init(0.0));
|
|
|
|
cl::opt<double> RelTolerance("rel-tolerance",
|
|
|
|
cl::desc("Relative error tolerated"),
|
|
|
|
cl::init(0.0));
|
|
|
|
|
|
|
|
cl::opt<OutputType> InterpreterSel(
|
|
|
|
cl::desc("Specify the \"test\" i.e. suspect back-end:"),
|
|
|
|
cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
|
|
|
|
clEnumValN(RunLLI, "run-int", "Execute with the interpreter"),
|
|
|
|
clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
|
|
|
|
clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
|
|
|
|
clEnumValN(RunLLCIA, "run-llc-ia",
|
|
|
|
"Compile with LLC with integrated assembler"),
|
|
|
|
clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
|
|
|
|
clEnumValN(CompileCustom, "compile-custom",
|
|
|
|
"Use -compile-command to define a command to "
|
|
|
|
"compile the bitcode. Useful to avoid linking."),
|
|
|
|
clEnumValN(Custom, "run-custom",
|
|
|
|
"Use -exec-command to define a command to execute "
|
2016-10-09 03:41:06 +08:00
|
|
|
"the bitcode. Useful for cross-compilation.")),
|
2016-09-02 09:21:37 +08:00
|
|
|
cl::init(AutoPick));
|
|
|
|
|
|
|
|
cl::opt<OutputType> SafeInterpreterSel(
|
|
|
|
cl::desc("Specify \"safe\" i.e. known-good backend:"),
|
|
|
|
cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
|
|
|
|
clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
|
|
|
|
clEnumValN(Custom, "safe-run-custom",
|
|
|
|
"Use -exec-command to define a command to execute "
|
2016-10-09 03:41:06 +08:00
|
|
|
"the bitcode. Useful for cross-compilation.")),
|
2016-09-02 09:21:37 +08:00
|
|
|
cl::init(AutoPick));
|
|
|
|
|
|
|
|
cl::opt<std::string> SafeInterpreterPath(
|
|
|
|
"safe-path", cl::desc("Specify the path to the \"safe\" backend program"),
|
|
|
|
cl::init(""));
|
|
|
|
|
|
|
|
cl::opt<bool> AppendProgramExitCode(
|
|
|
|
"append-exit-code",
|
|
|
|
cl::desc("Append the exit code to the output so it gets diff'd too"),
|
|
|
|
cl::init(false));
|
|
|
|
|
|
|
|
cl::opt<std::string>
|
|
|
|
InputFile("input", cl::init("/dev/null"),
|
|
|
|
cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
|
|
|
|
|
|
|
|
cl::list<std::string>
|
|
|
|
AdditionalSOs("additional-so", cl::desc("Additional shared objects to load "
|
|
|
|
"into executing programs"));
|
|
|
|
|
|
|
|
cl::list<std::string> AdditionalLinkerArgs(
|
|
|
|
"Xlinker", cl::desc("Additional arguments to pass to the linker"));
|
|
|
|
|
|
|
|
cl::opt<std::string> CustomCompileCommand(
|
|
|
|
"compile-command", cl::init("llc"),
|
|
|
|
cl::desc("Command to compile the bitcode (use with -compile-custom) "
|
|
|
|
"(default: llc)"));
|
|
|
|
|
|
|
|
cl::opt<std::string> CustomExecCommand(
|
|
|
|
"exec-command", cl::init("simulate"),
|
|
|
|
cl::desc("Command to execute the bitcode (use with -run-custom) "
|
|
|
|
"(default: simulate)"));
|
2002-12-24 07:50:16 +08:00
|
|
|
}
|
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
namespace llvm {
|
2016-09-02 09:21:37 +08:00
|
|
|
// Anything specified after the --args option are taken as arguments to the
|
|
|
|
// program being debugged.
|
|
|
|
cl::list<std::string> InputArgv("args", cl::Positional,
|
|
|
|
cl::desc("<program arguments>..."),
|
|
|
|
cl::ZeroOrMore, cl::PositionalEatsArgs);
|
|
|
|
|
|
|
|
cl::opt<std::string>
|
|
|
|
OutputPrefix("output-prefix", cl::init("bugpoint"),
|
|
|
|
cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
|
2008-12-08 12:02:47 +08:00
|
|
|
}
|
2004-05-05 05:09:16 +08:00
|
|
|
|
2008-12-08 12:02:47 +08:00
|
|
|
namespace {
|
2016-09-02 09:21:37 +08:00
|
|
|
cl::list<std::string> ToolArgv("tool-args", cl::Positional,
|
|
|
|
cl::desc("<tool arguments>..."), cl::ZeroOrMore,
|
|
|
|
cl::PositionalEatsArgs);
|
|
|
|
|
|
|
|
cl::list<std::string> SafeToolArgv("safe-tool-args", cl::Positional,
|
|
|
|
cl::desc("<safe-tool arguments>..."),
|
|
|
|
cl::ZeroOrMore, cl::PositionalEatsArgs);
|
|
|
|
|
|
|
|
cl::opt<std::string> CCBinary("gcc", cl::init(""),
|
|
|
|
cl::desc("The gcc binary to use."));
|
|
|
|
|
|
|
|
cl::list<std::string> CCToolArgv("gcc-tool-args", cl::Positional,
|
|
|
|
cl::desc("<gcc-tool arguments>..."),
|
|
|
|
cl::ZeroOrMore, cl::PositionalEatsArgs);
|
2004-01-14 11:38:37 +08:00
|
|
|
}
|
2003-07-31 04:15:44 +08:00
|
|
|
|
2002-12-24 07:50:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BugDriver method implementation
|
|
|
|
//
|
|
|
|
|
|
|
|
/// initializeExecutionEnvironment - This method is used to set up the
|
|
|
|
/// environment for executing LLVM programs.
|
|
|
|
///
|
2016-09-07 01:18:22 +08:00
|
|
|
Error BugDriver::initializeExecutionEnvironment() {
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << "Initializing execution environment: ";
|
2002-12-24 07:50:16 +08:00
|
|
|
|
2003-09-30 06:40:52 +08:00
|
|
|
// Create an instance of the AbstractInterpreter interface as specified on
|
|
|
|
// the command line
|
2014-04-25 12:24:47 +08:00
|
|
|
SafeInterpreter = nullptr;
|
2002-12-24 07:50:16 +08:00
|
|
|
std::string Message;
|
2004-05-05 05:09:16 +08:00
|
|
|
|
2015-10-15 04:29:54 +08:00
|
|
|
if (CCBinary.empty()) {
|
2015-10-15 03:48:01 +08:00
|
|
|
if (sys::findProgramByName("clang"))
|
2015-10-15 04:29:54 +08:00
|
|
|
CCBinary = "clang";
|
2015-10-15 03:48:01 +08:00
|
|
|
else
|
2015-10-15 04:29:54 +08:00
|
|
|
CCBinary = "gcc";
|
2015-10-15 03:48:01 +08:00
|
|
|
}
|
|
|
|
|
2003-05-03 11:19:41 +08:00
|
|
|
switch (InterpreterSel) {
|
2003-10-22 01:41:35 +08:00
|
|
|
case AutoPick:
|
|
|
|
if (!Interpreter) {
|
|
|
|
InterpreterSel = RunJIT;
|
2016-09-02 09:21:37 +08:00
|
|
|
Interpreter =
|
|
|
|
AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
|
2003-10-22 01:41:35 +08:00
|
|
|
}
|
|
|
|
if (!Interpreter) {
|
|
|
|
InterpreterSel = RunLLC;
|
2016-09-02 09:21:37 +08:00
|
|
|
Interpreter = AbstractInterpreter::createLLC(
|
|
|
|
getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv);
|
2003-10-22 01:41:35 +08:00
|
|
|
}
|
|
|
|
if (!Interpreter) {
|
|
|
|
InterpreterSel = RunLLI;
|
2016-09-02 09:21:37 +08:00
|
|
|
Interpreter =
|
|
|
|
AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
|
2003-10-22 01:41:35 +08:00
|
|
|
}
|
|
|
|
if (!Interpreter) {
|
|
|
|
InterpreterSel = AutoPick;
|
|
|
|
Message = "Sorry, I can't automatically select an interpreter!\n";
|
|
|
|
}
|
|
|
|
break;
|
2003-10-15 05:59:36 +08:00
|
|
|
case RunLLI:
|
2016-09-02 09:21:37 +08:00
|
|
|
Interpreter =
|
|
|
|
AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv);
|
2003-10-15 05:59:36 +08:00
|
|
|
break;
|
|
|
|
case RunLLC:
|
2010-03-16 14:41:47 +08:00
|
|
|
case RunLLCIA:
|
2008-12-08 12:02:47 +08:00
|
|
|
case LLC_Safe:
|
2016-09-02 09:21:37 +08:00
|
|
|
Interpreter = AbstractInterpreter::createLLC(
|
|
|
|
getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv,
|
|
|
|
InterpreterSel == RunLLCIA);
|
2003-10-15 05:59:36 +08:00
|
|
|
break;
|
|
|
|
case RunJIT:
|
2016-09-02 09:21:37 +08:00
|
|
|
Interpreter =
|
|
|
|
AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv);
|
2003-10-15 05:59:36 +08:00
|
|
|
break;
|
2011-02-09 02:20:48 +08:00
|
|
|
case CompileCustom:
|
2016-09-02 09:21:37 +08:00
|
|
|
Interpreter = AbstractInterpreter::createCustomCompiler(
|
|
|
|
Message, CustomCompileCommand);
|
2011-02-09 02:20:48 +08:00
|
|
|
break;
|
2008-04-29 04:53:48 +08:00
|
|
|
case Custom:
|
2011-02-09 02:20:48 +08:00
|
|
|
Interpreter =
|
2016-09-02 09:21:37 +08:00
|
|
|
AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
|
2008-04-29 04:53:48 +08:00
|
|
|
break;
|
2002-12-24 07:50:16 +08:00
|
|
|
}
|
2008-06-12 21:09:43 +08:00
|
|
|
if (!Interpreter)
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << Message;
|
2008-06-12 21:09:43 +08:00
|
|
|
else // Display informational messages on stdout instead of stderr
|
2009-07-16 23:30:09 +08:00
|
|
|
outs() << Message;
|
2002-12-24 07:50:16 +08:00
|
|
|
|
2008-12-08 12:02:47 +08:00
|
|
|
std::string Path = SafeInterpreterPath;
|
|
|
|
if (Path.empty())
|
|
|
|
Path = getToolName();
|
|
|
|
std::vector<std::string> SafeToolArgs = SafeToolArgv;
|
|
|
|
switch (SafeInterpreterSel) {
|
|
|
|
case AutoPick:
|
|
|
|
// In "llc-safe" mode, default to using LLC as the "safe" backend.
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!SafeInterpreter && InterpreterSel == LLC_Safe) {
|
2008-12-08 12:02:47 +08:00
|
|
|
SafeInterpreterSel = RunLLC;
|
|
|
|
SafeToolArgs.push_back("--relocation-model=pic");
|
2016-09-02 09:21:37 +08:00
|
|
|
SafeInterpreter = AbstractInterpreter::createLLC(
|
|
|
|
Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
|
2008-12-08 12:02:47 +08:00
|
|
|
}
|
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!SafeInterpreter && InterpreterSel != RunLLC &&
|
2008-12-08 12:02:47 +08:00
|
|
|
InterpreterSel != RunJIT) {
|
|
|
|
SafeInterpreterSel = RunLLC;
|
|
|
|
SafeToolArgs.push_back("--relocation-model=pic");
|
2016-09-02 09:21:37 +08:00
|
|
|
SafeInterpreter = AbstractInterpreter::createLLC(
|
|
|
|
Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv);
|
2008-12-08 12:02:47 +08:00
|
|
|
}
|
|
|
|
if (!SafeInterpreter) {
|
|
|
|
SafeInterpreterSel = AutoPick;
|
2013-01-25 00:49:14 +08:00
|
|
|
Message = "Sorry, I can't automatically select a safe interpreter!\n";
|
2008-12-08 12:02:47 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RunLLC:
|
2010-03-16 14:41:47 +08:00
|
|
|
case RunLLCIA:
|
2008-12-08 12:02:47 +08:00
|
|
|
SafeToolArgs.push_back("--relocation-model=pic");
|
2016-09-02 09:21:37 +08:00
|
|
|
SafeInterpreter = AbstractInterpreter::createLLC(
|
|
|
|
Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv,
|
|
|
|
SafeInterpreterSel == RunLLCIA);
|
2008-12-08 12:02:47 +08:00
|
|
|
break;
|
|
|
|
case Custom:
|
2011-02-09 02:20:48 +08:00
|
|
|
SafeInterpreter =
|
2016-09-02 09:21:37 +08:00
|
|
|
AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
|
2008-12-08 12:02:47 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Message = "Sorry, this back-end is not supported by bugpoint as the "
|
|
|
|
"\"safe\" backend right now!\n";
|
|
|
|
break;
|
2004-02-19 04:52:02 +08:00
|
|
|
}
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!SafeInterpreter) {
|
|
|
|
outs() << Message << "\nExiting.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-02-09 02:07:10 +08:00
|
|
|
|
2015-10-15 04:29:54 +08:00
|
|
|
cc = CC::create(Message, CCBinary, &CCToolArgv);
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!cc) {
|
|
|
|
outs() << Message << "\nExiting.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-07-25 05:59:10 +08:00
|
|
|
|
2002-12-24 07:50:16 +08:00
|
|
|
// If there was an error creating the selected interpreter, quit with error.
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Interpreter == nullptr)
|
|
|
|
return make_error<StringError>("Failed to init execution environment",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
return Error::success();
|
2002-12-24 07:50:16 +08:00
|
|
|
}
|
|
|
|
|
2010-04-12 13:08:25 +08:00
|
|
|
/// compileProgram - Try to compile the specified module, returning false and
|
|
|
|
/// setting Error if an error occurs. This is used for code generation
|
|
|
|
/// crash testing.
|
2004-02-19 07:25:22 +08:00
|
|
|
///
|
2016-09-07 01:18:22 +08:00
|
|
|
Error BugDriver::compileProgram(Module *M) const {
|
2007-07-05 05:55:50 +08:00
|
|
|
// Emit the program to a bitcode file...
|
2017-11-17 05:40:10 +08:00
|
|
|
auto Temp =
|
|
|
|
sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc");
|
|
|
|
if (!Temp) {
|
|
|
|
errs() << ToolName
|
|
|
|
<< ": Error making unique filename: " << toString(Temp.takeError())
|
2009-07-16 00:35:29 +08:00
|
|
|
<< "\n";
|
2006-08-24 04:34:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-11-17 05:40:10 +08:00
|
|
|
DiscardTemp Discard{*Temp};
|
|
|
|
if (writeProgramToFile(Temp->FD, M)) {
|
|
|
|
errs() << ToolName << ": Error emitting bitcode to file '" << Temp->TmpName
|
2013-06-19 00:14:09 +08:00
|
|
|
<< "'!\n";
|
2004-02-19 07:25:22 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actually compile the program!
|
2017-11-17 05:40:10 +08:00
|
|
|
return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit);
|
2004-02-19 07:25:22 +08:00
|
|
|
}
|
|
|
|
|
2002-12-24 07:50:16 +08:00
|
|
|
/// executeProgram - This method runs "Program", capturing the output of the
|
|
|
|
/// program to a file, returning the filename of the file. A recommended
|
|
|
|
/// filename may be optionally specified.
|
|
|
|
///
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<std::string> BugDriver::executeProgram(const Module *Program,
|
|
|
|
std::string OutputFile,
|
|
|
|
std::string BitcodeFile,
|
|
|
|
const std::string &SharedObj,
|
|
|
|
AbstractInterpreter *AI) const {
|
2016-09-02 09:21:37 +08:00
|
|
|
if (!AI)
|
|
|
|
AI = Interpreter;
|
2003-10-15 05:59:36 +08:00
|
|
|
assert(AI && "Interpreter should have been created already!");
|
2007-07-05 05:55:50 +08:00
|
|
|
bool CreatedBitcode = false;
|
|
|
|
if (BitcodeFile.empty()) {
|
|
|
|
// Emit the program to a bitcode file...
|
2013-06-19 00:14:09 +08:00
|
|
|
SmallString<128> UniqueFilename;
|
|
|
|
int UniqueFD;
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code EC = sys::fs::createUniqueFile(
|
2013-06-19 00:14:09 +08:00
|
|
|
OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);
|
|
|
|
if (EC) {
|
2016-09-02 09:21:37 +08:00
|
|
|
errs() << ToolName << ": Error making unique filename: " << EC.message()
|
|
|
|
<< "!\n";
|
2006-08-24 04:34:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-06-19 00:14:09 +08:00
|
|
|
BitcodeFile = UniqueFilename.str();
|
2002-12-24 07:50:16 +08:00
|
|
|
|
2013-06-19 00:14:09 +08:00
|
|
|
if (writeProgramToFile(BitcodeFile, UniqueFD, Program)) {
|
2016-09-02 09:21:37 +08:00
|
|
|
errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
|
|
|
|
<< "'!\n";
|
2002-12-24 07:50:16 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-07-05 05:55:50 +08:00
|
|
|
CreatedBitcode = true;
|
2002-12-24 07:50:16 +08:00
|
|
|
}
|
|
|
|
|
2007-07-05 05:55:50 +08:00
|
|
|
// Remove the temporary bitcode file when we are done.
|
2013-06-19 00:14:09 +08:00
|
|
|
std::string BitcodePath(BitcodeFile);
|
2016-09-02 09:21:37 +08:00
|
|
|
FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
|
2004-02-19 06:01:21 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
if (OutputFile.empty())
|
|
|
|
OutputFile = OutputPrefix + "-execution-output-%%%%%%%";
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
|
2002-12-24 07:50:16 +08:00
|
|
|
// Check to see if this is a valid output filename...
|
2013-06-19 00:14:09 +08:00
|
|
|
SmallString<128> UniqueFile;
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);
|
2013-06-19 00:14:09 +08:00
|
|
|
if (EC) {
|
2016-09-02 09:21:37 +08:00
|
|
|
errs() << ToolName << ": Error making unique filename: " << EC.message()
|
|
|
|
<< "\n";
|
2006-08-24 04:34:57 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2013-06-19 00:14:09 +08:00
|
|
|
OutputFile = UniqueFile.str();
|
2002-12-24 07:50:16 +08:00
|
|
|
|
2003-10-15 05:59:36 +08:00
|
|
|
// Figure out which shared objects to run, if any.
|
2003-10-15 06:24:31 +08:00
|
|
|
std::vector<std::string> SharedObjs(AdditionalSOs);
|
2003-10-15 05:59:36 +08:00
|
|
|
if (!SharedObj.empty())
|
|
|
|
SharedObjs.push_back(SharedObj);
|
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<int> RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
|
|
|
|
OutputFile, AdditionalLinkerArgs,
|
|
|
|
SharedObjs, Timeout, MemoryLimit);
|
|
|
|
if (Error E = RetVal.takeError())
|
|
|
|
return std::move(E);
|
2004-07-24 15:53:26 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
if (*RetVal == -1) {
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "<timeout>";
|
2004-07-24 15:53:26 +08:00
|
|
|
static bool FirstTimeout = true;
|
|
|
|
if (FirstTimeout) {
|
2016-09-02 09:21:37 +08:00
|
|
|
outs()
|
|
|
|
<< "\n"
|
|
|
|
"*** Program execution timed out! This mechanism is designed to "
|
|
|
|
"handle\n"
|
|
|
|
" programs stuck in infinite loops gracefully. The -timeout "
|
|
|
|
"option\n"
|
|
|
|
" can be used to change the timeout threshold or disable it "
|
|
|
|
"completely\n"
|
|
|
|
" (with -timeout=0). This message is only displayed once.\n";
|
2004-07-24 15:53:26 +08:00
|
|
|
FirstTimeout = false;
|
|
|
|
}
|
|
|
|
}
|
2003-10-15 05:59:36 +08:00
|
|
|
|
2006-11-28 15:04:10 +08:00
|
|
|
if (AppendProgramExitCode) {
|
|
|
|
std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
|
2016-09-07 01:18:22 +08:00
|
|
|
outFile << "exit " << *RetVal << '\n';
|
2006-11-28 15:04:10 +08:00
|
|
|
outFile.close();
|
|
|
|
}
|
|
|
|
|
2002-12-24 07:50:16 +08:00
|
|
|
// Return the filename we captured the output to.
|
|
|
|
return OutputFile;
|
|
|
|
}
|
|
|
|
|
2008-12-08 12:02:47 +08:00
|
|
|
/// executeProgramSafely - Used to create reference output with the "safe"
|
2004-02-12 02:37:32 +08:00
|
|
|
/// backend, if reference output is not provided.
|
|
|
|
///
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<std::string>
|
|
|
|
BugDriver::executeProgramSafely(const Module *Program,
|
|
|
|
const std::string &OutputFile) const {
|
|
|
|
return executeProgram(Program, OutputFile, "", "", SafeInterpreter);
|
2004-02-12 02:37:32 +08:00
|
|
|
}
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<std::string>
|
|
|
|
BugDriver::compileSharedObject(const std::string &BitcodeFile) {
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
assert(Interpreter && "Interpreter should have been created already!");
|
2013-06-18 03:21:38 +08:00
|
|
|
std::string OutputFile;
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
|
2008-12-08 12:02:47 +08:00
|
|
|
// Using the known-good backend.
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<CC::FileType> FT =
|
|
|
|
SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
|
|
|
|
if (Error E = FT.takeError())
|
|
|
|
return std::move(E);
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
|
2003-10-15 05:09:11 +08:00
|
|
|
std::string SharedObjectFile;
|
2016-09-07 01:18:22 +08:00
|
|
|
if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile,
|
|
|
|
AdditionalLinkerArgs))
|
|
|
|
return std::move(E);
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
|
|
|
|
// Remove the intermediate C file
|
2013-06-18 03:21:38 +08:00
|
|
|
sys::fs::remove(OutputFile);
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
|
2014-03-14 23:13:35 +08:00
|
|
|
return SharedObjectFile;
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
}
|
|
|
|
|
2006-08-16 00:40:49 +08:00
|
|
|
/// createReferenceFile - calls compileProgram and then records the output
|
2011-02-09 02:07:10 +08:00
|
|
|
/// into ReferenceOutputFile. Returns true if reference file created, false
|
2006-08-16 00:40:49 +08:00
|
|
|
/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
|
|
|
|
/// this function.
|
|
|
|
///
|
2016-09-07 01:18:22 +08:00
|
|
|
Error BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
|
|
|
|
if (Error E = compileProgram(Program))
|
|
|
|
return E;
|
2010-04-12 13:08:25 +08:00
|
|
|
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<std::string> Result = executeProgramSafely(Program, Filename);
|
|
|
|
if (Error E = Result.takeError()) {
|
2008-12-08 12:02:47 +08:00
|
|
|
if (Interpreter != SafeInterpreter) {
|
2016-09-07 01:18:22 +08:00
|
|
|
E = joinErrors(
|
|
|
|
std::move(E),
|
|
|
|
make_error<StringError>(
|
|
|
|
"*** There is a bug running the \"safe\" backend. Either"
|
|
|
|
" debug it (for example with the -run-jit bugpoint option,"
|
|
|
|
" if JIT is being used as the \"safe\" backend), or fix the"
|
|
|
|
" error some other way.\n",
|
|
|
|
inconvertibleErrorCode()));
|
2006-08-16 00:40:49 +08:00
|
|
|
}
|
2016-09-07 01:18:22 +08:00
|
|
|
return E;
|
2006-08-16 00:40:49 +08:00
|
|
|
}
|
2016-09-07 01:18:22 +08:00
|
|
|
ReferenceOutputFile = *Result;
|
2010-04-12 13:08:25 +08:00
|
|
|
outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
|
2016-09-07 01:18:22 +08:00
|
|
|
return Error::success();
|
2006-08-16 00:40:49 +08:00
|
|
|
}
|
Major addition to bugpoint: ability to debug code generators (LLC and LLI).
The C backend is assumed correct and is used to generate shared objects to be
loaded by the other two code generators.
LLC debugging should be functional now, LLI needs a few more additions to work,
the major one is renaming of external functions to call the JIT lazy function
resolver.
Bugpoint now has a command-line switch -mode with options 'compile' and
'codegen' to debug appropriate portions of tools.
ExecutionDriver.cpp: Added implementations of AbstractInterpreter for LLC and
GCC, broke out common code within other tools, and added ability to generate C
code with CBE individually, without executing the program, and the GCC tool can
generate executables shared objects or executables.
If no reference output is specified to Bugpoint, it will be generated with CBE,
because it is already assumed to be correct for the purposes of debugging using
this method. As a result, many functions now accept as an optional parameter a
shared object to be loaded in, if specified.
llvm-svn: 7293
2003-07-25 02:17:43 +08:00
|
|
|
|
2006-08-16 00:40:49 +08:00
|
|
|
/// diffProgram - This method executes the specified module and diffs the
|
|
|
|
/// output against the file specified by ReferenceOutputFile. If the output
|
2010-04-12 13:08:25 +08:00
|
|
|
/// is different, 1 is returned. If there is a problem with the code
|
2011-05-12 00:31:24 +08:00
|
|
|
/// generator (e.g., llc crashes), this will set ErrMsg.
|
2002-12-24 07:50:16 +08:00
|
|
|
///
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<bool> BugDriver::diffProgram(const Module *Program,
|
|
|
|
const std::string &BitcodeFile,
|
|
|
|
const std::string &SharedObject,
|
|
|
|
bool RemoveBitcode) const {
|
2002-12-24 07:50:16 +08:00
|
|
|
// Execute the program, generating an output file...
|
2016-09-07 01:18:22 +08:00
|
|
|
Expected<std::string> Output =
|
|
|
|
executeProgram(Program, "", BitcodeFile, SharedObject, nullptr);
|
|
|
|
if (Error E = Output.takeError())
|
|
|
|
return std::move(E);
|
2004-02-12 02:37:32 +08:00
|
|
|
|
2003-08-02 04:29:45 +08:00
|
|
|
std::string Error;
|
2002-12-24 07:50:16 +08:00
|
|
|
bool FilesDifferent = false;
|
2016-09-07 01:18:22 +08:00
|
|
|
if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output,
|
2005-01-23 11:45:26 +08:00
|
|
|
AbsTolerance, RelTolerance, &Error)) {
|
|
|
|
if (Diff == 2) {
|
2009-07-16 00:35:29 +08:00
|
|
|
errs() << "While diffing output: " << Error << '\n';
|
2003-08-02 04:29:45 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
FilesDifferent = true;
|
2016-09-02 09:21:37 +08:00
|
|
|
} else {
|
2009-07-11 05:39:28 +08:00
|
|
|
// Remove the generated output if there are no differences.
|
2016-09-07 01:18:22 +08:00
|
|
|
sys::fs::remove(*Output);
|
2009-07-11 05:39:28 +08:00
|
|
|
}
|
2002-12-24 07:50:16 +08:00
|
|
|
|
2007-07-05 05:55:50 +08:00
|
|
|
// Remove the bitcode file if we are supposed to.
|
|
|
|
if (RemoveBitcode)
|
2013-06-19 00:14:09 +08:00
|
|
|
sys::fs::remove(BitcodeFile);
|
2002-12-24 07:50:16 +08:00
|
|
|
return FilesDifferent;
|
|
|
|
}
|
2003-07-29 03:16:14 +08:00
|
|
|
|
2016-09-02 09:21:37 +08:00
|
|
|
bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; }
|