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