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