MLIR  20.0.0git
Visitors.cpp
Go to the documentation of this file.
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 
15  : numRegions(op->getNumRegions()), nextRegion(0) {}
16 
18  return range.getRegions();
19 }
20 
21 void detail::walk(Operation *op,
22  function_ref<void(Operation *, const WalkStage &)> callback) {
23  WalkStage stage(op);
24 
25  for (Region &region : op->getRegions()) {
26  // Invoke callback on the parent op before visiting each child region.
27  callback(op, stage);
28  stage.advance();
29 
30  for (Block &block : region) {
31  for (Operation &nestedOp : block)
32  walk(&nestedOp, callback);
33  }
34  }
35 
36  // Invoke callback after all regions have been visited.
37  callback(op, stage);
38 }
39 
41  Operation *op,
42  function_ref<WalkResult(Operation *, const WalkStage &)> callback) {
43  WalkStage stage(op);
44 
45  for (Region &region : op->getRegions()) {
46  // Invoke callback on the parent op before visiting each child region.
47  WalkResult result = callback(op, stage);
48 
49  if (result.wasSkipped())
50  return WalkResult::advance();
51  if (result.wasInterrupted())
52  return WalkResult::interrupt();
53 
54  stage.advance();
55 
56  for (Block &block : region) {
57  // Early increment here in the case where the operation is erased.
58  for (Operation &nestedOp : llvm::make_early_inc_range(block))
59  if (walk(&nestedOp, callback).wasInterrupted())
60  return WalkResult::interrupt();
61  }
62  }
63  return callback(op, stage);
64 }
Block represents an ordered list of Operations.
Definition: Block.h:33
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Definition: Operation.h:677
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:33
bool wasSkipped() const
Returns true if the walk was skipped.
Definition: Visitors.h:58
static WalkResult advance()
Definition: Visitors.h:51
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition: Visitors.h:55
static WalkResult interrupt()
Definition: Visitors.h:50
A utility class to encode the current walk stage for "generic" walkers.
Definition: Visitors.h:87
WalkStage(Operation *op)
Definition: Visitors.cpp:14
void walk(Operation *op, function_ref< void(Region *)> callback, WalkOrder order)
Walk all of the regions, blocks, or operations nested under (and including) the given operation.
Definition: Visitors.h:136
Include the generated interface declarations.
static MutableArrayRef< Region > makeIterable(Operation &range)
Make operations iterable: return the list of regions.
Definition: Visitors.cpp:17