MLIR 23.0.0git
Iterators.h
Go to the documentation of this file.
1//===- Iterators.h - IR iterators for IR visitors ---------------*- C++ -*-===//
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// The iterators defined in this file can be used together with IR visitors.
10// Note: These iterators cannot be defined in Visitors.h because that would
11// introduce a cyclic header dependency due to Operation.h.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef MLIR_IR_ITERATORS_H
16#define MLIR_IR_ITERATORS_H
17
18#include "mlir/IR/Operation.h"
21#include "mlir/Support/LLVM.h"
22
23#include "llvm/ADT/DepthFirstIterator.h"
24#include "llvm/ADT/PostOrderIterator.h"
25
26namespace mlir {
27/// This iterator enumerates elements in "reverse" order. It is a wrapper around
28/// llvm::reverse.
30 // llvm::reverse uses RangeT::rbegin and RangeT::rend.
31 template <typename RangeT>
32 static constexpr auto makeIterable(RangeT &&range) {
33 return llvm::reverse(
34 ForwardIterator::makeIterable(std::forward<RangeT>(range)));
35 }
36};
37
38/// This iterator enumerates elements according to their dominance relationship.
39/// Operations and regions are enumerated in "forward" order. Blocks are
40/// enumerated according to their successor relationship. Unreachable blocks are
41/// not enumerated. Blocks may not be erased during the traversal.
42///
43/// Note: If `SkipGraphRegion` is set to "true", graph regions (regions without
44/// SSA dominance) are silently skipped during the traversal. If set to "false"
45/// (the default), graph regions are visited but without dominance guarantees
46/// (i.e., defining ops are not guaranteed to be visited before their users).
47///
48/// Regions of unregistered ops are always treated as SSACFG regions (i.e.,
49/// they are visited even when `SkipGraphRegion=true`), because
50/// `mayHaveSSADominance` returns true for unregistered ops.
51template <bool SkipGraphRegion = false>
53 static Block &makeIterable(Block &range) {
55 }
56
57 static auto makeIterable(Region &region) {
58 Block *null = nullptr;
59 if (SkipGraphRegion && !mayHaveSSADominance(region)) {
60 // Skip graph regions.
61 return llvm::make_pointee_range(
62 llvm::make_range(llvm::df_end(null), llvm::df_end(null)));
63 }
64
65 // Create DFS iterator. Blocks are enumerated according to their successor
66 // relationship.
67 auto it = region.empty()
68 ? llvm::make_range(llvm::df_end(null), llvm::df_end(null))
69 : llvm::depth_first(&region.front());
70
71 // Walk API expects Block references instead of pointers.
72 return llvm::make_pointee_range(it);
73 }
74
78};
79
80/// This iterator enumerates elements according to their reverse dominance
81/// relationship. Operations and regions are enumerated in "reverse" order.
82/// Blocks are enumerated according to their successor relationship, but
83/// post-order. I.e., a block is visited after its successors have been visited.
84/// Cycles in the block graph are broken in an unspecified way. Unreachable
85/// blocks are not enumerated. Blocks may not be erased during the traversal.
86///
87/// Note: If `SkipGraphRegion` is set to "true", graph regions (regions without
88/// SSA dominance) are silently skipped during the traversal.
89///
90/// Regions of unregistered ops are always treated as SSACFG regions (i.e.,
91/// they are visited even when `SkipGraphRegion=true`), because
92/// `mayHaveSSADominance` returns true for unregistered ops.
93template <bool SkipGraphRegion = false>
95 // llvm::reverse uses RangeT::rbegin and RangeT::rend.
96 static constexpr auto makeIterable(Block &range) {
97 return llvm::reverse(ForwardIterator::makeIterable(range));
98 }
99
100 static constexpr auto makeIterable(Operation &range) {
101 return llvm::reverse(ForwardIterator::makeIterable(range));
102 }
103
104 static auto makeIterable(Region &region) {
105 Block *null = nullptr;
106 if (SkipGraphRegion && !mayHaveSSADominance(region)) {
107 // Skip graph regions.
108 return llvm::make_pointee_range(
109 llvm::make_range(llvm::po_end(null), llvm::po_end(null)));
110 }
111
112 // Create post-order iterator. Blocks are enumerated according to their
113 // successor relationship.
114 auto it = region.empty()
115 ? llvm::make_range(llvm::po_end(null), llvm::po_end(null))
116 : llvm::post_order(&region.front());
117
118 // Walk API expects Block references instead of pointers.
119 return llvm::make_pointee_range(it);
120 }
121};
122} // namespace mlir
123
124#endif // MLIR_IR_ITERATORS_H
Block represents an ordered list of Operations.
Definition Block.h:33
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Block & front()
Definition Region.h:65
bool empty()
Definition Region.h:60
Include the generated interface declarations.
bool mayHaveSSADominance(Region &region)
Return "true" if the given region may have SSA dominance.
This iterator enumerates elements according to their dominance relationship.
Definition Iterators.h:52
static MutableArrayRef< Region > makeIterable(Operation &range)
Definition Iterators.h:75
static Block & makeIterable(Block &range)
Definition Iterators.h:53
static auto makeIterable(Region &region)
Definition Iterators.h:57
static MutableArrayRef< Region > makeIterable(Operation &range)
Make operations iterable: return the list of regions.
Definition Visitors.cpp:17
This iterator enumerates elements according to their reverse dominance relationship.
Definition Iterators.h:94
static auto makeIterable(Region &region)
Definition Iterators.h:104
static constexpr auto makeIterable(Operation &range)
Definition Iterators.h:100
static constexpr auto makeIterable(Block &range)
Definition Iterators.h:96
This iterator enumerates elements in "reverse" order.
Definition Iterators.h:29
static constexpr auto makeIterable(RangeT &&range)
Definition Iterators.h:32