MLIR  22.0.0git
OpStats.cpp
Go to the documentation of this file.
1 //===- OpStats.cpp - Prints stats of operations in module -----------------===//
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 
11 #include "mlir/IR/Operation.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
15 
16 namespace mlir {
17 #define GEN_PASS_DEF_PRINTOPSTATS
18 #include "mlir/Transforms/Passes.h.inc"
19 } // namespace mlir
20 
21 using namespace mlir;
22 
23 namespace {
24 struct PrintOpStatsPass : public impl::PrintOpStatsBase<PrintOpStatsPass> {
25  explicit PrintOpStatsPass(raw_ostream &os) : os(os) {}
26 
27  explicit PrintOpStatsPass(raw_ostream &os, bool printAsJSON) : os(os) {
28  this->printAsJSON = printAsJSON;
29  }
30 
31  // Prints the resultant operation statistics post iterating over the module.
32  void runOnOperation() override;
33 
34  // Print summary of op stats.
35  void printSummary();
36 
37  // Print symmary of op stats in JSON.
38  void printSummaryInJSON();
39 
40 private:
41  llvm::StringMap<int64_t> opCount;
42  raw_ostream &os;
43 };
44 } // namespace
45 
46 void PrintOpStatsPass::runOnOperation() {
47  opCount.clear();
48 
49  // Compute the operation statistics for the currently visited operation.
50  getOperation()->walk(
51  [&](Operation *op) { ++opCount[op->getName().getStringRef()]; });
52  if (printAsJSON)
53  printSummaryInJSON();
54  else
55  printSummary();
56  markAllAnalysesPreserved();
57 }
58 
59 void PrintOpStatsPass::printSummary() {
60  os << "Operations encountered:\n";
61  os << "-----------------------\n";
62  SmallVector<StringRef, 64> sorted(opCount.keys());
63  llvm::sort(sorted);
64 
65  // Split an operation name from its dialect prefix.
66  auto splitOperationName = [](StringRef opName) {
67  auto splitName = opName.split('.');
68  return splitName.second.empty() ? std::make_pair("", splitName.first)
69  : splitName;
70  };
71 
72  // Compute the largest dialect and operation name.
73  size_t maxLenOpName = 0, maxLenDialect = 0;
74  for (const auto &key : sorted) {
75  auto [dialectName, opName] = splitOperationName(key);
76  maxLenDialect = std::max(maxLenDialect, dialectName.size());
77  maxLenOpName = std::max(maxLenOpName, opName.size());
78  }
79 
80  for (const auto &key : sorted) {
81  auto [dialectName, opName] = splitOperationName(key);
82 
83  // Left-align the names (aligning on the dialect) and right-align the count
84  // below. The alignment is for readability and does not affect CSV/FileCheck
85  // parsing.
86  if (dialectName.empty())
87  os.indent(maxLenDialect + 3);
88  else
89  os << llvm::right_justify(dialectName, maxLenDialect + 2) << '.';
90 
91  // Left justify the operation name.
92  os << llvm::left_justify(opName, maxLenOpName) << " , " << opCount[key]
93  << '\n';
94  }
95 }
96 
97 void PrintOpStatsPass::printSummaryInJSON() {
98  SmallVector<StringRef, 64> sorted(opCount.keys());
99  llvm::sort(sorted);
100 
101  os << "{\n";
102 
103  for (unsigned i = 0, e = sorted.size(); i != e; ++i) {
104  const auto &key = sorted[i];
105  os << " \"" << key << "\" : " << opCount[key];
106  if (i != e - 1)
107  os << ",\n";
108  else
109  os << "\n";
110  }
111  os << "}\n";
112 }
113 
114 std::unique_ptr<Pass> mlir::createPrintOpStatsPass(raw_ostream &os) {
115  return std::make_unique<PrintOpStatsPass>(os);
116 }
117 
118 std::unique_ptr<Pass> mlir::createPrintOpStatsPass(raw_ostream &os,
119  bool printAsJSON) {
120  return std::make_unique<PrintOpStatsPass>(os, printAsJSON);
121 }
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
OperationName getName()
The name of an operation is the key identifier for it.
Definition: Operation.h:119
Include the generated interface declarations.
std::unique_ptr< Pass > createPrintOpStatsPass(raw_ostream &os=llvm::errs())
Creates a pass which prints the list of ops and the number of occurrences in the module.
Definition: OpStats.cpp:114