| 1 | //===- OptimizerDriver.cpp - Allow BugPoint to run passes safely ----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines an interface that allows bugpoint to run various passes |
| 10 | // without the threat of a buggy pass corrupting bugpoint (of course, bugpoint |
| 11 | // may have its own bugs, but that's another story...). It achieves this by |
| 12 | // forking a copy of itself and having the child process do the optimizations. |
| 13 | // If this client dies, we can always fork a new one. :) |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "BugDriver.h" |
| 18 | #include "ToolRunner.h" |
| 19 | #include "llvm/Bitcode/BitcodeWriter.h" |
| 20 | #include "llvm/IR/DataLayout.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/FileUtilities.h" |
| 25 | #include "llvm/Support/Path.h" |
| 26 | #include "llvm/Support/Program.h" |
| 27 | #include "llvm/Support/ToolOutputFile.h" |
| 28 | |
| 29 | #define DONT_GET_PLUGIN_LOADER_OPTION |
| 30 | #include "llvm/Support/PluginLoader.h" |
| 31 | |
| 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | #define DEBUG_TYPE "bugpoint" |
| 36 | |
| 37 | namespace llvm { |
| 38 | extern cl::opt<std::string> OutputPrefix; |
| 39 | } |
| 40 | |
| 41 | static cl::opt<bool> PreserveBitcodeUseListOrder( |
| 42 | "preserve-bc-uselistorder" , |
| 43 | cl::desc("Preserve use-list order when writing LLVM bitcode." ), |
| 44 | cl::init(Val: true), cl::Hidden); |
| 45 | |
| 46 | static cl::opt<std::string> |
| 47 | OptCmd("opt-command" , cl::init(Val: "" ), |
| 48 | cl::desc("Path to opt. (default: search path " |
| 49 | "for 'opt'.)" )); |
| 50 | |
| 51 | /// This writes the current "Program" to the named bitcode file. If an error |
| 52 | /// occurs, true is returned. |
| 53 | static bool writeProgramToFileAux(ToolOutputFile &Out, const Module &M) { |
| 54 | WriteBitcodeToFile(M, Out&: Out.os(), ShouldPreserveUseListOrder: PreserveBitcodeUseListOrder); |
| 55 | Out.os().close(); |
| 56 | if (!Out.os().has_error()) { |
| 57 | Out.keep(); |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool BugDriver::writeProgramToFile(const std::string &Filename, int FD, |
| 64 | const Module &M) const { |
| 65 | ToolOutputFile Out(Filename, FD); |
| 66 | return writeProgramToFileAux(Out, M); |
| 67 | } |
| 68 | |
| 69 | bool BugDriver::writeProgramToFile(int FD, const Module &M) const { |
| 70 | raw_fd_ostream OS(FD, /*shouldClose*/ false); |
| 71 | WriteBitcodeToFile(M, Out&: OS, ShouldPreserveUseListOrder: PreserveBitcodeUseListOrder); |
| 72 | OS.flush(); |
| 73 | if (!OS.has_error()) |
| 74 | return false; |
| 75 | OS.clear_error(); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | bool BugDriver::writeProgramToFile(const std::string &Filename, |
| 80 | const Module &M) const { |
| 81 | std::error_code EC; |
| 82 | ToolOutputFile Out(Filename, EC, sys::fs::OF_None); |
| 83 | if (!EC) |
| 84 | return writeProgramToFileAux(Out, M); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /// This function is used to output the current Program to a file named |
| 89 | /// "bugpoint-ID.bc". |
| 90 | void BugDriver::EmitProgressBitcode(const Module &M, const std::string &ID, |
| 91 | bool NoFlyer) const { |
| 92 | // Output the input to the current pass to a bitcode file, emit a message |
| 93 | // telling the user how to reproduce it: opt -foo blah.bc |
| 94 | // |
| 95 | std::string Filename = OutputPrefix + "-" + ID + ".bc" ; |
| 96 | if (writeProgramToFile(Filename, M)) { |
| 97 | errs() << "Error opening file '" << Filename << "' for writing!\n" ; |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | outs() << "Emitted bitcode to '" << Filename << "'\n" ; |
| 102 | if (NoFlyer || PassesToRun.empty()) |
| 103 | return; |
| 104 | outs() << "\n*** You can reproduce the problem with: " ; |
| 105 | if (UseValgrind) |
| 106 | outs() << "valgrind " ; |
| 107 | outs() << "opt " << Filename; |
| 108 | for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) { |
| 109 | outs() << " -load " << PluginLoader::getPlugin(num: i); |
| 110 | } |
| 111 | outs() << " " << getPassesString(Passes: PassesToRun) << "\n" ; |
| 112 | } |
| 113 | |
| 114 | static cl::opt<bool> SilencePasses( |
| 115 | "silence-passes" , |
| 116 | cl::desc("Suppress output of running passes (both stdout and stderr)" )); |
| 117 | |
| 118 | static cl::list<std::string> OptArgs("opt-args" , cl::Positional, |
| 119 | cl::desc("<opt arguments>..." ), |
| 120 | cl::PositionalEatsArgs); |
| 121 | |
| 122 | /// runPasses - Run the specified passes on Program, outputting a bitcode file |
| 123 | /// and writing the filename into OutputFile if successful. If the |
| 124 | /// optimizations fail for some reason (optimizer crashes), return true, |
| 125 | /// otherwise return false. If DeleteOutput is set to true, the bitcode is |
| 126 | /// deleted on success, and the filename string is undefined. This prints to |
| 127 | /// outs() a single line message indicating whether compilation was successful |
| 128 | /// or failed. |
| 129 | /// |
| 130 | bool BugDriver::runPasses(Module &Program, |
| 131 | const std::vector<std::string> &Passes, |
| 132 | std::string &OutputFilename, bool DeleteOutput, |
| 133 | bool Quiet, ArrayRef<std::string> ) const { |
| 134 | // setup the output file name |
| 135 | outs().flush(); |
| 136 | SmallString<128> UniqueFilename; |
| 137 | std::error_code EC = sys::fs::createUniqueFile( |
| 138 | Model: OutputPrefix + "-output-%%%%%%%.bc" , ResultPath&: UniqueFilename); |
| 139 | if (EC) { |
| 140 | errs() << getToolName() |
| 141 | << ": Error making unique filename: " << EC.message() << "\n" ; |
| 142 | return true; |
| 143 | } |
| 144 | OutputFilename = std::string(UniqueFilename); |
| 145 | |
| 146 | // set up the input file name |
| 147 | Expected<sys::fs::TempFile> Temp = |
| 148 | sys::fs::TempFile::create(Model: OutputPrefix + "-input-%%%%%%%.bc" ); |
| 149 | if (!Temp) { |
| 150 | errs() << getToolName() |
| 151 | << ": Error making unique filename: " << toString(E: Temp.takeError()) |
| 152 | << "\n" ; |
| 153 | return true; |
| 154 | } |
| 155 | DiscardTemp Discard{.File: *Temp}; |
| 156 | raw_fd_ostream OS(Temp->FD, /*shouldClose*/ false); |
| 157 | |
| 158 | WriteBitcodeToFile(M: Program, Out&: OS, ShouldPreserveUseListOrder: PreserveBitcodeUseListOrder); |
| 159 | OS.flush(); |
| 160 | if (OS.has_error()) { |
| 161 | errs() << "Error writing bitcode file: " << Temp->TmpName << "\n" ; |
| 162 | OS.clear_error(); |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | std::string tool = OptCmd; |
| 167 | if (OptCmd.empty()) { |
| 168 | if (ErrorOr<std::string> Path = |
| 169 | FindProgramByName(ExeName: "opt" , Argv0: getToolName(), MainAddr: &OutputPrefix)) |
| 170 | tool = *Path; |
| 171 | else |
| 172 | errs() << Path.getError().message() << "\n" ; |
| 173 | } |
| 174 | if (tool.empty()) { |
| 175 | errs() << "Cannot find `opt' in PATH!\n" ; |
| 176 | return true; |
| 177 | } |
| 178 | if (!sys::fs::exists(Path: tool)) { |
| 179 | errs() << "Specified `opt' binary does not exist: " << tool << "\n" ; |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | std::string Prog; |
| 184 | if (UseValgrind) { |
| 185 | if (ErrorOr<std::string> Path = sys::findProgramByName(Name: "valgrind" )) |
| 186 | Prog = *Path; |
| 187 | else |
| 188 | errs() << Path.getError().message() << "\n" ; |
| 189 | } else |
| 190 | Prog = tool; |
| 191 | if (Prog.empty()) { |
| 192 | errs() << "Cannot find `valgrind' in PATH!\n" ; |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | // setup the child process' arguments |
| 197 | SmallVector<StringRef, 8> Args; |
| 198 | if (UseValgrind) { |
| 199 | Args.push_back(Elt: "valgrind" ); |
| 200 | Args.push_back(Elt: "--error-exitcode=1" ); |
| 201 | Args.push_back(Elt: "-q" ); |
| 202 | Args.push_back(Elt: tool); |
| 203 | } else |
| 204 | Args.push_back(Elt: tool); |
| 205 | |
| 206 | llvm::append_range(C&: Args, R&: OptArgs); |
| 207 | // Pin to legacy PM since bugpoint has lots of infra and hacks revolving |
| 208 | // around the legacy PM. |
| 209 | Args.push_back(Elt: "-bugpoint-enable-legacy-pm" ); |
| 210 | Args.push_back(Elt: "-disable-symbolication" ); |
| 211 | Args.push_back(Elt: "-o" ); |
| 212 | Args.push_back(Elt: OutputFilename); |
| 213 | std::vector<std::string> pass_args; |
| 214 | for (unsigned i = 0, e = PluginLoader::getNumPlugins(); i != e; ++i) { |
| 215 | pass_args.push_back(x: std::string("-load" )); |
| 216 | pass_args.push_back(x: PluginLoader::getPlugin(num: i)); |
| 217 | } |
| 218 | for (std::vector<std::string>::const_iterator I = Passes.begin(), |
| 219 | E = Passes.end(); |
| 220 | I != E; ++I) |
| 221 | pass_args.push_back(x: std::string("-" ) + (*I)); |
| 222 | for (std::vector<std::string>::const_iterator I = pass_args.begin(), |
| 223 | E = pass_args.end(); |
| 224 | I != E; ++I) |
| 225 | Args.push_back(Elt: *I); |
| 226 | Args.push_back(Elt: Temp->TmpName); |
| 227 | Args.append(in_start: ExtraArgs.begin(), in_end: ExtraArgs.end()); |
| 228 | |
| 229 | LLVM_DEBUG(errs() << "\nAbout to run:\t" ; |
| 230 | for (unsigned i = 0, e = Args.size() - 1; i != e; ++i) errs() |
| 231 | << " " << Args[i]; |
| 232 | errs() << "\n" ;); |
| 233 | |
| 234 | std::optional<StringRef> Redirects[3] = {std::nullopt, std::nullopt, |
| 235 | std::nullopt}; |
| 236 | // Redirect stdout and stderr to nowhere if SilencePasses is given. |
| 237 | if (SilencePasses) { |
| 238 | Redirects[1] = "" ; |
| 239 | Redirects[2] = "" ; |
| 240 | } |
| 241 | |
| 242 | std::string ErrMsg; |
| 243 | int result = sys::ExecuteAndWait(Program: Prog, Args, Env: std::nullopt, Redirects, SecondsToWait: Timeout, |
| 244 | MemoryLimit, ErrMsg: &ErrMsg); |
| 245 | |
| 246 | // If we are supposed to delete the bitcode file or if the passes crashed, |
| 247 | // remove it now. This may fail if the file was never created, but that's ok. |
| 248 | if (DeleteOutput || result != 0) |
| 249 | sys::fs::remove(path: OutputFilename); |
| 250 | |
| 251 | if (!Quiet) { |
| 252 | if (result == 0) |
| 253 | outs() << "Success!\n" ; |
| 254 | else if (result > 0) |
| 255 | outs() << "Exited with error code '" << result << "'\n" ; |
| 256 | else if (result < 0) { |
| 257 | if (result == -1) |
| 258 | outs() << "Execute failed: " << ErrMsg << "\n" ; |
| 259 | else |
| 260 | outs() << "Crashed: " << ErrMsg << "\n" ; |
| 261 | } |
| 262 | if (result & 0x01000000) |
| 263 | outs() << "Dumped core\n" ; |
| 264 | } |
| 265 | |
| 266 | // Was the child successful? |
| 267 | return result != 0; |
| 268 | } |
| 269 | |
| 270 | std::unique_ptr<Module> |
| 271 | BugDriver::runPassesOn(Module *M, const std::vector<std::string> &Passes, |
| 272 | ArrayRef<std::string> ) { |
| 273 | std::string BitcodeResult; |
| 274 | if (runPasses(Program&: *M, Passes, OutputFilename&: BitcodeResult, DeleteOutput: false /*delete*/, Quiet: true /*quiet*/, |
| 275 | ExtraArgs)) { |
| 276 | return nullptr; |
| 277 | } |
| 278 | |
| 279 | std::unique_ptr<Module> Ret = parseInputFile(InputFilename: BitcodeResult, ctxt&: Context); |
| 280 | if (!Ret) { |
| 281 | errs() << getToolName() << ": Error reading bitcode file '" << BitcodeResult |
| 282 | << "'!\n" ; |
| 283 | exit(status: 1); |
| 284 | } |
| 285 | sys::fs::remove(path: BitcodeResult); |
| 286 | return Ret; |
| 287 | } |
| 288 | |