Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===// |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 3da94ae | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 7c0e022 | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file contains code used to execute the program utilizing one of the |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 11 | // various ways of running LLVM bitcode. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 15 | #include "BugDriver.h" |
Chris Lattner | f1b20d8 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 16 | #include "ToolRunner.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 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 Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 22 | #include <fstream> |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 23 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | // OutputType - Allow the user to specify the way code should be run, to test |
| 28 | // for miscompilation. |
| 29 | // |
| 30 | enum OutputType { |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 31 | AutoPick, RunLLI, RunJIT, RunLLC, RunLLCIA, RunCBE, CBE_bug, LLC_Safe, |
| 32 | CompileCustom, Custom |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 33 | }; |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 34 | |
Chris Lattner | a328c51 | 2005-01-23 03:45:26 +0000 | [diff] [blame] | 35 | 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 Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 42 | cl::opt<OutputType> |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 43 | InterpreterSel(cl::desc("Specify the \"test\" i.e. suspect back-end:"), |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 44 | cl::values(clEnumValN(AutoPick, "auto", "Use best guess"), |
Misha Brukman | b687d82 | 2004-04-19 03:12:35 +0000 | [diff] [blame] | 45 | clEnumValN(RunLLI, "run-int", |
| 46 | "Execute with the interpreter"), |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 47 | clEnumValN(RunJIT, "run-jit", "Execute with JIT"), |
| 48 | clEnumValN(RunLLC, "run-llc", "Compile with LLC"), |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 49 | clEnumValN(RunLLCIA, "run-llc-ia", |
| 50 | "Compile with LLC with integrated assembler"), |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 51 | clEnumValN(RunCBE, "run-cbe", "Compile with CBE"), |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 52 | clEnumValN(CBE_bug,"cbe-bug", "Find CBE bugs"), |
Chris Lattner | cd6f46e | 2006-11-09 05:57:53 +0000 | [diff] [blame] | 53 | clEnumValN(LLC_Safe, "llc-safe", "Use LLC for all"), |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 54 | clEnumValN(CompileCustom, "compile-custom", |
| 55 | "Use -compile-command to define a command to " |
| 56 | "compile the bitcode. Useful to avoid linking."), |
Anton Korobeynikov | 9ef7425 | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 57 | clEnumValN(Custom, "run-custom", |
| 58 | "Use -exec-command to define a command to execute " |
| 59 | "the bitcode. Useful for cross-compilation."), |
Chris Lattner | 4d143ee | 2004-07-16 00:08:28 +0000 | [diff] [blame] | 60 | clEnumValEnd), |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 61 | cl::init(AutoPick)); |
Chris Lattner | 3c053a0 | 2003-04-23 20:31:37 +0000 | [diff] [blame] | 62 | |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 63 | cl::opt<OutputType> |
| 64 | SafeInterpreterSel(cl::desc("Specify \"safe\" i.e. known-good backend:"), |
Evan Cheng | 49419e2 | 2009-07-21 19:25:09 +0000 | [diff] [blame] | 65 | cl::values(clEnumValN(AutoPick, "safe-auto", "Use best guess"), |
| 66 | clEnumValN(RunLLC, "safe-run-llc", "Compile with LLC"), |
| 67 | clEnumValN(RunCBE, "safe-run-cbe", "Compile with CBE"), |
| 68 | clEnumValN(Custom, "safe-run-custom", |
| 69 | "Use -exec-command to define a command to execute " |
| 70 | "the bitcode. Useful for cross-compilation."), |
| 71 | clEnumValEnd), |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 72 | cl::init(AutoPick)); |
| 73 | |
| 74 | cl::opt<std::string> |
| 75 | SafeInterpreterPath("safe-path", |
Evan Cheng | 49419e2 | 2009-07-21 19:25:09 +0000 | [diff] [blame] | 76 | cl::desc("Specify the path to the \"safe\" backend program"), |
| 77 | cl::init("")); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 78 | |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 79 | cl::opt<bool> |
Reid Spencer | 5e1452c | 2006-11-28 07:04:10 +0000 | [diff] [blame] | 80 | AppendProgramExitCode("append-exit-code", |
| 81 | cl::desc("Append the exit code to the output so it gets diff'd too"), |
| 82 | cl::init(false)); |
| 83 | |
Chris Lattner | 3c053a0 | 2003-04-23 20:31:37 +0000 | [diff] [blame] | 84 | cl::opt<std::string> |
| 85 | InputFile("input", cl::init("/dev/null"), |
| 86 | cl::desc("Filename to pipe in as stdin (default: /dev/null)")); |
Chris Lattner | 7dac658 | 2003-10-14 22:24:31 +0000 | [diff] [blame] | 87 | |
| 88 | cl::list<std::string> |
| 89 | AdditionalSOs("additional-so", |
| 90 | cl::desc("Additional shared objects to load " |
| 91 | "into executing programs")); |
Chris Lattner | 7d91e49 | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 92 | |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 93 | cl::list<std::string> |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 94 | AdditionalLinkerArgs("Xlinker", |
Reid Spencer | 51ab5c8 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 95 | cl::desc("Additional arguments to pass to the linker")); |
Anton Korobeynikov | 9ef7425 | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 96 | |
| 97 | cl::opt<std::string> |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 98 | CustomCompileCommand("compile-command", cl::init("llc"), |
| 99 | cl::desc("Command to compile the bitcode (use with -compile-custom) " |
| 100 | "(default: llc)")); |
| 101 | |
| 102 | cl::opt<std::string> |
Anton Korobeynikov | 9ef7425 | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 103 | CustomExecCommand("exec-command", cl::init("simulate"), |
| 104 | cl::desc("Command to execute the bitcode (use with -run-custom) " |
| 105 | "(default: simulate)")); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 108 | namespace llvm { |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 109 | // Anything specified after the --args option are taken as arguments to the |
| 110 | // program being debugged. |
| 111 | cl::list<std::string> |
| 112 | InputArgv("args", cl::Positional, cl::desc("<program arguments>..."), |
Chris Lattner | 60083e2 | 2004-05-06 22:05:35 +0000 | [diff] [blame] | 113 | cl::ZeroOrMore, cl::PositionalEatsArgs); |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 114 | |
| 115 | cl::opt<std::string> |
| 116 | OutputPrefix("output-prefix", cl::init("bugpoint"), |
| 117 | cl::desc("Prefix to use for outputs (default: 'bugpoint')")); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 118 | } |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 119 | |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 120 | namespace { |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 121 | cl::list<std::string> |
| 122 | ToolArgv("tool-args", cl::Positional, cl::desc("<tool arguments>..."), |
Chris Lattner | 60083e2 | 2004-05-06 22:05:35 +0000 | [diff] [blame] | 123 | cl::ZeroOrMore, cl::PositionalEatsArgs); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 124 | |
| 125 | cl::list<std::string> |
| 126 | SafeToolArgv("safe-tool-args", cl::Positional, |
| 127 | cl::desc("<safe-tool arguments>..."), |
| 128 | cl::ZeroOrMore, cl::PositionalEatsArgs); |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 129 | |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 130 | cl::opt<std::string> |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 131 | GCCBinary("gcc", cl::init("gcc"), |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 132 | cl::desc("The gcc binary to use. (default 'gcc')")); |
| 133 | |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 134 | cl::list<std::string> |
| 135 | GCCToolArgv("gcc-tool-args", cl::Positional, |
| 136 | cl::desc("<gcc-tool arguments>..."), |
| 137 | cl::ZeroOrMore, cl::PositionalEatsArgs); |
Chris Lattner | fa76183 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 138 | } |
Misha Brukman | 9d679cb | 2003-07-30 20:15:44 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
| 141 | // BugDriver method implementation |
| 142 | // |
| 143 | |
| 144 | /// initializeExecutionEnvironment - This method is used to set up the |
| 145 | /// environment for executing LLVM programs. |
| 146 | /// |
| 147 | bool BugDriver::initializeExecutionEnvironment() { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 148 | outs() << "Initializing execution environment: "; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 149 | |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 150 | // Create an instance of the AbstractInterpreter interface as specified on |
| 151 | // the command line |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 152 | SafeInterpreter = 0; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 153 | std::string Message; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 154 | |
Chris Lattner | cc876a7 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 155 | switch (InterpreterSel) { |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 156 | case AutoPick: |
| 157 | InterpreterSel = RunCBE; |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 158 | Interpreter = |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 159 | AbstractInterpreter::createCBE(getToolName(), Message, GCCBinary, |
| 160 | &ToolArgv, &GCCToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 161 | if (!Interpreter) { |
| 162 | InterpreterSel = RunJIT; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 163 | Interpreter = AbstractInterpreter::createJIT(getToolName(), Message, |
| 164 | &ToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 165 | } |
| 166 | if (!Interpreter) { |
| 167 | InterpreterSel = RunLLC; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 168 | Interpreter = AbstractInterpreter::createLLC(getToolName(), Message, |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 169 | GCCBinary, &ToolArgv, |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 170 | &GCCToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 171 | } |
| 172 | if (!Interpreter) { |
| 173 | InterpreterSel = RunLLI; |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 174 | Interpreter = AbstractInterpreter::createLLI(getToolName(), Message, |
| 175 | &ToolArgv); |
Brian Gaeke | b5ee509 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 176 | } |
| 177 | if (!Interpreter) { |
| 178 | InterpreterSel = AutoPick; |
| 179 | Message = "Sorry, I can't automatically select an interpreter!\n"; |
| 180 | } |
| 181 | break; |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 182 | case RunLLI: |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 183 | Interpreter = AbstractInterpreter::createLLI(getToolName(), Message, |
| 184 | &ToolArgv); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 185 | break; |
| 186 | case RunLLC: |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 187 | case RunLLCIA: |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 188 | case LLC_Safe: |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 189 | Interpreter = AbstractInterpreter::createLLC(getToolName(), Message, |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 190 | GCCBinary, &ToolArgv, |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 191 | &GCCToolArgv, |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 192 | InterpreterSel == RunLLCIA); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 193 | break; |
| 194 | case RunJIT: |
Brian Gaeke | 636df3d | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 195 | Interpreter = AbstractInterpreter::createJIT(getToolName(), Message, |
| 196 | &ToolArgv); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 197 | break; |
| 198 | case RunCBE: |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 199 | case CBE_bug: |
| 200 | Interpreter = AbstractInterpreter::createCBE(getToolName(), Message, |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 201 | GCCBinary, &ToolArgv, |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 202 | &GCCToolArgv); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 203 | break; |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 204 | case CompileCustom: |
| 205 | Interpreter = |
| 206 | AbstractInterpreter::createCustomCompiler(Message, CustomCompileCommand); |
| 207 | break; |
Anton Korobeynikov | 9ef7425 | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 208 | case Custom: |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 209 | Interpreter = |
| 210 | AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand); |
Anton Korobeynikov | 9ef7425 | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 211 | break; |
Chris Lattner | cc876a7 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 212 | default: |
Misha Brukman | 4148556 | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 213 | Message = "Sorry, this back-end is not supported by bugpoint right now!\n"; |
Chris Lattner | cc876a7 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 214 | break; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 215 | } |
Matthijs Kooijman | ad6996d | 2008-06-12 13:09:43 +0000 | [diff] [blame] | 216 | if (!Interpreter) |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 217 | errs() << Message; |
Matthijs Kooijman | ad6996d | 2008-06-12 13:09:43 +0000 | [diff] [blame] | 218 | else // Display informational messages on stdout instead of stderr |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 219 | outs() << Message; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 220 | |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 221 | std::string Path = SafeInterpreterPath; |
| 222 | if (Path.empty()) |
| 223 | Path = getToolName(); |
| 224 | std::vector<std::string> SafeToolArgs = SafeToolArgv; |
| 225 | switch (SafeInterpreterSel) { |
| 226 | case AutoPick: |
| 227 | // In "cbe-bug" mode, default to using LLC as the "safe" backend. |
| 228 | if (!SafeInterpreter && |
| 229 | InterpreterSel == CBE_bug) { |
| 230 | SafeInterpreterSel = RunLLC; |
| 231 | SafeToolArgs.push_back("--relocation-model=pic"); |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 232 | SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 233 | GCCBinary, |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 234 | &SafeToolArgs, |
| 235 | &GCCToolArgv); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // In "llc-safe" mode, default to using LLC as the "safe" backend. |
| 239 | if (!SafeInterpreter && |
| 240 | InterpreterSel == LLC_Safe) { |
| 241 | SafeInterpreterSel = RunLLC; |
| 242 | SafeToolArgs.push_back("--relocation-model=pic"); |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 243 | SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 244 | GCCBinary, |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 245 | &SafeToolArgs, |
| 246 | &GCCToolArgv); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Pick a backend that's different from the test backend. The JIT and |
| 250 | // LLC backends share a lot of code, so prefer to use the CBE as the |
| 251 | // safe back-end when testing them. |
| 252 | if (!SafeInterpreter && |
| 253 | InterpreterSel != RunCBE) { |
| 254 | SafeInterpreterSel = RunCBE; |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 255 | SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message, |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 256 | GCCBinary, |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 257 | &SafeToolArgs, |
| 258 | &GCCToolArgv); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 259 | } |
| 260 | if (!SafeInterpreter && |
| 261 | InterpreterSel != RunLLC && |
| 262 | InterpreterSel != RunJIT) { |
| 263 | SafeInterpreterSel = RunLLC; |
| 264 | SafeToolArgs.push_back("--relocation-model=pic"); |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 265 | SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 266 | GCCBinary, |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 267 | &SafeToolArgs, |
| 268 | &GCCToolArgv); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 269 | } |
| 270 | if (!SafeInterpreter) { |
| 271 | SafeInterpreterSel = AutoPick; |
| 272 | Message = "Sorry, I can't automatically select an interpreter!\n"; |
| 273 | } |
| 274 | break; |
| 275 | case RunLLC: |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 276 | case RunLLCIA: |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 277 | SafeToolArgs.push_back("--relocation-model=pic"); |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 278 | SafeInterpreter = AbstractInterpreter::createLLC(Path.c_str(), Message, |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 279 | GCCBinary, &SafeToolArgs, |
Chris Lattner | 5001042 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 280 | &GCCToolArgv, |
| 281 | SafeInterpreterSel == RunLLCIA); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 282 | break; |
| 283 | case RunCBE: |
Dan Gohman | 197f728 | 2009-08-05 20:21:17 +0000 | [diff] [blame] | 284 | SafeInterpreter = AbstractInterpreter::createCBE(Path.c_str(), Message, |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 285 | GCCBinary, &SafeToolArgs, |
Bill Wendling | 38efa38 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 286 | &GCCToolArgv); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 287 | break; |
| 288 | case Custom: |
Andrew Trick | f73311b | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 289 | SafeInterpreter = |
| 290 | AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand); |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 291 | break; |
| 292 | default: |
| 293 | Message = "Sorry, this back-end is not supported by bugpoint as the " |
| 294 | "\"safe\" backend right now!\n"; |
| 295 | break; |
Chris Lattner | 7bb1154 | 2004-02-18 20:52:02 +0000 | [diff] [blame] | 296 | } |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 297 | if (!SafeInterpreter) { outs() << Message << "\nExiting.\n"; exit(1); } |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 298 | |
Kalle Raiskila | faa9576 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 299 | gcc = GCC::create(Message, GCCBinary, &GCCToolArgv); |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 300 | if (!gcc) { outs() << Message << "\nExiting.\n"; exit(1); } |
Misha Brukman | a259c9b | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 302 | // If there was an error creating the selected interpreter, quit with error. |
| 303 | return Interpreter == 0; |
| 304 | } |
| 305 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 306 | /// compileProgram - Try to compile the specified module, returning false and |
| 307 | /// setting Error if an error occurs. This is used for code generation |
| 308 | /// crash testing. |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 309 | /// |
Rafael Espindola | 248d1c6 | 2010-08-05 03:00:22 +0000 | [diff] [blame] | 310 | void BugDriver::compileProgram(Module *M, std::string *Error) const { |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 311 | // Emit the program to a bitcode file... |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 312 | sys::Path BitcodeFile (OutputPrefix + "-test-program.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 313 | std::string ErrMsg; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 314 | if (BitcodeFile.makeUnique(true, &ErrMsg)) { |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 315 | errs() << ToolName << ": Error making unique filename: " << ErrMsg |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 316 | << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 317 | exit(1); |
| 318 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 319 | if (writeProgramToFile(BitcodeFile.str(), M)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 320 | errs() << ToolName << ": Error emitting bitcode to file '" |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 321 | << BitcodeFile.str() << "'!\n"; |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 322 | exit(1); |
| 323 | } |
| 324 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 325 | // Remove the temporary bitcode file when we are done. |
Michael J. Spencer | c9c08fb | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 326 | FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps); |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 327 | |
| 328 | // Actually compile the program! |
Duncan Sands | 4139630 | 2010-05-24 07:49:55 +0000 | [diff] [blame] | 329 | Interpreter->compileProgram(BitcodeFile.str(), Error, Timeout, MemoryLimit); |
Chris Lattner | ea9212c | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 332 | |
| 333 | /// executeProgram - This method runs "Program", capturing the output of the |
| 334 | /// program to a file, returning the filename of the file. A recommended |
| 335 | /// filename may be optionally specified. |
| 336 | /// |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 337 | std::string BugDriver::executeProgram(const Module *Program, |
| 338 | std::string OutputFile, |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 339 | std::string BitcodeFile, |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 340 | const std::string &SharedObj, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 341 | AbstractInterpreter *AI, |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 342 | std::string *Error) const { |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 343 | if (AI == 0) AI = Interpreter; |
| 344 | assert(AI && "Interpreter should have been created already!"); |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 345 | bool CreatedBitcode = false; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 346 | std::string ErrMsg; |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 347 | if (BitcodeFile.empty()) { |
| 348 | // Emit the program to a bitcode file... |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 349 | sys::Path uniqueFilename(OutputPrefix + "-test-program.bc"); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 350 | if (uniqueFilename.makeUnique(true, &ErrMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 351 | errs() << ToolName << ": Error making unique filename: " |
| 352 | << ErrMsg << "!\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 353 | exit(1); |
| 354 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 355 | BitcodeFile = uniqueFilename.str(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 356 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 357 | if (writeProgramToFile(BitcodeFile, Program)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 358 | errs() << ToolName << ": Error emitting bitcode to file '" |
| 359 | << BitcodeFile << "'!\n"; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 360 | exit(1); |
| 361 | } |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 362 | CreatedBitcode = true; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 365 | // Remove the temporary bitcode file when we are done. |
Nick Lewycky | 16350f8 | 2010-04-10 23:18:13 +0000 | [diff] [blame] | 366 | sys::Path BitcodePath(BitcodeFile); |
Michael J. Spencer | c9c08fb | 2011-03-31 13:04:19 +0000 | [diff] [blame] | 367 | FileRemover BitcodeFileRemover(BitcodePath.str(), |
| 368 | CreatedBitcode && !SaveTemps); |
Chris Lattner | 9709272 | 2004-02-18 22:01:21 +0000 | [diff] [blame] | 369 | |
Daniel Dunbar | 68ccdaa | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 370 | if (OutputFile.empty()) OutputFile = OutputPrefix + "-execution-output"; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 372 | // Check to see if this is a valid output filename... |
Reid Spencer | 9718298 | 2004-12-15 01:53:08 +0000 | [diff] [blame] | 373 | sys::Path uniqueFile(OutputFile); |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 374 | if (uniqueFile.makeUnique(true, &ErrMsg)) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 375 | errs() << ToolName << ": Error making unique filename: " |
| 376 | << ErrMsg << "\n"; |
Reid Spencer | 51c5a28 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 377 | exit(1); |
| 378 | } |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 379 | OutputFile = uniqueFile.str(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 381 | // Figure out which shared objects to run, if any. |
Chris Lattner | 7dac658 | 2003-10-14 22:24:31 +0000 | [diff] [blame] | 382 | std::vector<std::string> SharedObjs(AdditionalSOs); |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 383 | if (!SharedObj.empty()) |
| 384 | SharedObjs.push_back(SharedObj); |
| 385 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 386 | int RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, OutputFile, |
| 387 | Error, AdditionalLinkerArgs, SharedObjs, |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 388 | Timeout, MemoryLimit); |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 389 | if (!Error->empty()) |
| 390 | return OutputFile; |
Chris Lattner | 7d91e49 | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 391 | |
| 392 | if (RetVal == -1) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 393 | errs() << "<timeout>"; |
Chris Lattner | 7d91e49 | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 394 | static bool FirstTimeout = true; |
| 395 | if (FirstTimeout) { |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 396 | outs() << "\n" |
Chris Lattner | 7d91e49 | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 397 | "*** Program execution timed out! This mechanism is designed to handle\n" |
| 398 | " programs stuck in infinite loops gracefully. The -timeout option\n" |
| 399 | " can be used to change the timeout threshold or disable it completely\n" |
| 400 | " (with -timeout=0). This message is only displayed once.\n"; |
| 401 | FirstTimeout = false; |
| 402 | } |
| 403 | } |
Chris Lattner | 769f1fe | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 404 | |
Reid Spencer | 5e1452c | 2006-11-28 07:04:10 +0000 | [diff] [blame] | 405 | if (AppendProgramExitCode) { |
| 406 | std::ofstream outFile(OutputFile.c_str(), std::ios_base::app); |
| 407 | outFile << "exit " << RetVal << '\n'; |
| 408 | outFile.close(); |
| 409 | } |
| 410 | |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 411 | // Return the filename we captured the output to. |
| 412 | return OutputFile; |
| 413 | } |
| 414 | |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 415 | /// executeProgramSafely - Used to create reference output with the "safe" |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 416 | /// backend, if reference output is not provided. |
| 417 | /// |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 418 | std::string BugDriver::executeProgramSafely(const Module *Program, |
| 419 | std::string OutputFile, |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 420 | std::string *Error) const { |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 421 | return executeProgram(Program, OutputFile, "", "", SafeInterpreter, Error); |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 422 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 423 | |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 424 | std::string BugDriver::compileSharedObject(const std::string &BitcodeFile, |
| 425 | std::string &Error) { |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 426 | assert(Interpreter && "Interpreter should have been created already!"); |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 427 | sys::Path OutputFile; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 428 | |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 429 | // Using the known-good backend. |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 430 | GCC::FileType FT = SafeInterpreter->OutputCode(BitcodeFile, OutputFile, |
| 431 | Error); |
| 432 | if (!Error.empty()) |
| 433 | return ""; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 434 | |
Chris Lattner | a0f5b15 | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 435 | std::string SharedObjectFile; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 436 | bool Failure = gcc->MakeSharedObject(OutputFile.str(), FT, SharedObjectFile, |
| 437 | AdditionalLinkerArgs, Error); |
| 438 | if (!Error.empty()) |
| 439 | return ""; |
| 440 | if (Failure) |
Chris Lattner | a0f5b15 | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 441 | exit(1); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 442 | |
| 443 | // Remove the intermediate C file |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 444 | OutputFile.eraseFromDisk(); |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 6ebe44d | 2003-10-19 21:54:13 +0000 | [diff] [blame] | 446 | return "./" + SharedObjectFile; |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 449 | /// createReferenceFile - calls compileProgram and then records the output |
Andrew Trick | de86cbd | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 450 | /// into ReferenceOutputFile. Returns true if reference file created, false |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 451 | /// otherwise. Note: initializeExecutionEnvironment should be called BEFORE |
| 452 | /// this function. |
| 453 | /// |
Chris Lattner | c600f3c | 2006-09-15 21:29:15 +0000 | [diff] [blame] | 454 | bool BugDriver::createReferenceFile(Module *M, const std::string &Filename) { |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 455 | std::string Error; |
| 456 | compileProgram(Program, &Error); |
| 457 | if (!Error.empty()) |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 458 | return false; |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 459 | |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 460 | ReferenceOutputFile = executeProgramSafely(Program, Filename, &Error); |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 461 | if (!Error.empty()) { |
| 462 | errs() << Error; |
Dan Gohman | 70ef449 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 463 | if (Interpreter != SafeInterpreter) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 464 | errs() << "*** There is a bug running the \"safe\" backend. Either" |
| 465 | << " debug it (for example with the -run-cbe bugpoint option," |
| 466 | << " if CBE is being used as the \"safe\" backend), or fix the" |
| 467 | << " error some other way.\n"; |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 468 | } |
| 469 | return false; |
| 470 | } |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 471 | outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n"; |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 472 | return true; |
| 473 | } |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 474 | |
Patrick Jenkins | 6a3f31c | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 475 | /// diffProgram - This method executes the specified module and diffs the |
| 476 | /// output against the file specified by ReferenceOutputFile. If the output |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 477 | /// is different, 1 is returned. If there is a problem with the code |
Andrew Trick | 7c863eb | 2011-05-11 16:31:24 +0000 | [diff] [blame] | 478 | /// generator (e.g., llc crashes), this will set ErrMsg. |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 479 | /// |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 480 | bool BugDriver::diffProgram(const Module *Program, |
| 481 | const std::string &BitcodeFile, |
Misha Brukman | 5073336 | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 482 | const std::string &SharedObject, |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 483 | bool RemoveBitcode, |
Rafael Espindola | 1379326 | 2010-07-31 14:34:49 +0000 | [diff] [blame] | 484 | std::string *ErrMsg) const { |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 485 | // Execute the program, generating an output file... |
Rafael Espindola | 10757dd | 2010-07-30 14:19:00 +0000 | [diff] [blame] | 486 | sys::Path Output(executeProgram(Program, "", BitcodeFile, SharedObject, 0, |
| 487 | ErrMsg)); |
Nick Lewycky | 22ff748 | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 488 | if (!ErrMsg->empty()) |
| 489 | return false; |
Brian Gaeke | c5cad21 | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 490 | |
Chris Lattner | 65f6279 | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 491 | std::string Error; |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 492 | bool FilesDifferent = false; |
Chris Lattner | a328c51 | 2005-01-23 03:45:26 +0000 | [diff] [blame] | 493 | if (int Diff = DiffFilesWithTolerance(sys::Path(ReferenceOutputFile), |
Chris Lattner | 74382b7 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 494 | sys::Path(Output.str()), |
Chris Lattner | a328c51 | 2005-01-23 03:45:26 +0000 | [diff] [blame] | 495 | AbsTolerance, RelTolerance, &Error)) { |
| 496 | if (Diff == 2) { |
Dan Gohman | 65f57c2 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 497 | errs() << "While diffing output: " << Error << '\n'; |
Chris Lattner | 65f6279 | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 498 | exit(1); |
| 499 | } |
| 500 | FilesDifferent = true; |
| 501 | } |
David Goodwin | 80becf1 | 2009-07-10 21:39:28 +0000 | [diff] [blame] | 502 | else { |
| 503 | // Remove the generated output if there are no differences. |
| 504 | Output.eraseFromDisk(); |
| 505 | } |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 506 | |
Gabor Greif | 8ff70c2 | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 507 | // Remove the bitcode file if we are supposed to. |
| 508 | if (RemoveBitcode) |
| 509 | sys::Path(BitcodeFile).eraseFromDisk(); |
Chris Lattner | 4a10645 | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 510 | return FilesDifferent; |
| 511 | } |
Misha Brukman | 91eabc1 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 512 | |
| 513 | bool BugDriver::isExecutingJIT() { |
| 514 | return InterpreterSel == RunJIT; |
| 515 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 516 | |