MLIR 24.0.0git
PassStatistics.cpp
Go to the documentation of this file.
1//===- PassStatistics.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
9#include "PassDetail.h"
11#include "llvm/ADT/StringExtras.h"
12#include "llvm/Support/Format.h"
13
14#include <cinttypes>
15
16using namespace mlir;
17using namespace mlir::detail;
18
19constexpr StringLiteral kPassStatsDescription =
20 "... Pass statistics report ...";
21
22namespace {
23/// Information pertaining to a specific statistic.
24struct Statistic {
25 const char *name, *desc;
26 uint64_t value;
27};
28} // namespace
29
30/// Utility to print a pass entry in the statistics output.
31static void printPassEntry(raw_ostream &os, unsigned indent, StringRef pass,
32 MutableArrayRef<Statistic> stats = {}) {
33 os.indent(indent) << pass << "\n";
34 if (stats.empty())
35 return;
36
37 // Make sure to sort the statistics by name.
38 llvm::array_pod_sort(
39 stats.begin(), stats.end(), [](const auto *lhs, const auto *rhs) {
40 return StringRef{lhs->name}.compare(StringRef{rhs->name});
41 });
42
43 // Collect the largest name and value length from each of the statistics.
44 size_t largestName = 0, largestValue = 0;
45 for (auto &stat : stats) {
46 largestName = std::max(largestName, (size_t)strlen(stat.name));
47 largestValue =
48 std::max(largestValue, (size_t)llvm::utostr(stat.value).size());
49 }
50
51 // Print each of the statistics.
52 for (auto &stat : stats) {
53 os.indent(indent + 2) << llvm::format(
54 "(S) %*" PRIu64 " %-*s - %s\n", static_cast<int>(largestValue),
55 stat.value, static_cast<int>(largestName), stat.name, stat.desc);
56 }
57}
58
59/// Print the statistics results in a list form, where each pass is sorted by
60/// name.
62 llvm::StringMap<std::vector<Statistic>> mergedStats;
63 std::function<void(Pass *)> addStats = [&](Pass *pass) {
64 auto *adaptor = dyn_cast<OpToOpPassAdaptor>(pass);
65
66 // If this is not an adaptor, add the stats to the list if there are any.
67 if (!adaptor) {
68#if LLVM_ENABLE_STATS
69 auto statistics = pass->getStatistics();
70 if (statistics.empty())
71 return;
72
73 auto &passEntry = mergedStats[pass->getName()];
74 if (passEntry.empty()) {
75 for (Pass::Statistic *it : pass->getStatistics())
76 passEntry.push_back({it->getName(), it->getDesc(), it->getValue()});
77 } else {
78 for (auto [idx, statistic] : llvm::enumerate(pass->getStatistics()))
79 passEntry[idx].value += statistic->getValue();
80 }
81#endif
82 return;
83 }
84
85 // Otherwise, recursively add each of the children.
86 for (auto &mgr : adaptor->getPassManagers())
87 for (Pass &pass : mgr.getPasses())
88 addStats(&pass);
89 };
90 for (Pass &pass : pm.getPasses())
91 addStats(&pass);
92
93 // Sort the statistics by pass name and then by record name.
94 auto passAndStatistics =
95 llvm::to_vector<16>(llvm::make_pointer_range(mergedStats));
96 llvm::array_pod_sort(passAndStatistics.begin(), passAndStatistics.end(),
97 [](const decltype(passAndStatistics)::value_type *lhs,
98 const decltype(passAndStatistics)::value_type *rhs) {
99 return (*lhs)->getKey().compare((*rhs)->getKey());
100 });
101
102 // Print the timing information sequentially.
103 for (auto &statData : passAndStatistics)
104 printPassEntry(os, /*indent=*/2, statData->first(), statData->second);
105}
106
107/// Print the results in pipeline mode that mirrors the internal pass manager
108/// structure.
110#if LLVM_ENABLE_STATS
111 std::function<void(unsigned, Pass *)> printPass = [&](unsigned indent,
112 Pass *pass) {
113 if (auto *adaptor = dyn_cast<OpToOpPassAdaptor>(pass)) {
114 // If this adaptor has more than one internal pipeline, print an entry for
115 // it.
116 auto mgrs = adaptor->getPassManagers();
117 if (mgrs.size() > 1) {
118 printPassEntry(os, indent, adaptor->getAdaptorName());
119 indent += 2;
120 }
121
122 // Print each of the children passes.
123 for (OpPassManager &mgr : mgrs) {
124 auto name = ("'" + mgr.getOpAnchorName() + "' Pipeline").str();
125 printPassEntry(os, indent, name);
126 for (Pass &pass : mgr.getPasses())
127 printPass(indent + 2, &pass);
128 }
129 return;
130 }
131
132 // Otherwise, we print the statistics for this pass.
133 std::vector<Statistic> stats;
134 for (Pass::Statistic *stat : pass->getStatistics())
135 stats.push_back({stat->getName(), stat->getDesc(), stat->getValue()});
136 printPassEntry(os, indent, pass->getName(), stats);
137 };
138 for (Pass &pass : pm.getPasses())
139 printPass(/*indent=*/0, &pass);
140#endif
141}
142
143static void printStatistics(OpPassManager &pm, PassDisplayMode displayMode) {
144 auto os = llvm::CreateInfoOutputFile();
145
146 // Print the stats header.
147 *os << "===" << std::string(73, '-') << "===\n";
148 // Figure out how many spaces for the description name.
149 unsigned padding = (80 - kPassStatsDescription.size()) / 2;
150 os->indent(padding) << kPassStatsDescription << '\n';
151 *os << "===" << std::string(73, '-') << "===\n";
152
153 // Defer to a specialized printer for each display mode.
154 switch (displayMode) {
156 printResultsAsList(*os, pm);
157 break;
159 printResultsAsPipeline(*os, pm);
160 break;
161 }
162 *os << "\n";
163 os->flush();
164}
165
166//===----------------------------------------------------------------------===//
167// PassStatistics
168//===----------------------------------------------------------------------===//
169
170Pass::Statistic::Statistic(Pass *owner, const char *name,
171 const char *description)
172 : llvm::Statistic{/*DebugType=*/"", name, description} {
173#if LLVM_ENABLE_STATS
174 // Always set the 'initialized' bit to true so that this statistic isn't
175 // placed in the static registry.
176 // TODO: This is sort of hack as `llvm::Statistic`s can't be setup to avoid
177 // automatic registration with the global registry. We should either add
178 // support for this in LLVM, or just write our own statistics classes.
179 Initialized = true;
180#endif
181
182 // Register this statistic with the parent.
183 owner->statistics.push_back(this);
184}
185
186auto Pass::Statistic::operator=(unsigned value) -> Statistic & {
187 llvm::Statistic::operator=(value);
188 return *this;
189}
190
191//===----------------------------------------------------------------------===//
192// PassManager
193//===----------------------------------------------------------------------===//
194
195/// Merge the pass statistics of this class into 'other'.
197 auto passes = getPasses(), otherPasses = other.getPasses();
198
199 for (auto passPair : llvm::zip(passes, otherPasses)) {
200 Pass &pass = std::get<0>(passPair), &otherPass = std::get<1>(passPair);
201
202 // If this is an adaptor, then recursively merge the pass managers.
203 if (auto *adaptorPass = dyn_cast<OpToOpPassAdaptor>(&pass)) {
204 auto *otherAdaptorPass = cast<OpToOpPassAdaptor>(&otherPass);
205 for (auto mgrs : llvm::zip(adaptorPass->getPassManagers(),
206 otherAdaptorPass->getPassManagers()))
207 std::get<0>(mgrs).mergeStatisticsInto(std::get<1>(mgrs));
208 continue;
209 }
210 // Otherwise, merge the statistics for the current pass.
211 assert(pass.statistics.size() == otherPass.statistics.size());
212 for (unsigned i = 0, e = pass.statistics.size(); i != e; ++i) {
213 assert(pass.statistics[i]->getName() ==
214 StringRef(otherPass.statistics[i]->getName()));
215 *otherPass.statistics[i] += *pass.statistics[i];
216 *pass.statistics[i] = 0;
217 }
218 }
219}
220
221/// Prepare the statistics of passes within the given pass manager for
222/// consumption(e.g. dumping).
224 for (auto &adaptor :
225 llvm::make_isa_range<OpToOpPassAdaptor>(pm.getPasses())) {
226 MutableArrayRef<OpPassManager> nestedPms = adaptor.getPassManagers();
227
228 // Merge the statistics from the async pass managers into the main nested
229 // pass managers. Prepare recursively before merging.
230 for (auto &asyncPM : adaptor.getParallelPassManagers()) {
231 for (unsigned i = 0, e = asyncPM.size(); i != e; ++i) {
232 prepareStatistics(asyncPM[i]);
233 asyncPM[i].mergeStatisticsInto(nestedPms[i]);
234 }
235 }
236
237 // Prepare the statistics of each of the nested passes.
238 for (OpPassManager &nestedPM : nestedPms)
239 prepareStatistics(nestedPM);
240 }
241}
242
243/// Dump the statistics of the passes within this pass manager.
244void PassManager::dumpStatistics() {
245 prepareStatistics(*this);
246 printStatistics(*this, *passStatisticsMode);
247}
248
249/// Dump the statistics for each pass after running.
251 passStatisticsMode = displayMode;
252}
static void printResultsAsList(raw_ostream &os, OpPassManager &pm)
Print the statistics results in a list form, where each pass is sorted by name.
static void printResultsAsPipeline(raw_ostream &os, OpPassManager &pm)
Print the results in pipeline mode that mirrors the internal pass manager structure.
static void printStatistics(OpPassManager &pm, PassDisplayMode displayMode)
static void printPassEntry(raw_ostream &os, unsigned indent, StringRef pass, MutableArrayRef< Statistic > stats={})
Utility to print a pass entry in the statistics output.
static void prepareStatistics(OpPassManager &pm)
Prepare the statistics of passes within the given pass manager for consumption(e.g.
constexpr StringLiteral kPassStatsDescription
This class represents a pass manager that runs passes on either a specific operation type,...
Definition PassManager.h:46
void mergeStatisticsInto(OpPassManager &other)
Merge the pass statistics of this class into 'other'.
OpPassManager(Nesting nesting=Nesting::Explicit)
Construct a new op-agnostic ("any") pass manager with the given operation type and nesting behavior.
Definition Pass.cpp:348
friend class Pass
iterator_range< pass_iterator > getPasses()
Definition PassManager.h:79
void enableStatistics(PassDisplayMode displayMode=PassDisplayMode::Pipeline)
Prompts the pass manager to print the statistics collected for each of the held passes after each cal...
This class represents a single pass statistic.
Definition Pass.h:134
Statistic(Pass *owner, const char *name, const char *description)
The statistic is initialized by the pass owner, a name, and a description.
Statistic & operator=(unsigned value)
Assign the statistic to the given value.
The abstract base pass class.
Definition Pass.h:52
Pass(TypeID passID, std::optional< StringRef > opName=std::nullopt)
Definition Pass.h:164
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
AttrTypeReplacer.
Include the generated interface declarations.
PassDisplayMode
An enum describing the different display modes for the information within the pass manager.