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"
21 #include "llvm/Support/raw_ostream.h"
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
31 AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, 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 the \"test\" i.e. suspect back-end:"),
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(RunLLCIA, "run-llc-ia",
49 "Compile with LLC with integrated assembler"),
50 clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
51 clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"),
52 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
53 clEnumValN(Custom, "run-custom",
54 "Use -exec-command to define a command to execute "
55 "the bitcode. Useful for cross-compilation."),
60 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
61 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
62 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
63 clEnumValN(RunCBE, "safe-run-cbe", "Compile with CBE"),
64 clEnumValN(Custom, "safe-run-custom",
65 "Use -exec-command to define a command to execute "
66 "the bitcode. Useful for cross-compilation."),
71 SafeInterpreterPath("safe-path",
72 cl::desc("Specify the path to the \"safe\" backend program"),
76 AppendProgramExitCode("append-exit-code",
77 cl::desc("Append the exit code to the output so it gets diff'd too"),
81 InputFile("input", cl::init("/dev/null"),
82 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
85 AdditionalSOs("additional-so",
86 cl::desc("Additional shared objects to load "
87 "into executing programs"));
90 AdditionalLinkerArgs("Xlinker",
91 cl::desc("Additional arguments to pass to the linker"));
94 CustomExecCommand("exec-command", cl::init("simulate"),
95 cl::desc("Command to execute the bitcode (use with -run-custom) "
96 "(default: simulate)"));
100 // Anything specified after the --args option are taken as arguments to the
101 // program being debugged.
102 cl::list<std::string>
103 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
104 cl::ZeroOrMore, cl::PositionalEatsArgs);
107 OutputPrefix("output-prefix", cl::init("bugpoint"),
108 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
112 cl::list<std::string>
113 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
114 cl::ZeroOrMore, cl::PositionalEatsArgs);
116 cl::list<std::string>
117 SafeToolArgv("safe-tool-args", cl::Positional,
118 cl::desc("<safe-tool arguments>..."),
119 cl::ZeroOrMore, cl::PositionalEatsArgs);
121 cl::list<std::string>
122 GCCToolArgv("gcc-tool-args", cl::Positional,
123 cl::desc("<gcc-tool arguments>..."),
124 cl::ZeroOrMore, cl::PositionalEatsArgs);
127 //===----------------------------------------------------------------------===//
128 // BugDriver method implementation
131 /// initializeExecutionEnvironment - This method is used to set up the
132 /// environment for executing LLVM programs.
134 bool BugDriver::initializeExecutionEnvironment() {
135 outs() << "Initializing execution environment: ";
137 // Create an instance of the AbstractInterpreter interface as specified on
142 switch (InterpreterSel) {
144 InterpreterSel = RunCBE;
146 AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv,
149 InterpreterSel = RunJIT;
150 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
154 InterpreterSel = RunLLC;
155 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
156 &ToolArgv, &GCCToolArgv);
159 InterpreterSel = RunLLI;
160 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
164 InterpreterSel = AutoPick;
165 Message = "Sorry, I can't automatically select an interpreter!\n";
169 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
175 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
176 &ToolArgv, &GCCToolArgv,
177 InterpreterSel == RunLLCIA);
180 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
185 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
186 &ToolArgv, &GCCToolArgv);
189 Interpreter = AbstractInterpreter::createCustom(Message, CustomExecCommand);
192 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
197 else // Display informational messages on stdout instead of stderr
200 std::string Path = SafeInterpreterPath;
202 Path = getToolName();
203 std::vector<std::string> SafeToolArgs = SafeToolArgv;
204 switch (SafeInterpreterSel) {
206 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
207 if (!SafeInterpreter &&
208 InterpreterSel == CBE_bug) {
209 SafeInterpreterSel = RunLLC;
210 SafeToolArgs.push_back("--relocation-model=pic");
211 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
216 // In "llc-safe" mode, default to using LLC as the "safe" backend.
217 if (!SafeInterpreter &&
218 InterpreterSel == LLC_Safe) {
219 SafeInterpreterSel = RunLLC;
220 SafeToolArgs.push_back("--relocation-model=pic");
221 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
226 // Pick a backend that's different from the test backend. The JIT and
227 // LLC backends share a lot of code, so prefer to use the CBE as the
228 // safe back-end when testing them.
229 if (!SafeInterpreter &&
230 InterpreterSel != RunCBE) {
231 SafeInterpreterSel = RunCBE;
232 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
236 if (!SafeInterpreter &&
237 InterpreterSel != RunLLC &&
238 InterpreterSel != RunJIT) {
239 SafeInterpreterSel = RunLLC;
240 SafeToolArgs.push_back("--relocation-model=pic");
241 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
245 if (!SafeInterpreter) {
246 SafeInterpreterSel = AutoPick;
247 Message = "Sorry, I can't automatically select an interpreter!\n";
252 SafeToolArgs.push_back("--relocation-model=pic");
253 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
256 SafeInterpreterSel == RunLLCIA);
259 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
264 SafeInterpreter = AbstractInterpreter::createCustom(Message,
268 Message = "Sorry, this back-end is not supported by bugpoint as the "
269 "\"safe\" backend right now!\n";
272 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
274 gcc = GCC::create(Message, &GCCToolArgv);
275 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
277 // If there was an error creating the selected interpreter, quit with error.
278 return Interpreter == 0;
281 /// compileProgram - Try to compile the specified module, returning false and
282 /// setting Error if an error occurs. This is used for code generation
285 void BugDriver::compileProgram(Module *M, std::string *Error) {
286 // Emit the program to a bitcode file...
287 sys::Path BitcodeFile (OutputPrefix + "-test-program.bc");
289 if (BitcodeFile.makeUnique(true, &ErrMsg)) {
290 errs() << ToolName << ": Error making unique filename: " << ErrMsg
294 if (writeProgramToFile(BitcodeFile.str(), M)) {
295 errs() << ToolName << ": Error emitting bitcode to file '"
296 << BitcodeFile.str() << "'!\n";
300 // Remove the temporary bitcode file when we are done.
301 FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps);
303 // Actually compile the program!
304 Interpreter->compileProgram(BitcodeFile.str(), Error);
308 /// executeProgram - This method runs "Program", capturing the output of the
309 /// program to a file, returning the filename of the file. A recommended
310 /// filename may be optionally specified.
312 std::string BugDriver::executeProgram(std::string OutputFile,
313 std::string BitcodeFile,
314 const std::string &SharedObj,
315 AbstractInterpreter *AI,
316 std::string *Error) {
317 if (AI == 0) AI = Interpreter;
318 assert(AI && "Interpreter should have been created already!");
319 bool CreatedBitcode = false;
321 if (BitcodeFile.empty()) {
322 // Emit the program to a bitcode file...
323 sys::Path uniqueFilename(OutputPrefix + "-test-program.bc");
324 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
325 errs() << ToolName << ": Error making unique filename: "
329 BitcodeFile = uniqueFilename.str();
331 if (writeProgramToFile(BitcodeFile, Program)) {
332 errs() << ToolName << ": Error emitting bitcode to file '"
333 << BitcodeFile << "'!\n";
336 CreatedBitcode = true;
339 // Remove the temporary bitcode file when we are done.
340 sys::Path BitcodePath(BitcodeFile);
341 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
343 if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output";
345 // Check to see if this is a valid output filename...
346 sys::Path uniqueFile(OutputFile);
347 if (uniqueFile.makeUnique(true, &ErrMsg)) {
348 errs() << ToolName << ": Error making unique filename: "
352 OutputFile = uniqueFile.str();
354 // Figure out which shared objects to run, if any.
355 std::vector<std::string> SharedObjs(AdditionalSOs);
356 if (!SharedObj.empty())
357 SharedObjs.push_back(SharedObj);
359 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
360 Error, AdditionalLinkerArgs, SharedObjs,
361 Timeout, MemoryLimit);
366 errs() << "<timeout>";
367 static bool FirstTimeout = true;
370 "*** Program execution timed out! This mechanism is designed to handle\n"
371 " programs stuck in infinite loops gracefully. The -timeout option\n"
372 " can be used to change the timeout threshold or disable it completely\n"
373 " (with -timeout=0). This message is only displayed once.\n";
374 FirstTimeout = false;
378 if (AppendProgramExitCode) {
379 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
380 outFile << "exit " << RetVal << '\n';
384 // Return the filename we captured the output to.
388 /// executeProgramSafely - Used to create reference output with the "safe"
389 /// backend, if reference output is not provided.
391 std::string BugDriver::executeProgramSafely(std::string OutputFile,
392 std::string *Error) {
393 return executeProgram(OutputFile, "", "", SafeInterpreter, Error);
396 std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
397 std::string &Error) {
398 assert(Interpreter && "Interpreter should have been created already!");
399 sys::Path OutputFile;
401 // Using the known-good backend.
402 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
407 std::string SharedObjectFile;
408 bool Failure = gcc->MakeSharedObject(OutputFile.str(), FT, SharedObjectFile,
409 AdditionalLinkerArgs, Error);
415 // Remove the intermediate C file
416 OutputFile.eraseFromDisk();
418 return "./" + SharedObjectFile;
421 /// createReferenceFile - calls compileProgram and then records the output
422 /// into ReferenceOutputFile. Returns true if reference file created, false
423 /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
426 bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
428 compileProgram(Program, &Error);
432 ReferenceOutputFile = executeProgramSafely(Filename, &Error);
433 if (!Error.empty()) {
435 if (Interpreter != SafeInterpreter) {
436 errs() << "*** There is a bug running the \"safe\" backend. Either"
437 << " debug it (for example with the -run-cbe bugpoint option,"
438 << " if CBE is being used as the \"safe\" backend), or fix the"
439 << " error some other way.\n";
443 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
447 /// diffProgram - This method executes the specified module and diffs the
448 /// output against the file specified by ReferenceOutputFile. If the output
449 /// is different, 1 is returned. If there is a problem with the code
450 /// generator (e.g., llc crashes), this will return -1 and set Error.
452 bool BugDriver::diffProgram(const std::string &BitcodeFile,
453 const std::string &SharedObject,
455 std::string *ErrMsg) {
456 // Execute the program, generating an output file...
457 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0, ErrMsg));
458 if (!ErrMsg->empty())
462 bool FilesDifferent = false;
463 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
464 sys::Path(Output.str()),
465 AbsTolerance, RelTolerance, &Error)) {
467 errs() << "While diffing output: " << Error << '\n';
470 FilesDifferent = true;
473 // Remove the generated output if there are no differences.
474 Output.eraseFromDisk();
477 // Remove the bitcode file if we are supposed to.
479 sys::Path(BitcodeFile).eraseFromDisk();
480 return FilesDifferent;
483 bool BugDriver::isExecutingJIT() {
484 return InterpreterSel == RunJIT;