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