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