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