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