ExecutionDriver.cpp revision 817d8d304688457ce57296fa8c5c5f62b682aa74
1//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains code used to execute the program utilizing one of the
11// various ways of running LLVM bytecode.
12//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
16#include "llvm/Support/ToolRunner.h"
17#include "Support/CommandLine.h"
18#include "Support/Debug.h"
19#include "Support/FileUtilities.h"
20#include "Support/SystemUtils.h"
21#include <fstream>
22#include <iostream>
23using namespace llvm;
24
25namespace {
26  // OutputType - Allow the user to specify the way code should be run, to test
27  // for miscompilation.
28  //
29  enum OutputType {
30    AutoPick, RunLLI, RunJIT, RunLLC, RunCBE
31  };
32
33  cl::opt<OutputType>
34  InterpreterSel(cl::desc("Specify how LLVM code should be executed:"),
35                 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
36                            clEnumValN(RunLLI, "run-int",
37                                       "Execute with the interpreter"),
38                            clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
39                            clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
40                            clEnumValN(RunCBE, "run-cbe", "Compile with CBE"),
41                            0),
42                 cl::init(AutoPick));
43
44  cl::opt<bool>
45  CheckProgramExitCode("check-exit-code",
46                       cl::desc("Assume nonzero exit code is failure (default on)"),
47                       cl::init(true));
48
49  cl::opt<std::string>
50  InputFile("input", cl::init("/dev/null"),
51            cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
52
53  cl::list<std::string>
54  AdditionalSOs("additional-so",
55                cl::desc("Additional shared objects to load "
56                         "into executing programs"));
57}
58
59namespace llvm {
60  // Anything specified after the --args option are taken as arguments to the
61  // program being debugged.
62  cl::list<std::string>
63  InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
64            cl::ZeroOrMore);
65
66  cl::list<std::string>
67  ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
68           cl::ZeroOrMore);
69}
70
71//===----------------------------------------------------------------------===//
72// BugDriver method implementation
73//
74
75/// initializeExecutionEnvironment - This method is used to set up the
76/// environment for executing LLVM programs.
77///
78bool BugDriver::initializeExecutionEnvironment() {
79  std::cout << "Initializing execution environment: ";
80
81  // Create an instance of the AbstractInterpreter interface as specified on
82  // the command line
83  cbe = 0;
84  std::string Message;
85
86  switch (InterpreterSel) {
87  case AutoPick:
88    InterpreterSel = RunCBE;
89    Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
90                                                       &ToolArgv);
91    if (!Interpreter) {
92      InterpreterSel = RunJIT;
93      Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
94                                                   &ToolArgv);
95    }
96    if (!Interpreter) {
97      InterpreterSel = RunLLC;
98      Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
99                                                   &ToolArgv);
100    }
101    if (!Interpreter) {
102      InterpreterSel = RunLLI;
103      Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
104                                                   &ToolArgv);
105    }
106    if (!Interpreter) {
107      InterpreterSel = AutoPick;
108      Message = "Sorry, I can't automatically select an interpreter!\n";
109    }
110    break;
111  case RunLLI:
112    Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
113                                                 &ToolArgv);
114    break;
115  case RunLLC:
116    Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
117                                                 &ToolArgv);
118    break;
119  case RunJIT:
120    Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
121                                                 &ToolArgv);
122    break;
123  case RunCBE:
124    Interpreter = cbe = AbstractInterpreter::createCBE(getToolName(), Message,
125                                                       &ToolArgv);
126    break;
127  default:
128    Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
129    break;
130  }
131  std::cerr << Message;
132
133  // Initialize auxiliary tools for debugging
134  if (!cbe) {
135    cbe = AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv);
136    if (!cbe) { std::cout << Message << "\nExiting.\n"; exit(1); }
137  }
138  gcc = GCC::create(getToolName(), Message);
139  if (!gcc) { std::cout << Message << "\nExiting.\n"; exit(1); }
140
141  // If there was an error creating the selected interpreter, quit with error.
142  return Interpreter == 0;
143}
144
145/// compileProgram - Try to compile the specified module, throwing an exception
146/// if an error occurs, or returning normally if not.  This is used for code
147/// generation crash testing.
148///
149void BugDriver::compileProgram(Module *M) {
150  // Emit the program to a bytecode file...
151  std::string BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
152  if (writeProgramToFile(BytecodeFile, M)) {
153    std::cerr << ToolName << ": Error emitting bytecode to file '"
154              << BytecodeFile << "'!\n";
155    exit(1);
156  }
157
158    // Remove the temporary bytecode file when we are done.
159  FileRemover BytecodeFileRemover(BytecodeFile);
160
161  // Actually compile the program!
162  Interpreter->compileProgram(BytecodeFile);
163}
164
165
166/// executeProgram - This method runs "Program", capturing the output of the
167/// program to a file, returning the filename of the file.  A recommended
168/// filename may be optionally specified.
169///
170std::string BugDriver::executeProgram(std::string OutputFile,
171                                      std::string BytecodeFile,
172                                      const std::string &SharedObj,
173                                      AbstractInterpreter *AI,
174                                      bool *ProgramExitedNonzero) {
175  if (AI == 0) AI = Interpreter;
176  assert(AI && "Interpreter should have been created already!");
177  bool CreatedBytecode = false;
178  if (BytecodeFile.empty()) {
179    // Emit the program to a bytecode file...
180    BytecodeFile = getUniqueFilename("bugpoint-test-program.bc");
181
182    if (writeProgramToFile(BytecodeFile, Program)) {
183      std::cerr << ToolName << ": Error emitting bytecode to file '"
184                << BytecodeFile << "'!\n";
185      exit(1);
186    }
187    CreatedBytecode = true;
188  }
189
190  // Remove the temporary bytecode file when we are done.
191  FileRemover BytecodeFileRemover(BytecodeFile, CreatedBytecode);
192
193  if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
194
195  // Check to see if this is a valid output filename...
196  OutputFile = getUniqueFilename(OutputFile);
197
198  // Figure out which shared objects to run, if any.
199  std::vector<std::string> SharedObjs(AdditionalSOs);
200  if (!SharedObj.empty())
201    SharedObjs.push_back(SharedObj);
202
203  // Actually execute the program!
204  int RetVal = AI->ExecuteProgram(BytecodeFile, InputArgv, InputFile,
205                                  OutputFile, SharedObjs);
206
207  if (ProgramExitedNonzero != 0)
208    *ProgramExitedNonzero = (RetVal != 0);
209
210  // Return the filename we captured the output to.
211  return OutputFile;
212}
213
214/// executeProgramWithCBE - Used to create reference output with the C
215/// backend, if reference output is not provided.
216///
217std::string BugDriver::executeProgramWithCBE(std::string OutputFile) {
218  bool ProgramExitedNonzero;
219  std::string outFN = executeProgram(OutputFile, "", "",
220                                     (AbstractInterpreter*)cbe,
221                                     &ProgramExitedNonzero);
222  if (ProgramExitedNonzero) {
223    std::cerr
224      << "Warning: While generating reference output, program exited with\n"
225      << "non-zero exit code. This will NOT be treated as a failure.\n";
226    CheckProgramExitCode = false;
227  }
228  return outFN;
229}
230
231std::string BugDriver::compileSharedObject(const std::string &BytecodeFile) {
232  assert(Interpreter && "Interpreter should have been created already!");
233  std::string OutputCFile;
234
235  // Using CBE
236  cbe->OutputC(BytecodeFile, OutputCFile);
237
238#if 0 /* This is an alternative, as yet unimplemented */
239  // Using LLC
240  std::string Message;
241  LLC *llc = createLLCtool(Message);
242  if (llc->OutputAsm(BytecodeFile, OutputFile)) {
243    std::cerr << "Could not generate asm code with `llc', exiting.\n";
244    exit(1);
245  }
246#endif
247
248  std::string SharedObjectFile;
249  if (gcc->MakeSharedObject(OutputCFile, GCC::CFile, SharedObjectFile))
250    exit(1);
251
252  // Remove the intermediate C file
253  removeFile(OutputCFile);
254
255  return "./" + SharedObjectFile;
256}
257
258
259/// diffProgram - This method executes the specified module and diffs the output
260/// against the file specified by ReferenceOutputFile.  If the output is
261/// different, true is returned.
262///
263bool BugDriver::diffProgram(const std::string &BytecodeFile,
264                            const std::string &SharedObject,
265                            bool RemoveBytecode) {
266  bool ProgramExitedNonzero;
267
268  // Execute the program, generating an output file...
269  std::string Output = executeProgram("", BytecodeFile, SharedObject, 0,
270                                      &ProgramExitedNonzero);
271
272  // If we're checking the program exit code, assume anything nonzero is bad.
273  if (CheckProgramExitCode && ProgramExitedNonzero) {
274    removeFile(Output);
275    if (RemoveBytecode) removeFile(BytecodeFile);
276    return true;
277  }
278
279  std::string Error;
280  bool FilesDifferent = false;
281  if (DiffFiles(ReferenceOutputFile, Output, &Error)) {
282    if (!Error.empty()) {
283      std::cerr << "While diffing output: " << Error << "\n";
284      exit(1);
285    }
286    FilesDifferent = true;
287  }
288
289  // Remove the generated output.
290  removeFile(Output);
291
292  // Remove the bytecode file if we are supposed to.
293  if (RemoveBytecode) removeFile(BytecodeFile);
294  return FilesDifferent;
295}
296
297bool BugDriver::isExecutingJIT() {
298  return InterpreterSel == RunJIT;
299}
300
301