ExecutionDriver.cpp revision c5cad211d6ec50fe90a0a716dee701c6c4721385
1//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains code used to execute the program utilizing one of the 11// various ways of running LLVM bytecode. 12// 13//===----------------------------------------------------------------------===// 14 15/* 16BUGPOINT NOTES: 17 181. Bugpoint should not leave any files behind if the program works properly 192. There should be an option to specify the program name, which specifies a 20 unique string to put into output files. This allows operation in the 21 SingleSource directory, e.g. default to the first input filename. 22*/ 23 24#include "BugDriver.h" 25#include "Support/CommandLine.h" 26#include "Support/Debug.h" 27#include "Support/FileUtilities.h" 28#include "Support/SystemUtils.h" 29#include "llvm/Support/ToolRunner.h" 30#include <fstream> 31#include <iostream> 32using namespace llvm; 33 34namespace { 35 // OutputType - Allow the user to specify the way code should be run, to test 36 // for miscompilation. 37 // 38 enum OutputType { 39 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE 40 }; 41 42 cl::opt<OutputType> 43 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"), 44 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"), 45 clEnumValN(RunLLI, "run-int", "Execute with the interpreter"), 46 clEnumValN(RunJIT, "run-jit", "Execute with JIT"), 47 clEnumValN(RunLLC, "run-llc", "Compile with LLC"), 48 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"), 49 0), 50 cl::init(AutoPick)); 51 52 cl::opt<bool> 53 CheckProgramExitCode("check-exit-code", 54 cl::desc("Assume nonzero exit code is failure (default on)"), 55 cl::init(true)); 56 57 cl::opt<std::string> 58 InputFile("input", cl::init("/dev/null"), 59 cl::desc("Filename to pipe in as stdin (default: /dev/null)")); 60 61 cl::list<std::string> 62 AdditionalSOs("additional-so", 63 cl::desc("Additional shared objects to load " 64 "into executing programs")); 65} 66 67namespace llvm { 68 // Anything specified after the --args option are taken as arguments to the 69 // program being debugged. 70 cl::list<std::string> 71 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."), 72 cl::ZeroOrMore); 73} 74 75//===----------------------------------------------------------------------===// 76// BugDriver method implementation 77// 78 79/// initializeExecutionEnvironment - This method is used to set up the 80/// environment for executing LLVM programs. 81/// 82bool BugDriver::initializeExecutionEnvironment() { 83 std::cout << "Initializing execution environment: "; 84 85 // Create an instance of the AbstractInterpreter interface as specified on 86 // the command line 87 std::string Message; 88 switch (InterpreterSel) { 89 case AutoPick: 90 InterpreterSel = RunCBE; 91 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message); 92 if (!Interpreter) { 93 InterpreterSel = RunJIT; 94 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message); 95 } 96 if (!Interpreter) { 97 InterpreterSel = RunLLC; 98 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message); 99 } 100 if (!Interpreter) { 101 InterpreterSel = RunLLI; 102 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message); 103 } 104 if (!Interpreter) { 105 InterpreterSel = AutoPick; 106 Message = "Sorry, I can't automatically select an interpreter!\n"; 107 } 108 break; 109 case RunLLI: 110 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message); 111 break; 112 case RunLLC: 113 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message); 114 break; 115 case RunJIT: 116 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message); 117 break; 118 case RunCBE: 119 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message); 120 break; 121 default: 122 Message = "Sorry, this back-end is not supported by bugpoint right now!\n"; 123 break; 124 } 125 std::cerr << Message; 126 127 // Initialize auxiliary tools for debugging 128 cbe = AbstractInterpreter::createCBE(getToolName(), Message); 129 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); } 130 gcc = GCC::create(getToolName(), Message); 131 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); } 132 133 // If there was an error creating the selected interpreter, quit with error. 134 return Interpreter == 0; 135} 136 137 138/// executeProgram - This method runs "Program", capturing the output of the 139/// program to a file, returning the filename of the file. A recommended 140/// filename may be optionally specified. 141/// 142std::string BugDriver::executeProgram(std::string OutputFile, 143 std::string BytecodeFile, 144 const std::string &SharedObj, 145 AbstractInterpreter *AI, 146 bool *ProgramExitedNonzero) { 147 if (AI == 0) AI = Interpreter; 148 assert(AI && "Interpreter should have been created already!"); 149 bool CreatedBytecode = false; 150 if (BytecodeFile.empty()) { 151 // Emit the program to a bytecode file... 152 BytecodeFile = getUniqueFilename("bugpoint-test-program.bc"); 153 154 if (writeProgramToFile(BytecodeFile, Program)) { 155 std::cerr << ToolName << ": Error emitting bytecode to file '" 156 << BytecodeFile << "'!\n"; 157 exit(1); 158 } 159 CreatedBytecode = true; 160 } 161 162 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output"; 163 164 // Check to see if this is a valid output filename... 165 OutputFile = getUniqueFilename(OutputFile); 166 167 // Figure out which shared objects to run, if any. 168 std::vector<std::string> SharedObjs(AdditionalSOs); 169 if (!SharedObj.empty()) 170 SharedObjs.push_back(SharedObj); 171 172 // Actually execute the program! 173 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile, 174 OutputFile, SharedObjs); 175 176 if (ProgramExitedNonzero != 0) 177 *ProgramExitedNonzero = (RetVal != 0); 178 179 // Remove the temporary bytecode file. 180 if (CreatedBytecode) removeFile(BytecodeFile); 181 182 // Return the filename we captured the output to. 183 return OutputFile; 184} 185 186/// executeProgramWithCBE - Used to create reference output with the C 187/// backend, if reference output is not provided. 188/// 189std::string BugDriver::executeProgramWithCBE(std::string OutputFile) { 190 bool ProgramExitedNonzero; 191 std::string outFN = executeProgram(OutputFile, "", "", 192 (AbstractInterpreter*)cbe, 193 &ProgramExitedNonzero); 194 if (ProgramExitedNonzero) { 195 std::cerr 196 << "Warning: While generating reference output, program exited with\n" 197 << "non-zero exit code. This will NOT be treated as a failure.\n"; 198 CheckProgramExitCode = false; 199 } 200 return outFN; 201} 202 203std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) { 204 assert(Interpreter && "Interpreter should have been created already!"); 205 std::string OutputCFile; 206 207 // Using CBE 208 cbe->OutputC(BytecodeFile, OutputCFile); 209 210#if 0 /* This is an alternative, as yet unimplemented */ 211 // Using LLC 212 std::string Message; 213 LLC *llc = createLLCtool(Message); 214 if (llc->OutputAsm(BytecodeFile, OutputFile)) { 215 std::cerr << "Could not generate asm code with `llc', exiting.\n"; 216 exit(1); 217 } 218#endif 219 220 std::string SharedObjectFile; 221 if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile)) 222 exit(1); 223 224 // Remove the intermediate C file 225 removeFile(OutputCFile); 226 227 return "./" + SharedObjectFile; 228} 229 230 231/// diffProgram - This method executes the specified module and diffs the output 232/// against the file specified by ReferenceOutputFile. If the output is 233/// different, true is returned. 234/// 235bool BugDriver::diffProgram(const std::string &BytecodeFile, 236 const std::string &SharedObject, 237 bool RemoveBytecode) { 238 bool ProgramExitedNonzero; 239 240 // Execute the program, generating an output file... 241 std::string Output = executeProgram("", BytecodeFile, SharedObject, 0, 242 &ProgramExitedNonzero); 243 244 // If we're checking the program exit code, assume anything nonzero is bad. 245 if (CheckProgramExitCode && ProgramExitedNonzero) 246 return true; 247 248 std::string Error; 249 bool FilesDifferent = false; 250 if (DiffFiles(ReferenceOutputFile, Output, &Error)) { 251 if (!Error.empty()) { 252 std::cerr << "While diffing output: " << Error << "\n"; 253 exit(1); 254 } 255 FilesDifferent = true; 256 } 257 258 // Remove the generated output. 259 removeFile(Output); 260 261 // Remove the bytecode file if we are supposed to. 262 if (RemoveBytecode) removeFile(BytecodeFile); 263 return FilesDifferent; 264} 265 266bool BugDriver::isExecutingJIT() { 267 return InterpreterSel == RunJIT; 268} 269 270