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
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"),
55 CheckProgramExitCode("check-exit-code",
56 cl::desc("Assume nonzero exit code is failure (default on)"),
60 AppendProgramExitCode("append-exit-code",
61 cl::desc("Append the exit code to the output so it gets diff'd too"),
65 InputFile("input", cl::init("/dev/null"),
66 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
69 AdditionalSOs("additional-so",
70 cl::desc("Additional shared objects to load "
71 "into executing programs"));
74 AdditionalLinkerArgs("Xlinker",
75 cl::desc("Additional arguments to pass to the linker"));
79 // Anything specified after the --args option are taken as arguments to the
80 // program being debugged.
82 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
83 cl::ZeroOrMore, cl::PositionalEatsArgs);
86 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
87 cl::ZeroOrMore, cl::PositionalEatsArgs);
90 //===----------------------------------------------------------------------===//
91 // BugDriver method implementation
94 /// initializeExecutionEnvironment - This method is used to set up the
95 /// environment for executing LLVM programs.
97 bool BugDriver::initializeExecutionEnvironment() {
98 std::cout << "Initializing execution environment: ";
100 // Create an instance of the AbstractInterpreter interface as specified on
105 switch (InterpreterSel) {
107 InterpreterSel = RunCBE;
108 Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
111 InterpreterSel = RunJIT;
112 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
116 InterpreterSel = RunLLC;
117 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
121 InterpreterSel = RunLLI;
122 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
126 InterpreterSel = AutoPick;
127 Message = "Sorry, I can't automatically select an interpreter!\n";
131 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
135 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
139 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
143 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
148 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
152 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
155 std::cerr << Message;
157 // Initialize auxiliary tools for debugging
158 if (InterpreterSel == RunCBE) {
159 // We already created a CBE, reuse it.
161 } else if (InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe) {
162 // We want to debug the CBE itself or LLC is known-good. Use LLC as the
163 // 'known-good' compiler.
164 std::vector<std::string> ToolArgs;
165 ToolArgs.push_back("--relocation-model=pic");
166 cbe = AbstractInterpreter::createLLC(getToolName(), Message, &ToolArgs);
168 cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
170 if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
172 gcc = GCC::create(getToolName(), Message);
173 if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
175 // If there was an error creating the selected interpreter, quit with error.
176 return Interpreter == 0;
179 /// compileProgram - Try to compile the specified module, throwing an exception
180 /// if an error occurs, or returning normally if not. This is used for code
181 /// generation crash testing.
183 void BugDriver::compileProgram(Module *M) {
184 // Emit the program to a bitcode file...
185 sys::Path BitcodeFile ("bugpoint-test-program.bc");
187 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
188 std::cerr << ToolName << ": Error making unique filename: " << ErrMsg
192 if (writeProgramToFile(BitcodeFile.toString(), M)) {
193 std::cerr << ToolName << ": Error emitting bitcode to file '"
194 << BitcodeFile << "'!\n";
198 // Remove the temporary bitcode file when we are done.
199 FileRemover BitcodeFileRemover(BitcodeFile);
201 // Actually compile the program!
202 Interpreter->compileProgram(BitcodeFile.toString());
206 /// executeProgram - This method runs "Program", capturing the output of the
207 /// program to a file, returning the filename of the file. A recommended
208 /// filename may be optionally specified.
210 std::string BugDriver::executeProgram(std::string OutputFile,
211 std::string BitcodeFile,
212 const std::string &SharedObj,
213 AbstractInterpreter *AI,
214 bool *ProgramExitedNonzero) {
215 if (AI == 0) AI = Interpreter;
216 assert(AI && "Interpreter should have been created already!");
217 bool CreatedBitcode = false;
219 if (BitcodeFile.empty()) {
220 // Emit the program to a bitcode file...
221 sys::Path uniqueFilename("bugpoint-test-program.bc");
222 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
223 std::cerr << ToolName << ": Error making unique filename: "
227 BitcodeFile = uniqueFilename.toString();
229 if (writeProgramToFile(BitcodeFile, Program)) {
230 std::cerr << ToolName << ": Error emitting bitcode to file '"
231 << BitcodeFile << "'!\n";
234 CreatedBitcode = true;
237 // Remove the temporary bitcode file when we are done.
238 sys::Path BitcodePath (BitcodeFile);
239 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode);
241 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
243 // Check to see if this is a valid output filename...
244 sys::Path uniqueFile(OutputFile);
245 if (uniqueFile.makeUnique(true, &ErrMsg)) {
246 std::cerr << ToolName << ": Error making unique filename: "
250 OutputFile = uniqueFile.toString();
252 // Figure out which shared objects to run, if any.
253 std::vector<std::string> SharedObjs(AdditionalSOs);
254 if (!SharedObj.empty())
255 SharedObjs.push_back(SharedObj);
258 // If this is an LLC or CBE run, then the GCC compiler might get run to
259 // compile the program. If so, we should pass the user's -Xlinker options
262 if (InterpreterSel == RunLLC || InterpreterSel == RunCBE ||
263 InterpreterSel == CBE_bug || InterpreterSel == LLC_Safe)
264 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
265 OutputFile, AdditionalLinkerArgs, SharedObjs,
266 Timeout, MemoryLimit);
268 RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
269 OutputFile, std::vector<std::string>(),
270 SharedObjs, Timeout, MemoryLimit);
273 std::cerr << "<timeout>";
274 static bool FirstTimeout = true;
277 "*** Program execution timed out! This mechanism is designed to handle\n"
278 " programs stuck in infinite loops gracefully. The -timeout option\n"
279 " can be used to change the timeout threshold or disable it completely\n"
280 " (with -timeout=0). This message is only displayed once.\n";
281 FirstTimeout = false;
285 if (AppendProgramExitCode) {
286 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
287 outFile << "exit " << RetVal << '\n';
291 if (ProgramExitedNonzero != 0)
292 *ProgramExitedNonzero = (RetVal != 0);
294 // Return the filename we captured the output to.
298 /// executeProgramWithCBE - Used to create reference output with the C
299 /// backend, if reference output is not provided.
301 std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
302 bool ProgramExitedNonzero;
303 std::string outFN = executeProgram(OutputFile, "", "", cbe,
304 &ProgramExitedNonzero);
305 if (ProgramExitedNonzero) {
307 << "Warning: While generating reference output, program exited with\n"
308 << "non-zero exit code. This will NOT be treated as a failure.\n";
309 CheckProgramExitCode = false;
314 std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
315 assert(Interpreter && "Interpreter should have been created already!");
316 sys::Path OutputFile;
319 GCC::FileType FT = cbe->OutputCode(BitcodeFile, OutputFile);
321 std::string SharedObjectFile;
322 if (gcc->MakeSharedObject(OutputFile.toString(), FT,
323 SharedObjectFile, AdditionalLinkerArgs))
326 // Remove the intermediate C file
327 OutputFile.eraseFromDisk();
329 return "./" + SharedObjectFile;
332 /// createReferenceFile - calls compileProgram and then records the output
333 /// into ReferenceOutputFile. Returns true if reference file created, false
334 /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
337 bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
339 compileProgram(Program);
340 } catch (ToolExecutionError &) {
344 ReferenceOutputFile = executeProgramWithCBE(Filename);
345 std::cout << "Reference output is: " << ReferenceOutputFile << "\n\n";
346 } catch (ToolExecutionError &TEE) {
347 std::cerr << TEE.what();
348 if (Interpreter != cbe) {
349 std::cerr << "*** There is a bug running the C backend. Either debug"
350 << " it (use the -run-cbe bugpoint option), or fix the error"
351 << " some other way.\n";
358 /// diffProgram - This method executes the specified module and diffs the
359 /// output against the file specified by ReferenceOutputFile. If the output
360 /// is different, true is returned. If there is a problem with the code
361 /// generator (e.g., llc crashes), this will throw an exception.
363 bool BugDriver::diffProgram(const std::string &BitcodeFile,
364 const std::string &SharedObject,
365 bool RemoveBitcode) {
366 bool ProgramExitedNonzero;
368 // Execute the program, generating an output file...
369 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
370 &ProgramExitedNonzero));
372 // If we're checking the program exit code, assume anything nonzero is bad.
373 if (CheckProgramExitCode && ProgramExitedNonzero) {
374 Output.eraseFromDisk();
376 sys::Path(BitcodeFile).eraseFromDisk();
381 bool FilesDifferent = false;
382 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
383 sys::Path(Output.toString()),
384 AbsTolerance, RelTolerance, &Error)) {
386 std::cerr << "While diffing output: " << Error << '\n';
389 FilesDifferent = true;
392 // Remove the generated output.
393 Output.eraseFromDisk();
395 // Remove the bitcode file if we are supposed to.
397 sys::Path(BitcodeFile).eraseFromDisk();
398 return FilesDifferent;
401 bool BugDriver::isExecutingJIT() {
402 return InterpreterSel == RunJIT;