xref: /openbsd/gnu/llvm/llvm/lib/IR/PrintPasses.cpp (revision d415bd75)
173471bf0Spatrick //===- PrintPasses.cpp ----------------------------------------------------===//
273471bf0Spatrick //
373471bf0Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
473471bf0Spatrick // See https://llvm.org/LICENSE.txt for license information.
573471bf0Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
673471bf0Spatrick //
773471bf0Spatrick //===----------------------------------------------------------------------===//
873471bf0Spatrick 
973471bf0Spatrick #include "llvm/IR/PrintPasses.h"
1073471bf0Spatrick #include "llvm/Support/CommandLine.h"
11*d415bd75Srobert #include "llvm/Support/Errc.h"
12*d415bd75Srobert #include "llvm/Support/FileSystem.h"
13*d415bd75Srobert #include "llvm/Support/MemoryBuffer.h"
14*d415bd75Srobert #include "llvm/Support/Program.h"
1573471bf0Spatrick #include <unordered_set>
1673471bf0Spatrick 
1773471bf0Spatrick using namespace llvm;
1873471bf0Spatrick 
1973471bf0Spatrick // Print IR out before/after specified passes.
2073471bf0Spatrick static cl::list<std::string>
2173471bf0Spatrick     PrintBefore("print-before",
2273471bf0Spatrick                 llvm::cl::desc("Print IR before specified passes"),
2373471bf0Spatrick                 cl::CommaSeparated, cl::Hidden);
2473471bf0Spatrick 
2573471bf0Spatrick static cl::list<std::string>
2673471bf0Spatrick     PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"),
2773471bf0Spatrick                cl::CommaSeparated, cl::Hidden);
2873471bf0Spatrick 
2973471bf0Spatrick static cl::opt<bool> PrintBeforeAll("print-before-all",
3073471bf0Spatrick                                     llvm::cl::desc("Print IR before each pass"),
3173471bf0Spatrick                                     cl::init(false), cl::Hidden);
3273471bf0Spatrick static cl::opt<bool> PrintAfterAll("print-after-all",
3373471bf0Spatrick                                    llvm::cl::desc("Print IR after each pass"),
3473471bf0Spatrick                                    cl::init(false), cl::Hidden);
3573471bf0Spatrick 
36*d415bd75Srobert // Print out the IR after passes, similar to -print-after-all except that it
37*d415bd75Srobert // only prints the IR after passes that change the IR. Those passes that do not
38*d415bd75Srobert // make changes to the IR are reported as not making any changes. In addition,
39*d415bd75Srobert // the initial IR is also reported.  Other hidden options affect the output from
40*d415bd75Srobert // this option. -filter-passes will limit the output to the named passes that
41*d415bd75Srobert // actually change the IR and other passes are reported as filtered out. The
42*d415bd75Srobert // specified passes will either be reported as making no changes (with no IR
43*d415bd75Srobert // reported) or the changed IR will be reported. Also, the -filter-print-funcs
44*d415bd75Srobert // and -print-module-scope options will do similar filtering based on function
45*d415bd75Srobert // name, reporting changed IRs as functions(or modules if -print-module-scope is
46*d415bd75Srobert // specified) for a particular function or indicating that the IR has been
47*d415bd75Srobert // filtered out. The extra options can be combined, allowing only changed IRs
48*d415bd75Srobert // for certain passes on certain functions to be reported in different formats,
49*d415bd75Srobert // with the rest being reported as filtered out.  The -print-before-changed
50*d415bd75Srobert // option will print the IR as it was before each pass that changed it. The
51*d415bd75Srobert // optional value of quiet will only report when the IR changes, suppressing all
52*d415bd75Srobert // other messages, including the initial IR. The values "diff" and "diff-quiet"
53*d415bd75Srobert // will present the changes in a form similar to a patch, in either verbose or
54*d415bd75Srobert // quiet mode, respectively. The lines that are removed and added are prefixed
55*d415bd75Srobert // with '-' and '+', respectively. The -filter-print-funcs and -filter-passes
56*d415bd75Srobert // can be used to filter the output.  This reporter relies on the linux diff
57*d415bd75Srobert // utility to do comparisons and insert the prefixes. For systems that do not
58*d415bd75Srobert // have the necessary facilities, the error message will be shown in place of
59*d415bd75Srobert // the expected output.
60*d415bd75Srobert cl::opt<ChangePrinter> llvm::PrintChanged(
61*d415bd75Srobert     "print-changed", cl::desc("Print changed IRs"), cl::Hidden,
62*d415bd75Srobert     cl::ValueOptional, cl::init(ChangePrinter::None),
63*d415bd75Srobert     cl::values(
64*d415bd75Srobert         clEnumValN(ChangePrinter::Quiet, "quiet", "Run in quiet mode"),
65*d415bd75Srobert         clEnumValN(ChangePrinter::DiffVerbose, "diff",
66*d415bd75Srobert                    "Display patch-like changes"),
67*d415bd75Srobert         clEnumValN(ChangePrinter::DiffQuiet, "diff-quiet",
68*d415bd75Srobert                    "Display patch-like changes in quiet mode"),
69*d415bd75Srobert         clEnumValN(ChangePrinter::ColourDiffVerbose, "cdiff",
70*d415bd75Srobert                    "Display patch-like changes with color"),
71*d415bd75Srobert         clEnumValN(ChangePrinter::ColourDiffQuiet, "cdiff-quiet",
72*d415bd75Srobert                    "Display patch-like changes in quiet mode with color"),
73*d415bd75Srobert         clEnumValN(ChangePrinter::DotCfgVerbose, "dot-cfg",
74*d415bd75Srobert                    "Create a website with graphical changes"),
75*d415bd75Srobert         clEnumValN(ChangePrinter::DotCfgQuiet, "dot-cfg-quiet",
76*d415bd75Srobert                    "Create a website with graphical changes in quiet mode"),
77*d415bd75Srobert         // Sentinel value for unspecified option.
78*d415bd75Srobert         clEnumValN(ChangePrinter::Verbose, "", "")));
79*d415bd75Srobert 
80*d415bd75Srobert // An option for specifying the diff used by print-changed=[diff | diff-quiet]
81*d415bd75Srobert static cl::opt<std::string>
82*d415bd75Srobert     DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"),
83*d415bd75Srobert                cl::desc("system diff used by change reporters"));
84*d415bd75Srobert 
8573471bf0Spatrick static cl::opt<bool>
8673471bf0Spatrick     PrintModuleScope("print-module-scope",
8773471bf0Spatrick                      cl::desc("When printing IR for print-[before|after]{-all} "
8873471bf0Spatrick                               "always print a module IR"),
8973471bf0Spatrick                      cl::init(false), cl::Hidden);
9073471bf0Spatrick 
91*d415bd75Srobert // See the description for -print-changed for an explanation of the use
92*d415bd75Srobert // of this option.
93*d415bd75Srobert static cl::list<std::string> FilterPasses(
94*d415bd75Srobert     "filter-passes", cl::value_desc("pass names"),
95*d415bd75Srobert     cl::desc("Only consider IR changes for passes whose names "
96*d415bd75Srobert              "match the specified value. No-op without -print-changed"),
97*d415bd75Srobert     cl::CommaSeparated, cl::Hidden);
98*d415bd75Srobert 
9973471bf0Spatrick static cl::list<std::string>
10073471bf0Spatrick     PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
10173471bf0Spatrick                    cl::desc("Only print IR for functions whose name "
10273471bf0Spatrick                             "match this for all print-[before|after][-all] "
10373471bf0Spatrick                             "options"),
10473471bf0Spatrick                    cl::CommaSeparated, cl::Hidden);
10573471bf0Spatrick 
10673471bf0Spatrick /// This is a helper to determine whether to print IR before or
10773471bf0Spatrick /// after a pass.
10873471bf0Spatrick 
shouldPrintBeforeSomePass()10973471bf0Spatrick bool llvm::shouldPrintBeforeSomePass() {
11073471bf0Spatrick   return PrintBeforeAll || !PrintBefore.empty();
11173471bf0Spatrick }
11273471bf0Spatrick 
shouldPrintAfterSomePass()11373471bf0Spatrick bool llvm::shouldPrintAfterSomePass() {
11473471bf0Spatrick   return PrintAfterAll || !PrintAfter.empty();
11573471bf0Spatrick }
11673471bf0Spatrick 
shouldPrintBeforeOrAfterPass(StringRef PassID,ArrayRef<std::string> PassesToPrint)11773471bf0Spatrick static bool shouldPrintBeforeOrAfterPass(StringRef PassID,
11873471bf0Spatrick                                          ArrayRef<std::string> PassesToPrint) {
11973471bf0Spatrick   return llvm::is_contained(PassesToPrint, PassID);
12073471bf0Spatrick }
12173471bf0Spatrick 
shouldPrintBeforeAll()12273471bf0Spatrick bool llvm::shouldPrintBeforeAll() { return PrintBeforeAll; }
12373471bf0Spatrick 
shouldPrintAfterAll()12473471bf0Spatrick bool llvm::shouldPrintAfterAll() { return PrintAfterAll; }
12573471bf0Spatrick 
shouldPrintBeforePass(StringRef PassID)12673471bf0Spatrick bool llvm::shouldPrintBeforePass(StringRef PassID) {
12773471bf0Spatrick   return PrintBeforeAll || shouldPrintBeforeOrAfterPass(PassID, PrintBefore);
12873471bf0Spatrick }
12973471bf0Spatrick 
shouldPrintAfterPass(StringRef PassID)13073471bf0Spatrick bool llvm::shouldPrintAfterPass(StringRef PassID) {
13173471bf0Spatrick   return PrintAfterAll || shouldPrintBeforeOrAfterPass(PassID, PrintAfter);
13273471bf0Spatrick }
13373471bf0Spatrick 
printBeforePasses()13473471bf0Spatrick std::vector<std::string> llvm::printBeforePasses() {
13573471bf0Spatrick   return std::vector<std::string>(PrintBefore);
13673471bf0Spatrick }
13773471bf0Spatrick 
printAfterPasses()13873471bf0Spatrick std::vector<std::string> llvm::printAfterPasses() {
13973471bf0Spatrick   return std::vector<std::string>(PrintAfter);
14073471bf0Spatrick }
14173471bf0Spatrick 
forcePrintModuleIR()14273471bf0Spatrick bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
14373471bf0Spatrick 
isPassInPrintList(StringRef PassName)144*d415bd75Srobert bool llvm::isPassInPrintList(StringRef PassName) {
145*d415bd75Srobert   static std::unordered_set<std::string> Set(FilterPasses.begin(),
146*d415bd75Srobert                                              FilterPasses.end());
147*d415bd75Srobert   return Set.empty() || Set.count(std::string(PassName));
148*d415bd75Srobert }
149*d415bd75Srobert 
isFilterPassesEmpty()150*d415bd75Srobert bool llvm::isFilterPassesEmpty() { return FilterPasses.empty(); }
151*d415bd75Srobert 
isFunctionInPrintList(StringRef FunctionName)15273471bf0Spatrick bool llvm::isFunctionInPrintList(StringRef FunctionName) {
15373471bf0Spatrick   static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
15473471bf0Spatrick                                                         PrintFuncsList.end());
15573471bf0Spatrick   return PrintFuncNames.empty() ||
15673471bf0Spatrick          PrintFuncNames.count(std::string(FunctionName));
15773471bf0Spatrick }
158*d415bd75Srobert 
cleanUpTempFilesImpl(ArrayRef<std::string> FileName,unsigned N)159*d415bd75Srobert std::error_code cleanUpTempFilesImpl(ArrayRef<std::string> FileName,
160*d415bd75Srobert                                      unsigned N) {
161*d415bd75Srobert   std::error_code RC;
162*d415bd75Srobert   for (unsigned I = 0; I < N; ++I) {
163*d415bd75Srobert     std::error_code EC = sys::fs::remove(FileName[I]);
164*d415bd75Srobert     if (EC)
165*d415bd75Srobert       RC = EC;
166*d415bd75Srobert   }
167*d415bd75Srobert   return RC;
168*d415bd75Srobert }
169*d415bd75Srobert 
prepareTempFiles(SmallVector<int> & FD,ArrayRef<StringRef> SR,SmallVector<std::string> & FileName)170*d415bd75Srobert std::error_code llvm::prepareTempFiles(SmallVector<int> &FD,
171*d415bd75Srobert                                        ArrayRef<StringRef> SR,
172*d415bd75Srobert                                        SmallVector<std::string> &FileName) {
173*d415bd75Srobert   assert(FD.size() >= SR.size() && FileName.size() == FD.size() &&
174*d415bd75Srobert          "Unexpected array sizes");
175*d415bd75Srobert   std::error_code EC;
176*d415bd75Srobert   unsigned I = 0;
177*d415bd75Srobert   for (; I < FD.size(); ++I) {
178*d415bd75Srobert     if (FD[I] == -1) {
179*d415bd75Srobert       SmallVector<char, 200> SV;
180*d415bd75Srobert       EC = sys::fs::createTemporaryFile("tmpfile", "txt", FD[I], SV);
181*d415bd75Srobert       if (EC)
182*d415bd75Srobert         break;
183*d415bd75Srobert       FileName[I] = Twine(SV).str();
184*d415bd75Srobert     }
185*d415bd75Srobert     if (I < SR.size()) {
186*d415bd75Srobert       EC = sys::fs::openFileForWrite(FileName[I], FD[I]);
187*d415bd75Srobert       if (EC)
188*d415bd75Srobert         break;
189*d415bd75Srobert       raw_fd_ostream OutStream(FD[I], /*shouldClose=*/true);
190*d415bd75Srobert       if (FD[I] == -1) {
191*d415bd75Srobert         EC = make_error_code(errc::io_error);
192*d415bd75Srobert         break;
193*d415bd75Srobert       }
194*d415bd75Srobert       OutStream << SR[I];
195*d415bd75Srobert     }
196*d415bd75Srobert   }
197*d415bd75Srobert   if (EC && I > 0)
198*d415bd75Srobert     // clean up created temporary files
199*d415bd75Srobert     cleanUpTempFilesImpl(FileName, I);
200*d415bd75Srobert   return EC;
201*d415bd75Srobert }
202*d415bd75Srobert 
cleanUpTempFiles(ArrayRef<std::string> FileName)203*d415bd75Srobert std::error_code llvm::cleanUpTempFiles(ArrayRef<std::string> FileName) {
204*d415bd75Srobert   return cleanUpTempFilesImpl(FileName, FileName.size());
205*d415bd75Srobert }
206*d415bd75Srobert 
doSystemDiff(StringRef Before,StringRef After,StringRef OldLineFormat,StringRef NewLineFormat,StringRef UnchangedLineFormat)207*d415bd75Srobert std::string llvm::doSystemDiff(StringRef Before, StringRef After,
208*d415bd75Srobert                                StringRef OldLineFormat, StringRef NewLineFormat,
209*d415bd75Srobert                                StringRef UnchangedLineFormat) {
210*d415bd75Srobert   // Store the 2 bodies into temporary files and call diff on them
211*d415bd75Srobert   // to get the body of the node.
212*d415bd75Srobert   static SmallVector<int> FD{-1, -1, -1};
213*d415bd75Srobert   SmallVector<StringRef> SR{Before, After};
214*d415bd75Srobert   static SmallVector<std::string> FileName{"", "", ""};
215*d415bd75Srobert   if (auto Err = prepareTempFiles(FD, SR, FileName))
216*d415bd75Srobert     return "Unable to create temporary file.";
217*d415bd75Srobert 
218*d415bd75Srobert   static ErrorOr<std::string> DiffExe = sys::findProgramByName(DiffBinary);
219*d415bd75Srobert   if (!DiffExe)
220*d415bd75Srobert     return "Unable to find diff executable.";
221*d415bd75Srobert 
222*d415bd75Srobert   SmallString<128> OLF, NLF, ULF;
223*d415bd75Srobert   ("--old-line-format=" + OldLineFormat).toVector(OLF);
224*d415bd75Srobert   ("--new-line-format=" + NewLineFormat).toVector(NLF);
225*d415bd75Srobert   ("--unchanged-line-format=" + UnchangedLineFormat).toVector(ULF);
226*d415bd75Srobert 
227*d415bd75Srobert   StringRef Args[] = {DiffBinary, "-w", "-d",        OLF,
228*d415bd75Srobert                       NLF,        ULF,  FileName[0], FileName[1]};
229*d415bd75Srobert   std::optional<StringRef> Redirects[] = {std::nullopt, StringRef(FileName[2]),
230*d415bd75Srobert                                           std::nullopt};
231*d415bd75Srobert   int Result = sys::ExecuteAndWait(*DiffExe, Args, std::nullopt, Redirects);
232*d415bd75Srobert   if (Result < 0)
233*d415bd75Srobert     return "Error executing system diff.";
234*d415bd75Srobert   std::string Diff;
235*d415bd75Srobert   auto B = MemoryBuffer::getFile(FileName[2]);
236*d415bd75Srobert   if (B && *B)
237*d415bd75Srobert     Diff = (*B)->getBuffer().str();
238*d415bd75Srobert   else
239*d415bd75Srobert     return "Unable to read result.";
240*d415bd75Srobert 
241*d415bd75Srobert   if (auto Err = cleanUpTempFiles(FileName))
242*d415bd75Srobert     return "Unable to remove temporary file.";
243*d415bd75Srobert 
244*d415bd75Srobert   return Diff;
245*d415bd75Srobert }
246