1//===- TestPrintNesting.cpp - Passes to illustrate the IR nesting ---------===//
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/BuiltinOps.h"
10#include "mlir/Pass/Pass.h"
11
12using namespace mlir;
13
14namespace {
15/// This pass illustrates the IR nesting through printing.
16struct TestPrintNestingPass
17 : public PassWrapper<TestPrintNestingPass, OperationPass<>> {
18 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestPrintNestingPass)
19
20 StringRef getArgument() const final { return "test-print-nesting"; }
21 StringRef getDescription() const final { return "Test various printing."; }
22 // Entry point for the pass.
23 void runOnOperation() override {
24 Operation *op = getOperation();
25 resetIndent();
26 printOperation(op);
27 }
28
29 /// The three methods below are mutually recursive and follow the nesting of
30 /// the IR: operation->region->block->operation->...
31
32 void printOperation(Operation *op) {
33 // Print the operation itself and some of its properties
34 printIndent() << "visiting op: '" << op->getName() << "' with "
35 << op->getNumOperands() << " operands and "
36 << op->getNumResults() << " results\n";
37 // Print the operation attributes
38 if (!op->getDiscardableAttrDictionary().getValue().empty()) {
39 printIndent() << op->getDiscardableAttrDictionary().getValue().size()
40 << " attributes:\n";
41 for (NamedAttribute attr : op->getDiscardableAttrDictionary().getValue())
42 printIndent() << " - '" << attr.getName().getValue() << "' : '"
43 << attr.getValue() << "'\n";
44 }
45
46 // Recurse into each of the regions attached to the operation.
47 printIndent() << " " << op->getNumRegions() << " nested regions:\n";
48 auto indent = pushIndent();
49 for (Region &region : op->getRegions())
50 printRegion(region);
51 }
52
53 void printRegion(Region &region) {
54 // A region does not hold anything by itself other than a list of blocks.
55 printIndent() << "Region with " << region.getBlocks().size()
56 << " blocks:\n";
57 auto indent = pushIndent();
58 for (Block &block : region.getBlocks())
59 printBlock(block);
60 }
61
62 void printBlock(Block &block) {
63 // Print the block intrinsics properties (basically: argument list)
64 printIndent()
65 << "Block with " << block.getNumArguments() << " arguments, "
66 << block.getNumSuccessors()
67 << " successors, and "
68 // Note, this `.size()` is traversing a linked-list and is O(n).
69 << block.getOperations().size() << " operations\n";
70
71 // Block main role is to hold a list of Operations: let's recurse.
72 auto indent = pushIndent();
73 for (Operation &op : block.getOperations())
74 printOperation(op: &op);
75 }
76
77 /// Manages the indentation as we traverse the IR nesting.
78 int indent;
79 struct IdentRAII {
80 int &indent;
81 IdentRAII(int &indent) : indent(indent) {}
82 ~IdentRAII() { --indent; }
83 };
84 void resetIndent() { indent = 0; }
85 IdentRAII pushIndent() { return IdentRAII(++indent); }
86
87 llvm::raw_ostream &printIndent() {
88 for (int i = 0; i < indent; ++i)
89 llvm::outs() << " ";
90 return llvm::outs();
91 }
92};
93} // namespace
94
95namespace mlir {
96void registerTestPrintNestingPass() {
97 PassRegistration<TestPrintNestingPass>();
98}
99} // namespace mlir
100