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 "mlir/IR/Module.h"
10 #include "mlir/IR/Operation.h"
11 #include "mlir/IR/OperationSupport.h"
12 #include "mlir/Pass/Pass.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
22     : public PassWrapper<PrintOpStatsPass, OperationPass<ModuleOp>> {
23 /// Include the generated pass utilities.
24 #define GEN_PASS_PrintOpStats
25 #include "mlir/Transforms/Passes.h.inc"
26 
27   explicit PrintOpStatsPass(raw_ostream &os = llvm::errs()) : os(os) {}
28 
29   // Prints the resultant operation statistics post iterating over the module.
30   void runOnOperation() override;
31 
32   // Print summary of op stats.
33   void printSummary();
34 
35 private:
36   llvm::StringMap<int64_t> opCount;
37   raw_ostream &os;
38 };
39 } // namespace
40 
41 void PrintOpStatsPass::runOnOperation() {
42   opCount.clear();
43 
44   // Compute the operation statistics for each function in the module.
45   for (auto &op : getOperation())
46     op.walk([&](Operation *op) { ++opCount[op->getName().getStringRef()]; });
47   printSummary();
48 }
49 
50 void PrintOpStatsPass::printSummary() {
51   os << "Operations encountered:\n";
52   os << "-----------------------\n";
53   SmallVector<StringRef, 64> sorted(opCount.keys());
54   llvm::sort(sorted);
55 
56   // Split an operation name from its dialect prefix.
57   auto splitOperationName = [](StringRef opName) {
58     auto splitName = opName.split('.');
59     return splitName.second.empty() ? std::make_pair("", splitName.first)
60                                     : splitName;
61   };
62 
63   // Compute the largest dialect and operation name.
64   StringRef dialectName, opName;
65   size_t maxLenOpName = 0, maxLenDialect = 0;
66   for (const auto &key : sorted) {
67     std::tie(dialectName, opName) = splitOperationName(key);
68     maxLenDialect = std::max(maxLenDialect, dialectName.size());
69     maxLenOpName = std::max(maxLenOpName, opName.size());
70   }
71 
72   for (const auto &key : sorted) {
73     std::tie(dialectName, opName) = splitOperationName(key);
74 
75     // Left-align the names (aligning on the dialect) and right-align the count
76     // below. The alignment is for readability and does not affect CSV/FileCheck
77     // parsing.
78     if (dialectName.empty())
79       os.indent(maxLenDialect + 3);
80     else
81       os << llvm::right_justify(dialectName, maxLenDialect + 2) << '.';
82 
83     // Left justify the operation name.
84     os << llvm::left_justify(opName, maxLenOpName) << " , " << opCount[key]
85        << '\n';
86   }
87 }
88 
89 std::unique_ptr<OperationPass<ModuleOp>> mlir::createPrintOpStatsPass() {
90   return std::make_unique<PrintOpStatsPass>();
91 }
92