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"
10#include "mlir/Pass/PassManager.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(NumSpaces: 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 Start: stats.begin(), End: stats.end(), Compare: [](const auto *lhs, const auto *rhs) {
40 return StringRef{lhs->name}.compare(RHS: 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(a: largestName, b: (size_t)strlen(s: stat.name));
47 largestValue =
48 std::max(a: largestValue, b: (size_t)llvm::utostr(X: stat.value).size());
49 }
50
51 // Print each of the statistics.
52 for (auto &stat : stats) {
53 os.indent(NumSpaces: indent + 2) << llvm::format(
54 Fmt: "(S) %*" PRIu64 " %-*s - %s\n", Vals: static_cast<int>(largestValue),
55 Vals: stat.value, Vals: static_cast<int>(largestName), Vals: stat.name, Vals: stat.desc);
56 }
57}
58
59/// Print the statistics results in a list form, where each pass is sorted by
60/// name.
61static void printResultsAsList(raw_ostream &os, OpPassManager &pm) {
62 llvm::StringMap<std::vector<Statistic>> mergedStats;
63 std::function<void(Pass *)> addStats = [&](Pass *pass) {
64 auto *adaptor = dyn_cast<OpToOpPassAdaptor>(Val: 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>(Range: llvm::make_pointer_range(Range&: mergedStats));
96 llvm::array_pod_sort(Start: passAndStatistics.begin(), End: passAndStatistics.end(),
97 Compare: [](const decltype(passAndStatistics)::value_type *lhs,
98 const decltype(passAndStatistics)::value_type *rhs) {
99 return (*lhs)->getKey().compare(RHS: (*rhs)->getKey());
100 });
101
102 // Print the timing information sequentially.
103 for (auto &statData : passAndStatistics)
104 printPassEntry(os, /*indent=*/2, pass: statData->first(), stats: statData->second);
105}
106
107/// Print the results in pipeline mode that mirrors the internal pass manager
108/// structure.
109static void printResultsAsPipeline(raw_ostream &os, OpPassManager &pm) {
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(NumSpaces: padding) << kPassStatsDescription << '\n';
151 *os << "===" << std::string(73, '-') << "===\n";
152
153 // Defer to a specialized printer for each display mode.
154 switch (displayMode) {
155 case PassDisplayMode::List:
156 printResultsAsList(os&: *os, pm);
157 break;
158 case PassDisplayMode::Pipeline:
159 printResultsAsPipeline(os&: *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(x: this);
184}
185
186auto Pass::Statistic::operator=(unsigned value) -> Statistic & {
187 llvm::Statistic::operator=(Val: value);
188 return *this;
189}
190
191//===----------------------------------------------------------------------===//
192// PassManager
193//===----------------------------------------------------------------------===//
194
195/// Merge the pass statistics of this class into 'other'.
196void OpPassManager::mergeStatisticsInto(OpPassManager &other) {
197 auto passes = getPasses(), otherPasses = other.getPasses();
198
199 for (auto passPair : llvm::zip(t&: passes, u&: otherPasses)) {
200 Pass &pass = std::get<0>(t&: passPair), &otherPass = std::get<1>(t&: passPair);
201
202 // If this is an adaptor, then recursively merge the pass managers.
203 if (auto *adaptorPass = dyn_cast<OpToOpPassAdaptor>(Val: &pass)) {
204 auto *otherAdaptorPass = cast<OpToOpPassAdaptor>(Val: &otherPass);
205 for (auto mgrs : llvm::zip(t: adaptorPass->getPassManagers(),
206 u: otherAdaptorPass->getPassManagers()))
207 std::get<0>(t&: mgrs).mergeStatisticsInto(other&: std::get<1>(t&: 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).
223static void prepareStatistics(OpPassManager &pm) {
224 for (auto &adaptor :
225 llvm::make_isa_range<OpToOpPassAdaptor>(Range: 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(pm&: asyncPM[i]);
233 asyncPM[i].mergeStatisticsInto(other&: nestedPms[i]);
234 }
235 }
236
237 // Prepare the statistics of each of the nested passes.
238 for (OpPassManager &nestedPM : nestedPms)
239 prepareStatistics(pm&: nestedPM);
240 }
241}
242
243/// Dump the statistics of the passes within this pass manager.
244void PassManager::dumpStatistics() {
245 prepareStatistics(pm&: *this);
246 printStatistics(pm&: *this, displayMode: *passStatisticsMode);
247}
248
249/// Dump the statistics for each pass after running.
250void PassManager::enableStatistics(PassDisplayMode displayMode) {
251 passStatisticsMode = displayMode;
252}
253