BugDriver.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //===- BugDriver.h - Top-Level BugPoint class -------------------*- C++ -*-===//
  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 class contains all of the shared state and information that is used by
  10. // the BugPoint tool to track down errors in optimizations. This class is the
  11. // main driver class that invokes all sub-functionality.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
  15. #define LLVM_TOOLS_BUGPOINT_BUGDRIVER_H
  16. #include "llvm/IR/ValueMap.h"
  17. #include "llvm/Support/Error.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Transforms/Utils/ValueMapper.h"
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. namespace llvm {
  24. class PassInfo;
  25. class Module;
  26. class GlobalVariable;
  27. class Function;
  28. class BasicBlock;
  29. class AbstractInterpreter;
  30. class Instruction;
  31. class LLVMContext;
  32. class CC;
  33. extern bool DisableSimplifyCFG;
  34. /// BugpointIsInterrupted - Set to true when the user presses ctrl-c.
  35. ///
  36. extern bool BugpointIsInterrupted;
  37. class BugDriver {
  38. LLVMContext &Context;
  39. const char *ToolName; // argv[0] of bugpoint
  40. std::string ReferenceOutputFile; // Name of `good' output file
  41. std::unique_ptr<Module> Program; // The raw program, linked together
  42. std::vector<std::string> PassesToRun;
  43. AbstractInterpreter *Interpreter; // How to run the program
  44. AbstractInterpreter *SafeInterpreter; // To generate reference output, etc.
  45. CC *cc;
  46. bool run_find_bugs;
  47. unsigned Timeout;
  48. unsigned MemoryLimit;
  49. bool UseValgrind;
  50. // FIXME: sort out public/private distinctions...
  51. friend class ReducePassList;
  52. friend class ReduceMisCodegenFunctions;
  53. public:
  54. BugDriver(const char *toolname, bool find_bugs, unsigned timeout,
  55. unsigned memlimit, bool use_valgrind, LLVMContext &ctxt);
  56. ~BugDriver();
  57. const char *getToolName() const { return ToolName; }
  58. LLVMContext &getContext() const { return Context; }
  59. // Set up methods... these methods are used to copy information about the
  60. // command line arguments into instance variables of BugDriver.
  61. //
  62. bool addSources(const std::vector<std::string> &FileNames);
  63. void addPass(std::string p) { PassesToRun.push_back(std::move(p)); }
  64. void setPassesToRun(const std::vector<std::string> &PTR) {
  65. PassesToRun = PTR;
  66. }
  67. const std::vector<std::string> &getPassesToRun() const { return PassesToRun; }
  68. /// run - The top level method that is invoked after all of the instance
  69. /// variables are set up from command line arguments. The \p as_child argument
  70. /// indicates whether the driver is to run in parent mode or child mode.
  71. ///
  72. Error run();
  73. /// debugOptimizerCrash - This method is called when some optimizer pass
  74. /// crashes on input. It attempts to prune down the testcase to something
  75. /// reasonable, and figure out exactly which pass is crashing.
  76. ///
  77. Error debugOptimizerCrash(const std::string &ID = "passes");
  78. /// debugCodeGeneratorCrash - This method is called when the code generator
  79. /// crashes on an input. It attempts to reduce the input as much as possible
  80. /// while still causing the code generator to crash.
  81. Error debugCodeGeneratorCrash();
  82. /// debugMiscompilation - This method is used when the passes selected are not
  83. /// crashing, but the generated output is semantically different from the
  84. /// input.
  85. Error debugMiscompilation();
  86. /// debugPassMiscompilation - This method is called when the specified pass
  87. /// miscompiles Program as input. It tries to reduce the testcase to
  88. /// something that smaller that still miscompiles the program.
  89. /// ReferenceOutput contains the filename of the file containing the output we
  90. /// are to match.
  91. ///
  92. bool debugPassMiscompilation(const PassInfo *ThePass,
  93. const std::string &ReferenceOutput);
  94. /// compileSharedObject - This method creates a SharedObject from a given
  95. /// BitcodeFile for debugging a code generator.
  96. ///
  97. Expected<std::string> compileSharedObject(const std::string &BitcodeFile);
  98. /// debugCodeGenerator - This method narrows down a module to a function or
  99. /// set of functions, using the CBE as a ``safe'' code generator for other
  100. /// functions that are not under consideration.
  101. Error debugCodeGenerator();
  102. /// isExecutingJIT - Returns true if bugpoint is currently testing the JIT
  103. ///
  104. bool isExecutingJIT();
  105. Module &getProgram() const { return *Program; }
  106. /// Set the current module to the specified module, returning the old one.
  107. std::unique_ptr<Module> swapProgramIn(std::unique_ptr<Module> M);
  108. AbstractInterpreter *switchToSafeInterpreter() {
  109. AbstractInterpreter *Old = Interpreter;
  110. Interpreter = (AbstractInterpreter *)SafeInterpreter;
  111. return Old;
  112. }
  113. void switchToInterpreter(AbstractInterpreter *AI) { Interpreter = AI; }
  114. /// If we reduce or update the program somehow, call this method to update
  115. /// bugdriver with it. This deletes the old module and sets the specified one
  116. /// as the current program.
  117. void setNewProgram(std::unique_ptr<Module> M);
  118. /// Try to compile the specified module. This is used for code generation
  119. /// crash testing.
  120. Error compileProgram(Module &M) const;
  121. /// This method runs "Program", capturing the output of the program to a file.
  122. /// A recommended filename may be optionally specified.
  123. Expected<std::string> executeProgram(const Module &Program,
  124. std::string OutputFilename,
  125. std::string Bitcode,
  126. const std::string &SharedObjects,
  127. AbstractInterpreter *AI) const;
  128. /// Used to create reference output with the "safe" backend, if reference
  129. /// output is not provided. If there is a problem with the code generator
  130. /// (e.g., llc crashes), this will return false and set Error.
  131. Expected<std::string>
  132. executeProgramSafely(const Module &Program,
  133. const std::string &OutputFile) const;
  134. /// Calls compileProgram and then records the output into ReferenceOutputFile.
  135. /// Returns true if reference file created, false otherwise. Note:
  136. /// initializeExecutionEnvironment should be called BEFORE this function.
  137. Error createReferenceFile(Module &M, const std::string &Filename =
  138. "bugpoint.reference.out-%%%%%%%");
  139. /// This method executes the specified module and diffs the output against the
  140. /// file specified by ReferenceOutputFile. If the output is different, 1 is
  141. /// returned. If there is a problem with the code generator (e.g., llc
  142. /// crashes), this will return -1 and set Error.
  143. Expected<bool> diffProgram(const Module &Program,
  144. const std::string &BitcodeFile = "",
  145. const std::string &SharedObj = "",
  146. bool RemoveBitcode = false) const;
  147. /// This function is used to output M to a file named "bugpoint-ID.bc".
  148. void EmitProgressBitcode(const Module &M, const std::string &ID,
  149. bool NoFlyer = false) const;
  150. /// This method clones the current Program and deletes the specified
  151. /// instruction from the cloned module. It then runs a series of cleanup
  152. /// passes (ADCE and SimplifyCFG) to eliminate any code which depends on the
  153. /// value. The modified module is then returned.
  154. ///
  155. std::unique_ptr<Module> deleteInstructionFromProgram(const Instruction *I,
  156. unsigned Simp);
  157. /// This method clones the current Program and performs a series of cleanups
  158. /// intended to get rid of extra cruft on the module. If the
  159. /// MayModifySemantics argument is true, then the cleanups is allowed to
  160. /// modify how the code behaves.
  161. ///
  162. std::unique_ptr<Module> performFinalCleanups(std::unique_ptr<Module> M,
  163. bool MayModifySemantics = false);
  164. /// Given a module, extract up to one loop from it into a new function. This
  165. /// returns null if there are no extractable loops in the program or if the
  166. /// loop extractor crashes.
  167. std::unique_ptr<Module> extractLoop(Module *M);
  168. /// Extract all but the specified basic blocks into their own functions. The
  169. /// only detail is that M is actually a module cloned from the one the BBs are
  170. /// in, so some mapping needs to be performed. If this operation fails for
  171. /// some reason (ie the implementation is buggy), this function should return
  172. /// null, otherwise it returns a new Module.
  173. std::unique_ptr<Module>
  174. extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
  175. Module *M);
  176. /// Carefully run the specified set of pass on the specified/ module,
  177. /// returning the transformed module on success, or a null pointer on failure.
  178. std::unique_ptr<Module> runPassesOn(Module *M,
  179. const std::vector<std::string> &Passes,
  180. ArrayRef<std::string> ExtraArgs = {});
  181. /// runPasses - Run the specified passes on Program, outputting a bitcode
  182. /// file and writting the filename into OutputFile if successful. If the
  183. /// optimizations fail for some reason (optimizer crashes), return true,
  184. /// otherwise return false. If DeleteOutput is set to true, the bitcode is
  185. /// deleted on success, and the filename string is undefined. This prints to
  186. /// outs() a single line message indicating whether compilation was successful
  187. /// or failed, unless Quiet is set. ExtraArgs specifies additional arguments
  188. /// to pass to the child bugpoint instance.
  189. ///
  190. bool runPasses(Module &Program, const std::vector<std::string> &PassesToRun,
  191. std::string &OutputFilename, bool DeleteOutput = false,
  192. bool Quiet = false,
  193. ArrayRef<std::string> ExtraArgs = {}) const;
  194. /// runPasses - Just like the method above, but this just returns true or
  195. /// false indicating whether or not the optimizer crashed on the specified
  196. /// input (true = crashed). Does not produce any output.
  197. ///
  198. bool runPasses(Module &M, const std::vector<std::string> &PassesToRun) const {
  199. std::string Filename;
  200. return runPasses(M, PassesToRun, Filename, true);
  201. }
  202. /// Take the specified pass list and create different combinations of passes
  203. /// to compile the program with. Compile the program with each set and mark
  204. /// test to see if it compiled correctly. If the passes compiled correctly
  205. /// output nothing and rearrange the passes into a new order. If the passes
  206. /// did not compile correctly, output the command required to recreate the
  207. /// failure.
  208. Error runManyPasses(const std::vector<std::string> &AllPasses);
  209. /// This writes the current "Program" to the named bitcode file. If an error
  210. /// occurs, true is returned.
  211. bool writeProgramToFile(const std::string &Filename, const Module &M) const;
  212. bool writeProgramToFile(const std::string &Filename, int FD,
  213. const Module &M) const;
  214. bool writeProgramToFile(int FD, const Module &M) const;
  215. private:
  216. /// initializeExecutionEnvironment - This method is used to set up the
  217. /// environment for executing LLVM programs.
  218. ///
  219. Error initializeExecutionEnvironment();
  220. };
  221. struct DiscardTemp {
  222. sys::fs::TempFile &File;
  223. ~DiscardTemp();
  224. };
  225. /// Given a bitcode or assembly input filename, parse and return it, or return
  226. /// null if not possible.
  227. ///
  228. std::unique_ptr<Module> parseInputFile(StringRef InputFilename,
  229. LLVMContext &ctxt);
  230. /// getPassesString - Turn a list of passes into a string which indicates the
  231. /// command line options that must be passed to add the passes.
  232. ///
  233. std::string getPassesString(const std::vector<std::string> &Passes);
  234. /// PrintFunctionList - prints out list of problematic functions
  235. ///
  236. void PrintFunctionList(const std::vector<Function *> &Funcs);
  237. /// PrintGlobalVariableList - prints out list of problematic global variables
  238. ///
  239. void PrintGlobalVariableList(const std::vector<GlobalVariable *> &GVs);
  240. // DeleteGlobalInitializer - "Remove" the global variable by deleting its
  241. // initializer, making it external.
  242. //
  243. void DeleteGlobalInitializer(GlobalVariable *GV);
  244. // DeleteFunctionBody - "Remove" the function by deleting all of it's basic
  245. // blocks, making it external.
  246. //
  247. void DeleteFunctionBody(Function *F);
  248. /// Given a module and a list of functions in the module, split the functions
  249. /// OUT of the specified module, and place them in the new module.
  250. std::unique_ptr<Module>
  251. SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
  252. ValueToValueMapTy &VMap);
  253. } // End llvm namespace
  254. #endif