|
15 | 15 | #define MLIR_INTERFACES_CONTROLFLOWINTERFACES_H |
16 | 16 |
|
17 | 17 | #include "mlir/IR/OpDefinition.h" |
18 | | -#include "mlir/IR/Operation.h" |
19 | | -#include "llvm/ADT/PointerUnion.h" |
20 | | -#include "llvm/ADT/STLExtras.h" |
21 | | -#include "llvm/Support/DebugLog.h" |
22 | | -#include "llvm/Support/raw_ostream.h" |
23 | 18 |
|
24 | 19 | namespace mlir { |
25 | 20 | class BranchOpInterface; |
26 | 21 | class RegionBranchOpInterface; |
27 | | -class RegionBranchTerminatorOpInterface; |
28 | 22 |
|
29 | 23 | /// This class models how operands are forwarded to block arguments in control |
30 | 24 | /// flow. It consists of a number, denoting how many of the successors block |
@@ -192,108 +186,92 @@ class RegionSuccessor { |
192 | 186 | public: |
193 | 187 | /// Initialize a successor that branches to another region of the parent |
194 | 188 | /// operation. |
195 | | - /// TODO: the default value for the regionInputs is somehow broken. |
196 | | - /// A region successor should have its input correctly set. |
197 | 189 | RegionSuccessor(Region *region, Block::BlockArgListType regionInputs = {}) |
198 | | - : successor(region), inputs(regionInputs) { |
199 | | - assert(region && "Region must not be null"); |
200 | | - } |
| 190 | + : region(region), inputs(regionInputs) {} |
201 | 191 | /// Initialize a successor that branches back to/out of the parent operation. |
202 | | - /// The target must be one of the recursive parent operations. |
203 | | - RegionSuccessor(Operation *successorOp, Operation::result_range results) |
204 | | - : successor(successorOp), inputs(ValueRange(results)) { |
205 | | - assert(successorOp && "Successor op must not be null"); |
206 | | - } |
| 192 | + RegionSuccessor(Operation::result_range results) |
| 193 | + : inputs(ValueRange(results)) {} |
| 194 | + /// Constructor with no arguments. |
| 195 | + RegionSuccessor() : inputs(ValueRange()) {} |
207 | 196 |
|
208 | 197 | /// Return the given region successor. Returns nullptr if the successor is the |
209 | 198 | /// parent operation. |
210 | | - Region *getSuccessor() const { return dyn_cast<Region *>(successor); } |
| 199 | + Region *getSuccessor() const { return region; } |
211 | 200 |
|
212 | 201 | /// Return true if the successor is the parent operation. |
213 | | - bool isParent() const { return isa<Operation *>(successor); } |
| 202 | + bool isParent() const { return region == nullptr; } |
214 | 203 |
|
215 | 204 | /// Return the inputs to the successor that are remapped by the exit values of |
216 | 205 | /// the current region. |
217 | 206 | ValueRange getSuccessorInputs() const { return inputs; } |
218 | 207 |
|
219 | | - bool operator==(RegionSuccessor rhs) const { |
220 | | - return successor == rhs.successor && inputs == rhs.inputs; |
221 | | - } |
222 | | - |
223 | | - friend bool operator!=(RegionSuccessor lhs, RegionSuccessor rhs) { |
224 | | - return !(lhs == rhs); |
225 | | - } |
226 | | - |
227 | 208 | private: |
228 | | - llvm::PointerUnion<Region *, Operation *> successor{nullptr}; |
| 209 | + Region *region{nullptr}; |
229 | 210 | ValueRange inputs; |
230 | 211 | }; |
231 | 212 |
|
232 | 213 | /// This class represents a point being branched from in the methods of the |
233 | 214 | /// `RegionBranchOpInterface`. |
234 | 215 | /// One can branch from one of two kinds of places: |
235 | 216 | /// * The parent operation (aka the `RegionBranchOpInterface` implementation) |
236 | | -/// * A RegionBranchTerminatorOpInterface inside a region within the parent |
237 | | -// operation. |
| 217 | +/// * A region within the parent operation. |
238 | 218 | class RegionBranchPoint { |
239 | 219 | public: |
240 | 220 | /// Returns an instance of `RegionBranchPoint` representing the parent |
241 | 221 | /// operation. |
242 | 222 | static constexpr RegionBranchPoint parent() { return RegionBranchPoint(); } |
243 | 223 |
|
244 | | - /// Creates a `RegionBranchPoint` that branches from the given terminator. |
245 | | - inline RegionBranchPoint(RegionBranchTerminatorOpInterface predecessor); |
| 224 | + /// Creates a `RegionBranchPoint` that branches from the given region. |
| 225 | + /// The pointer must not be null. |
| 226 | + RegionBranchPoint(Region *region) : maybeRegion(region) { |
| 227 | + assert(region && "Region must not be null"); |
| 228 | + } |
| 229 | + |
| 230 | + RegionBranchPoint(Region ®ion) : RegionBranchPoint(®ion) {} |
246 | 231 |
|
247 | 232 | /// Explicitly stops users from constructing with `nullptr`. |
248 | 233 | RegionBranchPoint(std::nullptr_t) = delete; |
249 | 234 |
|
| 235 | + /// Constructs a `RegionBranchPoint` from the the target of a |
| 236 | + /// `RegionSuccessor` instance. |
| 237 | + RegionBranchPoint(RegionSuccessor successor) { |
| 238 | + if (successor.isParent()) |
| 239 | + maybeRegion = nullptr; |
| 240 | + else |
| 241 | + maybeRegion = successor.getSuccessor(); |
| 242 | + } |
| 243 | + |
| 244 | + /// Assigns a region being branched from. |
| 245 | + RegionBranchPoint &operator=(Region ®ion) { |
| 246 | + maybeRegion = ®ion; |
| 247 | + return *this; |
| 248 | + } |
| 249 | + |
250 | 250 | /// Returns true if branching from the parent op. |
251 | | - bool isParent() const { return predecessor == nullptr; } |
| 251 | + bool isParent() const { return maybeRegion == nullptr; } |
252 | 252 |
|
253 | | - /// Returns the terminator if branching from a region. |
| 253 | + /// Returns the region if branching from a region. |
254 | 254 | /// A null pointer otherwise. |
255 | | - Operation *getTerminatorPredecessorOrNull() const { return predecessor; } |
| 255 | + Region *getRegionOrNull() const { return maybeRegion; } |
256 | 256 |
|
257 | 257 | /// Returns true if the two branch points are equal. |
258 | 258 | friend bool operator==(RegionBranchPoint lhs, RegionBranchPoint rhs) { |
259 | | - return lhs.predecessor == rhs.predecessor; |
| 259 | + return lhs.maybeRegion == rhs.maybeRegion; |
260 | 260 | } |
261 | 261 |
|
262 | 262 | private: |
263 | 263 | // Private constructor to encourage the use of `RegionBranchPoint::parent`. |
264 | | - constexpr RegionBranchPoint() = default; |
| 264 | + constexpr RegionBranchPoint() : maybeRegion(nullptr) {} |
265 | 265 |
|
266 | 266 | /// Internal encoding. Uses nullptr for representing branching from the parent |
267 | | - /// op and the region terminator being branched from otherwise. |
268 | | - Operation *predecessor = nullptr; |
| 267 | + /// op and the region being branched from otherwise. |
| 268 | + Region *maybeRegion; |
269 | 269 | }; |
270 | 270 |
|
271 | 271 | inline bool operator!=(RegionBranchPoint lhs, RegionBranchPoint rhs) { |
272 | 272 | return !(lhs == rhs); |
273 | 273 | } |
274 | 274 |
|
275 | | -inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, |
276 | | - RegionBranchPoint point) { |
277 | | - if (point.isParent()) |
278 | | - return os << "<from parent>"; |
279 | | - return os << "<region #" |
280 | | - << point.getTerminatorPredecessorOrNull() |
281 | | - ->getParentRegion() |
282 | | - ->getRegionNumber() |
283 | | - << ", terminator " |
284 | | - << OpWithFlags(point.getTerminatorPredecessorOrNull(), |
285 | | - OpPrintingFlags().skipRegions()) |
286 | | - << ">"; |
287 | | -} |
288 | | - |
289 | | -inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, |
290 | | - RegionSuccessor successor) { |
291 | | - if (successor.isParent()) |
292 | | - return os << "<to parent>"; |
293 | | - return os << "<to region #" << successor.getSuccessor()->getRegionNumber() |
294 | | - << " with " << successor.getSuccessorInputs().size() << " inputs>"; |
295 | | -} |
296 | | - |
297 | 275 | /// This class represents upper and lower bounds on the number of times a region |
298 | 276 | /// of a `RegionBranchOpInterface` can be invoked. The lower bound is at least |
299 | 277 | /// zero, but the upper bound may not be known. |
@@ -370,10 +348,4 @@ struct ReturnLike : public TraitBase<ConcreteType, ReturnLike> { |
370 | 348 | /// Include the generated interface declarations. |
371 | 349 | #include "mlir/Interfaces/ControlFlowInterfaces.h.inc" |
372 | 350 |
|
373 | | -namespace mlir { |
374 | | -inline RegionBranchPoint::RegionBranchPoint( |
375 | | - RegionBranchTerminatorOpInterface predecessor) |
376 | | - : predecessor(predecessor.getOperation()) {} |
377 | | -} // namespace mlir |
378 | | - |
379 | 351 | #endif // MLIR_INTERFACES_CONTROLFLOWINTERFACES_H |
0 commit comments