Remove use of exceptions from bugpoint. No deliberate functionality change!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@101013 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Nick Lewycky
2010-04-12 05:08:25 +00:00
parent 67a71b5306
commit 22ff748712
10 changed files with 460 additions and 338 deletions

View File

@@ -115,30 +115,25 @@ bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
assert(Program == 0 && "Cannot call addSources multiple times!"); assert(Program == 0 && "Cannot call addSources multiple times!");
assert(!Filenames.empty() && "Must specify at least on input filename!"); assert(!Filenames.empty() && "Must specify at least on input filename!");
try { // Load the first input file.
// Load the first input file. Program = ParseInputFile(Filenames[0], Context);
Program = ParseInputFile(Filenames[0], Context); if (Program == 0) return true;
if (Program == 0) return true;
if (!run_as_child)
outs() << "Read input file : '" << Filenames[0] << "'\n";
for (unsigned i = 1, e = Filenames.size(); i != e; ++i) {
std::auto_ptr<Module> M(ParseInputFile(Filenames[i], Context));
if (M.get() == 0) return true;
if (!run_as_child) if (!run_as_child)
outs() << "Read input file : '" << Filenames[0] << "'\n"; outs() << "Linking in input file: '" << Filenames[i] << "'\n";
std::string ErrorMessage;
for (unsigned i = 1, e = Filenames.size(); i != e; ++i) { if (Linker::LinkModules(Program, M.get(), &ErrorMessage)) {
std::auto_ptr<Module> M(ParseInputFile(Filenames[i], Context)); errs() << ToolName << ": error linking in '" << Filenames[i] << "': "
if (M.get() == 0) return true; << ErrorMessage << '\n';
return true;
if (!run_as_child)
outs() << "Linking in input file: '" << Filenames[i] << "'\n";
std::string ErrorMessage;
if (Linker::LinkModules(Program, M.get(), &ErrorMessage)) {
errs() << ToolName << ": error linking in '" << Filenames[i] << "': "
<< ErrorMessage << '\n';
return true;
}
} }
} catch (const std::string &Error) {
errs() << ToolName << ": error reading input '" << Error << "'\n";
return true;
} }
if (!run_as_child) if (!run_as_child)
@@ -153,7 +148,7 @@ bool BugDriver::addSources(const std::vector<std::string> &Filenames) {
/// run - The top level method that is invoked after all of the instance /// run - The top level method that is invoked after all of the instance
/// variables are set up from command line arguments. /// variables are set up from command line arguments.
/// ///
bool BugDriver::run() { bool BugDriver::run(std::string &ErrMsg) {
// The first thing to do is determine if we're running as a child. If we are, // The first thing to do is determine if we're running as a child. If we are,
// then what to do is very narrow. This form of invocation is only called // then what to do is very narrow. This form of invocation is only called
// from the runPasses method to actually run those passes in a child process. // from the runPasses method to actually run those passes in a child process.
@@ -165,7 +160,7 @@ bool BugDriver::run() {
if (run_find_bugs) { if (run_find_bugs) {
// Rearrange the passes and apply them to the program. Repeat this process // Rearrange the passes and apply them to the program. Repeat this process
// until the user kills the program or we find a bug. // until the user kills the program or we find a bug.
return runManyPasses(PassesToRun); return runManyPasses(PassesToRun, ErrMsg);
} }
// If we're not running as a child, the first thing that we must do is // If we're not running as a child, the first thing that we must do is
@@ -186,14 +181,13 @@ bool BugDriver::run() {
// Test to see if we have a code generator crash. // Test to see if we have a code generator crash.
outs() << "Running the code generator to test for a crash: "; outs() << "Running the code generator to test for a crash: ";
try { std::string Error;
compileProgram(Program); compileProgram(Program, &Error);
outs() << '\n'; if (!Error.empty()) {
} catch (ToolExecutionError &TEE) { outs() << Error;
outs() << TEE.what(); return debugCodeGeneratorCrash(ErrMsg);
return debugCodeGeneratorCrash();
} }
outs() << '\n';
// Run the raw input to see where we are coming from. If a reference output // Run the raw input to see where we are coming from. If a reference output
// was specified, make sure that the raw output matches it. If not, it's a // was specified, make sure that the raw output matches it. If not, it's a
@@ -202,8 +196,8 @@ bool BugDriver::run() {
bool CreatedOutput = false; bool CreatedOutput = false;
if (ReferenceOutputFile.empty()) { if (ReferenceOutputFile.empty()) {
outs() << "Generating reference output from raw program: "; outs() << "Generating reference output from raw program: ";
if(!createReferenceFile(Program)){ if (!createReferenceFile(Program)) {
return debugCodeGeneratorCrash(); return debugCodeGeneratorCrash(ErrMsg);
} }
CreatedOutput = true; CreatedOutput = true;
} }
@@ -217,24 +211,29 @@ bool BugDriver::run() {
// matches, then we assume there is a miscompilation bug and try to // matches, then we assume there is a miscompilation bug and try to
// diagnose it. // diagnose it.
outs() << "*** Checking the code generator...\n"; outs() << "*** Checking the code generator...\n";
try { bool Diff = diffProgram("", "", false, &Error);
if (!diffProgram()) { if (!Error.empty()) {
outs() << "\n*** Output matches: Debugging miscompilation!\n"; errs() << Error;
return debugMiscompilation(); return debugCodeGeneratorCrash(ErrMsg);
}
if (!Diff) {
outs() << "\n*** Output matches: Debugging miscompilation!\n";
debugMiscompilation(&Error);
if (!Error.empty()) {
errs() << Error;
return debugCodeGeneratorCrash(ErrMsg);
} }
} catch (ToolExecutionError &TEE) { return false;
errs() << TEE.what();
return debugCodeGeneratorCrash();
} }
outs() << "\n*** Input program does not match reference diff!\n"; outs() << "\n*** Input program does not match reference diff!\n";
outs() << "Debugging code generator problem!\n"; outs() << "Debugging code generator problem!\n";
try { bool Failure = debugCodeGenerator(&Error);
return debugCodeGenerator(); if (!Error.empty()) {
} catch (ToolExecutionError &TEE) { errs() << Error;
errs() << TEE.what(); return debugCodeGeneratorCrash(ErrMsg);
return debugCodeGeneratorCrash();
} }
return Failure;
} }
void llvm::PrintFunctionList(const std::vector<Function*> &Funcs) { void llvm::PrintFunctionList(const std::vector<Function*> &Funcs) {

View File

@@ -88,7 +88,7 @@ public:
/// variables are set up from command line arguments. The \p as_child argument /// variables are set up from command line arguments. The \p as_child argument
/// indicates whether the driver is to run in parent mode or child mode. /// indicates whether the driver is to run in parent mode or child mode.
/// ///
bool run(); bool run(std::string &ErrMsg);
/// debugOptimizerCrash - This method is called when some optimizer pass /// debugOptimizerCrash - This method is called when some optimizer pass
/// crashes on input. It attempts to prune down the testcase to something /// crashes on input. It attempts to prune down the testcase to something
@@ -99,12 +99,12 @@ public:
/// debugCodeGeneratorCrash - This method is called when the code generator /// debugCodeGeneratorCrash - This method is called when the code generator
/// crashes on an input. It attempts to reduce the input as much as possible /// crashes on an input. It attempts to reduce the input as much as possible
/// while still causing the code generator to crash. /// while still causing the code generator to crash.
bool debugCodeGeneratorCrash(); bool debugCodeGeneratorCrash(std::string &Error);
/// debugMiscompilation - This method is used when the passes selected are not /// debugMiscompilation - This method is used when the passes selected are not
/// crashing, but the generated output is semantically different from the /// crashing, but the generated output is semantically different from the
/// input. /// input.
bool debugMiscompilation(); void debugMiscompilation(std::string *Error);
/// debugPassMiscompilation - This method is called when the specified pass /// debugPassMiscompilation - This method is called when the specified pass
/// miscompiles Program as input. It tries to reduce the testcase to /// miscompiles Program as input. It tries to reduce the testcase to
@@ -118,12 +118,13 @@ public:
/// compileSharedObject - This method creates a SharedObject from a given /// compileSharedObject - This method creates a SharedObject from a given
/// BitcodeFile for debugging a code generator. /// BitcodeFile for debugging a code generator.
/// ///
std::string compileSharedObject(const std::string &BitcodeFile); std::string compileSharedObject(const std::string &BitcodeFile,
std::string &Error);
/// debugCodeGenerator - This method narrows down a module to a function or /// debugCodeGenerator - This method narrows down a module to a function or
/// set of functions, using the CBE as a ``safe'' code generator for other /// set of functions, using the CBE as a ``safe'' code generator for other
/// functions that are not under consideration. /// functions that are not under consideration.
bool debugCodeGenerator(); bool debugCodeGenerator(std::string *Error);
/// isExecutingJIT - Returns true if bugpoint is currently testing the JIT /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
/// ///
@@ -164,27 +165,29 @@ public:
/// the specified one as the current program. /// the specified one as the current program.
void setNewProgram(Module *M); void setNewProgram(Module *M);
/// compileProgram - Try to compile the specified module, throwing an /// compileProgram - Try to compile the specified module, returning false and
/// exception if an error occurs, or returning normally if not. This is used /// setting Error if an error occurs. This is used for code generation
/// for code generation crash testing. /// crash testing.
/// ///
void compileProgram(Module *M); void compileProgram(Module *M, std::string *Error);
/// executeProgram - This method runs "Program", capturing the output of the /// executeProgram - This method runs "Program", capturing the output of the
/// program to a file, returning the filename of the file. A recommended /// program to a file. The recommended filename will be filled in with the
/// filename may be optionally specified. If there is a problem with the code /// name of the file with the captured output. If there is a problem with
/// generator (e.g., llc crashes), this will throw an exception. /// the code generator (e.g., llc crashes), this will throw an exception.
/// ///
std::string executeProgram(std::string RequestedOutputFilename = "", std::string executeProgram(std::string OutputFilename,
std::string Bitcode = "", std::string Bitcode,
const std::string &SharedObjects = "", const std::string &SharedObjects,
AbstractInterpreter *AI = 0); AbstractInterpreter *AI,
std::string *Error);
/// executeProgramSafely - Used to create reference output with the "safe" /// executeProgramSafely - Used to create reference output with the "safe"
/// backend, if reference output is not provided. If there is a problem with /// backend, if reference output is not provided. If there is a problem with
/// the code generator (e.g., llc crashes), this will throw an exception. /// the code generator (e.g., llc crashes), this will return false and set
/// Error.
/// ///
std::string executeProgramSafely(std::string OutputFile = ""); std::string executeProgramSafely(std::string OutputFile, std::string *Error);
/// createReferenceFile - calls compileProgram and then records the output /// createReferenceFile - calls compileProgram and then records the output
/// into ReferenceOutputFile. Returns true if reference file created, false /// into ReferenceOutputFile. Returns true if reference file created, false
@@ -196,13 +199,14 @@ public:
/// diffProgram - This method executes the specified module and diffs the /// diffProgram - This method executes the specified module and diffs the
/// output against the file specified by ReferenceOutputFile. If the output /// output against the file specified by ReferenceOutputFile. If the output
/// is different, true is returned. If there is a problem with the code /// is different, 1 is returned. If there is a problem with the code
/// generator (e.g., llc crashes), this will throw an exception. /// generator (e.g., llc crashes), this will return -1 and set Error.
/// ///
bool diffProgram(const std::string &BitcodeFile = "", bool diffProgram(const std::string &BitcodeFile = "",
const std::string &SharedObj = "", const std::string &SharedObj = "",
bool RemoveBitcode = false); bool RemoveBitcode = false,
std::string *Error = 0);
/// EmitProgressBitcode - This function is used to output the current Program /// EmitProgressBitcode - This function is used to output the current Program
/// to a file named "bugpoint-ID.bc". /// to a file named "bugpoint-ID.bc".
/// ///
@@ -266,7 +270,8 @@ public:
/// If the passes did not compile correctly, output the command required to /// If the passes did not compile correctly, output the command required to
/// recreate the failure. This returns true if a compiler error is found. /// recreate the failure. This returns true if a compiler error is found.
/// ///
bool runManyPasses(const std::vector<const PassInfo*> &AllPasses); bool runManyPasses(const std::vector<const PassInfo*> &AllPasses,
std::string &ErrMsg);
/// writeProgramToFile - This writes the current "Program" to the named /// writeProgramToFile - This writes the current "Program" to the named
/// bitcode file. If an error occurs, true is returned. /// bitcode file. If an error occurs, true is returned.

View File

@@ -53,13 +53,15 @@ namespace llvm {
// passes. If we return true, we update the current module of bugpoint. // passes. If we return true, we update the current module of bugpoint.
// //
virtual TestResult doTest(std::vector<const PassInfo*> &Removed, virtual TestResult doTest(std::vector<const PassInfo*> &Removed,
std::vector<const PassInfo*> &Kept); std::vector<const PassInfo*> &Kept,
std::string &Error);
}; };
} }
ReducePassList::TestResult ReducePassList::TestResult
ReducePassList::doTest(std::vector<const PassInfo*> &Prefix, ReducePassList::doTest(std::vector<const PassInfo*> &Prefix,
std::vector<const PassInfo*> &Suffix) { std::vector<const PassInfo*> &Suffix,
std::string &Error) {
sys::Path PrefixOutput; sys::Path PrefixOutput;
Module *OrigProgram = 0; Module *OrigProgram = 0;
if (!Prefix.empty()) { if (!Prefix.empty()) {
@@ -107,27 +109,26 @@ namespace {
bool (*TestFn)(BugDriver &, Module *); bool (*TestFn)(BugDriver &, Module *);
public: public:
ReduceCrashingGlobalVariables(BugDriver &bd, ReduceCrashingGlobalVariables(BugDriver &bd,
bool (*testFn)(BugDriver&, Module*)) bool (*testFn)(BugDriver &, Module *))
: BD(bd), TestFn(testFn) {} : BD(bd), TestFn(testFn) {}
virtual TestResult doTest(std::vector<GlobalVariable*>& Prefix, virtual TestResult doTest(std::vector<GlobalVariable*> &Prefix,
std::vector<GlobalVariable*>& Kept) { std::vector<GlobalVariable*> &Kept,
std::string &Error) {
if (!Kept.empty() && TestGlobalVariables(Kept)) if (!Kept.empty() && TestGlobalVariables(Kept))
return KeepSuffix; return KeepSuffix;
if (!Prefix.empty() && TestGlobalVariables(Prefix)) if (!Prefix.empty() && TestGlobalVariables(Prefix))
return KeepPrefix; return KeepPrefix;
return NoFailure; return NoFailure;
} }
bool TestGlobalVariables(std::vector<GlobalVariable*>& GVs); bool TestGlobalVariables(std::vector<GlobalVariable*> &GVs);
}; };
} }
bool bool
ReduceCrashingGlobalVariables::TestGlobalVariables( ReduceCrashingGlobalVariables::TestGlobalVariables(
std::vector<GlobalVariable*>& GVs) { std::vector<GlobalVariable*> &GVs) {
// Clone the program to try hacking it apart... // Clone the program to try hacking it apart...
DenseMap<const Value*, Value*> ValueMap; DenseMap<const Value*, Value*> ValueMap;
Module *M = CloneModule(BD.getProgram(), ValueMap); Module *M = CloneModule(BD.getProgram(), ValueMap);
@@ -182,7 +183,8 @@ namespace llvm {
: BD(bd), TestFn(testFn) {} : BD(bd), TestFn(testFn) {}
virtual TestResult doTest(std::vector<Function*> &Prefix, virtual TestResult doTest(std::vector<Function*> &Prefix,
std::vector<Function*> &Kept) { std::vector<Function*> &Kept,
std::string &Error) {
if (!Kept.empty() && TestFuncs(Kept)) if (!Kept.empty() && TestFuncs(Kept))
return KeepSuffix; return KeepSuffix;
if (!Prefix.empty() && TestFuncs(Prefix)) if (!Prefix.empty() && TestFuncs(Prefix))
@@ -253,7 +255,8 @@ namespace {
: BD(bd), TestFn(testFn) {} : BD(bd), TestFn(testFn) {}
virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix, virtual TestResult doTest(std::vector<const BasicBlock*> &Prefix,
std::vector<const BasicBlock*> &Kept) { std::vector<const BasicBlock*> &Kept,
std::string &Error) {
if (!Kept.empty() && TestBlocks(Kept)) if (!Kept.empty() && TestBlocks(Kept))
return KeepSuffix; return KeepSuffix;
if (!Prefix.empty() && TestBlocks(Prefix)) if (!Prefix.empty() && TestBlocks(Prefix))
@@ -355,7 +358,8 @@ namespace {
: BD(bd), TestFn(testFn) {} : BD(bd), TestFn(testFn) {}
virtual TestResult doTest(std::vector<const Instruction*> &Prefix, virtual TestResult doTest(std::vector<const Instruction*> &Prefix,
std::vector<const Instruction*> &Kept) { std::vector<const Instruction*> &Kept,
std::string &Error) {
if (!Kept.empty() && TestInsts(Kept)) if (!Kept.empty() && TestInsts(Kept))
return KeepSuffix; return KeepSuffix;
if (!Prefix.empty() && TestInsts(Prefix)) if (!Prefix.empty() && TestInsts(Prefix))
@@ -421,7 +425,8 @@ bool ReduceCrashingInstructions::TestInsts(std::vector<const Instruction*>
/// DebugACrash - Given a predicate that determines whether a component crashes /// DebugACrash - Given a predicate that determines whether a component crashes
/// on a program, try to destructively reduce the program while still keeping /// on a program, try to destructively reduce the program while still keeping
/// the predicate true. /// the predicate true.
static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) { static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *),
std::string &Error) {
// See if we can get away with nuking some of the global variable initializers // See if we can get away with nuking some of the global variable initializers
// in the program... // in the program...
if (!NoGlobalRM && if (!NoGlobalRM &&
@@ -464,7 +469,9 @@ static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
<< "variables in the testcase\n"; << "variables in the testcase\n";
unsigned OldSize = GVs.size(); unsigned OldSize = GVs.size();
ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs); ReduceCrashingGlobalVariables(BD, TestFn).reduceList(GVs, Error);
if (!Error.empty())
return true;
if (GVs.size() < OldSize) if (GVs.size() < OldSize)
BD.EmitProgressBitcode("reduced-global-variables"); BD.EmitProgressBitcode("reduced-global-variables");
@@ -485,7 +492,7 @@ static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
"in the testcase\n"; "in the testcase\n";
unsigned OldSize = Functions.size(); unsigned OldSize = Functions.size();
ReduceCrashingFunctions(BD, TestFn).reduceList(Functions); ReduceCrashingFunctions(BD, TestFn).reduceList(Functions, Error);
if (Functions.size() < OldSize) if (Functions.size() < OldSize)
BD.EmitProgressBitcode("reduced-function"); BD.EmitProgressBitcode("reduced-function");
@@ -503,7 +510,7 @@ static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI) for (Function::const_iterator FI = I->begin(), E = I->end(); FI !=E; ++FI)
Blocks.push_back(FI); Blocks.push_back(FI);
unsigned OldSize = Blocks.size(); unsigned OldSize = Blocks.size();
ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks); ReduceCrashingBlocks(BD, TestFn).reduceList(Blocks, Error);
if (Blocks.size() < OldSize) if (Blocks.size() < OldSize)
BD.EmitProgressBitcode("reduced-blocks"); BD.EmitProgressBitcode("reduced-blocks");
} }
@@ -521,7 +528,7 @@ static bool DebugACrash(BugDriver &BD, bool (*TestFn)(BugDriver &, Module *)) {
if (!isa<TerminatorInst>(I)) if (!isa<TerminatorInst>(I))
Insts.push_back(I); Insts.push_back(I);
ReduceCrashingInstructions(BD, TestFn).reduceList(Insts); ReduceCrashingInstructions(BD, TestFn).reduceList(Insts, Error);
} }
// FIXME: This should use the list reducer to converge faster by deleting // FIXME: This should use the list reducer to converge faster by deleting
@@ -614,9 +621,11 @@ static bool TestForOptimizerCrash(BugDriver &BD, Module *M) {
bool BugDriver::debugOptimizerCrash(const std::string &ID) { bool BugDriver::debugOptimizerCrash(const std::string &ID) {
outs() << "\n*** Debugging optimizer crash!\n"; outs() << "\n*** Debugging optimizer crash!\n";
std::string Error;
// Reduce the list of passes which causes the optimizer to crash... // Reduce the list of passes which causes the optimizer to crash...
if (!BugpointIsInterrupted) if (!BugpointIsInterrupted)
ReducePassList(*this).reduceList(PassesToRun); ReducePassList(*this).reduceList(PassesToRun, Error);
assert(Error.empty());
outs() << "\n*** Found crashing pass" outs() << "\n*** Found crashing pass"
<< (PassesToRun.size() == 1 ? ": " : "es: ") << (PassesToRun.size() == 1 ? ": " : "es: ")
@@ -624,25 +633,27 @@ bool BugDriver::debugOptimizerCrash(const std::string &ID) {
EmitProgressBitcode(ID); EmitProgressBitcode(ID);
return DebugACrash(*this, TestForOptimizerCrash); bool Success = DebugACrash(*this, TestForOptimizerCrash, Error);
assert(Error.empty());
return Success;
} }
static bool TestForCodeGenCrash(BugDriver &BD, Module *M) { static bool TestForCodeGenCrash(BugDriver &BD, Module *M) {
try { std::string Error;
BD.compileProgram(M); BD.compileProgram(M, &Error);
errs() << '\n'; if (!Error.empty()) {
return false;
} catch (ToolExecutionError &) {
errs() << "<crash>\n"; errs() << "<crash>\n";
return true; // Tool is still crashing. return true; // Tool is still crashing.
} }
errs() << '\n';
return false;
} }
/// debugCodeGeneratorCrash - This method is called when the code generator /// debugCodeGeneratorCrash - This method is called when the code generator
/// crashes on an input. It attempts to reduce the input as much as possible /// crashes on an input. It attempts to reduce the input as much as possible
/// while still causing the code generator to crash. /// while still causing the code generator to crash.
bool BugDriver::debugCodeGeneratorCrash() { bool BugDriver::debugCodeGeneratorCrash(std::string &Error) {
errs() << "*** Debugging code generator crash!\n"; errs() << "*** Debugging code generator crash!\n";
return DebugACrash(*this, TestForCodeGenCrash); return DebugACrash(*this, TestForCodeGenCrash, Error);
} }

View File

@@ -278,15 +278,15 @@ bool BugDriver::initializeExecutionEnvironment() {
return Interpreter == 0; return Interpreter == 0;
} }
/// compileProgram - Try to compile the specified module, throwing an exception /// compileProgram - Try to compile the specified module, returning false and
/// if an error occurs, or returning normally if not. This is used for code /// setting Error if an error occurs. This is used for code generation
/// generation crash testing. /// crash testing.
/// ///
void BugDriver::compileProgram(Module *M) { void BugDriver::compileProgram(Module *M, std::string *Error) {
// Emit the program to a bitcode file... // Emit the program to a bitcode file...
sys::Path BitcodeFile (OutputPrefix + "-test-program.bc"); sys::Path BitcodeFile (OutputPrefix + "-test-program.bc");
std::string ErrMsg; std::string ErrMsg;
if (BitcodeFile.makeUnique(true,&ErrMsg)) { if (BitcodeFile.makeUnique(true, &ErrMsg)) {
errs() << ToolName << ": Error making unique filename: " << ErrMsg errs() << ToolName << ": Error making unique filename: " << ErrMsg
<< "\n"; << "\n";
exit(1); exit(1);
@@ -297,11 +297,11 @@ void BugDriver::compileProgram(Module *M) {
exit(1); exit(1);
} }
// Remove the temporary bitcode file when we are done. // Remove the temporary bitcode file when we are done.
FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps); FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps);
// Actually compile the program! // Actually compile the program!
Interpreter->compileProgram(BitcodeFile.str()); Interpreter->compileProgram(BitcodeFile.str(), Error);
} }
@@ -312,7 +312,8 @@ void BugDriver::compileProgram(Module *M) {
std::string BugDriver::executeProgram(std::string OutputFile, std::string BugDriver::executeProgram(std::string OutputFile,
std::string BitcodeFile, std::string BitcodeFile,
const std::string &SharedObj, const std::string &SharedObj,
AbstractInterpreter *AI) { AbstractInterpreter *AI,
std::string *Error) {
if (AI == 0) AI = Interpreter; if (AI == 0) AI = Interpreter;
assert(AI && "Interpreter should have been created already!"); assert(AI && "Interpreter should have been created already!");
bool CreatedBitcode = false; bool CreatedBitcode = false;
@@ -355,9 +356,11 @@ std::string BugDriver::executeProgram(std::string OutputFile,
if (!SharedObj.empty()) if (!SharedObj.empty())
SharedObjs.push_back(SharedObj); SharedObjs.push_back(SharedObj);
int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
OutputFile, AdditionalLinkerArgs, SharedObjs, Error, AdditionalLinkerArgs, SharedObjs,
Timeout, MemoryLimit); Timeout, MemoryLimit);
if (!Error->empty())
return OutputFile;
if (RetVal == -1) { if (RetVal == -1) {
errs() << "<timeout>"; errs() << "<timeout>";
@@ -385,21 +388,28 @@ std::string BugDriver::executeProgram(std::string OutputFile,
/// executeProgramSafely - Used to create reference output with the "safe" /// executeProgramSafely - Used to create reference output with the "safe"
/// backend, if reference output is not provided. /// backend, if reference output is not provided.
/// ///
std::string BugDriver::executeProgramSafely(std::string OutputFile) { std::string BugDriver::executeProgramSafely(std::string OutputFile,
std::string outFN = executeProgram(OutputFile, "", "", SafeInterpreter); std::string *Error) {
return outFN; return executeProgram(OutputFile, "", "", SafeInterpreter, Error);
} }
std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) { std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
std::string &Error) {
assert(Interpreter && "Interpreter should have been created already!"); assert(Interpreter && "Interpreter should have been created already!");
sys::Path OutputFile; sys::Path OutputFile;
// Using the known-good backend. // Using the known-good backend.
GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile); GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
Error);
if (!Error.empty())
return "";
std::string SharedObjectFile; std::string SharedObjectFile;
if (gcc->MakeSharedObject(OutputFile.str(), FT, bool Failure = gcc->MakeSharedObject(OutputFile.str(), FT, SharedObjectFile,
SharedObjectFile, AdditionalLinkerArgs)) AdditionalLinkerArgs, Error);
if (!Error.empty())
return "";
if (Failure)
exit(1); exit(1);
// Remove the intermediate C file // Remove the intermediate C file
@@ -414,16 +424,14 @@ std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
/// this function. /// this function.
/// ///
bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) { bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
try { std::string Error;
compileProgram(Program); compileProgram(Program, &Error);
} catch (ToolExecutionError &) { if (!Error.empty())
return false; return false;
}
try { ReferenceOutputFile = executeProgramSafely(Filename, &Error);
ReferenceOutputFile = executeProgramSafely(Filename); if (!Error.empty()) {
outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n"; errs() << Error;
} catch (ToolExecutionError &TEE) {
errs() << TEE.what();
if (Interpreter != SafeInterpreter) { if (Interpreter != SafeInterpreter) {
errs() << "*** There is a bug running the \"safe\" backend. Either" errs() << "*** There is a bug running the \"safe\" backend. Either"
<< " debug it (for example with the -run-cbe bugpoint option," << " debug it (for example with the -run-cbe bugpoint option,"
@@ -432,19 +440,23 @@ bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
} }
return false; return false;
} }
outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
return true; return true;
} }
/// diffProgram - This method executes the specified module and diffs the /// diffProgram - This method executes the specified module and diffs the
/// output against the file specified by ReferenceOutputFile. If the output /// output against the file specified by ReferenceOutputFile. If the output
/// is different, true is returned. If there is a problem with the code /// is different, 1 is returned. If there is a problem with the code
/// generator (e.g., llc crashes), this will throw an exception. /// generator (e.g., llc crashes), this will return -1 and set Error.
/// ///
bool BugDriver::diffProgram(const std::string &BitcodeFile, bool BugDriver::diffProgram(const std::string &BitcodeFile,
const std::string &SharedObject, const std::string &SharedObject,
bool RemoveBitcode) { bool RemoveBitcode,
std::string *ErrMsg) {
// Execute the program, generating an output file... // Execute the program, generating an output file...
sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0)); sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0, ErrMsg));
if (!ErrMsg->empty())
return false;
std::string Error; std::string Error;
bool FilesDifferent = false; bool FilesDifferent = false;

View File

@@ -29,7 +29,8 @@ using namespace llvm;
/// If the passes did not compile correctly, output the command required to /// If the passes did not compile correctly, output the command required to
/// recreate the failure. This returns true if a compiler error is found. /// recreate the failure. This returns true if a compiler error is found.
/// ///
bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses) { bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses,
std::string &ErrMsg) {
setPassesToRun(AllPasses); setPassesToRun(AllPasses);
outs() << "Starting bug finding procedure...\n\n"; outs() << "Starting bug finding procedure...\n\n";
@@ -74,33 +75,33 @@ bool BugDriver::runManyPasses(const std::vector<const PassInfo*> &AllPasses) {
// Step 3: Compile the optimized code. // Step 3: Compile the optimized code.
// //
outs() << "Running the code generator to test for a crash: "; outs() << "Running the code generator to test for a crash: ";
try { std::string Error;
compileProgram(Program); compileProgram(Program, &Error);
outs() << '\n'; if (!Error.empty()) {
} catch (ToolExecutionError &TEE) {
outs() << "\n*** compileProgram threw an exception: "; outs() << "\n*** compileProgram threw an exception: ";
outs() << TEE.what(); outs() << Error;
return debugCodeGeneratorCrash(); return debugCodeGeneratorCrash(ErrMsg);
} }
outs() << '\n';
// //
// Step 4: Run the program and compare its output to the reference // Step 4: Run the program and compare its output to the reference
// output (created above). // output (created above).
// //
outs() << "*** Checking if passes caused miscompliation:\n"; outs() << "*** Checking if passes caused miscompliation:\n";
try { bool Diff = diffProgram(Filename, "", false, &Error);
if (diffProgram(Filename, "", false)) { if (Error.empty() && Diff) {
outs() << "\n*** diffProgram returned true!\n"; outs() << "\n*** diffProgram returned true!\n";
debugMiscompilation(); debugMiscompilation(&Error);
if (Error.empty())
return true; return true;
} else { }
outs() << "\n*** diff'd output matches!\n"; if (!Error.empty()) {
} errs() << Error;
} catch (ToolExecutionError &TEE) { debugCodeGeneratorCrash(ErrMsg);
errs() << TEE.what();
debugCodeGeneratorCrash();
return true; return true;
} }
outs() << "\n*** diff'd output matches!\n";
sys::Path(Filename).eraseFromDisk(); sys::Path(Filename).eraseFromDisk();

View File

@@ -16,6 +16,7 @@
#define BUGPOINT_LIST_REDUCER_H #define BUGPOINT_LIST_REDUCER_H
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ErrorHandling.h"
#include <vector> #include <vector>
#include <cstdlib> #include <cstdlib>
#include <algorithm> #include <algorithm>
@@ -29,7 +30,8 @@ struct ListReducer {
enum TestResult { enum TestResult {
NoFailure, // No failure of the predicate was detected NoFailure, // No failure of the predicate was detected
KeepSuffix, // The suffix alone satisfies the predicate KeepSuffix, // The suffix alone satisfies the predicate
KeepPrefix // The prefix alone satisfies the predicate KeepPrefix, // The prefix alone satisfies the predicate
InternalError // Encountered an error trying to run the predicate
}; };
virtual ~ListReducer() {} virtual ~ListReducer() {}
@@ -40,16 +42,17 @@ struct ListReducer {
// the prefix anyway, it can. // the prefix anyway, it can.
// //
virtual TestResult doTest(std::vector<ElTy> &Prefix, virtual TestResult doTest(std::vector<ElTy> &Prefix,
std::vector<ElTy> &Kept) = 0; std::vector<ElTy> &Kept,
std::string &Error) = 0;
// reduceList - This function attempts to reduce the length of the specified // reduceList - This function attempts to reduce the length of the specified
// list while still maintaining the "test" property. This is the core of the // list while still maintaining the "test" property. This is the core of the
// "work" that bugpoint does. // "work" that bugpoint does.
// //
bool reduceList(std::vector<ElTy> &TheList) { bool reduceList(std::vector<ElTy> &TheList, std::string &Error) {
std::vector<ElTy> empty; std::vector<ElTy> empty;
std::srand(0x6e5ea738); // Seed the random number generator std::srand(0x6e5ea738); // Seed the random number generator
switch (doTest(TheList, empty)) { switch (doTest(TheList, empty, Error)) {
case KeepPrefix: case KeepPrefix:
if (TheList.size() == 1) // we are done, it's the base case and it fails if (TheList.size() == 1) // we are done, it's the base case and it fails
return true; return true;
@@ -58,11 +61,15 @@ struct ListReducer {
case KeepSuffix: case KeepSuffix:
// cannot be reached! // cannot be reached!
errs() << "bugpoint ListReducer internal error: selected empty set.\n"; llvm_unreachable("bugpoint ListReducer internal error: "
abort(); "selected empty set.");
case NoFailure: case NoFailure:
return false; // there is no failure with the full set of passes/funcs! return false; // there is no failure with the full set of passes/funcs!
case InternalError:
assert(!Error.empty());
return true;
} }
// Maximal number of allowed splitting iterations, // Maximal number of allowed splitting iterations,
@@ -90,7 +97,7 @@ Backjump:
std::random_shuffle(ShuffledList.begin(), ShuffledList.end()); std::random_shuffle(ShuffledList.begin(), ShuffledList.end());
errs() << "\n\n*** Testing shuffled set...\n\n"; errs() << "\n\n*** Testing shuffled set...\n\n";
// Check that random shuffle doesn't loose the bug // Check that random shuffle doesn't loose the bug
if (doTest(ShuffledList, empty) == KeepPrefix) { if (doTest(ShuffledList, empty, Error) == KeepPrefix) {
// If the bug is still here, use the shuffled list. // If the bug is still here, use the shuffled list.
TheList.swap(ShuffledList); TheList.swap(ShuffledList);
MidTop = TheList.size(); MidTop = TheList.size();
@@ -109,7 +116,7 @@ Backjump:
std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid); std::vector<ElTy> Prefix(TheList.begin(), TheList.begin()+Mid);
std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end()); std::vector<ElTy> Suffix(TheList.begin()+Mid, TheList.end());
switch (doTest(Prefix, Suffix)) { switch (doTest(Prefix, Suffix, Error)) {
case KeepSuffix: case KeepSuffix:
// The property still holds. We can just drop the prefix elements, and // The property still holds. We can just drop the prefix elements, and
// shorten the list to the "kept" elements. // shorten the list to the "kept" elements.
@@ -133,7 +140,10 @@ Backjump:
MidTop = Mid; MidTop = Mid;
NumOfIterationsWithoutProgress++; NumOfIterationsWithoutProgress++;
break; break;
case InternalError:
return true; // Error was set by doTest.
} }
assert(Error.empty() && "doTest did not return InternalError for error");
} }
// Probability of backjumping from the trimming loop back to the binary // Probability of backjumping from the trimming loop back to the binary
@@ -167,12 +177,14 @@ Backjump:
std::vector<ElTy> TestList(TheList); std::vector<ElTy> TestList(TheList);
TestList.erase(TestList.begin()+i); TestList.erase(TestList.begin()+i);
if (doTest(EmptyList, TestList) == KeepSuffix) { if (doTest(EmptyList, TestList, Error) == KeepSuffix) {
// We can trim down the list! // We can trim down the list!
TheList.swap(TestList); TheList.swap(TestList);
--i; // Don't skip an element of the list --i; // Don't skip an element of the list
Changed = true; Changed = true;
} }
if (!Error.empty())
return true;
} }
// This can take a long time if left uncontrolled. For now, don't // This can take a long time if left uncontrolled. For now, don't
// iterate. // iterate.

View File

@@ -49,7 +49,8 @@ namespace {
ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {} ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
virtual TestResult doTest(std::vector<const PassInfo*> &Prefix, virtual TestResult doTest(std::vector<const PassInfo*> &Prefix,
std::vector<const PassInfo*> &Suffix); std::vector<const PassInfo*> &Suffix,
std::string &Error);
}; };
} }
@@ -58,7 +59,8 @@ namespace {
/// ///
ReduceMiscompilingPasses::TestResult ReduceMiscompilingPasses::TestResult
ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix, ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
std::vector<const PassInfo*> &Suffix) { std::vector<const PassInfo*> &Suffix,
std::string &Error) {
// First, run the program with just the Suffix passes. If it is still broken // First, run the program with just the Suffix passes. If it is still broken
// with JUST the kept passes, discard the prefix passes. // with JUST the kept passes, discard the prefix passes.
outs() << "Checking to see if '" << getPassesString(Suffix) outs() << "Checking to see if '" << getPassesString(Suffix)
@@ -74,7 +76,11 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
} }
// Check to see if the finished program matches the reference output... // Check to see if the finished program matches the reference output...
if (BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/)) { bool Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/,
&Error);
if (!Error.empty())
return InternalError;
if (Diff) {
outs() << " nope.\n"; outs() << " nope.\n";
if (Suffix.empty()) { if (Suffix.empty()) {
errs() << BD.getToolName() << ": I'm confused: the test fails when " errs() << BD.getToolName() << ": I'm confused: the test fails when "
@@ -107,7 +113,10 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
} }
// If the prefix maintains the predicate by itself, only keep the prefix! // If the prefix maintains the predicate by itself, only keep the prefix!
if (BD.diffProgram(BitcodeResult)) { Diff = BD.diffProgram(BitcodeResult, "", false, &Error);
if (!Error.empty())
return InternalError;
if (Diff) {
outs() << " nope.\n"; outs() << " nope.\n";
sys::Path(BitcodeResult).eraseFromDisk(); sys::Path(BitcodeResult).eraseFromDisk();
return KeepPrefix; return KeepPrefix;
@@ -143,7 +152,10 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
} }
// Run the result... // Run the result...
if (BD.diffProgram(BitcodeResult, "", true/*delete bitcode*/)) { Diff = BD.diffProgram(BitcodeResult, "", true /*delete bitcode*/, &Error);
if (!Error.empty())
return InternalError;
if (Diff) {
outs() << " nope.\n"; outs() << " nope.\n";
delete OriginalInput; // We pruned down the original input... delete OriginalInput; // We pruned down the original input...
return KeepSuffix; return KeepSuffix;
@@ -158,22 +170,34 @@ ReduceMiscompilingPasses::doTest(std::vector<const PassInfo*> &Prefix,
namespace { namespace {
class ReduceMiscompilingFunctions : public ListReducer<Function*> { class ReduceMiscompilingFunctions : public ListReducer<Function*> {
BugDriver &BD; BugDriver &BD;
bool (*TestFn)(BugDriver &, Module *, Module *); bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
public: public:
ReduceMiscompilingFunctions(BugDriver &bd, ReduceMiscompilingFunctions(BugDriver &bd,
bool (*F)(BugDriver &, Module *, Module *)) bool (*F)(BugDriver &, Module *, Module *,
std::string &))
: BD(bd), TestFn(F) {} : BD(bd), TestFn(F) {}
virtual TestResult doTest(std::vector<Function*> &Prefix, virtual TestResult doTest(std::vector<Function*> &Prefix,
std::vector<Function*> &Suffix) { std::vector<Function*> &Suffix,
if (!Suffix.empty() && TestFuncs(Suffix)) std::string &Error) {
return KeepSuffix; if (!Suffix.empty()) {
if (!Prefix.empty() && TestFuncs(Prefix)) bool Ret = TestFuncs(Suffix, Error);
return KeepPrefix; if (!Error.empty())
return InternalError;
if (Ret)
return KeepSuffix;
}
if (!Prefix.empty()) {
bool Ret = TestFuncs(Prefix, Error);
if (!Error.empty())
return InternalError;
if (Ret)
return KeepPrefix;
}
return NoFailure; return NoFailure;
} }
bool TestFuncs(const std::vector<Function*> &Prefix); int TestFuncs(const std::vector<Function*> &Prefix, std::string &Error);
}; };
} }
@@ -184,7 +208,7 @@ namespace {
/// returns. /// returns.
/// ///
static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2, static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
bool DeleteInputs) { bool DeleteInputs, std::string &Error) {
// Link the two portions of the program back to together. // Link the two portions of the program back to together.
std::string ErrorMsg; std::string ErrorMsg;
if (!DeleteInputs) { if (!DeleteInputs) {
@@ -202,11 +226,12 @@ static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
// Execute the program. If it does not match the expected output, we must // Execute the program. If it does not match the expected output, we must
// return true. // return true.
bool Broken = BD.diffProgram(); bool Broken = BD.diffProgram("", "", false, &Error);
if (!Error.empty()) {
// Delete the linked module & restore the original // Delete the linked module & restore the original
BD.swapProgramIn(OldProgram); BD.swapProgramIn(OldProgram);
delete M1; delete M1;
}
return Broken; return Broken;
} }
@@ -214,7 +239,8 @@ static bool TestMergedProgram(BugDriver &BD, Module *M1, Module *M2,
/// under consideration for miscompilation vs. those that are not, and test /// under consideration for miscompilation vs. those that are not, and test
/// accordingly. Each group of functions becomes a separate Module. /// accordingly. Each group of functions becomes a separate Module.
/// ///
bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){ int ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*> &Funcs,
std::string &Error) {
// Test to see if the function is misoptimized if we ONLY run it on the // Test to see if the function is misoptimized if we ONLY run it on the
// functions listed in Funcs. // functions listed in Funcs.
outs() << "Checking to see if the program is misoptimized when " outs() << "Checking to see if the program is misoptimized when "
@@ -231,7 +257,7 @@ bool ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function*>&Funcs){
ValueMap); ValueMap);
// Run the predicate, note that the predicate will delete both input modules. // Run the predicate, note that the predicate will delete both input modules.
return TestFn(BD, ToOptimize, ToNotOptimize); return TestFn(BD, ToOptimize, ToNotOptimize, Error);
} }
/// DisambiguateGlobalSymbols - Give anonymous global values names. /// DisambiguateGlobalSymbols - Give anonymous global values names.
@@ -251,8 +277,10 @@ static void DisambiguateGlobalSymbols(Module *M) {
/// bug. If so, it reduces the amount of code identified. /// bug. If so, it reduces the amount of code identified.
/// ///
static bool ExtractLoops(BugDriver &BD, static bool ExtractLoops(BugDriver &BD,
bool (*TestFn)(BugDriver &, Module *, Module *), bool (*TestFn)(BugDriver &, Module *, Module *,
std::vector<Function*> &MiscompiledFunctions) { std::string &),
std::vector<Function*> &MiscompiledFunctions,
std::string &Error) {
bool MadeChange = false; bool MadeChange = false;
while (1) { while (1) {
if (BugpointIsInterrupted) return MadeChange; if (BugpointIsInterrupted) return MadeChange;
@@ -279,7 +307,11 @@ static bool ExtractLoops(BugDriver &BD,
// has broken. If something broke, then we'll inform the user and stop // has broken. If something broke, then we'll inform the user and stop
// extraction. // extraction.
AbstractInterpreter *AI = BD.switchToSafeInterpreter(); AbstractInterpreter *AI = BD.switchToSafeInterpreter();
if (TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize, false)) { bool Failure = TestMergedProgram(BD, ToOptimizeLoopExtracted, ToNotOptimize,
false, Error);
if (!Error.empty())
return false;
if (Failure) {
BD.switchToInterpreter(AI); BD.switchToInterpreter(AI);
// Merged program doesn't work anymore! // Merged program doesn't work anymore!
@@ -308,7 +340,10 @@ static bool ExtractLoops(BugDriver &BD,
// Clone modules, the tester function will free them. // Clone modules, the tester function will free them.
Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted); Module *TOLEBackup = CloneModule(ToOptimizeLoopExtracted);
Module *TNOBackup = CloneModule(ToNotOptimize); Module *TNOBackup = CloneModule(ToNotOptimize);
if (!TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize)) { Failure = TestFn(BD, ToOptimizeLoopExtracted, ToNotOptimize, Error);
if (!Error.empty())
return false;
if (!Failure) {
outs() << "*** Loop extraction masked the problem. Undoing.\n"; outs() << "*** Loop extraction masked the problem. Undoing.\n";
// If the program is not still broken, then loop extraction did something // If the program is not still broken, then loop extraction did something
// that masked the error. Stop loop extraction now. // that masked the error. Stop loop extraction now.
@@ -361,31 +396,44 @@ static bool ExtractLoops(BugDriver &BD,
namespace { namespace {
class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> { class ReduceMiscompiledBlocks : public ListReducer<BasicBlock*> {
BugDriver &BD; BugDriver &BD;
bool (*TestFn)(BugDriver &, Module *, Module *); bool (*TestFn)(BugDriver &, Module *, Module *, std::string &);
std::vector<Function*> FunctionsBeingTested; std::vector<Function*> FunctionsBeingTested;
public: public:
ReduceMiscompiledBlocks(BugDriver &bd, ReduceMiscompiledBlocks(BugDriver &bd,
bool (*F)(BugDriver &, Module *, Module *), bool (*F)(BugDriver &, Module *, Module *,
std::string &),
const std::vector<Function*> &Fns) const std::vector<Function*> &Fns)
: BD(bd), TestFn(F), FunctionsBeingTested(Fns) {} : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
virtual TestResult doTest(std::vector<BasicBlock*> &Prefix, virtual TestResult doTest(std::vector<BasicBlock*> &Prefix,
std::vector<BasicBlock*> &Suffix) { std::vector<BasicBlock*> &Suffix,
if (!Suffix.empty() && TestFuncs(Suffix)) std::string &Error) {
return KeepSuffix; if (!Suffix.empty()) {
if (TestFuncs(Prefix)) bool Ret = TestFuncs(Suffix, Error);
return KeepPrefix; if (!Error.empty())
return InternalError;
if (Ret)
return KeepSuffix;
}
if (!Prefix.empty()) {
bool Ret = TestFuncs(Prefix, Error);
if (!Error.empty())
return InternalError;
if (Ret)
return KeepPrefix;
}
return NoFailure; return NoFailure;
} }
bool TestFuncs(const std::vector<BasicBlock*> &Prefix); bool TestFuncs(const std::vector<BasicBlock*> &BBs, std::string &Error);
}; };
} }
/// TestFuncs - Extract all blocks for the miscompiled functions except for the /// TestFuncs - Extract all blocks for the miscompiled functions except for the
/// specified blocks. If the problem still exists, return true. /// specified blocks. If the problem still exists, return true.
/// ///
bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) { bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs,
std::string &Error) {
// Test to see if the function is misoptimized if we ONLY run it on the // Test to see if the function is misoptimized if we ONLY run it on the
// functions listed in Funcs. // functions listed in Funcs.
outs() << "Checking to see if the program is misoptimized when all "; outs() << "Checking to see if the program is misoptimized when all ";
@@ -411,7 +459,7 @@ bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) {
if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) { if (Module *New = BD.ExtractMappedBlocksFromModule(BBs, ToOptimize)) {
delete ToOptimize; delete ToOptimize;
// Run the predicate, not that the predicate will delete both input modules. // Run the predicate, not that the predicate will delete both input modules.
return TestFn(BD, New, ToNotOptimize); return TestFn(BD, New, ToNotOptimize, Error);
} }
delete ToOptimize; delete ToOptimize;
delete ToNotOptimize; delete ToNotOptimize;
@@ -424,8 +472,10 @@ bool ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock*> &BBs) {
/// the bug. /// the bug.
/// ///
static bool ExtractBlocks(BugDriver &BD, static bool ExtractBlocks(BugDriver &BD,
bool (*TestFn)(BugDriver &, Module *, Module *), bool (*TestFn)(BugDriver &, Module *, Module *,
std::vector<Function*> &MiscompiledFunctions) { std::string &),
std::vector<Function*> &MiscompiledFunctions,
std::string &Error) {
if (BugpointIsInterrupted) return false; if (BugpointIsInterrupted) return false;
std::vector<BasicBlock*> Blocks; std::vector<BasicBlock*> Blocks;
@@ -440,11 +490,17 @@ static bool ExtractBlocks(BugDriver &BD,
unsigned OldSize = Blocks.size(); unsigned OldSize = Blocks.size();
// Check to see if all blocks are extractible first. // Check to see if all blocks are extractible first.
if (ReduceMiscompiledBlocks(BD, TestFn, bool Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
MiscompiledFunctions).TestFuncs(std::vector<BasicBlock*>())) { .TestFuncs(std::vector<BasicBlock*>(), Error);
if (!Error.empty())
return false;
if (Ret) {
Blocks.clear(); Blocks.clear();
} else { } else {
ReduceMiscompiledBlocks(BD, TestFn,MiscompiledFunctions).reduceList(Blocks); ReduceMiscompiledBlocks(BD, TestFn,
MiscompiledFunctions).reduceList(Blocks, Error);
if (!Error.empty())
return false;
if (Blocks.size() == OldSize) if (Blocks.size() == OldSize)
return false; return false;
} }
@@ -505,7 +561,9 @@ static bool ExtractBlocks(BugDriver &BD,
/// ///
static std::vector<Function*> static std::vector<Function*>
DebugAMiscompilation(BugDriver &BD, DebugAMiscompilation(BugDriver &BD,
bool (*TestFn)(BugDriver &, Module *, Module *)) { bool (*TestFn)(BugDriver &, Module *, Module *,
std::string &),
std::string &Error) {
// Okay, now that we have reduced the list of passes which are causing the // Okay, now that we have reduced the list of passes which are causing the
// failure, see if we can pin down which functions are being // failure, see if we can pin down which functions are being
// miscompiled... first build a list of all of the non-external functions in // miscompiled... first build a list of all of the non-external functions in
@@ -518,7 +576,10 @@ DebugAMiscompilation(BugDriver &BD,
// Do the reduction... // Do the reduction...
if (!BugpointIsInterrupted) if (!BugpointIsInterrupted)
ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
Error);
if (!Error.empty())
return MiscompiledFunctions;
outs() << "\n*** The following function" outs() << "\n*** The following function"
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are") << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
@@ -529,37 +590,51 @@ DebugAMiscompilation(BugDriver &BD,
// See if we can rip any loops out of the miscompiled functions and still // See if we can rip any loops out of the miscompiled functions and still
// trigger the problem. // trigger the problem.
if (!BugpointIsInterrupted && !DisableLoopExtraction && if (!BugpointIsInterrupted && !DisableLoopExtraction) {
ExtractLoops(BD, TestFn, MiscompiledFunctions)) { bool Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions, Error);
// Okay, we extracted some loops and the problem still appears. See if we if (!Error.empty())
// can eliminate some of the created functions from being candidates. return MiscompiledFunctions;
DisambiguateGlobalSymbols(BD.getProgram()); if (Ret) {
// Okay, we extracted some loops and the problem still appears. See if
// we can eliminate some of the created functions from being candidates.
DisambiguateGlobalSymbols(BD.getProgram());
// Do the reduction... // Do the reduction...
if (!BugpointIsInterrupted) if (!BugpointIsInterrupted)
ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
Error);
if (!Error.empty())
return MiscompiledFunctions;
outs() << "\n*** The following function" outs() << "\n*** The following function"
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are") << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
<< " being miscompiled: "; << " being miscompiled: ";
PrintFunctionList(MiscompiledFunctions); PrintFunctionList(MiscompiledFunctions);
outs() << '\n'; outs() << '\n';
}
} }
if (!BugpointIsInterrupted && !DisableBlockExtraction && if (!BugpointIsInterrupted && !DisableBlockExtraction) {
ExtractBlocks(BD, TestFn, MiscompiledFunctions)) { bool Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions, Error);
// Okay, we extracted some blocks and the problem still appears. See if we if (!Error.empty())
// can eliminate some of the created functions from being candidates. return MiscompiledFunctions;
DisambiguateGlobalSymbols(BD.getProgram()); if (Ret) {
// Okay, we extracted some blocks and the problem still appears. See if
// we can eliminate some of the created functions from being candidates.
DisambiguateGlobalSymbols(BD.getProgram());
// Do the reduction... // Do the reduction...
ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions); ReduceMiscompilingFunctions(BD, TestFn).reduceList(MiscompiledFunctions,
Error);
if (!Error.empty())
return MiscompiledFunctions;
outs() << "\n*** The following function" outs() << "\n*** The following function"
<< (MiscompiledFunctions.size() == 1 ? " is" : "s are") << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
<< " being miscompiled: "; << " being miscompiled: ";
PrintFunctionList(MiscompiledFunctions); PrintFunctionList(MiscompiledFunctions);
outs() << '\n'; outs() << '\n';
}
} }
return MiscompiledFunctions; return MiscompiledFunctions;
@@ -569,7 +644,8 @@ DebugAMiscompilation(BugDriver &BD,
/// "Test" portion of the program is misoptimized. If so, return true. In any /// "Test" portion of the program is misoptimized. If so, return true. In any
/// case, both module arguments are deleted. /// case, both module arguments are deleted.
/// ///
static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) { static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe,
std::string &Error) {
// Run the optimization passes on ToOptimize, producing a transformed version // Run the optimization passes on ToOptimize, producing a transformed version
// of the functions being tested. // of the functions being tested.
outs() << " Optimizing functions being tested: "; outs() << " Optimizing functions being tested: ";
@@ -579,8 +655,8 @@ static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
delete Test; delete Test;
outs() << " Checking to see if the merged program executes correctly: "; outs() << " Checking to see if the merged program executes correctly: ";
bool Broken = TestMergedProgram(BD, Optimized, Safe, true); bool Broken = TestMergedProgram(BD, Optimized, Safe, true, Error);
outs() << (Broken ? " nope.\n" : " yup.\n"); if (Error.empty()) outs() << (Broken ? " nope.\n" : " yup.\n");
return Broken; return Broken;
} }
@@ -589,13 +665,14 @@ static bool TestOptimizer(BugDriver &BD, Module *Test, Module *Safe) {
/// crashing, but the generated output is semantically different from the /// crashing, but the generated output is semantically different from the
/// input. /// input.
/// ///
bool BugDriver::debugMiscompilation() { void BugDriver::debugMiscompilation(std::string *Error) {
// Make sure something was miscompiled... // Make sure something was miscompiled...
if (!BugpointIsInterrupted) if (!BugpointIsInterrupted)
if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun)) { if (!ReduceMiscompilingPasses(*this).reduceList(PassesToRun, *Error)) {
errs() << "*** Optimized program matches reference output! No problem" if (Error->empty())
<< " detected...\nbugpoint can't help you with your problem!\n"; errs() << "*** Optimized program matches reference output! No problem"
return false; << " detected...\nbugpoint can't help you with your problem!\n";
return;
} }
outs() << "\n*** Found miscompiling pass" outs() << "\n*** Found miscompiling pass"
@@ -603,8 +680,10 @@ bool BugDriver::debugMiscompilation() {
<< getPassesString(getPassesToRun()) << '\n'; << getPassesString(getPassesToRun()) << '\n';
EmitProgressBitcode("passinput"); EmitProgressBitcode("passinput");
std::vector<Function*> MiscompiledFunctions = std::vector<Function *> MiscompiledFunctions =
DebugAMiscompilation(*this, TestOptimizer); DebugAMiscompilation(*this, TestOptimizer, *Error);
if (!Error->empty())
return;
// Output a bunch of bitcode files for the user... // Output a bunch of bitcode files for the user...
outs() << "Outputting reduced bitcode files which expose the problem:\n"; outs() << "Outputting reduced bitcode files which expose the problem:\n";
@@ -624,7 +703,7 @@ bool BugDriver::debugMiscompilation() {
EmitProgressBitcode("tooptimize"); EmitProgressBitcode("tooptimize");
setNewProgram(ToOptimize); // Delete hacked module. setNewProgram(ToOptimize); // Delete hacked module.
return false; return;
} }
/// CleanupAndPrepareModules - Get the specified modules ready for code /// CleanupAndPrepareModules - Get the specified modules ready for code
@@ -797,7 +876,8 @@ static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test,
/// the "Test" portion of the program is miscompiled by the code generator under /// the "Test" portion of the program is miscompiled by the code generator under
/// test. If so, return true. In any case, both module arguments are deleted. /// test. If so, return true. In any case, both module arguments are deleted.
/// ///
static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) { static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe,
std::string &Error) {
CleanupAndPrepareModules(BD, Test, Safe); CleanupAndPrepareModules(BD, Test, Safe);
sys::Path TestModuleBC("bugpoint.test.bc"); sys::Path TestModuleBC("bugpoint.test.bc");
@@ -827,12 +907,16 @@ static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
<< "'\nExiting."; << "'\nExiting.";
exit(1); exit(1);
} }
std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str()); std::string SharedObject = BD.compileSharedObject(SafeModuleBC.str(), Error);
if (!Error.empty())
return -1;
delete Safe; delete Safe;
// Run the code generator on the `Test' code, loading the shared library. // Run the code generator on the `Test' code, loading the shared library.
// The function returns whether or not the new output differs from reference. // The function returns whether or not the new output differs from reference.
int Result = BD.diffProgram(TestModuleBC.str(), SharedObject, false); bool Result = BD.diffProgram(TestModuleBC.str(), SharedObject, false, &Error);
if (!Error.empty())
return false;
if (Result) if (Result)
errs() << ": still failing!\n"; errs() << ": still failing!\n";
@@ -848,23 +932,28 @@ static bool TestCodeGenerator(BugDriver &BD, Module *Test, Module *Safe) {
/// debugCodeGenerator - debug errors in LLC, LLI, or CBE. /// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
/// ///
bool BugDriver::debugCodeGenerator() { bool BugDriver::debugCodeGenerator(std::string *Error) {
if ((void*)SafeInterpreter == (void*)Interpreter) { if ((void*)SafeInterpreter == (void*)Interpreter) {
std::string Result = executeProgramSafely("bugpoint.safe.out"); std::string Result = executeProgramSafely("bugpoint.safe.out", Error);
outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match " if (Error->empty()) {
<< "the reference diff. This may be due to a\n front-end " outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
<< "bug or a bug in the original program, but this can also " << "the reference diff. This may be due to a\n front-end "
<< "happen if bugpoint isn't running the program with the " << "bug or a bug in the original program, but this can also "
<< "right flags or input.\n I left the result of executing " << "happen if bugpoint isn't running the program with the "
<< "the program with the \"safe\" backend in this file for " << "right flags or input.\n I left the result of executing "
<< "you: '" << "the program with the \"safe\" backend in this file for "
<< Result << "'.\n"; << "you: '"
<< Result << "'.\n";
}
return true; return true;
} }
DisambiguateGlobalSymbols(Program); DisambiguateGlobalSymbols(Program);
std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator); std::vector<Function*> Funcs = DebugAMiscompilation(*this, TestCodeGenerator,
*Error);
if (!Error->empty())
return true;
// Split the module into the two halves of the program we want. // Split the module into the two halves of the program we want.
DenseMap<const Value*, Value*> ValueMap; DenseMap<const Value*, Value*> ValueMap;
@@ -902,7 +991,9 @@ bool BugDriver::debugCodeGenerator() {
<< "'\nExiting."; << "'\nExiting.";
exit(1); exit(1);
} }
std::string SharedObject = compileSharedObject(SafeModuleBC.str()); std::string SharedObject = compileSharedObject(SafeModuleBC.str(), *Error);
if (!Error->empty())
return true;
delete ToNotCodeGen; delete ToNotCodeGen;
outs() << "You can reproduce the problem with the command line: \n"; outs() << "You can reproduce the problem with the command line: \n";
@@ -919,7 +1010,7 @@ bool BugDriver::debugCodeGenerator() {
outs() << "\n"; outs() << "\n";
outs() << " " << TestModuleBC.str() << ".exe"; outs() << " " << TestModuleBC.str() << ".exe";
} }
for (unsigned i=0, e = InputArgv.size(); i != e; ++i) for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)
outs() << " " << InputArgv[i]; outs() << " " << InputArgv[i];
outs() << '\n'; outs() << '\n';
outs() << "The shared object was created with:\n llc -march=c " outs() << "The shared object was created with:\n llc -march=c "

View File

@@ -50,11 +50,9 @@ namespace {
cl::desc("Remote execution (rsh/ssh) extra options")); cl::desc("Remote execution (rsh/ssh) extra options"));
} }
ToolExecutionError::~ToolExecutionError() throw() { }
/// RunProgramWithTimeout - This function provides an alternate interface /// RunProgramWithTimeout - This function provides an alternate interface
/// to the sys::Program::ExecuteAndWait interface. /// to the sys::Program::ExecuteAndWait interface.
/// @see sys:Program::ExecuteAndWait /// @see sys::Program::ExecuteAndWait
static int RunProgramWithTimeout(const sys::Path &ProgramPath, static int RunProgramWithTimeout(const sys::Path &ProgramPath,
const char **Args, const char **Args,
const sys::Path &StdInFile, const sys::Path &StdInFile,
@@ -86,7 +84,7 @@ static int RunProgramWithTimeout(const sys::Path &ProgramPath,
/// Returns the remote program exit code or reports a remote client error if it /// Returns the remote program exit code or reports a remote client error if it
/// fails. Remote client is required to return 255 if it failed or program exit /// fails. Remote client is required to return 255 if it failed or program exit
/// code otherwise. /// code otherwise.
/// @see sys:Program::ExecuteAndWait /// @see sys::Program::ExecuteAndWait
static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath, static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
const char **Args, const char **Args,
const sys::Path &StdInFile, const sys::Path &StdInFile,
@@ -129,13 +127,13 @@ static int RunProgramRemotelyWithTimeout(const sys::Path &RemoteClientPath,
ErrorFile.close(); ErrorFile.close();
} }
throw ToolExecutionError(OS.str()); errs() << OS;
} }
return ReturnCode; return ReturnCode;
} }
static void ProcessFailure(sys::Path ProgPath, const char** Args) { static std::string ProcessFailure(sys::Path ProgPath, const char** Args) {
std::ostringstream OS; std::ostringstream OS;
OS << "\nError running tool:\n "; OS << "\nError running tool:\n ";
for (const char **Arg = Args; *Arg; ++Arg) for (const char **Arg = Args; *Arg; ++Arg)
@@ -162,7 +160,7 @@ static void ProcessFailure(sys::Path ProgPath, const char** Args) {
} }
ErrorFilename.eraseFromDisk(); ErrorFilename.eraseFromDisk();
throw ToolExecutionError(OS.str()); return OS.str();
} }
//===---------------------------------------------------------------------===// //===---------------------------------------------------------------------===//
@@ -183,6 +181,7 @@ namespace {
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs, const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs = const std::vector<std::string> &SharedLibs =
std::vector<std::string>(), std::vector<std::string>(),
@@ -195,6 +194,7 @@ int LLI::ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs, const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs, const std::vector<std::string> &SharedLibs,
unsigned Timeout, unsigned Timeout,
@@ -263,9 +263,10 @@ namespace {
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs, const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs = const std::vector<std::string> &SharedLibs =
std::vector<std::string>(), std::vector<std::string>(),
unsigned Timeout = 0, unsigned Timeout = 0,
unsigned MemoryLimit = 0); unsigned MemoryLimit = 0);
}; };
@@ -275,6 +276,7 @@ int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs, const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs, const std::vector<std::string> &SharedLibs,
unsigned Timeout, unsigned Timeout,
@@ -289,7 +291,7 @@ int CustomExecutor::ExecuteProgram(const std::string &Bitcode,
ProgramArgs.push_back(0); ProgramArgs.push_back(0);
// Add optional parameters to the running program from Argv // Add optional parameters to the running program from Argv
for (unsigned i=0, e = Args.size(); i != e; ++i) for (unsigned i = 0, e = Args.size(); i != e; ++i)
ProgramArgs.push_back(Args[i].c_str()); ProgramArgs.push_back(Args[i].c_str());
return RunProgramWithTimeout( return RunProgramWithTimeout(
@@ -351,7 +353,7 @@ AbstractInterpreter *AbstractInterpreter::createCustom(
// LLC Implementation of AbstractIntepreter interface // LLC Implementation of AbstractIntepreter interface
// //
GCC::FileType LLC::OutputCode(const std::string &Bitcode, GCC::FileType LLC::OutputCode(const std::string &Bitcode,
sys::Path &OutputAsmFile) { sys::Path &OutputAsmFile, std::string &Error) {
const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s"); const char *Suffix = (UseIntegratedAssembler ? ".llc.o" : ".llc.s");
sys::Path uniqueFile(Bitcode + Suffix); sys::Path uniqueFile(Bitcode + Suffix);
std::string ErrMsg; std::string ErrMsg;
@@ -379,20 +381,19 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode,
outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>"); outs() << (UseIntegratedAssembler ? "<llc-ia>" : "<llc>");
outs().flush(); outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
errs() << " " << LLCArgs[i]; errs() << " " << LLCArgs[i];
errs() << "\n"; errs() << "\n";
); );
if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0], if (RunProgramWithTimeout(sys::Path(LLCPath), &LLCArgs[0],
sys::Path(), sys::Path(), sys::Path())) sys::Path(), sys::Path(), sys::Path()))
ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]); Error = ProcessFailure(sys::Path(LLCPath), &LLCArgs[0]);
return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
return UseIntegratedAssembler ? GCC::ObjectFile : GCC::AsmFile;
} }
void LLC::compileProgram(const std::string &Bitcode) { void LLC::compileProgram(const std::string &Bitcode, std::string *Error) {
sys::Path OutputAsmFile; sys::Path OutputAsmFile;
OutputCode(Bitcode, OutputAsmFile); OutputCode(Bitcode, OutputAsmFile, *Error);
OutputAsmFile.eraseFromDisk(); OutputAsmFile.eraseFromDisk();
} }
@@ -400,13 +401,14 @@ int LLC::ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &ArgsForGCC, const std::vector<std::string> &ArgsForGCC,
const std::vector<std::string> &SharedLibs, const std::vector<std::string> &SharedLibs,
unsigned Timeout, unsigned Timeout,
unsigned MemoryLimit) { unsigned MemoryLimit) {
sys::Path OutputAsmFile; sys::Path OutputAsmFile;
GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile); GCC::FileType FileKind = OutputCode(Bitcode, OutputAsmFile, *Error);
FileRemover OutFileRemover(OutputAsmFile, !SaveTemps); FileRemover OutFileRemover(OutputAsmFile, !SaveTemps);
std::vector<std::string> GCCArgs(ArgsForGCC); std::vector<std::string> GCCArgs(ArgsForGCC);
@@ -415,7 +417,7 @@ int LLC::ExecuteProgram(const std::string &Bitcode,
// Assuming LLC worked, compile the result with GCC and run it. // Assuming LLC worked, compile the result with GCC and run it.
return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind, return gcc->ExecuteProgram(OutputAsmFile.str(), Args, FileKind,
InputFile, OutputFile, GCCArgs, InputFile, OutputFile, Error, GCCArgs,
Timeout, MemoryLimit); Timeout, MemoryLimit);
} }
@@ -460,12 +462,13 @@ namespace {
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs = const std::vector<std::string> &GCCArgs =
std::vector<std::string>(), std::vector<std::string>(),
const std::vector<std::string> &SharedLibs = const std::vector<std::string> &SharedLibs =
std::vector<std::string>(), std::vector<std::string>(),
unsigned Timeout =0, unsigned Timeout = 0,
unsigned MemoryLimit =0); unsigned MemoryLimit = 0);
}; };
} }
@@ -473,6 +476,7 @@ int JIT::ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs, const std::vector<std::string> &GCCArgs,
const std::vector<std::string> &SharedLibs, const std::vector<std::string> &SharedLibs,
unsigned Timeout, unsigned Timeout,
@@ -524,7 +528,7 @@ AbstractInterpreter *AbstractInterpreter::createJIT(const char *Argv0,
} }
GCC::FileType CBE::OutputCode(const std::string &Bitcode, GCC::FileType CBE::OutputCode(const std::string &Bitcode,
sys::Path &OutputCFile) { sys::Path &OutputCFile, std::string &Error) {
sys::Path uniqueFile(Bitcode+".cbe.c"); sys::Path uniqueFile(Bitcode+".cbe.c");
std::string ErrMsg; std::string ErrMsg;
if (uniqueFile.makeUnique(true, &ErrMsg)) { if (uniqueFile.makeUnique(true, &ErrMsg)) {
@@ -533,34 +537,34 @@ GCC::FileType CBE::OutputCode(const std::string &Bitcode,
} }
OutputCFile = uniqueFile; OutputCFile = uniqueFile;
std::vector<const char *> LLCArgs; std::vector<const char *> LLCArgs;
LLCArgs.push_back (LLCPath.c_str()); LLCArgs.push_back(LLCPath.c_str());
// Add any extra LLC args. // Add any extra LLC args.
for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i) for (unsigned i = 0, e = ToolArgs.size(); i != e; ++i)
LLCArgs.push_back(ToolArgs[i].c_str()); LLCArgs.push_back(ToolArgs[i].c_str());
LLCArgs.push_back ("-o"); LLCArgs.push_back("-o");
LLCArgs.push_back (OutputCFile.c_str()); // Output to the C file LLCArgs.push_back(OutputCFile.c_str()); // Output to the C file
LLCArgs.push_back ("-march=c"); // Output C language LLCArgs.push_back("-march=c"); // Output C language
LLCArgs.push_back ("-f"); // Overwrite as necessary... LLCArgs.push_back("-f"); // Overwrite as necessary...
LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode LLCArgs.push_back(Bitcode.c_str()); // This is the input bitcode
LLCArgs.push_back (0); LLCArgs.push_back(0);
outs() << "<cbe>"; outs().flush(); outs() << "<cbe>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) for (unsigned i = 0, e = LLCArgs.size()-1; i != e; ++i)
errs() << " " << LLCArgs[i]; errs() << " " << LLCArgs[i];
errs() << "\n"; errs() << "\n";
); );
if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(), if (RunProgramWithTimeout(LLCPath, &LLCArgs[0], sys::Path(), sys::Path(),
sys::Path())) sys::Path()))
ProcessFailure(LLCPath, &LLCArgs[0]); Error = ProcessFailure(LLCPath, &LLCArgs[0]);
return GCC::CFile; return GCC::CFile;
} }
void CBE::compileProgram(const std::string &Bitcode) { void CBE::compileProgram(const std::string &Bitcode, std::string *Error) {
sys::Path OutputCFile; sys::Path OutputCFile;
OutputCode(Bitcode, OutputCFile); OutputCode(Bitcode, OutputCFile, *Error);
OutputCFile.eraseFromDisk(); OutputCFile.eraseFromDisk();
} }
@@ -568,12 +572,13 @@ int CBE::ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &ArgsForGCC, const std::vector<std::string> &ArgsForGCC,
const std::vector<std::string> &SharedLibs, const std::vector<std::string> &SharedLibs,
unsigned Timeout, unsigned Timeout,
unsigned MemoryLimit) { unsigned MemoryLimit) {
sys::Path OutputCFile; sys::Path OutputCFile;
OutputCode(Bitcode, OutputCFile); OutputCode(Bitcode, OutputCFile, *Error);
FileRemover CFileRemove(OutputCFile, !SaveTemps); FileRemover CFileRemove(OutputCFile, !SaveTemps);
@@ -581,7 +586,7 @@ int CBE::ExecuteProgram(const std::string &Bitcode,
GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end()); GCCArgs.insert(GCCArgs.end(), SharedLibs.begin(), SharedLibs.end());
return gcc->ExecuteProgram(OutputCFile.str(), Args, GCC::CFile, return gcc->ExecuteProgram(OutputCFile.str(), Args, GCC::CFile,
InputFile, OutputFile, GCCArgs, InputFile, OutputFile, Error, GCCArgs,
Timeout, MemoryLimit); Timeout, MemoryLimit);
} }
@@ -631,6 +636,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
FileType fileType, FileType fileType,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &ArgsForGCC, const std::vector<std::string> &ArgsForGCC,
unsigned Timeout, unsigned Timeout,
unsigned MemoryLimit) { unsigned MemoryLimit) {
@@ -694,14 +700,14 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
outs() << "<gcc>"; outs().flush(); outs() << "<gcc>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
errs() << " " << GCCArgs[i]; errs() << " " << GCCArgs[i];
errs() << "\n"; errs() << "\n";
); );
if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
sys::Path())) { sys::Path())) {
ProcessFailure(GCCPath, &GCCArgs[0]); *Error = ProcessFailure(GCCPath, &GCCArgs[0]);
exit(1); return -1;
} }
std::vector<const char*> ProgramArgs; std::vector<const char*> ProgramArgs;
@@ -734,14 +740,14 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
} }
// Add optional parameters to the running program from Argv // Add optional parameters to the running program from Argv
for (unsigned i=0, e = Args.size(); i != e; ++i) for (unsigned i = 0, e = Args.size(); i != e; ++i)
ProgramArgs.push_back(Args[i].c_str()); ProgramArgs.push_back(Args[i].c_str());
ProgramArgs.push_back(0); // NULL terminator ProgramArgs.push_back(0); // NULL terminator
// Now that we have a binary, run it! // Now that we have a binary, run it!
outs() << "<program>"; outs().flush(); outs() << "<program>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) for (unsigned i = 0, e = ProgramArgs.size()-1; i != e; ++i)
errs() << " " << ProgramArgs[i]; errs() << " " << ProgramArgs[i];
errs() << "\n"; errs() << "\n";
); );
@@ -749,7 +755,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
FileRemover OutputBinaryRemover(OutputBinary, !SaveTemps); FileRemover OutputBinaryRemover(OutputBinary, !SaveTemps);
if (RemoteClientPath.isEmpty()) { if (RemoteClientPath.isEmpty()) {
DEBUG(errs() << "<run locally>";); DEBUG(errs() << "<run locally>");
return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0], return RunProgramWithTimeout(OutputBinary, &ProgramArgs[0],
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Timeout, MemoryLimit); Timeout, MemoryLimit);
@@ -763,7 +769,8 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
std::string &OutputFile, std::string &OutputFile,
const std::vector<std::string> &ArgsForGCC) { const std::vector<std::string> &ArgsForGCC,
std::string &Error) {
sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT); sys::Path uniqueFilename(InputFile+LTDL_SHLIB_EXT);
std::string ErrMsg; std::string ErrMsg;
if (uniqueFilename.makeUnique(true, &ErrMsg)) { if (uniqueFilename.makeUnique(true, &ErrMsg)) {
@@ -831,13 +838,13 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
outs() << "<gcc>"; outs().flush(); outs() << "<gcc>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) for (unsigned i = 0, e = GCCArgs.size()-1; i != e; ++i)
errs() << " " << GCCArgs[i]; errs() << " " << GCCArgs[i];
errs() << "\n"; errs() << "\n";
); );
if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(), if (RunProgramWithTimeout(GCCPath, &GCCArgs[0], sys::Path(), sys::Path(),
sys::Path())) { sys::Path())) {
ProcessFailure(GCCPath, &GCCArgs[0]); Error = ProcessFailure(GCCPath, &GCCArgs[0]);
return 1; return 1;
} }
return 0; return 0;

