1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains code used to execute the program utilizing one of the
11 // various ways of running LLVM bytecode.
13 //===----------------------------------------------------------------------===//
15 #include "BugDriver.h"
16 #include "llvm/Support/ToolRunner.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/FileUtilities.h"
20 #include "llvm/Support/SystemUtils.h"
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
31 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
35 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
38 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
42 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
43 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
44 clEnumValN(RunLLI, "run-int",
45 "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"),
53 CheckProgramExitCode("check-exit-code",
54 cl::desc("Assume nonzero exit code is failure (default on)"),
58 InputFile("input", cl::init("/dev/null"),
59 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
62 AdditionalSOs("additional-so",
63 cl::desc("Additional shared objects to load "
64 "into executing programs"));
67 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
68 cl::desc("Number of seconds program is allowed to run before it "
69 "is killed (default is 300s), 0 disables timeout"));
72 AdditionalLinkerArgs("Xlinker",
73 cl::desc("Additional arguments to pass to the linker"));
77 // Anything specified after the --args option are taken as arguments to the
78 // program being debugged.
80 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
81 cl::ZeroOrMore, cl::PositionalEatsArgs);
84 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
85 cl::ZeroOrMore, cl::PositionalEatsArgs);
88 //===----------------------------------------------------------------------===//
89 // BugDriver method implementation
92 /// initializeExecutionEnvironment - This method is used to set up the
93 /// environment for executing LLVM programs.
95 bool BugDriver::initializeExecutionEnvironment() {
96 std::cout << "Initializing execution environment: ";
98 // Create an instance of the AbstractInterpreter interface as specified on
103 switch (InterpreterSel) {
105 InterpreterSel = RunCBE;
106 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
109 InterpreterSel = RunJIT;
110 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
114 InterpreterSel = RunLLC;
115 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
119 InterpreterSel = RunLLI;
120 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
124 InterpreterSel = AutoPick;
125 Message = "Sorry, I can't automatically select an interpreter!\n";
129 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
133 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
137 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
141 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
145 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
148 std::cerr << Message;
150 // Initialize auxiliary tools for debugging
152 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
153 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
155 gcc = GCC::create(getToolName(), Message);
156 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
158 // If there was an error creating the selected interpreter, quit with error.
159 return Interpreter == 0;
162 /// compileProgram - Try to compile the specified module, throwing an exception
163 /// if an error occurs, or returning normally if not. This is used for code
164 /// generation crash testing.
166 void BugDriver::compileProgram(Module *M) {
167 // Emit the program to a bytecode file...
168 sys::Path BytecodeFile ("bugpoint-test-program.bc");
169 BytecodeFile.makeUnique();
170 if (writeProgramToFile(BytecodeFile.toString(), M)) {
171 std::cerr << ToolName << ": Error emitting bytecode to file '"
172 << BytecodeFile << "'!\n";
176 // Remove the temporary bytecode file when we are done.
177 FileRemover BytecodeFileRemover(BytecodeFile);
179 // Actually compile the program!
180 Interpreter->compileProgram(BytecodeFile.toString());
184 /// executeProgram - This method runs "Program", capturing the output of the
185 /// program to a file, returning the filename of the file. A recommended
186 /// filename may be optionally specified.
188 std::string BugDriver::executeProgram(std::string OutputFile,
189 std::string BytecodeFile,
190 const std::string &SharedObj,
191 AbstractInterpreter *AI,
192 bool *ProgramExitedNonzero) {
193 if (AI == 0) AI = Interpreter;
194 assert(AI && "Interpreter should have been created already!");
195 bool CreatedBytecode = false;
196 if (BytecodeFile.empty()) {
197 // Emit the program to a bytecode file...
198 sys::Path uniqueFilename("bugpoint-test-program.bc");
199 uniqueFilename.makeUnique();
200 BytecodeFile = uniqueFilename.toString();
202 if (writeProgramToFile(BytecodeFile, Program)) {
203 std::cerr << ToolName << ": Error emitting bytecode to file '"
204 << BytecodeFile << "'!\n";
207 CreatedBytecode = true;
210 // Remove the temporary bytecode file when we are done.
211 sys::Path BytecodePath (BytecodeFile);
212 FileRemover BytecodeFileRemover(BytecodePath, CreatedBytecode);
214 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
216 // Check to see if this is a valid output filename...
217 sys::Path uniqueFile(OutputFile);
218 uniqueFile.makeUnique();
219 OutputFile = uniqueFile.toString();
221 // Figure out which shared objects to run, if any.
222 std::vector<std::string> SharedObjs(AdditionalSOs);
223 if (!SharedObj.empty())
224 SharedObjs.push_back(SharedObj);
227 // If this is an LLC or CBE run, then the GCC compiler might get run to
228 // compile the program. If so, we should pass the user's -Xlinker options
231 if (InterpreterSel == RunLLC || InterpreterSel == RunCBE)
232 RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
233 OutputFile, AdditionalLinkerArgs, SharedObjs,
236 RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
237 OutputFile, std::vector<std::string>(),
238 SharedObjs, TimeoutValue);
241 std::cerr << "<timeout>";
242 static bool FirstTimeout = true;
245 "*** Program execution timed out! This mechanism is designed to handle\n"
246 " programs stuck in infinite loops gracefully. The -timeout option\n"
247 " can be used to change the timeout threshold or disable it completely\n"
248 " (with -timeout=0). This message is only displayed once.\n";
249 FirstTimeout = false;
253 if (ProgramExitedNonzero != 0)
254 *ProgramExitedNonzero = (RetVal != 0);
256 // Return the filename we captured the output to.
260 /// executeProgramWithCBE - Used to create reference output with the C
261 /// backend, if reference output is not provided.
263 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
264 bool ProgramExitedNonzero;
265 std::string outFN = executeProgram(OutputFile, "", "",
266 (AbstractInterpreter*)cbe,
267 &ProgramExitedNonzero);
268 if (ProgramExitedNonzero) {
270 << "Warning: While generating reference output, program exited with\n"
271 << "non-zero exit code. This will NOT be treated as a failure.\n";
272 CheckProgramExitCode = false;
277 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
278 assert(Interpreter && "Interpreter should have been created already!");
279 sys::Path OutputCFile;
282 cbe->OutputC(BytecodeFile, OutputCFile);
284 #if 0 /* This is an alternative, as yet unimplemented */
287 LLC *llc = createLLCtool(Message);
288 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
289 std::cerr << "Could not generate asm code with `llc', exiting.\n";
294 std::string SharedObjectFile;
295 if (gcc->MakeSharedObject(OutputCFile.toString(), GCC::CFile,
299 // Remove the intermediate C file
300 OutputCFile.eraseFromDisk();
302 return "./" + SharedObjectFile;
306 /// diffProgram - This method executes the specified module and diffs the output
307 /// against the file specified by ReferenceOutputFile. If the output is
308 /// different, true is returned.
310 bool BugDriver::diffProgram(const std::string &BytecodeFile,
311 const std::string &SharedObject,
312 bool RemoveBytecode) {
313 bool ProgramExitedNonzero;
315 // Execute the program, generating an output file...
316 sys::Path Output (executeProgram("", BytecodeFile, SharedObject, 0,
317 &ProgramExitedNonzero));
319 // If we're checking the program exit code, assume anything nonzero is bad.
320 if (CheckProgramExitCode && ProgramExitedNonzero) {
321 Output.eraseFromDisk();
323 sys::Path(BytecodeFile).eraseFromDisk();
328 bool FilesDifferent = false;
329 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
330 sys::Path(Output.toString()),
331 AbsTolerance, RelTolerance, &Error)) {
333 std::cerr << "While diffing output: " << Error << '\n';
336 FilesDifferent = true;
339 // Remove the generated output.
340 Output.eraseFromDisk();
342 // Remove the bytecode file if we are supposed to.
344 sys::Path(BytecodeFile).eraseFromDisk();
345 return FilesDifferent;
348 bool BugDriver::isExecutingJIT() {
349 return InterpreterSel == RunJIT;