Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 1 | //===- ExecutionDriver.cpp - Allow execution of LLVM program --------------===// |
Misha Brukman | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 345353d | 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 | 650ba8e | 2005-04-22 00:00:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | de4aa4c | 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 | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 11 | // various ways of running LLVM bitcode. |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 15 | #include "BugDriver.h" |
Chris Lattner | ffac286 | 2006-06-06 22:30:59 +0000 | [diff] [blame] | 16 | #include "ToolRunner.h" |
Reid Spencer | 7c16caa | 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 | 8a5d043 | 2015-10-14 19:48:01 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Program.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | c521f54 | 2009-08-23 22:45:37 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 23 | #include <fstream> |
Reid Spencer | c3065b7 | 2006-06-06 00:00:42 +0000 | [diff] [blame] | 24 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 27 | namespace { |
Justin Bogner | 8d0a081 | 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 | 5bc6a8f | 2003-09-29 22:40:52 +0000 | [diff] [blame] | 41 | |
Justin Bogner | 8d0a081 | 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 | ece10a4 | 2005-01-23 03:45:26 +0000 | [diff] [blame] | 48 | |
Justin Bogner | 8d0a081 | 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 | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 63 | "the bitcode. Useful for cross-compilation.")), |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 64 | cl::init(AutoPick)); |
Chris Lattner | 68efaa7 | 2003-04-23 20:31:37 +0000 | [diff] [blame] | 65 | |
Justin Bogner | 8d0a081 | 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 | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 72 | "the bitcode. Useful for cross-compilation.")), |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 73 | cl::init(AutoPick)); |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 74 | |
Justin Bogner | 8d0a081 | 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 | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 78 | |
Justin Bogner | 8d0a081 | 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 | d077fe7 | 2006-11-28 07:04:10 +0000 | [diff] [blame] | 83 | |
Justin Bogner | 8d0a081 | 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 | dc92fa6 | 2003-10-14 22:24:31 +0000 | [diff] [blame] | 87 | |
Justin Bogner | 8d0a081 | 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 | 4e0969b | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 91 | |
Justin Bogner | 8d0a081 | 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 | c53565c | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 94 | |
Justin Bogner | 8d0a081 | 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 | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 99 | |
Justin Bogner | 8d0a081 | 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 | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 106 | namespace llvm { |
Justin Bogner | 8d0a081 | 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 | a53337f | 2009-09-07 19:26:11 +0000 | [diff] [blame] | 112 | |
Justin Bogner | 8d0a081 | 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 | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 116 | } |
Brian Gaeke | 4a278f0 | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 117 | |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 118 | namespace { |
Justin Bogner | 8d0a081 | 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 | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 122 | |
Justin Bogner | 8d0a081 | 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 | bf5d827 | 2009-03-02 23:13:18 +0000 | [diff] [blame] | 126 | |
Justin Bogner | 8d0a081 | 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 | 6be5829 | 2010-05-10 07:38:37 +0000 | [diff] [blame] | 129 | |
Justin Bogner | 8d0a081 | 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 | 2f1aa11 | 2004-01-14 03:38:37 +0000 | [diff] [blame] | 133 | } |
Misha Brukman | 40feb36 | 2003-07-30 20:15:44 +0000 | [diff] [blame] | 134 | |
Chris Lattner | de4aa4c | 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 | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 142 | Error BugDriver::initializeExecutionEnvironment() { |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 143 | outs() << "Initializing execution environment: "; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 144 | |
Misha Brukman | 5bc6a8f | 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 | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 147 | SafeInterpreter = nullptr; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 148 | std::string Message; |
Brian Gaeke | 4a278f0 | 2004-05-04 21:09:16 +0000 | [diff] [blame] | 149 | |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 150 | if (CCBinary.empty()) { |
Davide Italiano | 8a5d043 | 2015-10-14 19:48:01 +0000 | [diff] [blame] | 151 | if (sys::findProgramByName("clang")) |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 152 | CCBinary = "clang"; |
Davide Italiano | 8a5d043 | 2015-10-14 19:48:01 +0000 | [diff] [blame] | 153 | else |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 154 | CCBinary = "gcc"; |
Davide Italiano | 8a5d043 | 2015-10-14 19:48:01 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 7709ec5 | 2003-05-03 03:19:41 +0000 | [diff] [blame] | 157 | switch (InterpreterSel) { |
Brian Gaeke | 9a0bdb1 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 158 | case AutoPick: |
Brian Gaeke | 9a0bdb1 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 159 | if (!Interpreter) { |
| 160 | InterpreterSel = RunJIT; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 161 | Interpreter = |
| 162 | AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv); |
Brian Gaeke | 9a0bdb1 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 163 | } |
| 164 | if (!Interpreter) { |
| 165 | InterpreterSel = RunLLC; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 166 | Interpreter = AbstractInterpreter::createLLC( |
| 167 | getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv); |
Brian Gaeke | 9a0bdb1 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 168 | } |
| 169 | if (!Interpreter) { |
| 170 | InterpreterSel = RunLLI; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 171 | Interpreter = |
| 172 | AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv); |
Brian Gaeke | 9a0bdb1 | 2003-10-21 17:41:35 +0000 | [diff] [blame] | 173 | } |
| 174 | if (!Interpreter) { |
| 175 | InterpreterSel = AutoPick; |
| 176 | Message = "Sorry, I can't automatically select an interpreter!\n"; |
| 177 | } |
| 178 | break; |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 179 | case RunLLI: |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 180 | Interpreter = |
| 181 | AbstractInterpreter::createLLI(getToolName(), Message, &ToolArgv); |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 182 | break; |
| 183 | case RunLLC: |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 184 | case RunLLCIA: |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 185 | case LLC_Safe: |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 186 | Interpreter = AbstractInterpreter::createLLC( |
| 187 | getToolName(), Message, CCBinary, &ToolArgv, &CCToolArgv, |
| 188 | InterpreterSel == RunLLCIA); |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 189 | break; |
| 190 | case RunJIT: |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 191 | Interpreter = |
| 192 | AbstractInterpreter::createJIT(getToolName(), Message, &ToolArgv); |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 193 | break; |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 194 | case CompileCustom: |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 195 | Interpreter = AbstractInterpreter::createCustomCompiler( |
| 196 | Message, CustomCompileCommand); |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 197 | break; |
Anton Korobeynikov | c53565c | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 198 | case Custom: |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 199 | Interpreter = |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 200 | AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand); |
Anton Korobeynikov | c53565c | 2008-04-28 20:53:48 +0000 | [diff] [blame] | 201 | break; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 202 | } |
Matthijs Kooijman | 3bb1276 | 2008-06-12 13:09:43 +0000 | [diff] [blame] | 203 | if (!Interpreter) |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 204 | errs() << Message; |
Matthijs Kooijman | 3bb1276 | 2008-06-12 13:09:43 +0000 | [diff] [blame] | 205 | else // Display informational messages on stdout instead of stderr |
Dan Gohman | ee05152 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 206 | outs() << Message; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 207 | |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 208 | std::string Path = SafeInterpreterPath; |
| 209 | if (Path.empty()) |
| 210 | Path = getToolName(); |
| 211 | std::vector<std::string> SafeToolArgs = SafeToolArgv; |
| 212 | switch (SafeInterpreterSel) { |
| 213 | case AutoPick: |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 214 | // In "llc-safe" mode, default to using LLC as the "safe" backend. |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 215 | if (!SafeInterpreter && InterpreterSel == LLC_Safe) { |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 216 | SafeInterpreterSel = RunLLC; |
| 217 | SafeToolArgs.push_back("--relocation-model=pic"); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 218 | SafeInterpreter = AbstractInterpreter::createLLC( |
| 219 | Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv); |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 222 | if (!SafeInterpreter && InterpreterSel != RunLLC && |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 223 | InterpreterSel != RunJIT) { |
| 224 | SafeInterpreterSel = RunLLC; |
| 225 | SafeToolArgs.push_back("--relocation-model=pic"); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 226 | SafeInterpreter = AbstractInterpreter::createLLC( |
| 227 | Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv); |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 228 | } |
| 229 | if (!SafeInterpreter) { |
| 230 | SafeInterpreterSel = AutoPick; |
Saleem Abdulrasool | 5f8609b | 2013-01-24 16:49:14 +0000 | [diff] [blame] | 231 | Message = "Sorry, I can't automatically select a safe interpreter!\n"; |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 232 | } |
| 233 | break; |
| 234 | case RunLLC: |
Chris Lattner | fd38132 | 2010-03-16 06:41:47 +0000 | [diff] [blame] | 235 | case RunLLCIA: |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 236 | SafeToolArgs.push_back("--relocation-model=pic"); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 237 | SafeInterpreter = AbstractInterpreter::createLLC( |
| 238 | Path.c_str(), Message, CCBinary, &SafeToolArgs, &CCToolArgv, |
| 239 | SafeInterpreterSel == RunLLCIA); |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 240 | break; |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 241 | case Custom: |
Andrew Trick | 8665d59 | 2011-02-08 18:20:48 +0000 | [diff] [blame] | 242 | SafeInterpreter = |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 243 | AbstractInterpreter::createCustomExecutor(Message, CustomExecCommand); |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 244 | break; |
| 245 | default: |
| 246 | Message = "Sorry, this back-end is not supported by bugpoint as the " |
| 247 | "\"safe\" backend right now!\n"; |
| 248 | break; |
Chris Lattner | 898de4a | 2004-02-18 20:52:02 +0000 | [diff] [blame] | 249 | } |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 250 | if (!SafeInterpreter) { |
| 251 | outs() << Message << "\nExiting.\n"; |
| 252 | exit(1); |
| 253 | } |
Andrew Trick | 69a963e | 2011-02-08 18:07:10 +0000 | [diff] [blame] | 254 | |
Davide Italiano | ab25621 | 2015-10-14 20:29:54 +0000 | [diff] [blame] | 255 | cc = CC::create(Message, CCBinary, &CCToolArgv); |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 256 | if (!cc) { |
| 257 | outs() << Message << "\nExiting.\n"; |
| 258 | exit(1); |
| 259 | } |
Misha Brukman | 0fd3172 | 2003-07-24 21:59:10 +0000 | [diff] [blame] | 260 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 261 | // If there was an error creating the selected interpreter, quit with error. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 262 | if (Interpreter == nullptr) |
| 263 | return make_error<StringError>("Failed to init execution environment", |
| 264 | inconvertibleErrorCode()); |
| 265 | return Error::success(); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 268 | /// Try to compile the specified module, returning false and setting Error if an |
| 269 | /// error occurs. This is used for code generation crash testing. |
| 270 | Error BugDriver::compileProgram(Module &M) const { |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 271 | // Emit the program to a bitcode file... |
Rafael Espindola | b60bb69 | 2017-11-16 21:40:10 +0000 | [diff] [blame] | 272 | auto Temp = |
| 273 | sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc"); |
| 274 | if (!Temp) { |
| 275 | errs() << ToolName |
| 276 | << ": Error making unique filename: " << toString(Temp.takeError()) |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 277 | << "\n"; |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 278 | exit(1); |
| 279 | } |
Rafael Espindola | b60bb69 | 2017-11-16 21:40:10 +0000 | [diff] [blame] | 280 | DiscardTemp Discard{*Temp}; |
| 281 | if (writeProgramToFile(Temp->FD, M)) { |
| 282 | errs() << ToolName << ": Error emitting bitcode to file '" << Temp->TmpName |
Rafael Espindola | becba2b | 2013-06-18 16:14:09 +0000 | [diff] [blame] | 283 | << "'!\n"; |
Chris Lattner | 96d41dd | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 284 | exit(1); |
| 285 | } |
| 286 | |
Chris Lattner | 96d41dd | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 287 | // Actually compile the program! |
Rafael Espindola | b60bb69 | 2017-11-16 21:40:10 +0000 | [diff] [blame] | 288 | return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit); |
Chris Lattner | 96d41dd | 2004-02-18 23:25:22 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 291 | /// This method runs "Program", capturing the output of the program to a file, |
| 292 | /// returning the filename of the file. A recommended filename may be |
| 293 | /// optionally specified. |
| 294 | Expected<std::string> BugDriver::executeProgram(const Module &Program, |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 295 | std::string OutputFile, |
| 296 | std::string BitcodeFile, |
| 297 | const std::string &SharedObj, |
| 298 | AbstractInterpreter *AI) const { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 299 | if (!AI) |
| 300 | AI = Interpreter; |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 301 | assert(AI && "Interpreter should have been created already!"); |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 302 | if (BitcodeFile.empty()) { |
| 303 | // Emit the program to a bitcode file... |
Rafael Espindola | f9b2c73 | 2017-11-16 21:53:51 +0000 | [diff] [blame] | 304 | auto File = |
| 305 | sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc"); |
| 306 | if (!File) { |
| 307 | errs() << ToolName |
| 308 | << ": Error making unique filename: " << toString(File.takeError()) |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 309 | << "!\n"; |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 310 | exit(1); |
| 311 | } |
Rafael Espindola | f9b2c73 | 2017-11-16 21:53:51 +0000 | [diff] [blame] | 312 | DiscardTemp Discard{*File}; |
| 313 | BitcodeFile = File->TmpName; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 314 | |
Rafael Espindola | f9b2c73 | 2017-11-16 21:53:51 +0000 | [diff] [blame] | 315 | if (writeProgramToFile(File->FD, Program)) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 316 | errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile |
| 317 | << "'!\n"; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 318 | exit(1); |
| 319 | } |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 322 | if (OutputFile.empty()) |
| 323 | OutputFile = OutputPrefix + "-execution-output-%%%%%%%"; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 324 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 325 | // Check to see if this is a valid output filename... |
Rafael Espindola | becba2b | 2013-06-18 16:14:09 +0000 | [diff] [blame] | 326 | SmallString<128> UniqueFile; |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 327 | std::error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile); |
Rafael Espindola | becba2b | 2013-06-18 16:14:09 +0000 | [diff] [blame] | 328 | if (EC) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 329 | errs() << ToolName << ": Error making unique filename: " << EC.message() |
| 330 | << "\n"; |
Reid Spencer | e4ca722 | 2006-08-23 20:34:57 +0000 | [diff] [blame] | 331 | exit(1); |
| 332 | } |
Rafael Espindola | becba2b | 2013-06-18 16:14:09 +0000 | [diff] [blame] | 333 | OutputFile = UniqueFile.str(); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 335 | // Figure out which shared objects to run, if any. |
Chris Lattner | dc92fa6 | 2003-10-14 22:24:31 +0000 | [diff] [blame] | 336 | std::vector<std::string> SharedObjs(AdditionalSOs); |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 337 | if (!SharedObj.empty()) |
| 338 | SharedObjs.push_back(SharedObj); |
| 339 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 340 | Expected<int> RetVal = AI->ExecuteProgram(BitcodeFile, InputArgv, InputFile, |
| 341 | OutputFile, AdditionalLinkerArgs, |
| 342 | SharedObjs, Timeout, MemoryLimit); |
| 343 | if (Error E = RetVal.takeError()) |
| 344 | return std::move(E); |
Chris Lattner | 4e0969b | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 345 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 346 | if (*RetVal == -1) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 347 | errs() << "<timeout>"; |
Chris Lattner | 4e0969b | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 348 | static bool FirstTimeout = true; |
| 349 | if (FirstTimeout) { |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 350 | outs() |
| 351 | << "\n" |
| 352 | "*** Program execution timed out! This mechanism is designed to " |
| 353 | "handle\n" |
| 354 | " programs stuck in infinite loops gracefully. The -timeout " |
| 355 | "option\n" |
| 356 | " can be used to change the timeout threshold or disable it " |
| 357 | "completely\n" |
| 358 | " (with -timeout=0). This message is only displayed once.\n"; |
Chris Lattner | 4e0969b | 2004-07-24 07:53:26 +0000 | [diff] [blame] | 359 | FirstTimeout = false; |
| 360 | } |
| 361 | } |
Chris Lattner | 3f6e522 | 2003-10-14 21:59:36 +0000 | [diff] [blame] | 362 | |
Reid Spencer | d077fe7 | 2006-11-28 07:04:10 +0000 | [diff] [blame] | 363 | if (AppendProgramExitCode) { |
| 364 | std::ofstream outFile(OutputFile.c_str(), std::ios_base::app); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 365 | outFile << "exit " << *RetVal << '\n'; |
Reid Spencer | d077fe7 | 2006-11-28 07:04:10 +0000 | [diff] [blame] | 366 | outFile.close(); |
| 367 | } |
| 368 | |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 369 | // Return the filename we captured the output to. |
| 370 | return OutputFile; |
| 371 | } |
| 372 | |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 373 | /// Used to create reference output with the "safe" backend, if reference output |
| 374 | /// is not provided. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 375 | Expected<std::string> |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 376 | BugDriver::executeProgramSafely(const Module &Program, |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 377 | const std::string &OutputFile) const { |
| 378 | return executeProgram(Program, OutputFile, "", "", SafeInterpreter); |
Brian Gaeke | 35145be | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 379 | } |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 380 | |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 381 | Expected<std::string> |
| 382 | BugDriver::compileSharedObject(const std::string &BitcodeFile) { |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 383 | assert(Interpreter && "Interpreter should have been created already!"); |
Rafael Espindola | 34889ca | 2013-06-17 19:21:38 +0000 | [diff] [blame] | 384 | std::string OutputFile; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 385 | |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 386 | // Using the known-good backend. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 387 | Expected<CC::FileType> FT = |
| 388 | SafeInterpreter->OutputCode(BitcodeFile, OutputFile); |
| 389 | if (Error E = FT.takeError()) |
| 390 | return std::move(E); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 391 | |
Chris Lattner | f8a84db | 2003-10-14 21:09:11 +0000 | [diff] [blame] | 392 | std::string SharedObjectFile; |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 393 | if (Error E = cc->MakeSharedObject(OutputFile, *FT, SharedObjectFile, |
| 394 | AdditionalLinkerArgs)) |
| 395 | return std::move(E); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 396 | |
| 397 | // Remove the intermediate C file |
Rafael Espindola | 34889ca | 2013-06-17 19:21:38 +0000 | [diff] [blame] | 398 | sys::fs::remove(OutputFile); |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 399 | |
Rafael Espindola | c32573b | 2014-03-14 15:13:35 +0000 | [diff] [blame] | 400 | return SharedObjectFile; |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 403 | /// Calls compileProgram and then records the output into ReferenceOutputFile. |
| 404 | /// Returns true if reference file created, false otherwise. Note: |
| 405 | /// initializeExecutionEnvironment should be called BEFORE this function. |
| 406 | Error BugDriver::createReferenceFile(Module &M, const std::string &Filename) { |
| 407 | if (Error E = compileProgram(*Program)) |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 408 | return E; |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 409 | |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 410 | Expected<std::string> Result = executeProgramSafely(*Program, Filename); |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 411 | if (Error E = Result.takeError()) { |
Dan Gohman | 414cf50 | 2008-12-08 04:02:47 +0000 | [diff] [blame] | 412 | if (Interpreter != SafeInterpreter) { |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 413 | E = joinErrors( |
| 414 | std::move(E), |
| 415 | make_error<StringError>( |
| 416 | "*** There is a bug running the \"safe\" backend. Either" |
| 417 | " debug it (for example with the -run-jit bugpoint option," |
| 418 | " if JIT is being used as the \"safe\" backend), or fix the" |
| 419 | " error some other way.\n", |
| 420 | inconvertibleErrorCode())); |
Patrick Jenkins | c46c038 | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 421 | } |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 422 | return E; |
Patrick Jenkins | c46c038 | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 423 | } |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 424 | ReferenceOutputFile = *Result; |
Nick Lewycky | 6ba630b | 2010-04-12 05:08:25 +0000 | [diff] [blame] | 425 | outs() << "\nReference output is: " << ReferenceOutputFile << "\n\n"; |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 426 | return Error::success(); |
Patrick Jenkins | c46c038 | 2006-08-15 16:40:49 +0000 | [diff] [blame] | 427 | } |
Misha Brukman | d792c9b | 2003-07-24 18:17:43 +0000 | [diff] [blame] | 428 | |
Rafael Espindola | f6074ed | 2018-02-14 21:44:34 +0000 | [diff] [blame] | 429 | /// This method executes the specified module and diffs the output against the |
| 430 | /// file specified by ReferenceOutputFile. If the output is different, 1 is |
| 431 | /// returned. If there is a problem with the code generator (e.g., llc |
| 432 | /// crashes), this will set ErrMsg. |
| 433 | Expected<bool> BugDriver::diffProgram(const Module &Program, |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 434 | const std::string &BitcodeFile, |
| 435 | const std::string &SharedObject, |
| 436 | bool RemoveBitcode) const { |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 437 | // Execute the program, generating an output file... |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 438 | Expected<std::string> Output = |
| 439 | executeProgram(Program, "", BitcodeFile, SharedObject, nullptr); |
| 440 | if (Error E = Output.takeError()) |
| 441 | return std::move(E); |
Brian Gaeke | 35145be | 2004-02-11 18:37:32 +0000 | [diff] [blame] | 442 | |
Chris Lattner | aa997fb | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 443 | std::string Error; |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 444 | bool FilesDifferent = false; |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 445 | if (int Diff = DiffFilesWithTolerance(ReferenceOutputFile, *Output, |
Chris Lattner | ece10a4 | 2005-01-23 03:45:26 +0000 | [diff] [blame] | 446 | AbsTolerance, RelTolerance, &Error)) { |
| 447 | if (Diff == 2) { |
Dan Gohman | d8db376 | 2009-07-15 16:35:29 +0000 | [diff] [blame] | 448 | errs() << "While diffing output: " << Error << '\n'; |
Chris Lattner | aa997fb | 2003-08-01 20:29:45 +0000 | [diff] [blame] | 449 | exit(1); |
| 450 | } |
| 451 | FilesDifferent = true; |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 452 | } else { |
David Goodwin | 73f4e3f | 2009-07-10 21:39:28 +0000 | [diff] [blame] | 453 | // Remove the generated output if there are no differences. |
Justin Bogner | 1c03915 | 2016-09-06 17:18:22 +0000 | [diff] [blame] | 454 | sys::fs::remove(*Output); |
David Goodwin | 73f4e3f | 2009-07-10 21:39:28 +0000 | [diff] [blame] | 455 | } |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 456 | |
Gabor Greif | 0e535c3c | 2007-07-04 21:55:50 +0000 | [diff] [blame] | 457 | // Remove the bitcode file if we are supposed to. |
| 458 | if (RemoveBitcode) |
Rafael Espindola | becba2b | 2013-06-18 16:14:09 +0000 | [diff] [blame] | 459 | sys::fs::remove(BitcodeFile); |
Chris Lattner | de4aa4c | 2002-12-23 23:50:16 +0000 | [diff] [blame] | 460 | return FilesDifferent; |
| 461 | } |
Misha Brukman | 539f959 | 2003-07-28 19:16:14 +0000 | [diff] [blame] | 462 | |
Justin Bogner | 8d0a081 | 2016-09-02 01:21:37 +0000 | [diff] [blame] | 463 | bool BugDriver::isExecutingJIT() { return InterpreterSel == RunJIT; } |