MLIR  22.0.0git
LoopLikeInterface.cpp
Go to the documentation of this file.
1 //===- LoopLikeInterface.cpp - Loop-like operations in MLIR ---------------===//
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 
10 
12 
13 using namespace mlir;
14 
15 /// Include the definitions of the loop-like interfaces.
16 #include "mlir/Interfaces/LoopLikeInterface.cpp.inc"
17 
18 bool LoopLikeOpInterface::blockIsInLoop(Block *block) {
19  Operation *parent = block->getParentOp();
20 
21  // The block could be inside a loop-like operation
22  if (isa<LoopLikeOpInterface>(parent) ||
23  parent->getParentOfType<LoopLikeOpInterface>())
24  return true;
25 
26  // This block might be nested inside another block, which is in a loop
27  if (!isa<FunctionOpInterface>(parent))
28  if (mlir::Block *parentBlock = parent->getBlock())
29  if (blockIsInLoop(parentBlock))
30  return true;
31 
32  // Or the block could be inside a control flow graph loop:
33  // A block is in a control flow graph loop if it can reach itself in a graph
34  // traversal
35  DenseSet<Block *> visited;
37  stack.push_back(block);
38  while (!stack.empty()) {
39  Block *current = stack.pop_back_val();
40  auto [it, inserted] = visited.insert(current);
41  if (!inserted) {
42  // loop detected
43  if (current == block)
44  return true;
45  continue;
46  }
47 
48  stack.reserve(stack.size() + current->getNumSuccessors());
49  for (Block *successor : current->getSuccessors())
50  stack.push_back(successor);
51  }
52  return false;
53 }
54 
56  // Note: These invariants are also verified by the RegionBranchOpInterface,
57  // but the LoopLikeOpInterface provides better error messages.
58  auto loopLikeOp = cast<LoopLikeOpInterface>(op);
59 
60  // Verify number of inits/iter_args/yielded values/loop results.
61  if (loopLikeOp.getInits().size() != loopLikeOp.getRegionIterArgs().size())
62  return op->emitOpError("different number of inits and region iter_args: ")
63  << loopLikeOp.getInits().size()
64  << " != " << loopLikeOp.getRegionIterArgs().size();
65  if (!loopLikeOp.getYieldedValues().empty() &&
66  loopLikeOp.getRegionIterArgs().size() !=
67  loopLikeOp.getYieldedValues().size())
68  return op->emitOpError(
69  "different number of region iter_args and yielded values: ")
70  << loopLikeOp.getRegionIterArgs().size()
71  << " != " << loopLikeOp.getYieldedValues().size();
72  if (loopLikeOp.getLoopResults() && loopLikeOp.getLoopResults()->size() !=
73  loopLikeOp.getRegionIterArgs().size())
74  return op->emitOpError(
75  "different number of loop results and region iter_args: ")
76  << loopLikeOp.getLoopResults()->size()
77  << " != " << loopLikeOp.getRegionIterArgs().size();
78 
79  // Verify types of inits/iter_args/yielded values/loop results.
80  int64_t i = 0;
81  auto yieldedValues = loopLikeOp.getYieldedValues();
82  for (const auto [index, init, regionIterArg] :
83  llvm::enumerate(loopLikeOp.getInits(), loopLikeOp.getRegionIterArgs())) {
84  if (init.getType() != regionIterArg.getType())
85  return op->emitOpError(std::to_string(index))
86  << "-th init and " << index
87  << "-th region iter_arg have different type: " << init.getType()
88  << " != " << regionIterArg.getType();
89  if (!yieldedValues.empty()) {
90  if (regionIterArg.getType() != yieldedValues[index].getType())
91  return op->emitOpError(std::to_string(index))
92  << "-th region iter_arg and " << index
93  << "-th yielded value have different type: "
94  << regionIterArg.getType()
95  << " != " << yieldedValues[index].getType();
96  }
97  ++i;
98  }
99  i = 0;
100  if (loopLikeOp.getLoopResults()) {
101  for (const auto it : llvm::zip_equal(loopLikeOp.getRegionIterArgs(),
102  *loopLikeOp.getLoopResults())) {
103  if (std::get<0>(it).getType() != std::get<1>(it).getType())
104  return op->emitOpError(std::to_string(i))
105  << "-th region iter_arg and " << i
106  << "-th loop result have different type: "
107  << std::get<0>(it).getType()
108  << " != " << std::get<1>(it).getType();
109  }
110  ++i;
111  }
112 
113  return success();
114 }
Block represents an ordered list of Operations.
Definition: Block.h:33
unsigned getNumSuccessors()
Definition: Block.cpp:265
SuccessorRange getSuccessors()
Definition: Block.h:270
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
Definition: Block.cpp:31
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Block * getBlock()
Returns the operation block that contains this operation.
Definition: Operation.h:213
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition: Operation.h:238
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Definition: Operation.cpp:672
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:344
LogicalResult verifyLoopLikeOpInterface(Operation *op)
Verify invariants of the LoopLikeOpInterface.
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition: Utils.cpp:304