MLIR 23.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
13
14using namespace mlir;
15
16/// Include the definitions of the loop-like interfaces.
17#include "mlir/Interfaces/LoopLikeInterface.cpp.inc"
18
19bool LoopLikeOpInterface::blockIsInLoop(Block *block) {
20 Operation *parent = block->getParentOp();
21
22 // The block could be inside a loop-like operation
23 if (isa<LoopLikeOpInterface>(parent) ||
24 parent->getParentOfType<LoopLikeOpInterface>())
25 return true;
26
27 // This block might be nested inside another block, which is in a loop
28 if (!isa<FunctionOpInterface>(parent))
29 if (mlir::Block *parentBlock = parent->getBlock())
30 if (blockIsInLoop(parentBlock))
31 return true;
32
33 // Or the block could be inside a control flow graph loop:
34 // A block is in a control flow graph loop if it can reach itself in a graph
35 // traversal
36 DenseSet<Block *> visited;
38 stack.push_back(block);
39 while (!stack.empty()) {
40 Block *current = stack.pop_back_val();
41 auto [it, inserted] = visited.insert(current);
42 if (!inserted) {
43 // loop detected
44 if (current == block)
45 return true;
46 continue;
47 }
48
49 stack.reserve(stack.size() + current->getNumSuccessors());
50 for (Block *successor : current->getSuccessors())
51 stack.push_back(successor);
52 }
53 return false;
54}
55
57 auto loopLikeOp = cast<LoopLikeOpInterface>(op);
58
59 // Verify number of inits/iter_args/yielded values/loop results.
60 if (loopLikeOp.getInits().size() != loopLikeOp.getRegionIterArgs().size())
61 return op->emitOpError("different number of inits and region iter_args: ")
62 << loopLikeOp.getInits().size()
63 << " != " << loopLikeOp.getRegionIterArgs().size();
64 if (!loopLikeOp.getYieldedValues().empty() &&
65 loopLikeOp.getRegionIterArgs().size() !=
66 loopLikeOp.getYieldedValues().size())
67 return op->emitOpError(
68 "different number of region iter_args and yielded values: ")
69 << loopLikeOp.getRegionIterArgs().size()
70 << " != " << loopLikeOp.getYieldedValues().size();
71 if (loopLikeOp.getLoopResults() && loopLikeOp.getLoopResults()->size() !=
72 loopLikeOp.getRegionIterArgs().size())
73 return op->emitOpError(
74 "different number of loop results and region iter_args: ")
75 << loopLikeOp.getLoopResults()->size()
76 << " != " << loopLikeOp.getRegionIterArgs().size();
77
78 // Verify types of inits/iter_args/yielded values/loop results.
79 // If the op also implements RegionBranchOpInterface, type compatibility is
80 // already verified by that interface's verifier (which also provides an
81 // overridable areTypesCompatible hook), so skip the check here.
82 if (!isa<RegionBranchOpInterface>(op)) {
83 auto yieldedValues = loopLikeOp.getYieldedValues();
84 for (const auto [index, init, regionIterArg] : llvm::enumerate(
85 loopLikeOp.getInits(), loopLikeOp.getRegionIterArgs())) {
86 if (init.getType() != regionIterArg.getType())
87 return op->emitOpError(std::to_string(index))
88 << "-th init and " << index
89 << "-th region iter_arg have different type: " << init.getType()
90 << " != " << regionIterArg.getType();
91 if (!yieldedValues.empty()) {
92 if (regionIterArg.getType() != yieldedValues[index].getType())
93 return op->emitOpError(std::to_string(index))
94 << "-th region iter_arg and " << index
95 << "-th yielded value have different type: "
96 << regionIterArg.getType()
97 << " != " << yieldedValues[index].getType();
98 }
99 }
100 if (loopLikeOp.getLoopResults()) {
101 for (const auto [index, regionIterArg, loopResult] : llvm::enumerate(
102 loopLikeOp.getRegionIterArgs(), *loopLikeOp.getLoopResults())) {
103 if (regionIterArg.getType() != loopResult.getType())
104 return op->emitOpError(std::to_string(index))
105 << "-th region iter_arg and " << index
106 << "-th loop result have different type: "
107 << regionIterArg.getType() << " != " << loopResult.getType();
108 }
109 }
110 }
111
112 // Verify that all induction variables have valid types.
113 auto inductionVars = loopLikeOp.getLoopInductionVars();
114 if (inductionVars.has_value()) {
115 for (auto [index, inductionVar] : llvm::enumerate(*inductionVars)) {
116 if (!loopLikeOp.isValidInductionVarType(inductionVar.getType()))
117 return op->emitOpError(std::to_string(index))
118 << "-th induction variable has invalid type: "
119 << inductionVar.getType();
120 }
121 }
122
123 return success();
124}
return success()
*if copies could not be generated due to yet unimplemented cases *copyInPlacementStart and copyOutPlacementStart in copyPlacementBlock *specify the insertion points where the incoming copies and outgoing should be inserted(the insertion happens right before the *insertion point). Since `begin` can itself be invalidated due to the memref *rewriting done from this method
Block represents an ordered list of Operations.
Definition Block.h:33
unsigned getNumSuccessors()
Definition Block.cpp:270
SuccessorRange getSuccessors()
Definition Block.h:280
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:231
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Definition Operation.h:256
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
LogicalResult verifyLoopLikeOpInterface(Operation *op)
Verify invariants of the LoopLikeOpInterface.
Include the generated interface declarations.
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:122