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