1//===- TestSymbolUses.cpp - Pass to test symbol uselists ------------------===//
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/Func/IR/FuncOps.h"
10#include "mlir/IR/BuiltinOps.h"
11#include "mlir/Pass/Pass.h"
12
13using namespace mlir;
14
15namespace {
16/// This is a symbol test pass that tests the symbol uselist functionality
17/// provided by the symbol table along with erasing from the symbol table.
18struct SymbolUsesPass
19 : public PassWrapper<SymbolUsesPass, OperationPass<ModuleOp>> {
20 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SymbolUsesPass)
21
22 StringRef getArgument() const final { return "test-symbol-uses"; }
23 StringRef getDescription() const final {
24 return "Test detection of symbol uses";
25 }
26 WalkResult operateOnSymbol(SymbolOpInterface symbol, ModuleOp module,
27 SmallVectorImpl<func::FuncOp> &deadFunctions) {
28 // Test computing uses on a non symboltable op.
29 std::optional<SymbolTable::UseRange> symbolUses =
30 SymbolTable::getSymbolUses(from: symbol);
31
32 // Test the conservative failure case.
33 if (!symbolUses) {
34 symbol->emitRemark()
35 << "symbol contains an unknown nested operation that "
36 "'may' define a new symbol table";
37 return WalkResult::interrupt();
38 }
39 if (unsigned numUses = llvm::size(Range&: *symbolUses))
40 symbol->emitRemark() << "symbol contains " << numUses
41 << " nested references";
42
43 // Test the functionality of symbolKnownUseEmpty.
44 if (SymbolTable::symbolKnownUseEmpty(symbol, from: &module.getBodyRegion())) {
45 func::FuncOp funcSymbol = dyn_cast<func::FuncOp>(Val: symbol.getOperation());
46 // Only track functions that are direct children of the enclosing module.
47 // Functions in nested symbol tables (e.g., nested modules) belong to a
48 // different symbol table and cannot be erased via the outer module's
49 // SymbolTable instance.
50 if (funcSymbol && funcSymbol.isExternal() &&
51 funcSymbol->getParentOp() == module.getOperation())
52 deadFunctions.push_back(Elt: funcSymbol);
53
54 symbol->emitRemark() << "symbol has no uses";
55 return WalkResult::advance();
56 }
57
58 // Test the functionality of getSymbolUses.
59 symbolUses = symbol.getSymbolUses(from: module);
60 assert(symbolUses && "expected no unknown operations");
61 for (SymbolTable::SymbolUse symbolUse : *symbolUses) {
62 // Check that we can resolve back to our symbol.
63 if (SymbolTable::lookupNearestSymbolFrom(
64 from: symbolUse.getUser()->getParentOp(), symbol: symbolUse.getSymbolRef())) {
65 symbolUse.getUser()->emitRemark()
66 << "found use of symbol : " << symbolUse.getSymbolRef() << " : "
67 << symbol.getNameAttr();
68 } else {
69 symbolUse.getUser()->emitRemark()
70 << "failed to resolve use of symbol : " << symbolUse.getSymbolRef()
71 << " : " << symbol.getNameAttr();
72 }
73 }
74 symbol->emitRemark() << "symbol has " << llvm::size(Range&: *symbolUses) << " uses";
75 return WalkResult::advance();
76 }
77
78 void runOnOperation() override {
79 auto module = getOperation();
80
81 // Walk nested symbols.
82 SmallVector<func::FuncOp, 4> deadFunctions;
83 module.getBodyRegion().walk(callback: [&](SymbolOpInterface nestedOp) {
84 return operateOnSymbol(symbol: nestedOp, module, deadFunctions);
85 });
86
87 SymbolTable table(module);
88 for (Operation *op : deadFunctions) {
89 // In order to test the SymbolTable::erase method, also erase completely
90 // useless functions.
91 auto name = SymbolTable::getSymbolName(symbol: op);
92 assert(table.lookup(name) && "expected no unknown operations");
93 table.erase(symbol: op);
94 assert(!table.lookup(name) &&
95 "expected erased operation to be unknown now");
96 module.emitRemark() << name.getValue() << " function successfully erased";
97 }
98 }
99};
100
101/// This is a symbol test pass that tests the symbol use replacement
102/// functionality provided by the symbol table.
103struct SymbolReplacementPass
104 : public PassWrapper<SymbolReplacementPass, OperationPass<ModuleOp>> {
105 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SymbolReplacementPass)
106
107 StringRef getArgument() const final { return "test-symbol-rauw"; }
108 StringRef getDescription() const final {
109 return "Test replacement of symbol uses";
110 }
111 void runOnOperation() override {
112 ModuleOp module = getOperation();
113
114 // Don't try to replace if we can't collect symbol uses.
115 if (!SymbolTable::getSymbolUses(from: &module.getBodyRegion()))
116 return;
117
118 SymbolTableCollection symbolTable;
119 SymbolUserMap symbolUsers(symbolTable, module);
120 module.getBodyRegion().walk(callback: [&](Operation *nestedOp) {
121 StringAttr newName =
122 nestedOp->getDiscardableAttrOfType<StringAttr>(name: "sym.new_name");
123 if (!newName)
124 return;
125 symbolUsers.replaceAllUsesWith(symbol: nestedOp, newSymbolName: newName);
126 SymbolTable::setSymbolName(symbol: nestedOp, name: newName);
127 });
128 }
129};
130} // namespace
131
132namespace mlir {
133void registerSymbolTestPasses() {
134 PassRegistration<SymbolUsesPass>();
135
136 PassRegistration<SymbolReplacementPass>();
137}
138} // namespace mlir
139