13 #ifndef MLIR_IR_VISITORS_H
14 #define MLIR_IR_VISITORS_H
17 #include "llvm/ADT/STLExtras.h"
21 class InFlightDiagnostic;
34 enum ResultEnum { Interrupt, Advance, Skip } result;
37 WalkResult(ResultEnum result = Advance) : result(result) {}
41 : result(failed(result) ? Interrupt : Advance) {}
98 bool isAfterRegion(
int region)
const {
return nextRegion == region + 1; }
107 const int numRegions;
113 template <
typename Ret,
typename Arg,
typename... Rest>
115 template <
typename Ret,
typename F,
typename Arg,
typename... Rest>
117 template <
typename Ret,
typename F,
typename Arg,
typename... Rest>
119 template <
typename F>
123 template <typename T>
135 template <typename Iterator>
140 for (
auto ®ion : Iterator::makeIterable(*op)) {
143 for (
auto &block : Iterator::makeIterable(region)) {
144 for (
auto &nestedOp : Iterator::makeIterable(block))
145 walk<Iterator>(&nestedOp, callback, order);
152 template <
typename Iterator>
155 for (
auto ®ion : Iterator::makeIterable(*op)) {
158 llvm::make_early_inc_range(Iterator::makeIterable(region))) {
161 for (
auto &nestedOp : Iterator::makeIterable(block))
162 walk<Iterator>(&nestedOp, callback, order);
169 template <
typename Iterator>
176 for (
auto ®ion : Iterator::makeIterable(*op)) {
177 for (
auto &block : Iterator::makeIterable(region)) {
179 for (
auto &nestedOp :
180 llvm::make_early_inc_range(Iterator::makeIterable(block)))
181 walk<Iterator>(&nestedOp, callback, order);
199 template <
typename Iterator>
204 for (
auto ®ion : Iterator::makeIterable(*op)) {
212 for (
auto &block : Iterator::makeIterable(region)) {
213 for (
auto &nestedOp : Iterator::makeIterable(block))
214 if (walk<Iterator>(&nestedOp, callback, order).wasInterrupted())
218 if (callback(®ion).wasInterrupted())
227 template <
typename Iterator>
230 for (
auto ®ion : Iterator::makeIterable(*op)) {
233 llvm::make_early_inc_range(Iterator::makeIterable(region))) {
241 for (
auto &nestedOp : Iterator::makeIterable(block))
242 if (walk<Iterator>(&nestedOp, callback, order).wasInterrupted())
245 if (callback(&block).wasInterrupted())
255 template <
typename Iterator>
268 for (
auto ®ion : Iterator::makeIterable(*op)) {
269 for (
auto &block : Iterator::makeIterable(region)) {
271 for (
auto &nestedOp :
272 llvm::make_early_inc_range(Iterator::makeIterable(block))) {
273 if (walk<Iterator>(&nestedOp, callback, order).wasInterrupted())
308 typename RetT = decltype(std::declval<FuncTy>()(std::declval<ArgT>()))>
309 std::enable_if_t<llvm::is_one_of<ArgT, Operation *, Region *, Block *>::value,
312 return detail::walk<Iterator>(op,
function_ref<RetT(ArgT)>(callback), Order);
330 typename RetT = decltype(std::declval<FuncTy>()(std::declval<ArgT>()))>
332 !llvm::is_one_of<ArgT, Operation *, Region *, Block *>::value &&
333 std::is_same<RetT, void>::value,
337 if (
auto derivedOp = dyn_cast<ArgT>(op))
367 typename RetT = decltype(std::declval<FuncTy>()(std::declval<ArgT>()))>
369 !llvm::is_one_of<ArgT, Operation *, Region *, Block *>::value &&
370 std::is_same<RetT, WalkResult>::value,
374 if (
auto derivedOp = dyn_cast<ArgT>(op))
375 return callback(derivedOp);
407 template <
typename FuncTy,
typename ArgT = detail::first_argument<FuncTy>,
408 typename RetT = decltype(std::declval<FuncTy>()(
409 std::declval<ArgT>(), std::declval<const WalkStage &>()))>
410 std::enable_if_t<std::is_same<ArgT, Operation *>::value, RetT>
422 template <
typename FuncTy,
typename ArgT = detail::first_argument<FuncTy>,
423 typename RetT = decltype(std::declval<FuncTy>()(
424 std::declval<ArgT>(), std::declval<const WalkStage &>()))>
425 std::enable_if_t<!std::is_same<ArgT, Operation *>::value &&
426 std::is_same<RetT, void>::value,
430 if (
auto derivedOp = dyn_cast<ArgT>(op))
431 callback(derivedOp, stage);
447 template <
typename FuncTy,
typename ArgT = detail::first_argument<FuncTy>,
448 typename RetT = decltype(std::declval<FuncTy>()(
449 std::declval<ArgT>(), std::declval<const WalkStage &>()))>
450 std::enable_if_t<!std::is_same<ArgT, Operation *>::value &&
451 std::is_same<RetT, WalkResult>::value,
455 if (
auto derivedOp = dyn_cast<ArgT>(op))
456 return callback(derivedOp, stage);
464 template <
typename FnT>
Block represents an ordered list of Operations.
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
This class represents a diagnostic that is inflight and set to be reported.
Operation is the basic unit of execution within MLIR.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
A utility result that is used to signal how to proceed with an ongoing walk:
WalkResult(InFlightDiagnostic &&)
bool operator==(const WalkResult &rhs) const
WalkResult(LogicalResult result)
Allow LogicalResult to interrupt the walk on failure.
WalkResult(ResultEnum result=Advance)
bool wasSkipped() const
Returns true if the walk was skipped.
static WalkResult advance()
bool wasInterrupted() const
Returns true if the walk was interrupted.
WalkResult(Diagnostic &&)
Allow diagnostics to interrupt the walk.
static WalkResult interrupt()
bool operator!=(const WalkResult &rhs) const
A utility class to encode the current walk stage for "generic" walkers.
void advance()
Advance the walk stage.
int getNextRegion() const
Returns the next region that will be visited.
bool isBeforeRegion(int region) const
Returns true if parent operation is being visited just before visiting region number region.
bool isAfterAllRegions() const
Return true if parent operation is being visited after all regions.
bool isAfterRegion(int region) const
Returns true if parent operation is being visited just after visiting region number region.
bool isBeforeAllRegions() const
Return true if parent operation is being visited before all regions.
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.
decltype(walk(nullptr, std::declval< FnT >())) walkResultType
Utility to provide the return type of a templated walk method.
decltype(first_argument_type(&F::operator())) first_argument_type(F)
decltype(first_argument_type(std::declval< T >())) first_argument
Type definition of the first argument to the given callable 'T'.
Include the generated interface declarations.
WalkOrder
Traversal order for region, block and operation walk utilities.
This iterator enumerates the elements in "forward" order.
static MutableArrayRef< Region > makeIterable(Operation &range)
Make operations iterable: return the list of regions.
static constexpr T & makeIterable(T &range)
Regions and block are already iterable.