PrintPasses.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //===- PrintPasses.cpp ----------------------------------------------------===//
  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. #include "llvm/IR/PrintPasses.h"
  9. #include "llvm/Support/CommandLine.h"
  10. #include "llvm/Support/Errc.h"
  11. #include "llvm/Support/FileSystem.h"
  12. #include "llvm/Support/MemoryBuffer.h"
  13. #include "llvm/Support/Program.h"
  14. #include <unordered_set>
  15. using namespace llvm;
  16. // Print IR out before/after specified passes.
  17. static cl::list<std::string>
  18. PrintBefore("print-before",
  19. llvm::cl::desc("Print IR before specified passes"),
  20. cl::CommaSeparated, cl::Hidden);
  21. static cl::list<std::string>
  22. PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"),
  23. cl::CommaSeparated, cl::Hidden);
  24. static cl::opt<bool> PrintBeforeAll("print-before-all",
  25. llvm::cl::desc("Print IR before each pass"),
  26. cl::init(false), cl::Hidden);
  27. static cl::opt<bool> PrintAfterAll("print-after-all",
  28. llvm::cl::desc("Print IR after each pass"),
  29. cl::init(false), cl::Hidden);
  30. // Print out the IR after passes, similar to -print-after-all except that it
  31. // only prints the IR after passes that change the IR. Those passes that do not
  32. // make changes to the IR are reported as not making any changes. In addition,
  33. // the initial IR is also reported. Other hidden options affect the output from
  34. // this option. -filter-passes will limit the output to the named passes that
  35. // actually change the IR and other passes are reported as filtered out. The
  36. // specified passes will either be reported as making no changes (with no IR
  37. // reported) or the changed IR will be reported. Also, the -filter-print-funcs
  38. // and -print-module-scope options will do similar filtering based on function
  39. // name, reporting changed IRs as functions(or modules if -print-module-scope is
  40. // specified) for a particular function or indicating that the IR has been
  41. // filtered out. The extra options can be combined, allowing only changed IRs
  42. // for certain passes on certain functions to be reported in different formats,
  43. // with the rest being reported as filtered out. The -print-before-changed
  44. // option will print the IR as it was before each pass that changed it. The
  45. // optional value of quiet will only report when the IR changes, suppressing all
  46. // other messages, including the initial IR. The values "diff" and "diff-quiet"
  47. // will present the changes in a form similar to a patch, in either verbose or
  48. // quiet mode, respectively. The lines that are removed and added are prefixed
  49. // with '-' and '+', respectively. The -filter-print-funcs and -filter-passes
  50. // can be used to filter the output. This reporter relies on the linux diff
  51. // utility to do comparisons and insert the prefixes. For systems that do not
  52. // have the necessary facilities, the error message will be shown in place of
  53. // the expected output.
  54. cl::opt<ChangePrinter> llvm::PrintChanged(
  55. "print-changed", cl::desc("Print changed IRs"), cl::Hidden,
  56. cl::ValueOptional, cl::init(ChangePrinter::None),
  57. cl::values(
  58. clEnumValN(ChangePrinter::Quiet, "quiet", "Run in quiet mode"),
  59. clEnumValN(ChangePrinter::DiffVerbose, "diff",
  60. "Display patch-like changes"),
  61. clEnumValN(ChangePrinter::DiffQuiet, "diff-quiet",
  62. "Display patch-like changes in quiet mode"),
  63. clEnumValN(ChangePrinter::ColourDiffVerbose, "cdiff",
  64. "Display patch-like changes with color"),
  65. clEnumValN(ChangePrinter::ColourDiffQuiet, "cdiff-quiet",
  66. "Display patch-like changes in quiet mode with color"),
  67. clEnumValN(ChangePrinter::DotCfgVerbose, "dot-cfg",
  68. "Create a website with graphical changes"),
  69. clEnumValN(ChangePrinter::DotCfgQuiet, "dot-cfg-quiet",
  70. "Create a website with graphical changes in quiet mode"),
  71. // Sentinel value for unspecified option.
  72. clEnumValN(ChangePrinter::Verbose, "", "")));
  73. // An option for specifying the diff used by print-changed=[diff | diff-quiet]
  74. static cl::opt<std::string>
  75. DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"),
  76. cl::desc("system diff used by change reporters"));
  77. static cl::opt<bool>
  78. PrintModuleScope("print-module-scope",
  79. cl::desc("When printing IR for print-[before|after]{-all} "
  80. "always print a module IR"),
  81. cl::init(false), cl::Hidden);
  82. // See the description for -print-changed for an explanation of the use
  83. // of this option.
  84. static cl::list<std::string> FilterPasses(
  85. "filter-passes", cl::value_desc("pass names"),
  86. cl::desc("Only consider IR changes for passes whose names "
  87. "match the specified value. No-op without -print-changed"),
  88. cl::CommaSeparated, cl::Hidden);
  89. static cl::list<std::string>
  90. PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
  91. cl::desc("Only print IR for functions whose name "
  92. "match this for all print-[before|after][-all] "
  93. "options"),
  94. cl::CommaSeparated, cl::Hidden);
  95. /// This is a helper to determine whether to print IR before or
  96. /// after a pass.
  97. bool llvm::shouldPrintBeforeSomePass() {
  98. return PrintBeforeAll || !PrintBefore.empty();
  99. }
  100. bool llvm::shouldPrintAfterSomePass() {
  101. return PrintAfterAll || !PrintAfter.empty();
  102. }
  103. static bool shouldPrintBeforeOrAfterPass(StringRef PassID,
  104. ArrayRef<std::string> PassesToPrint) {
  105. return llvm::is_contained(PassesToPrint, PassID);
  106. }
  107. bool llvm::shouldPrintBeforeAll() { return PrintBeforeAll; }
  108. bool llvm::shouldPrintAfterAll() { return PrintAfterAll; }
  109. bool llvm::shouldPrintBeforePass(StringRef PassID) {
  110. return PrintBeforeAll || shouldPrintBeforeOrAfterPass(PassID, PrintBefore);
  111. }
  112. bool llvm::shouldPrintAfterPass(StringRef PassID) {
  113. return PrintAfterAll || shouldPrintBeforeOrAfterPass(PassID, PrintAfter);
  114. }
  115. std::vector<std::string> llvm::printBeforePasses() {
  116. return std::vector<std::string>(PrintBefore);
  117. }
  118. std::vector<std::string> llvm::printAfterPasses() {
  119. return std::vector<std::string>(PrintAfter);
  120. }
  121. bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
  122. bool llvm::isPassInPrintList(StringRef PassName) {
  123. static std::unordered_set<std::string> Set(FilterPasses.begin(),
  124. FilterPasses.end());
  125. return Set.empty() || Set.count(std::string(PassName));
  126. }
  127. bool llvm::isFilterPassesEmpty() { return FilterPasses.empty(); }
  128. bool llvm::isFunctionInPrintList(StringRef FunctionName) {
  129. static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
  130. PrintFuncsList.end());
  131. return PrintFuncNames.empty() ||
  132. PrintFuncNames.count(std::string(FunctionName));
  133. }
  134. std::error_code cleanUpTempFilesImpl(ArrayRef<std::string> FileName,
  135. unsigned N) {
  136. std::error_code RC;
  137. for (unsigned I = 0; I < N; ++I) {
  138. std::error_code EC = sys::fs::remove(FileName[I]);
  139. if (EC)
  140. RC = EC;
  141. }
  142. return RC;
  143. }
  144. std::error_code llvm::prepareTempFiles(SmallVector<int> &FD,
  145. ArrayRef<StringRef> SR,
  146. SmallVector<std::string> &FileName) {
  147. assert(FD.size() >= SR.size() && FileName.size() == FD.size() &&
  148. "Unexpected array sizes");
  149. std::error_code EC;
  150. unsigned I = 0;
  151. for (; I < FD.size(); ++I) {
  152. if (FD[I] == -1) {
  153. SmallVector<char, 200> SV;
  154. EC = sys::fs::createTemporaryFile("tmpfile", "txt", FD[I], SV);
  155. if (EC)
  156. break;
  157. FileName[I] = Twine(SV).str();
  158. }
  159. if (I < SR.size()) {
  160. EC = sys::fs::openFileForWrite(FileName[I], FD[I]);
  161. if (EC)
  162. break;
  163. raw_fd_ostream OutStream(FD[I], /*shouldClose=*/true);
  164. if (FD[I] == -1) {
  165. EC = make_error_code(errc::io_error);
  166. break;
  167. }
  168. OutStream << SR[I];
  169. }
  170. }
  171. if (EC && I > 0)
  172. // clean up created temporary files
  173. cleanUpTempFilesImpl(FileName, I);
  174. return EC;
  175. }
  176. std::error_code llvm::cleanUpTempFiles(ArrayRef<std::string> FileName) {
  177. return cleanUpTempFilesImpl(FileName, FileName.size());
  178. }
  179. std::string llvm::doSystemDiff(StringRef Before, StringRef After,
  180. StringRef OldLineFormat, StringRef NewLineFormat,
  181. StringRef UnchangedLineFormat) {
  182. // Store the 2 bodies into temporary files and call diff on them
  183. // to get the body of the node.
  184. static SmallVector<int> FD{-1, -1, -1};
  185. SmallVector<StringRef> SR{Before, After};
  186. static SmallVector<std::string> FileName{"", "", ""};
  187. if (auto Err = prepareTempFiles(FD, SR, FileName))
  188. return "Unable to create temporary file.";
  189. static ErrorOr<std::string> DiffExe = sys::findProgramByName(DiffBinary);
  190. if (!DiffExe)
  191. return "Unable to find diff executable.";
  192. SmallString<128> OLF, NLF, ULF;
  193. ("--old-line-format=" + OldLineFormat).toVector(OLF);
  194. ("--new-line-format=" + NewLineFormat).toVector(NLF);
  195. ("--unchanged-line-format=" + UnchangedLineFormat).toVector(ULF);
  196. StringRef Args[] = {DiffBinary, "-w", "-d", OLF,
  197. NLF, ULF, FileName[0], FileName[1]};
  198. std::optional<StringRef> Redirects[] = {std::nullopt, StringRef(FileName[2]),
  199. std::nullopt};
  200. int Result = sys::ExecuteAndWait(*DiffExe, Args, std::nullopt, Redirects);
  201. if (Result < 0)
  202. return "Error executing system diff.";
  203. std::string Diff;
  204. auto B = MemoryBuffer::getFile(FileName[2]);
  205. if (B && *B)
  206. Diff = (*B)->getBuffer().str();
  207. else
  208. return "Unable to read result.";
  209. if (auto Err = cleanUpTempFiles(FileName))
  210. return "Unable to remove temporary file.";
  211. return Diff;
  212. }