blob: 5490115598125b665c6f9f3a57dea3359889acc1 [file] [log] [blame]
Chris Lattner4a106452002-12-23 23:50:16 +00001//===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a106452002-12-23 23:50:16 +00009//
10// This file contains code used to execute the program utilizing one of the
Gabor Greif8ff70c22007-07-04 21:55:50 +000011// various ways of running LLVM bitcode.
Chris Lattner4a106452002-12-23 23:50:16 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4a106452002-12-23 23:50:16 +000015#include "BugDriver.h"
Chris Lattnerf1b20d82006-06-06 22:30:59 +000016#include "ToolRunner.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/Support/CommandLine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/FileUtilities.h"
Rafael Espindolaf656a1d2013-06-17 19:21:38 +000020#include "llvm/Support/PathV1.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/SystemUtils.h"
Chris Lattner74382b72009-08-23 22:45:37 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner4a106452002-12-23 23:50:16 +000023#include <fstream>
Reid Spencer51ab5c82006-06-06 00:00:42 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025using namespace llvm;
26
Chris Lattner4a106452002-12-23 23:50:16 +000027namespace {
28 // OutputType - Allow the user to specify the way code should be run, to test
29 // for miscompilation.
30 //
31 enum OutputType {
Eric Christophera443e5b2012-03-23 05:50:46 +000032 AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, LLC_Safe, CompileCustom, Custom
Chris Lattner4a106452002-12-23 23:50:16 +000033 };
Misha Brukman41485562003-09-29 22:40:52 +000034
Chris Lattnera328c512005-01-23 03:45:26 +000035 cl::opt<double>
36 AbsTolerance("abs-tolerance", cl::desc("Absolute error tolerated"),
37 cl::init(0.0));
38 cl::opt<double>
39 RelTolerance("rel-tolerance", cl::desc("Relative error tolerated"),
40 cl::init(0.0));
41
Chris Lattner4a106452002-12-23 23:50:16 +000042 cl::opt<OutputType>
Dan Gohman70ef4492008-12-08 04:02:47 +000043 InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000044 cl::values(clEnumValN(AutoPick, "auto", "Use best guess"),
Misha Brukmanb687d822004-04-19 03:12:35 +000045 clEnumValN(RunLLI, "run-int",
46 "Execute with the interpreter"),
Misha Brukman50733362003-07-24 18:17:43 +000047 clEnumValN(RunJIT, "run-jit", "Execute with JIT"),
48 clEnumValN(RunLLC, "run-llc", "Compile with LLC"),
Chris Lattner50010422010-03-16 06:41:47 +000049 clEnumValN(RunLLCIA, "run-llc-ia",
50 "Compile with LLC with integrated assembler"),
Chris Lattnercd6f46e2006-11-09 05:57:53 +000051 clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"),
Andrew Trickf73311b2011-02-08 18:20:48 +000052 clEnumValN(CompileCustom, "compile-custom",
53 "Use -compile-command to define a command to "
54 "compile the bitcode. Useful to avoid linking."),
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000055 clEnumValN(Custom, "run-custom",
56 "Use -exec-command to define a command to execute "
57 "the bitcode. Useful for cross-compilation."),
Chris Lattner4d143ee2004-07-16 00:08:28 +000058 clEnumValEnd),
Brian Gaekeb5ee5092003-10-21 17:41:35 +000059 cl::init(AutoPick));
Chris Lattner3c053a02003-04-23 20:31:37 +000060
Dan Gohman70ef4492008-12-08 04:02:47 +000061 cl::opt<OutputType>
62 SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"),
Evan Cheng49419e22009-07-21 19:25:09 +000063 cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"),
64 clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"),
Evan Cheng49419e22009-07-21 19:25:09 +000065 clEnumValN(Custom, "safe-run-custom",
66 "Use -exec-command to define a command to execute "
67 "the bitcode. Useful for cross-compilation."),
68 clEnumValEnd),
Dan Gohman70ef4492008-12-08 04:02:47 +000069 cl::init(AutoPick));
70
71 cl::opt<std::string>
72 SafeInterpreterPath("safe-path",
Evan Cheng49419e22009-07-21 19:25:09 +000073 cl::desc("Specify the path to the \"safe\" backend program"),
74 cl::init(""));
Dan Gohman70ef4492008-12-08 04:02:47 +000075
Brian Gaekec5cad212004-02-11 18:37:32 +000076 cl::opt<bool>
Reid Spencer5e1452c2006-11-28 07:04:10 +000077 AppendProgramExitCode("append-exit-code",
78 cl::desc("Append the exit code to the output so it gets diff'd too"),
79 cl::init(false));
80
Chris Lattner3c053a02003-04-23 20:31:37 +000081 cl::opt<std::string>
82 InputFile("input", cl::init("/dev/null"),
83 cl::desc("Filename to pipe in as stdin (default: /dev/null)"));
Chris Lattner7dac6582003-10-14 22:24:31 +000084
85 cl::list<std::string>
86 AdditionalSOs("additional-so",
87 cl::desc("Additional shared objects to load "
88 "into executing programs"));
Chris Lattner7d91e492004-07-24 07:53:26 +000089
Reid Spencer51ab5c82006-06-06 00:00:42 +000090 cl::list<std::string>
Andrew Trickde86cbd2011-02-08 18:07:10 +000091 AdditionalLinkerArgs("Xlinker",
Reid Spencer51ab5c82006-06-06 00:00:42 +000092 cl::desc("Additional arguments to pass to the linker"));
Anton Korobeynikov9ef74252008-04-28 20:53:48 +000093
94 cl::opt<std::string>
Andrew Trickf73311b2011-02-08 18:20:48 +000095 CustomCompileCommand("compile-command", cl::init("llc"),
96 cl::desc("Command to compile the bitcode (use with -compile-custom) "
97 "(default: llc)"));
98
99 cl::opt<std::string>
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000100 CustomExecCommand("exec-command", cl::init("simulate"),
101 cl::desc("Command to execute the bitcode (use with -run-custom) "
102 "(default: simulate)"));
Chris Lattner4a106452002-12-23 23:50:16 +0000103}
104
Brian Gaeked0fde302003-11-11 22:41:34 +0000105namespace llvm {
Chris Lattnerfa761832004-01-14 03:38:37 +0000106 // Anything specified after the --args option are taken as arguments to the
107 // program being debugged.
108 cl::list<std::string>
109 InputArgv("args", cl::Positional, cl::desc("<program arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000110 cl::ZeroOrMore, cl::PositionalEatsArgs);
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000111
112 cl::opt<std::string>
113 OutputPrefix("output-prefix", cl::init("bugpoint"),
114 cl::desc("Prefix to use for outputs (default: 'bugpoint')"));
Dan Gohman70ef4492008-12-08 04:02:47 +0000115}
Brian Gaeke636df3d2004-05-04 21:09:16 +0000116
Dan Gohman70ef4492008-12-08 04:02:47 +0000117namespace {
Brian Gaeke636df3d2004-05-04 21:09:16 +0000118 cl::list<std::string>
119 ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."),
Chris Lattner60083e22004-05-06 22:05:35 +0000120 cl::ZeroOrMore, cl::PositionalEatsArgs);
Dan Gohman70ef4492008-12-08 04:02:47 +0000121
122 cl::list<std::string>
123 SafeToolArgv("safe-tool-args", cl::Positional,
124 cl::desc("<safe-tool arguments>..."),
125 cl::ZeroOrMore, cl::PositionalEatsArgs);
Bill Wendling38efa382009-03-02 23:13:18 +0000126
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000127 cl::opt<std::string>
Andrew Trickde86cbd2011-02-08 18:07:10 +0000128 GCCBinary("gcc", cl::init("gcc"),
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000129 cl::desc("The gcc binary to use. (default 'gcc')"));
130
Bill Wendling38efa382009-03-02 23:13:18 +0000131 cl::list<std::string>
132 GCCToolArgv("gcc-tool-args", cl::Positional,
133 cl::desc("<gcc-tool arguments>..."),
134 cl::ZeroOrMore, cl::PositionalEatsArgs);
Chris Lattnerfa761832004-01-14 03:38:37 +0000135}
Misha Brukman9d679cb2003-07-30 20:15:44 +0000136
Chris Lattner4a106452002-12-23 23:50:16 +0000137//===----------------------------------------------------------------------===//
138// BugDriver method implementation
139//
140
141/// initializeExecutionEnvironment - This method is used to set up the
142/// environment for executing LLVM programs.
143///
144bool BugDriver::initializeExecutionEnvironment() {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000145 outs() << "Initializing execution environment: ";
Chris Lattner4a106452002-12-23 23:50:16 +0000146
Misha Brukman41485562003-09-29 22:40:52 +0000147 // Create an instance of the AbstractInterpreter interface as specified on
148 // the command line
Dan Gohman70ef4492008-12-08 04:02:47 +0000149 SafeInterpreter = 0;
Chris Lattner4a106452002-12-23 23:50:16 +0000150 std::string Message;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000151
Chris Lattnercc876a72003-05-03 03:19:41 +0000152 switch (InterpreterSel) {
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000153 case AutoPick:
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000154 if (!Interpreter) {
155 InterpreterSel = RunJIT;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000156 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
157 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000158 }
159 if (!Interpreter) {
160 InterpreterSel = RunLLC;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000161 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000162 GCCBinary, &ToolArgv,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000163 &GCCToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000164 }
165 if (!Interpreter) {
166 InterpreterSel = RunLLI;
Brian Gaeke636df3d2004-05-04 21:09:16 +0000167 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
168 &ToolArgv);
Brian Gaekeb5ee5092003-10-21 17:41:35 +0000169 }
170 if (!Interpreter) {
171 InterpreterSel = AutoPick;
172 Message = "Sorry, I can't automatically select an interpreter!\n";
173 }
174 break;
Chris Lattner769f1fe2003-10-14 21:59:36 +0000175 case RunLLI:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000176 Interpreter = AbstractInterpreter::createLLI(getToolName(), Message,
177 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000178 break;
179 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000180 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000181 case LLC_Safe:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000182 Interpreter = AbstractInterpreter::createLLC(getToolName(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000183 GCCBinary, &ToolArgv,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000184 &GCCToolArgv,
Chris Lattner50010422010-03-16 06:41:47 +0000185 InterpreterSel == RunLLCIA);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000186 break;
187 case RunJIT:
Brian Gaeke636df3d2004-05-04 21:09:16 +0000188 Interpreter = AbstractInterpreter::createJIT(getToolName(), Message,
189 &ToolArgv);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000190 break;
Andrew Trickf73311b2011-02-08 18:20:48 +0000191 case CompileCustom:
192 Interpreter =
193 AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand);
194 break;
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000195 case Custom:
Andrew Trickf73311b2011-02-08 18:20:48 +0000196 Interpreter =
197 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Anton Korobeynikov9ef74252008-04-28 20:53:48 +0000198 break;
Chris Lattner4a106452002-12-23 23:50:16 +0000199 }
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000200 if (!Interpreter)
Dan Gohman65f57c22009-07-15 16:35:29 +0000201 errs() << Message;
Matthijs Kooijmanad6996d2008-06-12 13:09:43 +0000202 else // Display informational messages on stdout instead of stderr
Dan Gohmanac95cc72009-07-16 15:30:09 +0000203 outs() << Message;
Chris Lattner4a106452002-12-23 23:50:16 +0000204
Dan Gohman70ef4492008-12-08 04:02:47 +0000205 std::string Path = SafeInterpreterPath;
206 if (Path.empty())
207 Path = getToolName();
208 std::vector<std::string> SafeToolArgs = SafeToolArgv;
209 switch (SafeInterpreterSel) {
210 case AutoPick:
Dan Gohman70ef4492008-12-08 04:02:47 +0000211 // In "llc-safe" mode, default to using LLC as the "safe" backend.
212 if (!SafeInterpreter &&
213 InterpreterSel == LLC_Safe) {
214 SafeInterpreterSel = RunLLC;
215 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000216 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000217 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000218 &SafeToolArgs,
219 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000220 }
221
Dan Gohman70ef4492008-12-08 04:02:47 +0000222 if (!SafeInterpreter &&
223 InterpreterSel != RunLLC &&
224 InterpreterSel != RunJIT) {
225 SafeInterpreterSel = RunLLC;
226 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000227 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Andrew Trickde86cbd2011-02-08 18:07:10 +0000228 GCCBinary,
Bill Wendling38efa382009-03-02 23:13:18 +0000229 &SafeToolArgs,
230 &GCCToolArgv);
Dan Gohman70ef4492008-12-08 04:02:47 +0000231 }
232 if (!SafeInterpreter) {
233 SafeInterpreterSel = AutoPick;
Saleem Abdulrasooled7fcf42013-01-24 16:49:14 +0000234 Message = "Sorry, I can't automatically select a safe interpreter!\n";
Dan Gohman70ef4492008-12-08 04:02:47 +0000235 }
236 break;
237 case RunLLC:
Chris Lattner50010422010-03-16 06:41:47 +0000238 case RunLLCIA:
Dan Gohman70ef4492008-12-08 04:02:47 +0000239 SafeToolArgs.push_back("--relocation-model=pic");
Dan Gohman197f7282009-08-05 20:21:17 +0000240 SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message,
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000241 GCCBinary, &SafeToolArgs,
Chris Lattner50010422010-03-16 06:41:47 +0000242 &GCCToolArgv,
243 SafeInterpreterSel == RunLLCIA);
Dan Gohman70ef4492008-12-08 04:02:47 +0000244 break;
Dan Gohman70ef4492008-12-08 04:02:47 +0000245 case Custom:
Andrew Trickf73311b2011-02-08 18:20:48 +0000246 SafeInterpreter =
247 AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand);
Dan Gohman70ef4492008-12-08 04:02:47 +0000248 break;
249 default:
250 Message = "Sorry, this back-end is not supported by bugpoint as the "
251 "\"safe\" backend right now!\n";
252 break;
Chris Lattner7bb11542004-02-18 20:52:02 +0000253 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000254 if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); }
Andrew Trickde86cbd2011-02-08 18:07:10 +0000255
Kalle Raiskilafaa95762010-05-10 07:38:37 +0000256 gcc = GCC::create(Message, GCCBinary, &GCCToolArgv);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000257 if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); }
Misha Brukmana259c9b2003-07-24 21:59:10 +0000258
Chris Lattner4a106452002-12-23 23:50:16 +0000259 // If there was an error creating the selected interpreter, quit with error.
260 return Interpreter == 0;
261}
262
Nick Lewycky22ff7482010-04-12 05:08:25 +0000263/// compileProgram - Try to compile the specified module, returning false and
264/// setting Error if an error occurs. This is used for code generation
265/// crash testing.
Chris Lattnerea9212c2004-02-18 23:25:22 +0000266///
Rafael Espindola248d1c62010-08-05 03:00:22 +0000267void BugDriver::compileProgram(Module *M, std::string *Error) const {
Gabor Greif8ff70c22007-07-04 21:55:50 +0000268 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000269 sys::Path BitcodeFile (OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000270 std::string ErrMsg;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000271 if (BitcodeFile.makeUnique(true, &ErrMsg)) {
Andrew Trickde86cbd2011-02-08 18:07:10 +0000272 errs() << ToolName << ": Error making unique filename: " << ErrMsg
Dan Gohman65f57c22009-07-15 16:35:29 +0000273 << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000274 exit(1);
275 }
Chris Lattner74382b72009-08-23 22:45:37 +0000276 if (writeProgramToFile(BitcodeFile.str(), M)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000277 errs() << ToolName << ": Error emitting bitcode to file '"
Chris Lattner74382b72009-08-23 22:45:37 +0000278 << BitcodeFile.str() << "'!\n";
Chris Lattnerea9212c2004-02-18 23:25:22 +0000279 exit(1);
280 }
281
Nick Lewycky22ff7482010-04-12 05:08:25 +0000282 // Remove the temporary bitcode file when we are done.
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000283 FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000284
285 // Actually compile the program!
Duncan Sands41396302010-05-24 07:49:55 +0000286 Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit);
Chris Lattnerea9212c2004-02-18 23:25:22 +0000287}
288
Chris Lattner4a106452002-12-23 23:50:16 +0000289
290/// executeProgram - This method runs "Program", capturing the output of the
291/// program to a file, returning the filename of the file. A recommended
292/// filename may be optionally specified.
293///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000294std::string BugDriver::executeProgram(const Module *Program,
295 std::string OutputFile,
Gabor Greif8ff70c22007-07-04 21:55:50 +0000296 std::string BitcodeFile,
Chris Lattner769f1fe2003-10-14 21:59:36 +0000297 const std::string &SharedObj,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000298 AbstractInterpreter *AI,
Rafael Espindola13793262010-07-31 14:34:49 +0000299 std::string *Error) const {
Chris Lattner769f1fe2003-10-14 21:59:36 +0000300 if (AI == 0) AI = Interpreter;
301 assert(AI && "Interpreter should have been created already!");
Gabor Greif8ff70c22007-07-04 21:55:50 +0000302 bool CreatedBitcode = false;
Reid Spencer51c5a282006-08-23 20:34:57 +0000303 std::string ErrMsg;
Gabor Greif8ff70c22007-07-04 21:55:50 +0000304 if (BitcodeFile.empty()) {
305 // Emit the program to a bitcode file...
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000306 sys::Path uniqueFilename(OutputPrefix + "-test-program.bc");
Reid Spencer51c5a282006-08-23 20:34:57 +0000307 if (uniqueFilename.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000308 errs() << ToolName << ": Error making unique filename: "
309 << ErrMsg << "!\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000310 exit(1);
311 }
Chris Lattner74382b72009-08-23 22:45:37 +0000312 BitcodeFile = uniqueFilename.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000313
Gabor Greif8ff70c22007-07-04 21:55:50 +0000314 if (writeProgramToFile(BitcodeFile, Program)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000315 errs() << ToolName << ": Error emitting bitcode to file '"
316 << BitcodeFile << "'!\n";
Chris Lattner4a106452002-12-23 23:50:16 +0000317 exit(1);
318 }
Gabor Greif8ff70c22007-07-04 21:55:50 +0000319 CreatedBitcode = true;
Chris Lattner4a106452002-12-23 23:50:16 +0000320 }
321
Gabor Greif8ff70c22007-07-04 21:55:50 +0000322 // Remove the temporary bitcode file when we are done.
Nick Lewycky16350f82010-04-10 23:18:13 +0000323 sys::Path BitcodePath(BitcodeFile);
Michael J. Spencerc9c08fb2011-03-31 13:04:19 +0000324 FileRemover BitcodeFileRemover(BitcodePath.str(),
325 CreatedBitcode && !SaveTemps);
Chris Lattner97092722004-02-18 22:01:21 +0000326
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000327 if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output";
Misha Brukman50733362003-07-24 18:17:43 +0000328
Chris Lattner4a106452002-12-23 23:50:16 +0000329 // Check to see if this is a valid output filename...
Reid Spencer97182982004-12-15 01:53:08 +0000330 sys::Path uniqueFile(OutputFile);
Reid Spencer51c5a282006-08-23 20:34:57 +0000331 if (uniqueFile.makeUnique(true, &ErrMsg)) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000332 errs() << ToolName << ": Error making unique filename: "
333 << ErrMsg << "\n";
Reid Spencer51c5a282006-08-23 20:34:57 +0000334 exit(1);
335 }
Chris Lattner74382b72009-08-23 22:45:37 +0000336 OutputFile = uniqueFile.str();
Chris Lattner4a106452002-12-23 23:50:16 +0000337
Chris Lattner769f1fe2003-10-14 21:59:36 +0000338 // Figure out which shared objects to run, if any.
Chris Lattner7dac6582003-10-14 22:24:31 +0000339 std::vector<std::string> SharedObjs(AdditionalSOs);
Chris Lattner769f1fe2003-10-14 21:59:36 +0000340 if (!SharedObj.empty())
341 SharedObjs.push_back(SharedObj);
342
Nick Lewycky22ff7482010-04-12 05:08:25 +0000343 int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile,
344 Error, AdditionalLinkerArgs, SharedObjs,
Dan Gohman70ef4492008-12-08 04:02:47 +0000345 Timeout, MemoryLimit);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000346 if (!Error->empty())
347 return OutputFile;
Chris Lattner7d91e492004-07-24 07:53:26 +0000348
349 if (RetVal == -1) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000350 errs() << "<timeout>";
Chris Lattner7d91e492004-07-24 07:53:26 +0000351 static bool FirstTimeout = true;
352 if (FirstTimeout) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000353 outs() << "\n"
Chris Lattner7d91e492004-07-24 07:53:26 +0000354 "*** Program execution timed out! This mechanism is designed to handle\n"
355 " programs stuck in infinite loops gracefully. The -timeout option\n"
356 " can be used to change the timeout threshold or disable it completely\n"
357 " (with -timeout=0). This message is only displayed once.\n";
358 FirstTimeout = false;
359 }
360 }
Chris Lattner769f1fe2003-10-14 21:59:36 +0000361
Reid Spencer5e1452c2006-11-28 07:04:10 +0000362 if (AppendProgramExitCode) {
363 std::ofstream outFile(OutputFile.c_str(), std::ios_base::app);
364 outFile << "exit " << RetVal << '\n';
365 outFile.close();
366 }
367
Chris Lattner4a106452002-12-23 23:50:16 +0000368 // Return the filename we captured the output to.
369 return OutputFile;
370}
371
Dan Gohman70ef4492008-12-08 04:02:47 +0000372/// executeProgramSafely - Used to create reference output with the "safe"
Brian Gaekec5cad212004-02-11 18:37:32 +0000373/// backend, if reference output is not provided.
374///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000375std::string BugDriver::executeProgramSafely(const Module *Program,
376 std::string OutputFile,
Rafael Espindola13793262010-07-31 14:34:49 +0000377 std::string *Error) const {
Rafael Espindola10757dd2010-07-30 14:19:00 +0000378 return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error);
Brian Gaekec5cad212004-02-11 18:37:32 +0000379}
Misha Brukman50733362003-07-24 18:17:43 +0000380
Nick Lewycky22ff7482010-04-12 05:08:25 +0000381std::string BugDriver::compileSharedObject(const std::string &BitcodeFile,
382 std::string &Error) {
Misha Brukman50733362003-07-24 18:17:43 +0000383 assert(Interpreter && "Interpreter should have been created already!");
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000384 std::string OutputFile;
Misha Brukman50733362003-07-24 18:17:43 +0000385
Dan Gohman70ef4492008-12-08 04:02:47 +0000386 // Using the known-good backend.
Nick Lewycky22ff7482010-04-12 05:08:25 +0000387 GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile,
388 Error);
389 if (!Error.empty())
390 return "";
Misha Brukman50733362003-07-24 18:17:43 +0000391
Chris Lattnera0f5b152003-10-14 21:09:11 +0000392 std::string SharedObjectFile;
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000393 bool Failure = gcc->MakeSharedObject(OutputFile, FT, SharedObjectFile,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000394 AdditionalLinkerArgs, Error);
395 if (!Error.empty())
396 return "";
397 if (Failure)
Chris Lattnera0f5b152003-10-14 21:09:11 +0000398 exit(1);
Misha Brukman50733362003-07-24 18:17:43 +0000399
400 // Remove the intermediate C file
Rafael Espindolaf656a1d2013-06-17 19:21:38 +0000401 sys::fs::remove(OutputFile);
Misha Brukman50733362003-07-24 18:17:43 +0000402
Chris Lattner6ebe44d2003-10-19 21:54:13 +0000403 return "./" + SharedObjectFile;
Misha Brukman50733362003-07-24 18:17:43 +0000404}
405
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000406/// createReferenceFile - calls compileProgram and then records the output
Andrew Trickde86cbd2011-02-08 18:07:10 +0000407/// into ReferenceOutputFile. Returns true if reference file created, false
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000408/// otherwise. Note: initializeExecutionEnvironment should be called BEFORE
409/// this function.
410///
Chris Lattnerc600f3c2006-09-15 21:29:15 +0000411bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) {
Nick Lewycky22ff7482010-04-12 05:08:25 +0000412 std::string Error;
413 compileProgram(Program, &Error);
414 if (!Error.empty())
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000415 return false;
Nick Lewycky22ff7482010-04-12 05:08:25 +0000416
Rafael Espindola10757dd2010-07-30 14:19:00 +0000417 ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error);
Nick Lewycky22ff7482010-04-12 05:08:25 +0000418 if (!Error.empty()) {
419 errs() << Error;
Dan Gohman70ef4492008-12-08 04:02:47 +0000420 if (Interpreter != SafeInterpreter) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000421 errs() << "*** There is a bug running the \"safe\" backend. Either"
Eric Christophera443e5b2012-03-23 05:50:46 +0000422 << " debug it (for example with the -run-jit bugpoint option,"
423 << " if JIT is being used as the \"safe\" backend), or fix the"
Dan Gohman65f57c22009-07-15 16:35:29 +0000424 << " error some other way.\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000425 }
426 return false;
427 }
Nick Lewycky22ff7482010-04-12 05:08:25 +0000428 outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n";
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000429 return true;
430}
Misha Brukman50733362003-07-24 18:17:43 +0000431
Patrick Jenkins6a3f31c2006-08-15 16:40:49 +0000432/// diffProgram - This method executes the specified module and diffs the
433/// output against the file specified by ReferenceOutputFile. If the output
Nick Lewycky22ff7482010-04-12 05:08:25 +0000434/// is different, 1 is returned. If there is a problem with the code
Andrew Trick7c863eb2011-05-11 16:31:24 +0000435/// generator (e.g., llc crashes), this will set ErrMsg.
Chris Lattner4a106452002-12-23 23:50:16 +0000436///
Rafael Espindola10757dd2010-07-30 14:19:00 +0000437bool BugDriver::diffProgram(const Module *Program,
438 const std::string &BitcodeFile,
Misha Brukman50733362003-07-24 18:17:43 +0000439 const std::string &SharedObject,
Nick Lewycky22ff7482010-04-12 05:08:25 +0000440 bool RemoveBitcode,
Rafael Espindola13793262010-07-31 14:34:49 +0000441 std::string *ErrMsg) const {
Chris Lattner4a106452002-12-23 23:50:16 +0000442 // Execute the program, generating an output file...
Rafael Espindola10757dd2010-07-30 14:19:00 +0000443 sys::Path Output(executeProgram(Program, "", BitcodeFile, SharedObject, 0,
444 ErrMsg));
Nick Lewycky22ff7482010-04-12 05:08:25 +0000445 if (!ErrMsg->empty())
446 return false;
Brian Gaekec5cad212004-02-11 18:37:32 +0000447
Chris Lattner65f62792003-08-01 20:29:45 +0000448 std::string Error;
Chris Lattner4a106452002-12-23 23:50:16 +0000449 bool FilesDifferent = false;
Rafael Espindolabbf97ea2013-06-13 20:41:00 +0000450 if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile,
451 Output.str(),
Chris Lattnera328c512005-01-23 03:45:26 +0000452 AbsTolerance, RelTolerance, &Error)) {
453 if (Diff == 2) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000454 errs() << "While diffing output: " << Error << '\n';
Chris Lattner65f62792003-08-01 20:29:45 +0000455 exit(1);
456 }
457 FilesDifferent = true;
458 }
David Goodwin80becf12009-07-10 21:39:28 +0000459 else {
460 // Remove the generated output if there are no differences.
461 Output.eraseFromDisk();
462 }
Chris Lattner4a106452002-12-23 23:50:16 +0000463
Gabor Greif8ff70c22007-07-04 21:55:50 +0000464 // Remove the bitcode file if we are supposed to.
465 if (RemoveBitcode)
466 sys::Path(BitcodeFile).eraseFromDisk();
Chris Lattner4a106452002-12-23 23:50:16 +0000467 return FilesDifferent;
468}
Misha Brukman91eabc12003-07-28 19:16:14 +0000469
470bool BugDriver::isExecutingJIT() {
471 return InterpreterSel == RunJIT;
472}
Brian Gaeked0fde302003-11-11 22:41:34 +0000473