1 //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 bitcode.
13 //===----------------------------------------------------------------------===//
15 #include "BugDriver.h"
16 #include "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, CBE_bug, LLC_Safe, Custom
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"),
49 clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"),
50 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
51 clEnumValN(Custom, "run-custom",
52 "Use -exec-command to define a command to execute "
53 "the bitcode. Useful for cross-compilation."),
58 CheckProgramExitCode("check-exit-code",
59 cl::desc("Assume nonzero exit code is failure (default on)"),
63 AppendProgramExitCode("append-exit-code",
64 cl::desc("Append the exit code to the output so it gets diff'd too"),
68 InputFile("input", cl::init("/dev/null"),
69 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
72 AdditionalSOs("additional-so",
73 cl::desc("Additional shared objects to load "
74 "into executing programs"));
77 AdditionalLinkerArgs("Xlinker",
78 cl::desc("Additional arguments to pass to the linker"));
81 CustomExecCommand("exec-command", cl::init("simulate"),
82 cl::desc("Command to execute the bitcode (use with -run-custom) "
83 "(default: simulate)"));
87 // Anything specified after the --args option are taken as arguments to the
88 // program being debugged.
90 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
91 cl::ZeroOrMore, cl::PositionalEatsArgs);
94 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
95 cl::ZeroOrMore, cl::PositionalEatsArgs);
98 //===----------------------------------------------------------------------===//
99 // BugDriver method implementation
102 /// initializeExecutionEnvironment - This method is used to set up the
103 /// environment for executing LLVM programs.
105 bool BugDriver::initializeExecutionEnvironment() {
106 std::cout << "Initializing execution environment: ";
108 // Create an instance of the AbstractInterpreter interface as specified on
113 switch (InterpreterSel) {
115 InterpreterSel = RunCBE;
116 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
119 InterpreterSel = RunJIT;
120 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
124 InterpreterSel = RunLLC;
125 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
129 InterpreterSel = RunLLI;
130 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
134 InterpreterSel = AutoPick;
135 Message = "Sorry, I can't automatically select an interpreter!\n";
139 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
143 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
147 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
151 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
156 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
160 Interpreter = AbstractInterpreter::createCustom(getToolName(), Message,
164 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
168 std::cerr << Message;
169 else // Display informational messages on stdout instead of stderr
170 std::cout << Message;
172 // Initialize auxiliary tools for debugging
173 if (InterpreterSel == RunCBE) {
174 // We already created a CBE, reuse it.
176 } else if (InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe) {
177 // We want to debug the CBE itself or LLC is known-good. Use LLC as the
178 // 'known-good' compiler.
179 std::vector<std::string> ToolArgs;
180 ToolArgs.push_back("--relocation-model=pic");
181 cbe = AbstractInterpreter::createLLC(getToolName(), Message, &ToolArgs);
183 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
185 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
187 gcc = GCC::create(getToolName(), Message);
188 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
190 // If there was an error creating the selected interpreter, quit with error.
191 return Interpreter == 0;
194 /// compileProgram - Try to compile the specified module, throwing an exception
195 /// if an error occurs, or returning normally if not. This is used for code
196 /// generation crash testing.
198 void BugDriver::compileProgram(Module *M) {
199 // Emit the program to a bitcode file...
200 sys::Path BitcodeFile ("bugpoint-test-program.bc");
202 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
203 std::cerr << ToolName << ": Error making unique filename: " << ErrMsg
207 if (writeProgramToFile(BitcodeFile.toString(), M)) {
208 std::cerr << ToolName << ": Error emitting bitcode to file '"
209 << BitcodeFile << "'!\n";
213 // Remove the temporary bitcode file when we are done.
214 FileRemover BitcodeFileRemover(BitcodeFile);
216 // Actually compile the program!
217 Interpreter->compileProgram(BitcodeFile.toString());
221 /// executeProgram - This method runs "Program", capturing the output of the
222 /// program to a file, returning the filename of the file. A recommended
223 /// filename may be optionally specified.
225 std::string BugDriver::executeProgram(std::string OutputFile,
226 std::string BitcodeFile,
227 const std::string &SharedObj,
228 AbstractInterpreter *AI,
229 bool *ProgramExitedNonzero) {
230 if (AI == 0) AI = Interpreter;
231 assert(AI && "Interpreter should have been created already!");
232 bool CreatedBitcode = false;
234 if (BitcodeFile.empty()) {
235 // Emit the program to a bitcode file...
236 sys::Path uniqueFilename("bugpoint-test-program.bc");
237 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
238 std::cerr << ToolName << ": Error making unique filename: "
242 BitcodeFile = uniqueFilename.toString();
244 if (writeProgramToFile(BitcodeFile, Program)) {
245 std::cerr << ToolName << ": Error emitting bitcode to file '"
246 << BitcodeFile << "'!\n";
249 CreatedBitcode = true;
252 // Remove the temporary bitcode file when we are done.
253 sys::Path BitcodePath (BitcodeFile);
254 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode);
256 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
258 // Check to see if this is a valid output filename...
259 sys::Path uniqueFile(OutputFile);
260 if (uniqueFile.makeUnique(true, &ErrMsg)) {
261 std::cerr << ToolName << ": Error making unique filename: "
265 OutputFile = uniqueFile.toString();
267 // Figure out which shared objects to run, if any.
268 std::vector<std::string> SharedObjs(AdditionalSOs);
269 if (!SharedObj.empty())
270 SharedObjs.push_back(SharedObj);
273 // If this is an LLC or CBE run, then the GCC compiler might get run to
274 // compile the program. If so, we should pass the user's -Xlinker options
277 if (InterpreterSel == RunLLC || InterpreterSel == RunCBE ||
278 InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe)
279 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
280 OutputFile, AdditionalLinkerArgs, SharedObjs,
281 Timeout, MemoryLimit);
283 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
284 OutputFile, std::vector<std::string>(),
285 SharedObjs, Timeout, MemoryLimit);
288 std::cerr << "<timeout>";
289 static bool FirstTimeout = true;
292 "*** Program execution timed out! This mechanism is designed to handle\n"
293 " programs stuck in infinite loops gracefully. The -timeout option\n"
294 " can be used to change the timeout threshold or disable it completely\n"
295 " (with -timeout=0). This message is only displayed once.\n";
296 FirstTimeout = false;
300 if (AppendProgramExitCode) {
301 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
302 outFile << "exit " << RetVal << '\n';
306 if (ProgramExitedNonzero != 0)
307 *ProgramExitedNonzero = (RetVal != 0);
309 // Return the filename we captured the output to.
313 /// executeProgramWithCBE - Used to create reference output with the C
314 /// backend, if reference output is not provided.
316 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
317 bool ProgramExitedNonzero;
318 std::string outFN = executeProgram(OutputFile, "", "", cbe,
319 &ProgramExitedNonzero);
320 if (ProgramExitedNonzero) {
322 << "Warning: While generating reference output, program exited with\n"
323 << "non-zero exit code. This will NOT be treated as a failure.\n";
324 CheckProgramExitCode = false;
329 std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
330 assert(Interpreter && "Interpreter should have been created already!");
331 sys::Path OutputFile;
334 GCC::FileType FT = cbe->OutputCode(BitcodeFile, OutputFile);
336 std::string SharedObjectFile;
337 if (gcc->MakeSharedObject(OutputFile.toString(), FT,
338 SharedObjectFile, AdditionalLinkerArgs))
341 // Remove the intermediate C file
342 OutputFile.eraseFromDisk();
344 return "./" + SharedObjectFile;
347 /// createReferenceFile - calls compileProgram and then records the output
348 /// into ReferenceOutputFile. Returns true if reference file created, false
349 /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
352 bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
354 compileProgram(Program);
355 } catch (ToolExecutionError &) {
359 ReferenceOutputFile = executeProgramWithCBE(Filename);
360 std::cout << "Reference output is: " << ReferenceOutputFile << "\n\n";
361 } catch (ToolExecutionError &TEE) {
362 std::cerr << TEE.what();
363 if (Interpreter != cbe) {
364 std::cerr << "*** There is a bug running the C backend. Either debug"
365 << " it (use the -run-cbe bugpoint option), or fix the error"
366 << " some other way.\n";
373 /// diffProgram - This method executes the specified module and diffs the
374 /// output against the file specified by ReferenceOutputFile. If the output
375 /// is different, true is returned. If there is a problem with the code
376 /// generator (e.g., llc crashes), this will throw an exception.
378 bool BugDriver::diffProgram(const std::string &BitcodeFile,
379 const std::string &SharedObject,
380 bool RemoveBitcode) {
381 bool ProgramExitedNonzero;
383 // Execute the program, generating an output file...
384 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
385 &ProgramExitedNonzero));
387 // If we're checking the program exit code, assume anything nonzero is bad.
388 if (CheckProgramExitCode && ProgramExitedNonzero) {
389 Output.eraseFromDisk();
391 sys::Path(BitcodeFile).eraseFromDisk();
396 bool FilesDifferent = false;
397 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
398 sys::Path(Output.toString()),
399 AbsTolerance, RelTolerance, &Error)) {
401 std::cerr << "While diffing output: " << Error << '\n';
404 FilesDifferent = true;
407 // Remove the generated output.
408 Output.eraseFromDisk();
410 // Remove the bitcode file if we are supposed to.
412 sys::Path(BitcodeFile).eraseFromDisk();
413 return FilesDifferent;
416 bool BugDriver::isExecutingJIT() {
417 return InterpreterSel == RunJIT;