]>
git.proxmox.com Git - rustc.git/blob - src/llvm/tools/bugpoint/ExecutionDriver.cpp
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
, LLC_Safe
, CompileCustom
, 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(LLC_Safe
, "llc-safe", "Use LLC for all"),
51 clEnumValN(CompileCustom
, "compile-custom",
52 "Use -compile-command to define a command to "
53 "compile the bitcode. Useful to avoid linking."),
54 clEnumValN(Custom
, "run-custom",
55 "Use -exec-command to define a command to execute "
56 "the bitcode. Useful for cross-compilation."),
61 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
62 cl::values(clEnumValN(AutoPick
, "safe-auto", "Use best guess"),
63 clEnumValN(RunLLC
, "safe-run-llc", "Compile with LLC"),
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 CustomCompileCommand("compile-command", cl::init("llc"),
95 cl::desc("Command to compile the bitcode (use with -compile-custom) "
99 CustomExecCommand("exec-command", cl::init("simulate"),
100 cl::desc("Command to execute the bitcode (use with -run-custom) "
101 "(default: simulate)"));
105 // Anything specified after the --args option are taken as arguments to the
106 // program being debugged.
107 cl::list
<std::string
>
108 InputArgv("args", cl::Positional
, cl::desc("<program arguments>..."),
109 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
112 OutputPrefix("output-prefix", cl::init("bugpoint"),
113 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
117 cl::list
<std::string
>
118 ToolArgv("tool-args", cl::Positional
, cl::desc("<tool arguments>..."),
119 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
121 cl::list
<std::string
>
122 SafeToolArgv("safe-tool-args", cl::Positional
,
123 cl::desc("<safe-tool arguments>..."),
124 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
127 GCCBinary("gcc", cl::init("gcc"),
128 cl::desc("The gcc binary to use. (default 'gcc')"));
130 cl::list
<std::string
>
131 GCCToolArgv("gcc-tool-args", cl::Positional
,
132 cl::desc("<gcc-tool arguments>..."),
133 cl::ZeroOrMore
, cl::PositionalEatsArgs
);
136 //===----------------------------------------------------------------------===//
137 // BugDriver method implementation
140 /// initializeExecutionEnvironment - This method is used to set up the
141 /// environment for executing LLVM programs.
143 bool BugDriver::initializeExecutionEnvironment() {
144 outs() << "Initializing execution environment: ";
146 // Create an instance of the AbstractInterpreter interface as specified on
151 switch (InterpreterSel
) {
154 InterpreterSel
= RunJIT
;
155 Interpreter
= AbstractInterpreter::createJIT(getToolName(), Message
,
159 InterpreterSel
= RunLLC
;
160 Interpreter
= AbstractInterpreter::createLLC(getToolName(), Message
,
161 GCCBinary
, &ToolArgv
,
165 InterpreterSel
= RunLLI
;
166 Interpreter
= AbstractInterpreter::createLLI(getToolName(), Message
,
170 InterpreterSel
= AutoPick
;
171 Message
= "Sorry, I can't automatically select an interpreter!\n";
175 Interpreter
= AbstractInterpreter::createLLI(getToolName(), Message
,
181 Interpreter
= AbstractInterpreter::createLLC(getToolName(), Message
,
182 GCCBinary
, &ToolArgv
,
184 InterpreterSel
== RunLLCIA
);
187 Interpreter
= AbstractInterpreter::createJIT(getToolName(), Message
,
192 AbstractInterpreter::createCustomCompiler(Message
, CustomCompileCommand
);
196 AbstractInterpreter::createCustomExecutor(Message
, CustomExecCommand
);
201 else // Display informational messages on stdout instead of stderr
204 std::string Path
= SafeInterpreterPath
;
206 Path
= getToolName();
207 std::vector
<std::string
> SafeToolArgs
= SafeToolArgv
;
208 switch (SafeInterpreterSel
) {
210 // In "llc-safe" mode, default to using LLC as the "safe" backend.
211 if (!SafeInterpreter
&&
212 InterpreterSel
== LLC_Safe
) {
213 SafeInterpreterSel
= RunLLC
;
214 SafeToolArgs
.push_back("--relocation-model=pic");
215 SafeInterpreter
= AbstractInterpreter::createLLC(Path
.c_str(), Message
,
221 if (!SafeInterpreter
&&
222 InterpreterSel
!= RunLLC
&&
223 InterpreterSel
!= RunJIT
) {
224 SafeInterpreterSel
= RunLLC
;
225 SafeToolArgs
.push_back("--relocation-model=pic");
226 SafeInterpreter
= AbstractInterpreter::createLLC(Path
.c_str(), Message
,
231 if (!SafeInterpreter
) {
232 SafeInterpreterSel
= AutoPick
;
233 Message
= "Sorry, I can't automatically select an interpreter!\n";
238 SafeToolArgs
.push_back("--relocation-model=pic");
239 SafeInterpreter
= AbstractInterpreter::createLLC(Path
.c_str(), Message
,
240 GCCBinary
, &SafeToolArgs
,
242 SafeInterpreterSel
== RunLLCIA
);
246 AbstractInterpreter::createCustomExecutor(Message
, CustomExecCommand
);
249 Message
= "Sorry, this back-end is not supported by bugpoint as the "
250 "\"safe\" backend right now!\n";
253 if (!SafeInterpreter
) { outs() << Message
<< "\nExiting.\n"; exit(1); }
255 gcc
= GCC::create(Message
, GCCBinary
, &GCCToolArgv
);
256 if (!gcc
) { outs() << Message
<< "\nExiting.\n"; exit(1); }
258 // If there was an error creating the selected interpreter, quit with error.
259 return Interpreter
== 0;
262 /// compileProgram - Try to compile the specified module, returning false and
263 /// setting Error if an error occurs. This is used for code generation
266 void BugDriver::compileProgram(Module
*M
, std::string
*Error
) const {
267 // Emit the program to a bitcode file...
268 sys::Path
BitcodeFile (OutputPrefix
+ "-test-program.bc");
270 if (BitcodeFile
.makeUnique(true, &ErrMsg
)) {
271 errs() << ToolName
<< ": Error making unique filename: " << ErrMsg
275 if (writeProgramToFile(BitcodeFile
.str(), M
)) {
276 errs() << ToolName
<< ": Error emitting bitcode to file '"
277 << BitcodeFile
.str() << "'!\n";
281 // Remove the temporary bitcode file when we are done.
282 FileRemover
BitcodeFileRemover(BitcodeFile
.str(), !SaveTemps
);
284 // Actually compile the program!
285 Interpreter
->compileProgram(BitcodeFile
.str(), Error
, Timeout
, MemoryLimit
);
289 /// executeProgram - This method runs "Program", capturing the output of the
290 /// program to a file, returning the filename of the file. A recommended
291 /// filename may be optionally specified.
293 std::string
BugDriver::executeProgram(const Module
*Program
,
294 std::string OutputFile
,
295 std::string BitcodeFile
,
296 const std::string
&SharedObj
,
297 AbstractInterpreter
*AI
,
298 std::string
*Error
) const {
299 if (AI
== 0) AI
= Interpreter
;
300 assert(AI
&& "Interpreter should have been created already!");
301 bool CreatedBitcode
= false;
303 if (BitcodeFile
.empty()) {
304 // Emit the program to a bitcode file...
305 sys::Path
uniqueFilename(OutputPrefix
+ "-test-program.bc");
306 if (uniqueFilename
.makeUnique(true, &ErrMsg
)) {
307 errs() << ToolName
<< ": Error making unique filename: "
311 BitcodeFile
= uniqueFilename
.str();
313 if (writeProgramToFile(BitcodeFile
, Program
)) {
314 errs() << ToolName
<< ": Error emitting bitcode to file '"
315 << BitcodeFile
<< "'!\n";
318 CreatedBitcode
= true;
321 // Remove the temporary bitcode file when we are done.
322 sys::Path
BitcodePath(BitcodeFile
);
323 FileRemover
BitcodeFileRemover(BitcodePath
.str(),
324 CreatedBitcode
&& !SaveTemps
);
326 if (OutputFile
.empty()) OutputFile
= OutputPrefix
+ "-execution-output";
328 // Check to see if this is a valid output filename...
329 sys::Path
uniqueFile(OutputFile
);
330 if (uniqueFile
.makeUnique(true, &ErrMsg
)) {
331 errs() << ToolName
<< ": Error making unique filename: "
335 OutputFile
= uniqueFile
.str();
337 // Figure out which shared objects to run, if any.
338 std::vector
<std::string
> SharedObjs(AdditionalSOs
);
339 if (!SharedObj
.empty())
340 SharedObjs
.push_back(SharedObj
);
342 int RetVal
= AI
->ExecuteProgram(BitcodeFile
, InputArgv
, InputFile
, OutputFile
,
343 Error
, AdditionalLinkerArgs
, SharedObjs
,
344 Timeout
, MemoryLimit
);
349 errs() << "<timeout>";
350 static bool FirstTimeout
= true;
353 "*** Program execution timed out! This mechanism is designed to handle\n"
354 " programs stuck in infinite loops gracefully. The -timeout option\n"
355 " can be used to change the timeout threshold or disable it completely\n"
356 " (with -timeout=0). This message is only displayed once.\n";
357 FirstTimeout
= false;
361 if (AppendProgramExitCode
) {
362 std::ofstream
outFile(OutputFile
.c_str(), std::ios_base::app
);
363 outFile
<< "exit " << RetVal
<< '\n';
367 // Return the filename we captured the output to.
371 /// executeProgramSafely - Used to create reference output with the "safe"
372 /// backend, if reference output is not provided.
374 std::string
BugDriver::executeProgramSafely(const Module
*Program
,
375 std::string OutputFile
,
376 std::string
*Error
) const {
377 return executeProgram(Program
, OutputFile
, "", "", SafeInterpreter
, Error
);
380 std::string
BugDriver::compileSharedObject(const std::string
&BitcodeFile
,
381 std::string
&Error
) {
382 assert(Interpreter
&& "Interpreter should have been created already!");
383 sys::Path OutputFile
;
385 // Using the known-good backend.
386 GCC::FileType FT
= SafeInterpreter
->OutputCode(BitcodeFile
, OutputFile
,
391 std::string SharedObjectFile
;
392 bool Failure
= gcc
->MakeSharedObject(OutputFile
.str(), FT
, SharedObjectFile
,
393 AdditionalLinkerArgs
, Error
);
399 // Remove the intermediate C file
400 OutputFile
.eraseFromDisk();
402 return "./" + SharedObjectFile
;
405 /// createReferenceFile - calls compileProgram and then records the output
406 /// into ReferenceOutputFile. Returns true if reference file created, false
407 /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
410 bool BugDriver::createReferenceFile(Module
*M
, const std::string
&Filename
) {
412 compileProgram(Program
, &Error
);
416 ReferenceOutputFile
= executeProgramSafely(Program
, Filename
, &Error
);
417 if (!Error
.empty()) {
419 if (Interpreter
!= SafeInterpreter
) {
420 errs() << "*** There is a bug running the \"safe\" backend. Either"
421 << " debug it (for example with the -run-jit bugpoint option,"
422 << " if JIT is being used as the \"safe\" backend), or fix the"
423 << " error some other way.\n";
427 outs() << "\nReference output is: " << ReferenceOutputFile
<< "\n\n";
431 /// diffProgram - This method executes the specified module and diffs the
432 /// output against the file specified by ReferenceOutputFile. If the output
433 /// is different, 1 is returned. If there is a problem with the code
434 /// generator (e.g., llc crashes), this will set ErrMsg.
436 bool BugDriver::diffProgram(const Module
*Program
,
437 const std::string
&BitcodeFile
,
438 const std::string
&SharedObject
,
440 std::string
*ErrMsg
) const {
441 // Execute the program, generating an output file...
442 sys::Path
Output(executeProgram(Program
, "", BitcodeFile
, SharedObject
, 0,
444 if (!ErrMsg
->empty())
448 bool FilesDifferent
= false;
449 if (int Diff
= DiffFilesWithTolerance(sys::Path(ReferenceOutputFile
),
450 sys::Path(Output
.str()),
451 AbsTolerance
, RelTolerance
, &Error
)) {
453 errs() << "While diffing output: " << Error
<< '\n';
456 FilesDifferent
= true;
459 // Remove the generated output if there are no differences.
460 Output
.eraseFromDisk();
463 // Remove the bitcode file if we are supposed to.
465 sys::Path(BitcodeFile
).eraseFromDisk();
466 return FilesDifferent
;
469 bool BugDriver::isExecutingJIT() {
470 return InterpreterSel
== RunJIT
;