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"
25 // OutputType - Allow the user to specify the way code should be run, to test
26 // for miscompilation.
29 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
33 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
36 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
40 InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
41 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
42 clEnumValN(RunLLI, "run-int",
43 "Execute with the interpreter"),
44 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
45 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
46 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
51 CheckProgramExitCode("check-exit-code",
52 cl::desc("Assume nonzero exit code is failure (default on)"),
56 InputFile("input", cl::init("/dev/null"),
57 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
60 AdditionalSOs("additional-so",
61 cl::desc("Additional shared objects to load "
62 "into executing programs"));
65 TimeoutValue("timeout", cl::init(300), cl::value_desc("seconds"),
66 cl::desc("Number of seconds program is allowed to run before it "
67 "is killed (default is 300s), 0 disables timeout"));
71 // Anything specified after the --args option are taken as arguments to the
72 // program being debugged.
74 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
75 cl::ZeroOrMore, cl::PositionalEatsArgs);
78 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
79 cl::ZeroOrMore, cl::PositionalEatsArgs);
82 //===----------------------------------------------------------------------===//
83 // BugDriver method implementation
86 /// initializeExecutionEnvironment - This method is used to set up the
87 /// environment for executing LLVM programs.
89 bool BugDriver::initializeExecutionEnvironment() {
90 std::cout << "Initializing execution environment: ";
92 // Create an instance of the AbstractInterpreter interface as specified on
97 switch (InterpreterSel) {
99 InterpreterSel = RunCBE;
100 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
103 InterpreterSel = RunJIT;
104 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
108 InterpreterSel = RunLLC;
109 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
113 InterpreterSel = RunLLI;
114 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
118 InterpreterSel = AutoPick;
119 Message = "Sorry, I can't automatically select an interpreter!\n";
123 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
127 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
131 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
135 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
139 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
142 std::cerr << Message;
144 // Initialize auxiliary tools for debugging
146 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
147 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
149 gcc = GCC::create(getToolName(), Message);
150 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
152 // If there was an error creating the selected interpreter, quit with error.
153 return Interpreter == 0;
156 /// compileProgram - Try to compile the specified module, throwing an exception
157 /// if an error occurs, or returning normally if not. This is used for code
158 /// generation crash testing.
160 void BugDriver::compileProgram(Module *M) {
161 // Emit the program to a bytecode file...
162 sys::Path BytecodeFile ("bugpoint-test-program.bc");
163 BytecodeFile.makeUnique();
164 if (writeProgramToFile(BytecodeFile.toString(), M)) {
165 std::cerr << ToolName << ": Error emitting bytecode to file '"
166 << BytecodeFile << "'!\n";
170 // Remove the temporary bytecode file when we are done.
171 FileRemover BytecodeFileRemover(BytecodeFile);
173 // Actually compile the program!
174 Interpreter->compileProgram(BytecodeFile.toString());
178 /// executeProgram - This method runs "Program", capturing the output of the
179 /// program to a file, returning the filename of the file. A recommended
180 /// filename may be optionally specified.
182 std::string BugDriver::executeProgram(std::string OutputFile,
183 std::string BytecodeFile,
184 const std::string &SharedObj,
185 AbstractInterpreter *AI,
186 bool *ProgramExitedNonzero) {
187 if (AI == 0) AI = Interpreter;
188 assert(AI && "Interpreter should have been created already!");
189 bool CreatedBytecode = false;
190 if (BytecodeFile.empty()) {
191 // Emit the program to a bytecode file...
192 sys::Path uniqueFilename("bugpoint-test-program.bc");
193 uniqueFilename.makeUnique();
194 BytecodeFile = uniqueFilename.toString();
196 if (writeProgramToFile(BytecodeFile, Program)) {
197 std::cerr << ToolName << ": Error emitting bytecode to file '"
198 << BytecodeFile << "'!\n";
201 CreatedBytecode = true;
204 // Remove the temporary bytecode file when we are done.
205 sys::Path BytecodePath (BytecodeFile);
206 FileRemover BytecodeFileRemover(BytecodePath, CreatedBytecode);
208 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
210 // Check to see if this is a valid output filename...
211 sys::Path uniqueFile(OutputFile);
212 uniqueFile.makeUnique();
213 OutputFile = uniqueFile.toString();
215 // Figure out which shared objects to run, if any.
216 std::vector<std::string> SharedObjs(AdditionalSOs);
217 if (!SharedObj.empty())
218 SharedObjs.push_back(SharedObj);
220 // Actually execute the program!
221 int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
222 OutputFile, SharedObjs, TimeoutValue);
225 std::cerr << "<timeout>";
226 static bool FirstTimeout = true;
229 "*** Program execution timed out! This mechanism is designed to handle\n"
230 " programs stuck in infinite loops gracefully. The -timeout option\n"
231 " can be used to change the timeout threshold or disable it completely\n"
232 " (with -timeout=0). This message is only displayed once.\n";
233 FirstTimeout = false;
237 if (ProgramExitedNonzero != 0)
238 *ProgramExitedNonzero = (RetVal != 0);
240 // Return the filename we captured the output to.
244 /// executeProgramWithCBE - Used to create reference output with the C
245 /// backend, if reference output is not provided.
247 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
248 bool ProgramExitedNonzero;
249 std::string outFN = executeProgram(OutputFile, "", "",
250 (AbstractInterpreter*)cbe,
251 &ProgramExitedNonzero);
252 if (ProgramExitedNonzero) {
254 << "Warning: While generating reference output, program exited with\n"
255 << "non-zero exit code. This will NOT be treated as a failure.\n";
256 CheckProgramExitCode = false;
261 std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
262 assert(Interpreter && "Interpreter should have been created already!");
263 sys::Path OutputCFile;
266 cbe->OutputC(BytecodeFile, OutputCFile);
268 #if 0 /* This is an alternative, as yet unimplemented */
271 LLC *llc = createLLCtool(Message);
272 if (llc->OutputAsm(BytecodeFile, OutputFile)) {
273 std::cerr << "Could not generate asm code with `llc', exiting.\n";
278 std::string SharedObjectFile;
279 if (gcc->MakeSharedObject(OutputCFile.toString(), GCC::CFile,
283 // Remove the intermediate C file
284 OutputCFile.destroyFile();
286 return "./" + SharedObjectFile;
290 /// diffProgram - This method executes the specified module and diffs the output
291 /// against the file specified by ReferenceOutputFile. If the output is
292 /// different, true is returned.
294 bool BugDriver::diffProgram(const std::string &BytecodeFile,
295 const std::string &SharedObject,
296 bool RemoveBytecode) {
297 bool ProgramExitedNonzero;
299 // Execute the program, generating an output file...
300 sys::Path Output (executeProgram("", BytecodeFile, SharedObject, 0,
301 &ProgramExitedNonzero));
303 // If we're checking the program exit code, assume anything nonzero is bad.
304 if (CheckProgramExitCode && ProgramExitedNonzero) {
305 Output.destroyFile();
307 sys::Path(BytecodeFile).destroyFile();
312 bool FilesDifferent = false;
313 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
314 sys::Path(Output.toString()),
315 AbsTolerance, RelTolerance, &Error)) {
317 std::cerr << "While diffing output: " << Error << '\n';
320 FilesDifferent = true;
323 // Remove the generated output.
324 Output.destroyFile();
326 // Remove the bytecode file if we are supposed to.
327 if (RemoveBytecode) sys::Path(BytecodeFile).destroyFile();
328 return FilesDifferent;
331 bool BugDriver::isExecutingJIT() {
332 return InterpreterSel == RunJIT;