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