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
, 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(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 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
59 cl::values(clEnumValN(AutoPick
, "safe-auto", "Use best guess"),
60 clEnumValN(RunLLC
, "safe-run-llc", "Compile with LLC"),
61 clEnumValN(RunCBE
, "safe-run-cbe", "Compile with CBE"),
62 clEnumValN(Custom
, "safe-run-custom",
63 "Use -exec-command to define a command to execute "
64 "the bitcode. Useful for cross-compilation."),
69 SafeInterpreterPath("safe-path",
70 cl::desc("Specify the path to the \"safe\" backend program"),
74 AppendProgramExitCode("append-exit-code",
75 cl::desc("Append the exit code to the output so it gets diff'd too"),
79 InputFile("input", cl::init("/dev/null"),
80 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
83 AdditionalSOs("additional-so",
84 cl::desc("Additional shared objects to load "
85 "into executing programs"));
88 AdditionalLinkerArgs("Xlinker",
89 cl::desc("Additional arguments to pass to the linker"));
92 CustomExecCommand("exec-command", cl::init("simulate"),
93 cl::desc("Command to execute the bitcode (use with -run-custom) "
94 "(default: simulate)"));
98 // Anything specified after the --args option are taken as arguments to the
99 // program being debugged.
100 cl::list
<std::string
>
101 InputArgv("args", cl::Positional
, cl::desc("<program arguments>..."),
102 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
105 OutputPrefix("output-prefix", cl::init("bugpoint"),
106 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
110 cl::list
<std::string
>
111 ToolArgv("tool-args", cl::Positional
, cl::desc("<tool arguments>..."),
112 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
114 cl::list
<std::string
>
115 SafeToolArgv("safe-tool-args", cl::Positional
,
116 cl::desc("<safe-tool arguments>..."),
117 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
119 cl::list
<std::string
>
120 GCCToolArgv("gcc-tool-args", cl::Positional
,
121 cl::desc("<gcc-tool arguments>..."),
122 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
125 //===----------------------------------------------------------------------===//
126 // BugDriver method implementation
129 /// initializeExecutionEnvironment - This method is used to set up the
130 /// environment for executing LLVM programs.
132 bool BugDriver::initializeExecutionEnvironment() {
133 outs() << "Initializing execution environment: ";
135 // Create an instance of the AbstractInterpreter interface as specified on
140 switch (InterpreterSel
) {
142 InterpreterSel
= RunCBE
;
144 AbstractInterpreter::createCBE(getToolName(), Message
, &ToolArgv
,
147 InterpreterSel
= RunJIT
;
148 Interpreter
= AbstractInterpreter::createJIT(getToolName(), Message
,
152 InterpreterSel
= RunLLC
;
153 Interpreter
= AbstractInterpreter::createLLC(getToolName(), Message
,
154 &ToolArgv
, &GCCToolArgv
);
157 InterpreterSel
= RunLLI
;
158 Interpreter
= AbstractInterpreter::createLLI(getToolName(), Message
,
162 InterpreterSel
= AutoPick
;
163 Message
= "Sorry, I can't automatically select an interpreter!\n";
167 Interpreter
= AbstractInterpreter::createLLI(getToolName(), Message
,
172 Interpreter
= AbstractInterpreter::createLLC(getToolName(), Message
,
173 &ToolArgv
, &GCCToolArgv
);
176 Interpreter
= AbstractInterpreter::createJIT(getToolName(), Message
,
181 Interpreter
= AbstractInterpreter::createCBE(getToolName(), Message
,
182 &ToolArgv
, &GCCToolArgv
);
185 Interpreter
= AbstractInterpreter::createCustom(Message
, CustomExecCommand
);
188 Message
= "Sorry, this back-end is not supported by bugpoint right now!\n";
193 else // Display informational messages on stdout instead of stderr
196 std::string Path
= SafeInterpreterPath
;
198 Path
= getToolName();
199 std::vector
<std::string
> SafeToolArgs
= SafeToolArgv
;
200 switch (SafeInterpreterSel
) {
202 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
203 if (!SafeInterpreter
&&
204 InterpreterSel
== CBE_bug
) {
205 SafeInterpreterSel
= RunLLC
;
206 SafeToolArgs
.push_back("--relocation-model=pic");
207 SafeInterpreter
= AbstractInterpreter::createLLC(Path
.c_str(), Message
,
212 // In "llc-safe" mode, default to using LLC as the "safe" backend.
213 if (!SafeInterpreter
&&
214 InterpreterSel
== LLC_Safe
) {
215 SafeInterpreterSel
= RunLLC
;
216 SafeToolArgs
.push_back("--relocation-model=pic");
217 SafeInterpreter
= AbstractInterpreter::createLLC(Path
.c_str(), Message
,
222 // Pick a backend that's different from the test backend. The JIT and
223 // LLC backends share a lot of code, so prefer to use the CBE as the
224 // safe back-end when testing them.
225 if (!SafeInterpreter
&&
226 InterpreterSel
!= RunCBE
) {
227 SafeInterpreterSel
= RunCBE
;
228 SafeInterpreter
= AbstractInterpreter::createCBE(Path
.c_str(), Message
,
232 if (!SafeInterpreter
&&
233 InterpreterSel
!= RunLLC
&&
234 InterpreterSel
!= RunJIT
) {
235 SafeInterpreterSel
= RunLLC
;
236 SafeToolArgs
.push_back("--relocation-model=pic");
237 SafeInterpreter
= AbstractInterpreter::createLLC(Path
.c_str(), Message
,
241 if (!SafeInterpreter
) {
242 SafeInterpreterSel
= AutoPick
;
243 Message
= "Sorry, I can't automatically select an interpreter!\n";
247 SafeToolArgs
.push_back("--relocation-model=pic");
248 SafeInterpreter
= AbstractInterpreter::createLLC(Path
.c_str(), Message
,
253 SafeInterpreter
= AbstractInterpreter::createCBE(Path
.c_str(), Message
,
258 SafeInterpreter
= AbstractInterpreter::createCustom(Message
,
262 Message
= "Sorry, this back-end is not supported by bugpoint as the "
263 "\"safe\" backend right now!\n";
266 if (!SafeInterpreter
) { outs() << Message
<< "\nExiting.\n"; exit(1); }
268 gcc
= GCC::create(Message
, &GCCToolArgv
);
269 if (!gcc
) { outs() << Message
<< "\nExiting.\n"; exit(1); }
271 // If there was an error creating the selected interpreter, quit with error.
272 return Interpreter
== 0;
275 /// compileProgram - Try to compile the specified module, throwing an exception
276 /// if an error occurs, or returning normally if not. This is used for code
277 /// generation crash testing.
279 void BugDriver::compileProgram(Module
*M
) {
280 // Emit the program to a bitcode file...
281 sys::Path
BitcodeFile (OutputPrefix
+ "-test-program.bc");
283 if (BitcodeFile
.makeUnique(true,&ErrMsg
)) {
284 errs() << ToolName
<< ": Error making unique filename: " << ErrMsg
288 if (writeProgramToFile(BitcodeFile
.str(), M
)) {
289 errs() << ToolName
<< ": Error emitting bitcode to file '"
290 << BitcodeFile
.str() << "'!\n";
294 // Remove the temporary bitcode file when we are done.
295 FileRemover
BitcodeFileRemover(BitcodeFile
, !SaveTemps
);
297 // Actually compile the program!
298 Interpreter
->compileProgram(BitcodeFile
.str());
302 /// executeProgram - This method runs "Program", capturing the output of the
303 /// program to a file, returning the filename of the file. A recommended
304 /// filename may be optionally specified.
306 std::string
BugDriver::executeProgram(std::string OutputFile
,
307 std::string BitcodeFile
,
308 const std::string
&SharedObj
,
309 AbstractInterpreter
*AI
,
310 bool *ProgramExitedNonzero
) {
311 if (AI
== 0) AI
= Interpreter
;
312 assert(AI
&& "Interpreter should have been created already!");
313 bool CreatedBitcode
= false;
315 if (BitcodeFile
.empty()) {
316 // Emit the program to a bitcode file...
317 sys::Path
uniqueFilename(OutputPrefix
+ "-test-program.bc");
318 if (uniqueFilename
.makeUnique(true, &ErrMsg
)) {
319 errs() << ToolName
<< ": Error making unique filename: "
323 BitcodeFile
= uniqueFilename
.str();
325 if (writeProgramToFile(BitcodeFile
, Program
)) {
326 errs() << ToolName
<< ": Error emitting bitcode to file '"
327 << BitcodeFile
<< "'!\n";
330 CreatedBitcode
= true;
333 // Remove the temporary bitcode file when we are done.
334 sys::Path
BitcodePath (BitcodeFile
);
335 FileRemover
BitcodeFileRemover(BitcodePath
, CreatedBitcode
&& !SaveTemps
);
337 if (OutputFile
.empty()) OutputFile
= OutputPrefix
+ "-execution-output";
339 // Check to see if this is a valid output filename...
340 sys::Path
uniqueFile(OutputFile
);
341 if (uniqueFile
.makeUnique(true, &ErrMsg
)) {
342 errs() << ToolName
<< ": Error making unique filename: "
346 OutputFile
= uniqueFile
.str();
348 // Figure out which shared objects to run, if any.
349 std::vector
<std::string
> SharedObjs(AdditionalSOs
);
350 if (!SharedObj
.empty())
351 SharedObjs
.push_back(SharedObj
);
353 int RetVal
= AI
->ExecuteProgram(BitcodeFile
, InputArgv
, InputFile
,
354 OutputFile
, AdditionalLinkerArgs
, SharedObjs
,
355 Timeout
, MemoryLimit
);
358 errs() << "<timeout>";
359 static bool FirstTimeout
= true;
362 "*** Program execution timed out! This mechanism is designed to handle\n"
363 " programs stuck in infinite loops gracefully. The -timeout option\n"
364 " can be used to change the timeout threshold or disable it completely\n"
365 " (with -timeout=0). This message is only displayed once.\n";
366 FirstTimeout
= false;
370 if (AppendProgramExitCode
) {
371 std::ofstream
outFile(OutputFile
.c_str(), std::ios_base::app
);
372 outFile
<< "exit " << RetVal
<< '\n';
376 if (ProgramExitedNonzero
!= 0)
377 *ProgramExitedNonzero
= (RetVal
!= 0);
379 // Return the filename we captured the output to.
383 /// executeProgramSafely - Used to create reference output with the "safe"
384 /// backend, if reference output is not provided.
386 std::string
BugDriver::executeProgramSafely(std::string OutputFile
) {
387 bool ProgramExitedNonzero
;
388 std::string outFN
= executeProgram(OutputFile
, "", "", SafeInterpreter
,
389 &ProgramExitedNonzero
);
393 std::string
BugDriver::compileSharedObject(const std::string
&BitcodeFile
) {
394 assert(Interpreter
&& "Interpreter should have been created already!");
395 sys::Path OutputFile
;
397 // Using the known-good backend.
398 GCC::FileType FT
= SafeInterpreter
->OutputCode(BitcodeFile
, OutputFile
);
400 std::string SharedObjectFile
;
401 if (gcc
->MakeSharedObject(OutputFile
.str(), FT
,
402 SharedObjectFile
, AdditionalLinkerArgs
))
405 // Remove the intermediate C file
406 OutputFile
.eraseFromDisk();
408 return "./" + SharedObjectFile
;
411 /// createReferenceFile - calls compileProgram and then records the output
412 /// into ReferenceOutputFile. Returns true if reference file created, false
413 /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
416 bool BugDriver::createReferenceFile(Module
*M
, const std::string
&Filename
) {
418 compileProgram(Program
);
419 } catch (ToolExecutionError
&) {
423 ReferenceOutputFile
= executeProgramSafely(Filename
);
424 outs() << "\nReference output is: " << ReferenceOutputFile
<< "\n\n";
425 } catch (ToolExecutionError
&TEE
) {
426 errs() << TEE
.what();
427 if (Interpreter
!= SafeInterpreter
) {
428 errs() << "*** There is a bug running the \"safe\" backend. Either"
429 << " debug it (for example with the -run-cbe bugpoint option,"
430 << " if CBE is being used as the \"safe\" backend), or fix the"
431 << " error some other way.\n";
438 /// diffProgram - This method executes the specified module and diffs the
439 /// output against the file specified by ReferenceOutputFile. If the output
440 /// is different, true is returned. If there is a problem with the code
441 /// generator (e.g., llc crashes), this will throw an exception.
443 bool BugDriver::diffProgram(const std::string
&BitcodeFile
,
444 const std::string
&SharedObject
,
445 bool RemoveBitcode
) {
446 bool ProgramExitedNonzero
;
448 // Execute the program, generating an output file...
449 sys::Path
Output(executeProgram("", BitcodeFile
, SharedObject
, 0,
450 &ProgramExitedNonzero
));
453 bool FilesDifferent
= false;
454 if (int Diff
= DiffFilesWithTolerance(sys::Path(ReferenceOutputFile
),
455 sys::Path(Output
.str()),
456 AbsTolerance
, RelTolerance
, &Error
)) {
458 errs() << "While diffing output: " << Error
<< '\n';
461 FilesDifferent
= true;
464 // Remove the generated output if there are no differences.
465 Output
.eraseFromDisk();
468 // Remove the bitcode file if we are supposed to.
470 sys::Path(BitcodeFile
).eraseFromDisk();
471 return FilesDifferent
;
474 bool BugDriver::isExecutingJIT() {
475 return InterpreterSel
== RunJIT
;