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