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