1 //===- Visitors.cpp - MLIR Visitor Utilities ------------------------------===//
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/Visitors.h"
10 #include "mlir/IR/Operation.h"
11 
12 using namespace mlir;
13 
14 /// Walk all of the operations nested under and including the given operations.
15 void detail::walkOperations(Operation *op,
16                             function_ref<void(Operation *op)> callback) {
17   // TODO: This walk should be iterative over the operations.
18   for (auto &region : op->getRegions())
19     for (auto &block : region)
20       // Early increment here in the case where the operation is erased.
21       for (auto &nestedOp : llvm::make_early_inc_range(block))
22         walkOperations(&nestedOp, callback);
23 
24   callback(op);
25 }
26 
27 /// Walk all of the operations nested under and including the given operations.
28 /// This methods walks operations until an interrupt signal is received.
29 WalkResult
30 detail::walkOperations(Operation *op,
31                        function_ref<WalkResult(Operation *op)> callback) {
32   // TODO: This walk should be iterative over the operations.
33   for (auto &region : op->getRegions()) {
34     for (auto &block : region) {
35       // Early increment here in the case where the operation is erased.
36       for (auto &nestedOp : llvm::make_early_inc_range(block))
37         if (walkOperations(&nestedOp, callback).wasInterrupted())
38           return WalkResult::interrupt();
39     }
40   }
41   return callback(op);
42 }
43