OptimizerDriver.cpp 9.5 KB

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