My Project
Region.h
Go to the documentation of this file.
1 //===- Region.h - MLIR Region Class -----------------------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 // This file defines the Region class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_REGION_H
14 #define MLIR_IR_REGION_H
15 
16 #include "mlir/IR/Block.h"
17 
18 namespace mlir {
19 class BlockAndValueMapping;
20 
23 class Region {
24 public:
25  Region() = default;
26  explicit Region(Operation *container);
27  ~Region();
28 
32 
35  Location getLoc();
36 
37  using BlockListType = llvm::iplist<Block>;
38  BlockListType &getBlocks() { return blocks; }
39 
40  // Iteration over the blocks in the region.
41  using iterator = BlockListType::iterator;
42  using reverse_iterator = BlockListType::reverse_iterator;
43 
44  iterator begin() { return blocks.begin(); }
45  iterator end() { return blocks.end(); }
46  reverse_iterator rbegin() { return blocks.rbegin(); }
47  reverse_iterator rend() { return blocks.rend(); }
48 
49  bool empty() { return blocks.empty(); }
50  void push_back(Block *block) { blocks.push_back(block); }
51  void push_front(Block *block) { blocks.push_front(block); }
52 
53  Block &back() { return blocks.back(); }
54  Block &front() { return blocks.front(); }
55 
58  return &Region::blocks;
59  }
60 
64 
67 
70  template <typename ParentT> ParentT getParentOfType() {
71  auto *region = this;
72  do {
73  if (auto parent = dyn_cast_or_null<ParentT>(region->container))
74  return parent;
75  } while ((region = region->getParentRegion()));
76  return ParentT();
77  }
78 
80  unsigned getRegionNumber();
81 
83  bool isProperAncestor(Region *other);
84 
87  bool isAncestor(Region *other) {
88  return this == other || isProperAncestor(other);
89  }
90 
95  void cloneInto(Region *dest, BlockAndValueMapping &mapper);
97  void cloneInto(Region *dest, Region::iterator destPos,
98  BlockAndValueMapping &mapper);
99 
102  void takeBody(Region &other) {
103  blocks.clear();
104  blocks.splice(blocks.end(), other.getBlocks());
105  }
106 
112 
116  void dropAllReferences();
117 
121  template <typename FnT, typename RetT = detail::walkResultType<FnT>>
122  typename std::enable_if<std::is_same<RetT, void>::value, RetT>::type
123  walk(FnT &&callback) {
124  for (auto &block : *this)
125  block.walk(callback);
126  }
127 
131  template <typename FnT, typename RetT = detail::walkResultType<FnT>>
132  typename std::enable_if<std::is_same<RetT, WalkResult>::value, RetT>::type
133  walk(FnT &&callback) {
134  for (auto &block : *this)
135  if (block.walk(callback).wasInterrupted())
136  return WalkResult::interrupt();
137  return WalkResult::advance();
138  }
139 
144  void viewGraph(const Twine &regionName);
145  void viewGraph();
146 
147 private:
148  BlockListType blocks;
149 
151  Operation *container;
152 };
153 
161  RegionRange, PointerUnion<Region *, const std::unique_ptr<Region> *>,
162  Region *, Region *, Region *> {
166 
167 public:
169 
171 
172  template <typename Arg,
173  typename = typename std::enable_if_t<std::is_constructible<
174  ArrayRef<std::unique_ptr<Region>>, Arg>::value>>
175  RegionRange(Arg &&arg)
176  : RegionRange(ArrayRef<std::unique_ptr<Region>>(std::forward<Arg>(arg))) {
177  }
178  RegionRange(ArrayRef<std::unique_ptr<Region>> regions);
179 
180 private:
182  static OwnerT offset_base(const OwnerT &owner, ptrdiff_t index);
184  static Region *dereference_iterator(const OwnerT &owner, ptrdiff_t index);
185 
187  friend RangeBaseT;
188 };
189 
190 } // end namespace mlir
191 
192 #endif // MLIR_IR_REGION_H
Definition: InferTypeOpInterface.cpp:20
Definition: Region.h:23
Definition: Operation.h:27
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition: Region.cpp:41
BlockListType & getBlocks()
Definition: Region.h:38
Block represents an ordered list of Operations.
Definition: Block.h:21
Block & front()
Definition: Region.h:54
void push_front(Block *block)
Definition: Region.h:51
void push_back(Block *block)
Definition: Region.h:50
BlockListType::iterator iterator
Definition: Region.h:41
Definition: LLVM.h:40
std::enable_if< std::is_same< RetT, WalkResult >::value, RetT >::type walk(FnT &&callback)
Definition: Region.h:133
void takeBody(Region &other)
Definition: Region.h:102
void dropAllReferences()
Definition: Region.cpp:111
BlockListType::reverse_iterator reverse_iterator
Definition: Region.h:42
Definition: Location.h:52
Region * getParentRegion()
Definition: Region.cpp:36
RegionRange(Arg &&arg)
Definition: Region.h:175
Definition: LLVM.h:37
bool empty()
Definition: Region.h:49
unsigned getRegionNumber()
Return the number of this region in the parent operation.
Definition: Region.cpp:55
iterator begin()
Definition: Region.h:44
Definition: LLVM.h:38
Block & back()
Definition: Region.h:53
static WalkResult advance()
Definition: Visitors.h:44
static WalkResult interrupt()
Definition: Visitors.h:43
void cloneInto(Region *dest, BlockAndValueMapping &mapper)
Definition: Region.cpp:63
reverse_iterator rend()
Definition: Region.h:47
ParentT getParentOfType()
Definition: Region.h:70
Region()=default
bool isIsolatedFromAbove(Optional< Location > noteLoc=llvm::None)
Definition: Region.cpp:168
~Region()
Definition: Region.cpp:16
Definition: BlockAndValueMapping.h:26
reverse_iterator rbegin()
Definition: Region.h:46
MLIRContext * getContext()
Definition: Region.cpp:24
iterator end()
Definition: Region.h:45
bool isProperAncestor(Region *other)
Return true if this region is a proper ancestor of the other region.
Definition: Region.cpp:43
Definition: MLIRContext.h:34
Definition: Region.h:159
static BlockListType Region::* getSublistAccess(Block *)
getSublistAccess() - Returns pointer to member of region.
Definition: Region.h:57
Definition: StandardTypes.h:63
void viewGraph()
Definition: ViewRegionGraph.cpp:60
Location getLoc()
Definition: Region.cpp:31
std::enable_if< std::is_same< RetT, void >::value, RetT >::type walk(FnT &&callback)
Definition: Region.h:123
bool isAncestor(Region *other)
Definition: Region.h:87
llvm::iplist< Block > BlockListType
Definition: Region.h:37
Definition: LLVM.h:41