20 #include "mlir/Interfaces/ControlFlowInterfaces.cpp.inc"
23 : producedOperandCount(0), forwardedOperands(std::move(forwardedOperands)) {
28 : producedOperandCount(producedOperandCount),
29 forwardedOperands(std::move(forwardedOperands)) {}
38 std::optional<BlockArgument>
40 unsigned operandIndex,
Block *successor) {
43 if (forwardedOperands.empty())
48 if (operandIndex < operandsStart ||
49 operandIndex >= (operandsStart + forwardedOperands.size()))
63 unsigned operandCount = operands.
size();
66 return op->
emitError() <<
"branch has " << operandCount
67 <<
" operands for successor #" << succNo
68 <<
", but target block has "
74 if (!cast<BranchOpInterface>(op).areTypesCompatible(
76 return op->
emitError() <<
"type mismatch for bb argument #" << i
77 <<
" of successor #" << succNo;
88 std::size_t expectedWeightsNum,
89 llvm::StringRef weightAnchorName,
90 llvm::StringRef weightRefName) {
94 if (weights.size() != expectedWeightsNum)
95 return op->
emitError() <<
"expects number of " << weightAnchorName
96 <<
" weights to match number of " << weightRefName
97 <<
": " << weights.size() <<
" vs "
98 << expectedWeightsNum;
100 if (llvm::all_of(weights, [](int32_t value) {
return value == 0; }))
101 return op->
emitError() <<
"branch weights cannot all be zero";
108 cast<WeightedBranchOpInterface>(op).getWeights();
119 cast<WeightedRegionBranchOpInterface>(op).getWeights();
132 diag <<
"Region #" << region->getRegionNumber();
134 diag <<
"parent operands";
138 diag <<
"Region #" << region->getRegionNumber();
140 diag <<
"parent results";
150 getInputsTypesForRegion) {
151 auto regionInterface = cast<RegionBranchOpInterface>(op);
154 regionInterface.getSuccessorRegions(sourcePoint, successors);
157 FailureOr<TypeRange> sourceTypes = getInputsTypesForRegion(succ);
161 TypeRange succInputsTypes = succ.getSuccessorInputs().getTypes();
162 if (sourceTypes->size() != succInputsTypes.size()) {
165 <<
": source has " << sourceTypes->size()
166 <<
" operands, but target successor needs "
167 << succInputsTypes.size();
170 for (
const auto &typesIdx :
172 Type sourceType = std::get<0>(typesIdx.value());
173 Type inputType = std::get<1>(typesIdx.value());
174 if (!regionInterface.areTypesCompatible(sourceType, inputType)) {
177 <<
": source type #" << typesIdx.index() <<
" " << sourceType
178 <<
" should match input type #" << typesIdx.index() <<
" "
188 auto regionInterface = cast<RegionBranchOpInterface>(op);
191 return regionInterface.getEntrySuccessorOperands(point).getTypes();
196 inputTypesFromParent)))
200 if (lhs.size() != rhs.size())
202 for (
auto types : llvm::zip(lhs, rhs)) {
203 if (!regionInterface.areTypesCompatible(std::get<0>(types),
204 std::get<1>(types))) {
219 for (
Block &block : region)
221 if (
auto terminator =
222 dyn_cast<RegionBranchTerminatorOpInterface>(block.back()))
223 regionReturnOps.push_back(terminator);
227 if (regionReturnOps.empty())
230 auto inputTypesForRegion =
232 std::optional<OperandRange> regionReturnOperands;
233 for (RegionBranchTerminatorOpInterface regionReturnOp : regionReturnOps) {
234 auto terminatorOperands = regionReturnOp.getSuccessorOperands(point);
236 if (!regionReturnOperands) {
237 regionReturnOperands = terminatorOperands;
243 if (!areTypesCompatible(regionReturnOperands->getTypes(),
244 terminatorOperands.getTypes())) {
247 <<
" operands mismatch between return-like terminators";
252 return TypeRange(regionReturnOperands->getTypes());
274 auto op = cast<RegionBranchOpInterface>(begin->
getParentOp());
280 auto enqueueAllSuccessors = [&](
Region *region) {
282 op.getSuccessorRegions(region, successors);
284 if (!successor.isParent())
285 worklist.push_back(successor.getSuccessor());
287 enqueueAllSuccessors(begin);
290 while (!worklist.empty()) {
291 Region *nextRegion = worklist.pop_back_val();
292 if (stopConditionFn(nextRegion, visited))
297 enqueueAllSuccessors(nextRegion);
307 "expected that both regions belong to the same op");
311 return nextRegion == r;
325 assert(a &&
"expected non-empty operation");
326 assert(b &&
"expected non-empty operation");
331 if (!branchOp->isProperAncestor(b)) {
333 branchOp = branchOp->getParentOfType<RegionBranchOpInterface>();
339 Region *regionA =
nullptr, *regionB =
nullptr;
340 for (
Region &r : branchOp->getRegions()) {
341 if (r.findAncestorOpInRegion(*a)) {
342 assert(!regionA &&
"already found a region for a");
345 if (r.findAncestorOpInRegion(*b)) {
346 assert(!regionB &&
"already found a region for b");
350 assert(regionA && regionB &&
"could not find region of op");
364 Region *region = &getOperation()->getRegion(index);
368 bool RegionBranchOpInterface::hasLoop() {
372 if (!successor.isParent() &&
377 return visited[nextRegion->getRegionNumber()];
386 if (
auto branchOp = dyn_cast<RegionBranchOpInterface>(op))
397 if (
auto branchOp = dyn_cast<RegionBranchOpInterface>(op))
static bool isRepetitiveRegion(Region *region, const BufferizationOptions &options)
static LogicalResult verifyWeights(Operation *op, llvm::ArrayRef< int32_t > weights, std::size_t expectedWeightsNum, llvm::StringRef weightAnchorName, llvm::StringRef weightRefName)
static bool traverseRegionGraph(Region *begin, StopConditionFn stopConditionFn)
Traverse the region graph starting at begin.
static InFlightDiagnostic & printRegionEdgeName(InFlightDiagnostic &diag, RegionBranchPoint sourceNo, RegionBranchPoint succRegionNo)
static LogicalResult verifyTypesAlongAllEdges(Operation *op, RegionBranchPoint sourcePoint, function_ref< FailureOr< TypeRange >(RegionBranchPoint)> getInputsTypesForRegion)
Verify that types match along all region control flow edges originating from sourcePoint.
static bool isRegionReachable(Region *begin, Region *r)
Return true if region r is reachable from region begin according to the RegionBranchOpInterface (by t...
static std::string diag(const llvm::Value &value)
Block represents an ordered list of Operations.
BlockArgument getArgument(unsigned i)
unsigned getNumArguments()
This class represents a diagnostic that is inflight and set to be reported.
This class provides a mutable adaptor for a range of operands.
This class implements the operand iterators for the Operation class.
unsigned getBeginOperandIndex() const
Return the operand index of the first element of this range.
Operation is the basic unit of execution within MLIR.
Block * getSuccessor(unsigned index)
unsigned getNumSuccessors()
unsigned getNumRegions()
Returns the number of regions held by this operation.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
Region * getParentRegion()
Returns the region to which the instruction belongs.
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class represents a point being branched from in the methods of the RegionBranchOpInterface.
static constexpr RegionBranchPoint parent()
Returns an instance of RegionBranchPoint representing the parent operation.
Region * getRegionOrNull() const
Returns the region if branching from a region.
This class represents a successor of a region.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
unsigned getRegionNumber()
Return the number of this region in the parent operation.
Operation * getParentOp()
Return the parent operation this region is attached to.
This class models how operands are forwarded to block arguments in control flow.
SuccessorOperands(MutableOperandRange forwardedOperands)
Constructs a SuccessorOperands with no produced operands that simply forwards operands to the success...
unsigned getProducedOperandCount() const
Returns the amount of operands that are produced internally by the operation.
unsigned size() const
Returns the amount of operands passed to the successor.
OperandRange getForwardedOperands() const
Get the range of operands that are simply forwarded to the successor.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Region * getParentRegion()
Return the Region in which this Value is defined.
std::optional< BlockArgument > getBranchSuccessorArgument(const SuccessorOperands &operands, unsigned operandIndex, Block *successor)
Return the BlockArgument corresponding to operand operandIndex in some successor if operandIndex is w...
LogicalResult verifyRegionBranchWeights(Operation *op)
Verify that the region weights attached to an operation implementing WeightedRegiobBranchOpInterface ...
LogicalResult verifyBranchSuccessorOperands(Operation *op, unsigned succNo, const SuccessorOperands &operands)
Verify that the given operands match those of the given successor block.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
LogicalResult verifyTypesAlongControlFlowEdges(Operation *op)
Verify that types match along control flow edges described the given op.
LogicalResult verifyBranchWeights(Operation *op)
Verify that the branch weights attached to an operation implementing WeightedBranchOpInterface are co...
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
bool insideMutuallyExclusiveRegions(Operation *a, Operation *b)
Return true if a and b are in mutually exclusive regions as per RegionBranchOpInterface.
Region * getEnclosingRepetitiveRegion(Operation *op)
Return the first enclosing region of the given op that may be executed repetitively as per RegionBran...