| 1 | //===- TestIRVisitors.cpp - Pass to test the IR visitors ------------------===// |
| 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/IR/Iterators.h" |
| 11 | #include "mlir/Interfaces/FunctionInterfaces.h" |
| 12 | #include "mlir/Pass/Pass.h" |
| 13 | |
| 14 | using namespace mlir; |
| 15 | |
| 16 | static void printRegion(Region *region) { |
| 17 | llvm::outs() << "region " << region->getRegionNumber() << " from operation '" |
| 18 | << region->getParentOp()->getName() << "'" ; |
| 19 | } |
| 20 | |
| 21 | static void printBlock(Block *block) { |
| 22 | llvm::outs() << "block " ; |
| 23 | block->printAsOperand(os&: llvm::outs(), /*printType=*/false); |
| 24 | llvm::outs() << " from " ; |
| 25 | printRegion(region: block->getParent()); |
| 26 | } |
| 27 | |
| 28 | static void printOperation(Operation *op) { |
| 29 | llvm::outs() << "op '" << op->getName() << "'" ; |
| 30 | } |
| 31 | |
| 32 | /// Tests pure callbacks. |
| 33 | static void testPureCallbacks(Operation *op) { |
| 34 | auto opPure = [](Operation *op) { |
| 35 | llvm::outs() << "Visiting " ; |
| 36 | printOperation(op); |
| 37 | llvm::outs() << "\n" ; |
| 38 | }; |
| 39 | auto blockPure = [](Block *block) { |
| 40 | llvm::outs() << "Visiting " ; |
| 41 | printBlock(block); |
| 42 | llvm::outs() << "\n" ; |
| 43 | }; |
| 44 | auto regionPure = [](Region *region) { |
| 45 | llvm::outs() << "Visiting " ; |
| 46 | printRegion(region); |
| 47 | llvm::outs() << "\n" ; |
| 48 | }; |
| 49 | |
| 50 | llvm::outs() << "Op pre-order visits" |
| 51 | << "\n" ; |
| 52 | op->walk<WalkOrder::PreOrder>(callback&: opPure); |
| 53 | llvm::outs() << "Block pre-order visits" |
| 54 | << "\n" ; |
| 55 | op->walk<WalkOrder::PreOrder>(callback&: blockPure); |
| 56 | llvm::outs() << "Region pre-order visits" |
| 57 | << "\n" ; |
| 58 | op->walk<WalkOrder::PreOrder>(callback&: regionPure); |
| 59 | |
| 60 | llvm::outs() << "Op post-order visits" |
| 61 | << "\n" ; |
| 62 | op->walk<WalkOrder::PostOrder>(callback&: opPure); |
| 63 | llvm::outs() << "Block post-order visits" |
| 64 | << "\n" ; |
| 65 | op->walk<WalkOrder::PostOrder>(callback&: blockPure); |
| 66 | llvm::outs() << "Region post-order visits" |
| 67 | << "\n" ; |
| 68 | op->walk<WalkOrder::PostOrder>(callback&: regionPure); |
| 69 | |
| 70 | llvm::outs() << "Op reverse post-order visits" |
| 71 | << "\n" ; |
| 72 | op->walk<WalkOrder::PostOrder, ReverseIterator>(callback&: opPure); |
| 73 | llvm::outs() << "Block reverse post-order visits" |
| 74 | << "\n" ; |
| 75 | op->walk<WalkOrder::PostOrder, ReverseIterator>(callback&: blockPure); |
| 76 | llvm::outs() << "Region reverse post-order visits" |
| 77 | << "\n" ; |
| 78 | op->walk<WalkOrder::PostOrder, ReverseIterator>(callback&: regionPure); |
| 79 | |
| 80 | // This test case tests "SkipGraphRegion = true", so start the walk with |
| 81 | // functions. |
| 82 | op->walk(callback: [&](FunctionOpInterface funcOp) { |
| 83 | llvm::outs() << "Op forward dominance post-order visits" |
| 84 | << "\n" ; |
| 85 | funcOp->walk<WalkOrder::PostOrder, |
| 86 | ForwardDominanceIterator</*SkipGraphRegion=*/true>>(callback&: opPure); |
| 87 | llvm::outs() << "Block forward dominance post-order visits" |
| 88 | << "\n" ; |
| 89 | funcOp->walk<WalkOrder::PostOrder, |
| 90 | ForwardDominanceIterator</*SkipGraphRegion=*/true>>(callback&: blockPure); |
| 91 | llvm::outs() << "Region forward dominance post-order visits" |
| 92 | << "\n" ; |
| 93 | funcOp->walk<WalkOrder::PostOrder, |
| 94 | ForwardDominanceIterator</*SkipGraphRegion=*/true>>( |
| 95 | callback&: regionPure); |
| 96 | |
| 97 | llvm::outs() << "Op reverse dominance post-order visits" |
| 98 | << "\n" ; |
| 99 | funcOp->walk<WalkOrder::PostOrder, |
| 100 | ReverseDominanceIterator</*SkipGraphRegion=*/true>>(callback&: opPure); |
| 101 | llvm::outs() << "Block reverse dominance post-order visits" |
| 102 | << "\n" ; |
| 103 | funcOp->walk<WalkOrder::PostOrder, |
| 104 | ReverseDominanceIterator</*SkipGraphRegion=*/true>>(callback&: blockPure); |
| 105 | llvm::outs() << "Region reverse dominance post-order visits" |
| 106 | << "\n" ; |
| 107 | funcOp->walk<WalkOrder::PostOrder, |
| 108 | ReverseDominanceIterator</*SkipGraphRegion=*/true>>( |
| 109 | callback&: regionPure); |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | /// Tests erasure callbacks that skip the walk. |
| 114 | static void testSkipErasureCallbacks(Operation *op) { |
| 115 | auto skipOpErasure = [](Operation *op) { |
| 116 | // Do not erase module and module children operations. Otherwise, there |
| 117 | // wouldn't be too much to test in pre-order. |
| 118 | if (isa<ModuleOp>(Val: op) || isa<ModuleOp>(Val: op->getParentOp())) |
| 119 | return WalkResult::advance(); |
| 120 | |
| 121 | llvm::outs() << "Erasing " ; |
| 122 | printOperation(op); |
| 123 | llvm::outs() << "\n" ; |
| 124 | op->dropAllUses(); |
| 125 | op->erase(); |
| 126 | return WalkResult::skip(); |
| 127 | }; |
| 128 | auto skipBlockErasure = [](Block *block) { |
| 129 | // Do not erase module and module children blocks. Otherwise there wouldn't |
| 130 | // be too much to test in pre-order. |
| 131 | Operation *parentOp = block->getParentOp(); |
| 132 | if (isa<ModuleOp>(Val: parentOp) || isa<ModuleOp>(Val: parentOp->getParentOp())) |
| 133 | return WalkResult::advance(); |
| 134 | |
| 135 | if (block->use_empty()) { |
| 136 | llvm::outs() << "Erasing " ; |
| 137 | printBlock(block); |
| 138 | llvm::outs() << "\n" ; |
| 139 | block->dropAllDefinedValueUses(); |
| 140 | block->erase(); |
| 141 | return WalkResult::skip(); |
| 142 | } |
| 143 | llvm::outs() << "Cannot erase " ; |
| 144 | printBlock(block); |
| 145 | llvm::outs() << ", still has uses\n" ; |
| 146 | return WalkResult::advance(); |
| 147 | |
| 148 | }; |
| 149 | |
| 150 | llvm::outs() << "Op pre-order erasures (skip)" |
| 151 | << "\n" ; |
| 152 | Operation *cloned = op->clone(); |
| 153 | cloned->walk<WalkOrder::PreOrder>(callback&: skipOpErasure); |
| 154 | cloned->erase(); |
| 155 | |
| 156 | llvm::outs() << "Block pre-order erasures (skip)" |
| 157 | << "\n" ; |
| 158 | cloned = op->clone(); |
| 159 | cloned->walk<WalkOrder::PreOrder>(callback&: skipBlockErasure); |
| 160 | cloned->erase(); |
| 161 | |
| 162 | llvm::outs() << "Op post-order erasures (skip)" |
| 163 | << "\n" ; |
| 164 | cloned = op->clone(); |
| 165 | cloned->walk<WalkOrder::PostOrder>(callback&: skipOpErasure); |
| 166 | cloned->erase(); |
| 167 | |
| 168 | llvm::outs() << "Block post-order erasures (skip)" |
| 169 | << "\n" ; |
| 170 | cloned = op->clone(); |
| 171 | cloned->walk<WalkOrder::PostOrder>(callback&: skipBlockErasure); |
| 172 | cloned->erase(); |
| 173 | } |
| 174 | |
| 175 | /// Tests callbacks that erase the op or block but don't return 'Skip'. This |
| 176 | /// callbacks are only valid in post-order. |
| 177 | static void testNoSkipErasureCallbacks(Operation *op) { |
| 178 | auto noSkipOpErasure = [](Operation *op) { |
| 179 | llvm::outs() << "Erasing " ; |
| 180 | printOperation(op); |
| 181 | llvm::outs() << "\n" ; |
| 182 | op->dropAllUses(); |
| 183 | op->erase(); |
| 184 | }; |
| 185 | auto noSkipBlockErasure = [](Block *block) { |
| 186 | if (block->use_empty()) { |
| 187 | llvm::outs() << "Erasing " ; |
| 188 | printBlock(block); |
| 189 | llvm::outs() << "\n" ; |
| 190 | |
| 191 | // Only drop uses from operations within the same parent region. This |
| 192 | // avoids erasing operations with their uses still intact and eliminates |
| 193 | // such crashes. |
| 194 | // Note: We do not drop the use when the parent region is different for |
| 195 | // it, because this means that the use's region holding op is a child of |
| 196 | // the region holding op containing the current block and was expected to |
| 197 | // be visited and erased first - we should correctly fail here. |
| 198 | Region *blockParentRegion = block->getParent(); |
| 199 | for (Operation &op : *block) { |
| 200 | for (OpOperand &use : llvm::make_early_inc_range(Range: op.getUses())) { |
| 201 | // Early continue if the parent regions are not same. |
| 202 | if (blockParentRegion != use.getOwner()->getParentRegion()) |
| 203 | continue; |
| 204 | use.drop(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // Also drop uses of block arguments that are consumed within the same |
| 209 | // region. This handles cases like function entry blocks whose arguments |
| 210 | // are used by ops in sibling blocks. |
| 211 | for (BlockArgument arg : block->getArguments()) { |
| 212 | for (OpOperand &use : llvm::make_early_inc_range(Range: arg.getUses())) { |
| 213 | if (blockParentRegion != use.getOwner()->getParentRegion()) |
| 214 | continue; |
| 215 | use.drop(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | block->erase(); |
| 220 | } else { |
| 221 | llvm::outs() << "Cannot erase " ; |
| 222 | printBlock(block); |
| 223 | llvm::outs() << ", still has uses\n" ; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | llvm::outs() << "Op post-order erasures (no skip)" |
| 228 | << "\n" ; |
| 229 | Operation *cloned = op->clone(); |
| 230 | cloned->walk<WalkOrder::PostOrder>(callback&: noSkipOpErasure); |
| 231 | |
| 232 | llvm::outs() << "Block post-order erasures (no skip)" |
| 233 | << "\n" ; |
| 234 | cloned = op->clone(); |
| 235 | cloned->walk<WalkOrder::PostOrder>(callback&: noSkipBlockErasure); |
| 236 | cloned->erase(); |
| 237 | } |
| 238 | |
| 239 | /// Invoke region/block walks on regions/blocks. |
| 240 | static void testBlockAndRegionWalkers(Operation *op) { |
| 241 | auto blockPure = [](Block *block) { |
| 242 | llvm::outs() << "Visiting " ; |
| 243 | printBlock(block); |
| 244 | llvm::outs() << "\n" ; |
| 245 | }; |
| 246 | auto regionPure = [](Region *region) { |
| 247 | llvm::outs() << "Visiting " ; |
| 248 | printRegion(region); |
| 249 | llvm::outs() << "\n" ; |
| 250 | }; |
| 251 | |
| 252 | llvm::outs() << "Invoke block pre-order visits on blocks\n" ; |
| 253 | op->walk(callback: [&](Operation *op) { |
| 254 | if (!op->hasDiscardableAttr(name: "walk_blocks" )) |
| 255 | return; |
| 256 | for (Region ®ion : op->getRegions()) { |
| 257 | for (Block &block : region.getBlocks()) { |
| 258 | block.walk<WalkOrder::PreOrder>(callback&: blockPure); |
| 259 | } |
| 260 | } |
| 261 | }); |
| 262 | |
| 263 | llvm::outs() << "Invoke block post-order visits on blocks\n" ; |
| 264 | op->walk(callback: [&](Operation *op) { |
| 265 | if (!op->hasDiscardableAttr(name: "walk_blocks" )) |
| 266 | return; |
| 267 | for (Region ®ion : op->getRegions()) { |
| 268 | for (Block &block : region.getBlocks()) { |
| 269 | block.walk<WalkOrder::PostOrder>(callback&: blockPure); |
| 270 | } |
| 271 | } |
| 272 | }); |
| 273 | |
| 274 | llvm::outs() << "Invoke region pre-order visits on region\n" ; |
| 275 | op->walk(callback: [&](Operation *op) { |
| 276 | if (!op->hasDiscardableAttr(name: "walk_regions" )) |
| 277 | return; |
| 278 | for (Region ®ion : op->getRegions()) { |
| 279 | region.walk<WalkOrder::PreOrder>(callback&: regionPure); |
| 280 | } |
| 281 | }); |
| 282 | |
| 283 | llvm::outs() << "Invoke region post-order visits on region\n" ; |
| 284 | op->walk(callback: [&](Operation *op) { |
| 285 | if (!op->hasDiscardableAttr(name: "walk_regions" )) |
| 286 | return; |
| 287 | for (Region ®ion : op->getRegions()) { |
| 288 | region.walk<WalkOrder::PostOrder>(callback&: regionPure); |
| 289 | } |
| 290 | }); |
| 291 | } |
| 292 | |
| 293 | namespace { |
| 294 | /// This pass exercises the different configurations of the IR visitors. |
| 295 | struct TestIRVisitorsPass |
| 296 | : public PassWrapper<TestIRVisitorsPass, OperationPass<>> { |
| 297 | MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestIRVisitorsPass) |
| 298 | |
| 299 | StringRef getArgument() const final { return "test-ir-visitors" ; } |
| 300 | StringRef getDescription() const final { return "Test various visitors." ; } |
| 301 | void runOnOperation() override { |
| 302 | Operation *op = getOperation(); |
| 303 | testPureCallbacks(op); |
| 304 | testBlockAndRegionWalkers(op); |
| 305 | testSkipErasureCallbacks(op); |
| 306 | testNoSkipErasureCallbacks(op); |
| 307 | } |
| 308 | }; |
| 309 | } // namespace |
| 310 | |
| 311 | namespace mlir { |
| 312 | namespace test { |
| 313 | void registerTestIRVisitorsPass() { PassRegistration<TestIRVisitorsPass>(); } |
| 314 | } // namespace test |
| 315 | } // namespace mlir |
| 316 | |