View File

@@ -19,6 +19,7 @@
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/SystemUtils.h" #include "llvm/Support/SystemUtils.h"
#include "llvm/System/Path.h" #include "llvm/System/Path.h"
#include <exception> #include <exception>
@@ -32,19 +33,6 @@ extern Triple TargetTriple;
class CBE; class CBE;
class LLC; class LLC;
/// ToolExecutionError - An instance of this class is thrown by the
/// AbstractInterpreter instances if there is an error running a tool (e.g., LLC
/// crashes) which prevents execution of the program.
///
class ToolExecutionError : std::exception {
std::string Message;
public:
explicit ToolExecutionError(const std::string &M) : Message(M) {}
virtual ~ToolExecutionError() throw();
virtual const char* what() const throw() { return Message.c_str(); }
};
//===---------------------------------------------------------------------===// //===---------------------------------------------------------------------===//
// GCC abstraction // GCC abstraction
// //
@@ -75,6 +63,7 @@ public:
FileType fileType, FileType fileType,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error = 0,
const std::vector<std::string> &GCCArgs = const std::vector<std::string> &GCCArgs =
std::vector<std::string>(), std::vector<std::string>(),
unsigned Timeout = 0, unsigned Timeout = 0,
@@ -85,7 +74,8 @@ public:
/// ///
int MakeSharedObject(const std::string &InputFile, FileType fileType, int MakeSharedObject(const std::string &InputFile, FileType fileType,
std::string &OutputFile, std::string &OutputFile,
const std::vector<std::string> &ArgsForGCC); const std::vector<std::string> &ArgsForGCC,
std::string &Error);
}; };
@@ -118,26 +108,29 @@ public:
/// compileProgram - Compile the specified program from bitcode to executable /// compileProgram - Compile the specified program from bitcode to executable
/// code. This does not produce any output, it is only used when debugging /// code. This does not produce any output, it is only used when debugging
/// the code generator. If the code generator fails, an exception should be /// the code generator. It returns false if the code generator fails.
/// thrown, otherwise, this function will just return. virtual void compileProgram(const std::string &Bitcode, std::string *Error) {}
virtual void compileProgram(const std::string &Bitcode) {}
/// OutputCode - Compile the specified program from bitcode to code /// OutputCode - Compile the specified program from bitcode to code
/// understood by the GCC driver (either C or asm). If the code generator /// understood by the GCC driver (either C or asm). If the code generator
/// fails, an exception should be thrown, otherwise, this function returns the /// fails, it sets Error, otherwise, this function returns the type of code
/// type of code emitted. /// emitted.
virtual GCC::FileType OutputCode(const std::string &Bitcode, virtual GCC::FileType OutputCode(const std::string &Bitcode,
sys::Path &OutFile) { sys::Path &OutFile, std::string &Error) {
throw std::string("OutputCode not supported by this AbstractInterpreter!"); Error = "OutputCode not supported by this AbstractInterpreter!";
return GCC::AsmFile;
} }
/// ExecuteProgram - Run the specified bitcode file, emitting output to the /// ExecuteProgram - Run the specified bitcode file, emitting output to the
/// specified filename. This returns the exit code of the program. /// specified filename. This sets RetVal to the exit code of the program or
/// returns false if a problem was encountered that prevented execution of
/// the program.
/// ///
virtual int ExecuteProgram(const std::string &Bitcode, virtual int ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs = const std::vector<std::string> &GCCArgs =
std::vector<std::string>(), std::vector<std::string>(),
const std::vector<std::string> &SharedLibs = const std::vector<std::string> &SharedLibs =
@@ -164,14 +157,14 @@ public:
/// compileProgram - Compile the specified program from bitcode to executable /// compileProgram - Compile the specified program from bitcode to executable
/// code. This does not produce any output, it is only used when debugging /// code. This does not produce any output, it is only used when debugging
/// the code generator. If the code generator fails, an exception should be /// the code generator. Returns false if the code generator fails.
/// thrown, otherwise, this function will just return. virtual void compileProgram(const std::string &Bitcode, std::string *Error);
virtual void compileProgram(const std::string &Bitcode);
virtual int ExecuteProgram(const std::string &Bitcode, virtual int ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs = const std::vector<std::string> &GCCArgs =
std::vector<std::string>(), std::vector<std::string>(),
const std::vector<std::string> &SharedLibs = const std::vector<std::string> &SharedLibs =
@@ -181,10 +174,10 @@ public:
/// OutputCode - Compile the specified program from bitcode to code /// OutputCode - Compile the specified program from bitcode to code
/// understood by the GCC driver (either C or asm). If the code generator /// understood by the GCC driver (either C or asm). If the code generator
/// fails, an exception should be thrown, otherwise, this function returns the /// fails, it sets Error, otherwise, this function returns the type of code
/// type of code emitted. /// emitted.
virtual GCC::FileType OutputCode(const std::string &Bitcode, virtual GCC::FileType OutputCode(const std::string &Bitcode,
sys::Path &OutFile); sys::Path &OutFile, std::string &Error);
}; };
@@ -212,14 +205,14 @@ public:
/// compileProgram - Compile the specified program from bitcode to executable /// compileProgram - Compile the specified program from bitcode to executable
/// code. This does not produce any output, it is only used when debugging /// code. This does not produce any output, it is only used when debugging
/// the code generator. If the code generator fails, an exception should be /// the code generator. Returns false if the code generator fails.
/// thrown, otherwise, this function will just return. virtual void compileProgram(const std::string &Bitcode, std::string *Error);
virtual void compileProgram(const std::string &Bitcode);
virtual int ExecuteProgram(const std::string &Bitcode, virtual int ExecuteProgram(const std::string &Bitcode,
const std::vector<std::string> &Args, const std::vector<std::string> &Args,
const std::string &InputFile, const std::string &InputFile,
const std::string &OutputFile, const std::string &OutputFile,
std::string *Error,
const std::vector<std::string> &GCCArgs = const std::vector<std::string> &GCCArgs =
std::vector<std::string>(), std::vector<std::string>(),
const std::vector<std::string> &SharedLibs = const std::vector<std::string> &SharedLibs =
@@ -227,9 +220,12 @@ public:
unsigned Timeout = 0, unsigned Timeout = 0,
unsigned MemoryLimit = 0); unsigned MemoryLimit = 0);
/// OutputCode - Compile the specified program from bitcode to code
/// understood by the GCC driver (either C or asm). If the code generator
/// fails, it sets Error, otherwise, this function returns the type of code
/// emitted.
virtual GCC::FileType OutputCode(const std::string &Bitcode, virtual GCC::FileType OutputCode(const std::string &Bitcode,
sys::Path &OutFile); sys::Path &OutFile, std::string &Error);
}; };
} // End llvm namespace } // End llvm namespace

View File

@@ -149,23 +149,11 @@ int main(int argc, char **argv) {
// avoid filling up the disk, we prevent it // avoid filling up the disk, we prevent it
sys::Process::PreventCoreFiles(); sys::Process::PreventCoreFiles();
try { std::string Error;
return D.run(); bool Failure = D.run(Error);
} catch (ToolExecutionError &TEE) { if (!Error.empty()) {
errs() << "Tool execution error: " << TEE.what() << '\n'; errs() << Error;
} catch (const std::string& msg) { return 1;
errs() << argv[0] << ": " << msg << "\n";
} catch (const std::bad_alloc&) {
errs() << "Oh no, a bugpoint process ran out of memory!\n"
"To increase the allocation limits for bugpoint child\n"
"processes, use the -mlimit option.\n";
} catch (const std::exception &e) {
errs() << "Whoops, a std::exception leaked out of bugpoint: "
<< e.what() << "\n"
<< "This is a bug in bugpoint!\n";
} catch (...) {
errs() << "Whoops, an exception leaked out of bugpoint. "
<< "This is a bug in bugpoint!\n";
} }
return 1; return Failure;
} }