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