blob: 72b971764197ee73a7ceadddb41ae29b4a851cee [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains code used to execute the program utilizing one of the
11// various ways of running LLVM bitcode.
12//
13//===----------------------------------------------------------------------===//
14
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"
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000021#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <fstream>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023
24using namespace llvm;
25
26namespace {
27 // OutputType - Allow the user to specify the way code should be run, to test
28 // for miscompilation.
29 //
30 enum OutputType {
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +000031 AutoPick, RunLLI, RunJIT, RunLLC, RunCBE, CBE_bug, LLC_Safe, Custom
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 };
33
34 cl::opt<double>
35 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
36 cl::init(0.0));
37 cl::opt<double>
38 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
39 cl::init(0.0));
40
41 cl::opt<OutputType>
Dan Gohman7fb02ed2008-12-08 04:02:47 +000042 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 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"),
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +000051 clEnumValN(Custom, "run-custom",
52 "Use -exec-command to define a command to execute "
53 "the bitcode. Useful for cross-compilation."),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 clEnumValEnd),
55 cl::init(AutoPick));
56
Dan Gohman7fb02ed2008-12-08 04:02:47 +000057 cl::opt<OutputType>
58 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng899dcd62009-07-21 19:25:09 +000059 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."),
65 clEnumValEnd),
Dan Gohman7fb02ed2008-12-08 04:02:47 +000066 cl::init(AutoPick));
67
68 cl::opt<std::string>
69 SafeInterpreterPath("safe-path",
Evan Cheng899dcd62009-07-21 19:25:09 +000070 cl::desc("Specify the path to the \"safe\" backend program"),
71 cl::init(""));
Dan Gohman7fb02ed2008-12-08 04:02:47 +000072
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 AppendProgramExitCode("append-exit-code",
75 cl::desc("Append the exit code to the output so it gets diff'd too"),
76 cl::init(false));
77
78 cl::opt<std::string>
79 InputFile("input", cl::init("/dev/null"),
80 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
81
82 cl::list<std::string>
83 AdditionalSOs("additional-so",
84 cl::desc("Additional shared objects to load "
85 "into executing programs"));
86
87 cl::list<std::string>
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +000088 AdditionalLinkerArgs("Xlinker",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +000090
91 cl::opt<std::string>
92 CustomExecCommand("exec-command", cl::init("simulate"),
93 cl::desc("Command to execute the bitcode (use with -run-custom) "
94 "(default: simulate)"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095}
96
97namespace llvm {
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);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000103}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000105namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 cl::list<std::string>
107 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
108 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000109
110 cl::list<std::string>
111 SafeToolArgv("safe-tool-args", cl::Positional,
112 cl::desc("<safe-tool arguments>..."),
113 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000114
115 cl::list<std::string>
116 GCCToolArgv("gcc-tool-args", cl::Positional,
117 cl::desc("<gcc-tool arguments>..."),
118 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119}
120
121//===----------------------------------------------------------------------===//
122// BugDriver method implementation
123//
124
125/// initializeExecutionEnvironment - This method is used to set up the
126/// environment for executing LLVM programs.
127///
128bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000129 outs() << "Initializing execution environment: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130
131 // Create an instance of the AbstractInterpreter interface as specified on
132 // the command line
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000133 SafeInterpreter = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 std::string Message;
135
136 switch (InterpreterSel) {
137 case AutoPick:
138 InterpreterSel = RunCBE;
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000139 Interpreter =
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000140 AbstractInterpreter::createCBE(getToolName(), Message, &ToolArgv,
141 &GCCToolArgv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 if (!Interpreter) {
143 InterpreterSel = RunJIT;
144 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
145 &ToolArgv);
146 }
147 if (!Interpreter) {
148 InterpreterSel = RunLLC;
149 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000150 &ToolArgv, &GCCToolArgv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 }
152 if (!Interpreter) {
153 InterpreterSel = RunLLI;
154 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
155 &ToolArgv);
156 }
157 if (!Interpreter) {
158 InterpreterSel = AutoPick;
159 Message = "Sorry, I can't automatically select an interpreter!\n";
160 }
161 break;
162 case RunLLI:
163 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
164 &ToolArgv);
165 break;
166 case RunLLC:
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000167 case LLC_Safe:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000169 &ToolArgv, &GCCToolArgv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
171 case RunJIT:
172 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
173 &ToolArgv);
174 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 case RunCBE:
176 case CBE_bug:
177 Interpreter = AbstractInterpreter::createCBE(getToolName(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000178 &ToolArgv, &GCCToolArgv);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 break;
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +0000180 case Custom:
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000181 Interpreter = AbstractInterpreter::createCustom(Message, CustomExecCommand);
Anton Korobeynikovfc0116f2008-04-28 20:53:48 +0000182 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 default:
184 Message = "Sorry, this back-end is not supported by bugpoint right now!\n";
185 break;
186 }
Matthijs Kooijmanfa2277b2008-06-12 13:09:43 +0000187 if (!Interpreter)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000188 errs() << Message;
Matthijs Kooijmanfa2277b2008-06-12 13:09:43 +0000189 else // Display informational messages on stdout instead of stderr
Dan Gohmanb714fab2009-07-16 15:30:09 +0000190 outs() << Message;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000192 std::string Path = SafeInterpreterPath;
193 if (Path.empty())
194 Path = getToolName();
195 std::vector<std::string> SafeToolArgs = SafeToolArgv;
196 switch (SafeInterpreterSel) {
197 case AutoPick:
198 // In "cbe-bug" mode, default to using LLC as the "safe" backend.
199 if (!SafeInterpreter &&
200 InterpreterSel == CBE_bug) {
201 SafeInterpreterSel = RunLLC;
202 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000203 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000204 &SafeToolArgs,
205 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000206 }
207
208 // In "llc-safe" mode, default to using LLC as the "safe" backend.
209 if (!SafeInterpreter &&
210 InterpreterSel == LLC_Safe) {
211 SafeInterpreterSel = RunLLC;
212 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000213 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000214 &SafeToolArgs,
215 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000216 }
217
218 // Pick a backend that's different from the test backend. The JIT and
219 // LLC backends share a lot of code, so prefer to use the CBE as the
220 // safe back-end when testing them.
221 if (!SafeInterpreter &&
222 InterpreterSel != RunCBE) {
223 SafeInterpreterSel = RunCBE;
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000224 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000225 &SafeToolArgs,
226 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000227 }
228 if (!SafeInterpreter &&
229 InterpreterSel != RunLLC &&
230 InterpreterSel != RunJIT) {
231 SafeInterpreterSel = RunLLC;
232 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000233 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000234 &SafeToolArgs,
235 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000236 }
237 if (!SafeInterpreter) {
238 SafeInterpreterSel = AutoPick;
239 Message = "Sorry, I can't automatically select an interpreter!\n";
240 }
241 break;
242 case RunLLC:
243 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000244 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000245 &SafeToolArgs,
246 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000247 break;
248 case RunCBE:
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000249 SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message,
Bill Wendlingcd1389d2009-03-02 23:13:18 +0000250 &SafeToolArgs,
251 &GCCToolArgv);
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000252 break;
253 case Custom:
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000254 SafeInterpreter = AbstractInterpreter::createCustom(Message,
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000255 CustomExecCommand);
256 break;
257 default:
258 Message = "Sorry, this back-end is not supported by bugpoint as the "
259 "\"safe\" backend right now!\n";
260 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 }
Dan Gohmanb714fab2009-07-16 15:30:09 +0000262 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263
Dan Gohman8e0b7b9d2009-08-05 20:21:17 +0000264 gcc = GCC::create(Message, &GCCToolArgv);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000265 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
267 // If there was an error creating the selected interpreter, quit with error.
268 return Interpreter == 0;
269}
270
271/// compileProgram - Try to compile the specified module, throwing an exception
272/// if an error occurs, or returning normally if not. This is used for code
273/// generation crash testing.
274///
275void BugDriver::compileProgram(Module *M) {
276 // Emit the program to a bitcode file...
277 sys::Path BitcodeFile ("bugpoint-test-program.bc");
278 std::string ErrMsg;
279 if (BitcodeFile.makeUnique(true,&ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000280 errs() << ToolName << ": Error making unique filename: " << ErrMsg
281 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 exit(1);
283 }
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000284 if (writeProgramToFile(BitcodeFile.str(), M)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000285 errs() << ToolName << ": Error emitting bitcode to file '"
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000286 << BitcodeFile.str() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 exit(1);
288 }
289
290 // Remove the temporary bitcode file when we are done.
Anton Korobeynikov43cf4f12009-08-05 09:32:10 +0000291 FileRemover BitcodeFileRemover(BitcodeFile, !SaveTemps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
293 // Actually compile the program!
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000294 Interpreter->compileProgram(BitcodeFile.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295}
296
297
298/// executeProgram - This method runs "Program", capturing the output of the
299/// program to a file, returning the filename of the file. A recommended
300/// filename may be optionally specified.
301///
302std::string BugDriver::executeProgram(std::string OutputFile,
303 std::string BitcodeFile,
304 const std::string &SharedObj,
305 AbstractInterpreter *AI,
306 bool *ProgramExitedNonzero) {
307 if (AI == 0) AI = Interpreter;
308 assert(AI && "Interpreter should have been created already!");
309 bool CreatedBitcode = false;
310 std::string ErrMsg;
311 if (BitcodeFile.empty()) {
312 // Emit the program to a bitcode file...
313 sys::Path uniqueFilename("bugpoint-test-program.bc");
314 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000315 errs() << ToolName << ": Error making unique filename: "
316 << ErrMsg << "!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 exit(1);
318 }
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000319 BitcodeFile = uniqueFilename.str();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
321 if (writeProgramToFile(BitcodeFile, Program)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000322 errs() << ToolName << ": Error emitting bitcode to file '"
323 << BitcodeFile << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 exit(1);
325 }
326 CreatedBitcode = true;
327 }
328
329 // Remove the temporary bitcode file when we are done.
330 sys::Path BitcodePath (BitcodeFile);
Anton Korobeynikov43cf4f12009-08-05 09:32:10 +0000331 FileRemover BitcodeFileRemover(BitcodePath, CreatedBitcode && !SaveTemps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
333 if (OutputFile.empty()) OutputFile = "bugpoint-execution-output";
334
335 // Check to see if this is a valid output filename...
336 sys::Path uniqueFile(OutputFile);
337 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000338 errs() << ToolName << ": Error making unique filename: "
339 << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 exit(1);
341 }
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000342 OutputFile = uniqueFile.str();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 // Figure out which shared objects to run, if any.
345 std::vector<std::string> SharedObjs(AdditionalSOs);
346 if (!SharedObj.empty())
347 SharedObjs.push_back(SharedObj);
348
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000349 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile,
350 OutputFile, AdditionalLinkerArgs, SharedObjs,
351 Timeout, MemoryLimit);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352
353 if (RetVal == -1) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000354 errs() << "<timeout>";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 static bool FirstTimeout = true;
356 if (FirstTimeout) {
Dan Gohmanb714fab2009-07-16 15:30:09 +0000357 outs() << "\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 "*** Program execution timed out! This mechanism is designed to handle\n"
359 " programs stuck in infinite loops gracefully. The -timeout option\n"
360 " can be used to change the timeout threshold or disable it completely\n"
361 " (with -timeout=0). This message is only displayed once.\n";
362 FirstTimeout = false;
363 }
364 }
365
366 if (AppendProgramExitCode) {
367 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
368 outFile << "exit " << RetVal << '\n';
369 outFile.close();
370 }
371
372 if (ProgramExitedNonzero != 0)
373 *ProgramExitedNonzero = (RetVal != 0);
374
375 // Return the filename we captured the output to.
376 return OutputFile;
377}
378
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000379/// executeProgramSafely - Used to create reference output with the "safe"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380/// backend, if reference output is not provided.
381///
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000382std::string BugDriver::executeProgramSafely(std::string OutputFile) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 bool ProgramExitedNonzero;
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000384 std::string outFN = executeProgram(OutputFile, "", "", SafeInterpreter,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 &ProgramExitedNonzero);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 return outFN;
387}
388
389std::string BugDriver::compileSharedObject(const std::string &BitcodeFile) {
390 assert(Interpreter && "Interpreter should have been created already!");
391 sys::Path OutputFile;
392
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000393 // Using the known-good backend.
394 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395
396 std::string SharedObjectFile;
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000397 if (gcc->MakeSharedObject(OutputFile.str(), FT,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 SharedObjectFile, AdditionalLinkerArgs))
399 exit(1);
400
401 // Remove the intermediate C file
402 OutputFile.eraseFromDisk();
403
404 return "./" + SharedObjectFile;
405}
406
407/// createReferenceFile - calls compileProgram and then records the output
408/// into ReferenceOutputFile. Returns true if reference file created, false
409/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
410/// this function.
411///
412bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
413 try {
414 compileProgram(Program);
415 } catch (ToolExecutionError &) {
416 return false;
417 }
418 try {
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000419 ReferenceOutputFile = executeProgramSafely(Filename);
Dan Gohmanb714fab2009-07-16 15:30:09 +0000420 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 } catch (ToolExecutionError &TEE) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000422 errs() << TEE.what();
Dan Gohman7fb02ed2008-12-08 04:02:47 +0000423 if (Interpreter != SafeInterpreter) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000424 errs() << "*** There is a bug running the \"safe\" backend. Either"
425 << " debug it (for example with the -run-cbe bugpoint option,"
426 << " if CBE is being used as the \"safe\" backend), or fix the"
427 << " error some other way.\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 }
429 return false;
430 }
431 return true;
432}
433
434/// diffProgram - This method executes the specified module and diffs the
435/// output against the file specified by ReferenceOutputFile. If the output
436/// is different, true is returned. If there is a problem with the code
437/// generator (e.g., llc crashes), this will throw an exception.
438///
439bool BugDriver::diffProgram(const std::string &BitcodeFile,
440 const std::string &SharedObject,
441 bool RemoveBitcode) {
442 bool ProgramExitedNonzero;
443
444 // Execute the program, generating an output file...
445 sys::Path Output(executeProgram("", BitcodeFile, SharedObject, 0,
446 &ProgramExitedNonzero));
447
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 std::string Error;
449 bool FilesDifferent = false;
450 if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile),
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000451 sys::Path(Output.str()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 AbsTolerance, RelTolerance, &Error)) {
453 if (Diff == 2) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000454 errs() << "While diffing output: " << Error << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 exit(1);
456 }
457 FilesDifferent = true;
458 }
David Goodwinec1edba2009-07-10 21:39:28 +0000459 else {
460 // Remove the generated output if there are no differences.
461 Output.eraseFromDisk();
462 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463
464 // Remove the bitcode file if we are supposed to.
465 if (RemoveBitcode)
466 sys::Path(BitcodeFile).eraseFromDisk();
467 return FilesDifferent;
468}
469
470bool BugDriver::isExecutingJIT() {
471 return InterpreterSel == RunJIT;
472}
473