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