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