1 //===- TestPrintDefUse.cpp - Passes to illustrate the IR def-use chains ---===//
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/Dialect/StandardOps/IR/Ops.h"
10 #include "mlir/IR/BuiltinOps.h"
11 #include "mlir/Pass/Pass.h"
12 
13 using namespace mlir;
14 
15 namespace {
16 /// This pass illustrates the IR def-use chains through printing.
17 struct TestPrintDefUsePass
18     : public PassWrapper<TestPrintDefUsePass, OperationPass<>> {
19   StringRef getArgument() const final { return "test-print-defuse"; }
20   StringRef getDescription() const final { return "Test various printing."; }
21   void runOnOperation() override {
22     // Recursively traverse the IR nested under the current operation and print
23     // every single operation and their operands and users.
24     getOperation()->walk([](Operation *op) {
25       llvm::outs() << "Visiting op '" << op->getName() << "' with "
26                    << op->getNumOperands() << " operands:\n";
27 
28       // Print information about the producer of each of the operands.
29       for (Value operand : op->getOperands()) {
30         if (Operation *producer = operand.getDefiningOp()) {
31           llvm::outs() << "  - Operand produced by operation '"
32                        << producer->getName() << "'\n";
33         } else {
34           // If there is no defining op, the Value is necessarily a Block
35           // argument.
36           auto blockArg = operand.cast<BlockArgument>();
37           llvm::outs() << "  - Operand produced by Block argument, number "
38                        << blockArg.getArgNumber() << "\n";
39         }
40       }
41 
42       // Print information about the user of each of the result.
43       llvm::outs() << "Has " << op->getNumResults() << " results:\n";
44       for (auto indexedResult : llvm::enumerate(op->getResults())) {
45         Value result = indexedResult.value();
46         llvm::outs() << "  - Result " << indexedResult.index();
47         if (result.use_empty()) {
48           llvm::outs() << " has no uses\n";
49           continue;
50         }
51         if (result.hasOneUse()) {
52           llvm::outs() << " has a single use: ";
53         } else {
54           llvm::outs() << " has "
55                        << std::distance(result.getUses().begin(),
56                                         result.getUses().end())
57                        << " uses:\n";
58         }
59         for (Operation *userOp : result.getUsers()) {
60           llvm::outs() << "    - " << userOp->getName() << "\n";
61         }
62       }
63     });
64   }
65 };
66 } // namespace
67 
68 namespace mlir {
69 void registerTestPrintDefUsePass() { PassRegistration<TestPrintDefUsePass>(); }
70 } // namespace mlir
71