LLVM 23.0.0git
PrintPasses.cpp
Go to the documentation of this file.
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
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/Twine.h"
13#include "llvm/Support/Errc.h"
20#include <unordered_set>
21
22using namespace llvm;
23
24// Print IR out before/after specified passes.
26 PrintBefore("print-before",
27 llvm::cl::desc("Print IR before specified passes"),
29
31 PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"),
33
34static cl::opt<bool> PrintBeforeAll("print-before-all",
35 llvm::cl::desc("Print IR before each pass"),
36 cl::init(false), cl::Hidden);
37static cl::opt<bool> PrintAfterAll("print-after-all",
38 llvm::cl::desc("Print IR after each pass"),
39 cl::init(false), cl::Hidden);
40
41// Print out the IR after passes, similar to -print-after-all except that it
42// only prints the IR after passes that change the IR. Those passes that do not
43// make changes to the IR are reported as not making any changes. In addition,
44// the initial IR is also reported. Other hidden options affect the output from
45// this option. -filter-passes will limit the output to the named passes that
46// actually change the IR and other passes are reported as filtered out. The
47// specified passes will either be reported as making no changes (with no IR
48// reported) or the changed IR will be reported. Also, the -filter-print-funcs
49// and -print-module-scope options will do similar filtering based on function
50// name, reporting changed IRs as functions(or modules if -print-module-scope is
51// specified) for a particular function or indicating that the IR has been
52// filtered out. The extra options can be combined, allowing only changed IRs
53// for certain passes on certain functions to be reported in different formats,
54// with the rest being reported as filtered out. The -print-before-changed
55// option will print the IR as it was before each pass that changed it. The
56// optional value of quiet will only report when the IR changes, suppressing all
57// other messages, including the initial IR. The values "diff" and "diff-quiet"
58// will present the changes in a form similar to a patch, in either verbose or
59// quiet mode, respectively. The lines that are removed and added are prefixed
60// with '-' and '+', respectively. The -filter-print-funcs and -filter-passes
61// can be used to filter the output. This reporter relies on the linux diff
62// utility to do comparisons and insert the prefixes. For systems that do not
63// have the necessary facilities, the error message will be shown in place of
64// the expected output.
66 "print-changed", cl::desc("Print changed IRs"), cl::Hidden,
69 clEnumValN(ChangePrinter::Quiet, "quiet", "Run in quiet mode"),
71 "Display patch-like changes"),
73 "Display patch-like changes in quiet mode"),
75 "Display patch-like changes with color"),
77 "Display patch-like changes in quiet mode with color"),
79 "Create a website with graphical changes"),
81 "Create a website with graphical changes in quiet mode"),
82 // Sentinel value for unspecified option.
84
85// An option for specifying the diff used by print-changed=[diff | diff-quiet]
87 DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"),
88 cl::desc("system diff used by change reporters"));
89
90static cl::opt<bool>
91 PrintModuleScope("print-module-scope",
92 cl::desc("When printing IR for print-[before|after]{-all} "
93 "always print a module IR"),
94 cl::init(false), cl::Hidden);
95
97 "print-loop-func-scope",
98 cl::desc("When printing IR for print-[before|after]{-all} "
99 "for a loop pass, always print function IR"),
100 cl::init(false), cl::Hidden);
101
102// See the description for -print-changed for an explanation of the use
103// of this option.
105 "filter-passes", cl::value_desc("pass names"),
106 cl::desc("Only consider IR changes for passes whose names "
107 "match the specified value. No-op without -print-changed"),
109
111 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
112 cl::desc("Only print IR for functions whose name "
113 "match this for all print-[before|after][-all] "
114 "options"),
116
117/// This is a helper to determine whether to print IR before or
118/// after a pass.
119
121 return PrintBeforeAll || !PrintBefore.empty();
122}
123
125 return PrintAfterAll || !PrintAfter.empty();
126}
127
129 ArrayRef<std::string> PassesToPrint) {
130 return llvm::is_contained(PassesToPrint, PassID);
131}
132
134
136
140
144
145std::vector<std::string> llvm::printBeforePasses() {
146 return std::vector<std::string>(PrintBefore);
147}
148
149std::vector<std::string> llvm::printAfterPasses() {
150 return std::vector<std::string>(PrintAfter);
151}
152
154
156
158 static std::unordered_set<std::string> Set(FilterPasses.begin(),
159 FilterPasses.end());
160 return Set.empty() || Set.count(std::string(PassName));
161}
162
163bool llvm::isFilterPassesEmpty() { return FilterPasses.empty(); }
164
166 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
167 PrintFuncsList.end());
168 return PrintFuncNames.empty() ||
169 PrintFuncNames.count(std::string(FunctionName));
170}
171
173 unsigned N) {
174 std::error_code RC;
175 for (unsigned I = 0; I < N; ++I) {
176 std::error_code EC = sys::fs::remove(FileName[I]);
177 if (EC)
178 RC = EC;
179 }
180 return RC;
181}
182
185 SmallVector<std::string> &FileName) {
186 assert(FD.size() >= SR.size() && FileName.size() == FD.size() &&
187 "Unexpected array sizes");
188 std::error_code EC;
189 unsigned I = 0;
190 for (; I < FD.size(); ++I) {
191 if (FD[I] == -1) {
193 EC = sys::fs::createTemporaryFile("tmpfile", "txt", FD[I], SV);
194 if (EC)
195 break;
196 FileName[I] = Twine(SV).str();
197 }
198 if (I < SR.size()) {
199 EC = sys::fs::openFileForWrite(FileName[I], FD[I]);
200 if (EC)
201 break;
202 raw_fd_ostream OutStream(FD[I], /*shouldClose=*/true);
203 if (FD[I] == -1) {
205 break;
206 }
207 OutStream << SR[I];
208 }
209 }
210 if (EC && I > 0)
211 // clean up created temporary files
212 cleanUpTempFilesImpl(FileName, I);
213 return EC;
214}
215
217 return cleanUpTempFilesImpl(FileName, FileName.size());
218}
219
220std::string llvm::doSystemDiff(StringRef Before, StringRef After,
221 StringRef OldLineFormat, StringRef NewLineFormat,
222 StringRef UnchangedLineFormat) {
223 auto BypassSandbox = sys::sandbox::scopedDisable();
224
225 // Store the 2 bodies into temporary files and call diff on them
226 // to get the body of the node.
227 static SmallVector<int> FD{-1, -1, -1};
228 SmallVector<StringRef> SR{Before, After};
229 static SmallVector<std::string> FileName{"", "", ""};
230 if (prepareTempFiles(FD, SR, FileName))
231 return "Unable to create temporary file.";
232
234 if (!DiffExe)
235 return "Unable to find diff executable.";
236
237 SmallString<128> OLF, NLF, ULF;
238 ("--old-line-format=" + OldLineFormat).toVector(OLF);
239 ("--new-line-format=" + NewLineFormat).toVector(NLF);
240 ("--unchanged-line-format=" + UnchangedLineFormat).toVector(ULF);
241
242 StringRef Args[] = {DiffBinary, "-w", "-d", OLF,
243 NLF, ULF, FileName[0], FileName[1]};
244 std::optional<StringRef> Redirects[] = {std::nullopt, StringRef(FileName[2]),
245 std::nullopt};
246 int Result = sys::ExecuteAndWait(*DiffExe, Args, std::nullopt, Redirects);
247 if (Result < 0)
248 return "Error executing system diff.";
249 std::string Diff;
250 auto B = MemoryBuffer::getFile(FileName[2]);
251 if (B && *B)
252 Diff = (*B)->getBuffer().str();
253 else
254 return "Unable to read result.";
255
256 if (cleanUpTempFiles(FileName))
257 return "Unable to remove temporary file.";
258
259 return Diff;
260}
261
264 StringRef IRName, bool IsInteresting,
265 bool ShouldReport) {
266 if (!ShouldReport && IsInteresting)
267 return;
268
269 if (IsInteresting && Before != After) {
270 errs() << ("*** IR Dump After " + PassName + " (" + PassID + ") on " +
271 IRName + " ***\n");
272 switch (PrintChanged) {
277 case ChangePrinter::DotCfgQuiet: // unimplemented
278 case ChangePrinter::DotCfgVerbose: // unimplemented
279 errs() << After;
280 break;
285 bool Color = llvm::is_contained(
287 PrintChanged.getValue());
288 StringRef Removed = Color ? "\033[31m-%l\033[0m\n" : "-%l\n";
289 StringRef Added = Color ? "\033[32m+%l\033[0m\n" : "+%l\n";
290 StringRef NoChange = " %l\n";
291 errs() << doSystemDiff(Before, After, Removed, Added, NoChange);
292 break;
293 }
294 }
298 PrintChanged.getValue())) {
299 const char *Reason =
300 IsInteresting ? " omitted because no change" : " filtered out";
301 errs() << "*** IR Dump After " << PassName;
302 if (!PassID.empty())
303 errs() << " (" << PassID << ")";
304 errs() << " on " << IRName + Reason + " ***\n";
305 }
306}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define I(x, y, z)
Definition MD5.cpp:57
static bool shouldPrintBeforeOrAfterPass(StringRef PassID, ArrayRef< std::string > PassesToPrint)
static cl::opt< bool > PrintBeforeAll("print-before-all", llvm::cl::desc("Print IR before each pass"), cl::init(false), cl::Hidden)
static cl::opt< bool > PrintModuleScope("print-module-scope", cl::desc("When printing IR for print-[before|after]{-all} " "always print a module IR"), cl::init(false), cl::Hidden)
static cl::list< std::string > PrintBefore("print-before", llvm::cl::desc("Print IR before specified passes"), cl::CommaSeparated, cl::Hidden)
static cl::list< std::string > FilterPasses("filter-passes", cl::value_desc("pass names"), cl::desc("Only consider IR changes for passes whose names " "match the specified value. No-op without -print-changed"), cl::CommaSeparated, cl::Hidden)
static cl::list< std::string > PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"), cl::CommaSeparated, cl::Hidden)
static cl::opt< bool > LoopPrintFuncScope("print-loop-func-scope", cl::desc("When printing IR for print-[before|after]{-all} " "for a loop pass, always print function IR"), cl::init(false), cl::Hidden)
static cl::opt< bool > PrintAfterAll("print-after-all", llvm::cl::desc("Print IR after each pass"), cl::init(false), cl::Hidden)
static cl::opt< std::string > DiffBinary("print-changed-diff-path", cl::Hidden, cl::init("diff"), cl::desc("system diff used by change reporters"))
std::error_code cleanUpTempFilesImpl(ArrayRef< std::string > FileName, unsigned N)
static cl::list< std::string > PrintFuncsList("filter-print-funcs", cl::value_desc("function names"), cl::desc("Only print IR for functions whose name " "match this for all print-[before|after][-all] " "options"), cl::CommaSeparated, cl::Hidden)
This file contains some templates that are useful if you are working with the STL at all.
static const char PassName[]
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
Represents either an error or a value T.
Definition ErrorOr.h:56
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
A raw_ostream that writes to a file descriptor.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
LLVM_ABI std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
LLVM_ABI std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None)
Create a file in the system temporary directory.
Definition Path.cpp:936
ScopedSetting scopedDisable()
Definition IOSandbox.h:36
LLVM_ABI ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
LLVM_ABI int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env=std::nullopt, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, std::optional< ProcessStatistics > *ProcStat=nullptr, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
Definition Program.cpp:32
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code prepareTempFiles(SmallVector< int > &FD, ArrayRef< StringRef > SR, SmallVector< std::string > &FileName)
LLVM_ABI bool forcePrintModuleIR()
std::error_code make_error_code(BitcodeError E)
LLVM_ABI std::vector< std::string > printAfterPasses()
void reportChangedIR(StringRef Before, StringRef After, StringRef PassName, StringRef PassID, StringRef IRName, bool IsInteresting, bool ShouldReport)
LLVM_ABI bool shouldPrintBeforeAll()
LLVM_ABI bool shouldPrintAfterAll()
LLVM_ABI cl::opt< ChangePrinter > PrintChanged
@ io_error
Definition Errc.h:58
LLVM_ABI std::vector< std::string > printBeforePasses()
LLVM_ABI bool shouldPrintBeforeSomePass()
This is a helper to determine whether to print IR before or after a pass.
LLVM_ABI bool shouldPrintAfterSomePass()
LLVM_ABI bool isFunctionInPrintList(StringRef FunctionName)
LLVM_ABI bool isPassInPrintList(StringRef PassName)
LLVM_ABI std::error_code cleanUpTempFiles(ArrayRef< std::string > FileName)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI std::string doSystemDiff(StringRef Before, StringRef After, StringRef OldLineFormat, StringRef NewLineFormat, StringRef UnchangedLineFormat)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
LLVM_ABI bool shouldPrintBeforePass(StringRef PassID)
LLVM_ABI bool shouldPrintAfterPass(StringRef PassID)
LLVM_ABI bool isFilterPassesEmpty()
LLVM_ABI bool forcePrintFuncIR()
#define N