2020-03-10 12:20:24 -07:00
|
|
|
//===- ControlFlowInterfaces.cpp - ControlFlow Interfaces -----------------===//
|
2020-03-05 12:40:23 -08:00
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2022-04-16 08:06:25 +00:00
|
|
|
#include <utility>
|
|
|
|
|
|
2022-06-13 22:02:02 +00:00
|
|
|
#include "mlir/IR/BuiltinTypes.h"
|
2020-03-10 12:20:24 -07:00
|
|
|
#include "mlir/Interfaces/ControlFlowInterfaces.h"
|
2020-07-15 11:12:38 -07:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2020-03-05 12:40:23 -08:00
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// ControlFlowInterfaces
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2020-03-10 12:20:24 -07:00
|
|
|
#include "mlir/Interfaces/ControlFlowInterfaces.cpp.inc"
|
2020-03-05 12:40:23 -08:00
|
|
|
|
2022-04-08 08:17:36 +02:00
|
|
|
SuccessorOperands::SuccessorOperands(MutableOperandRange forwardedOperands)
|
2022-04-16 08:06:25 +00:00
|
|
|
: producedOperandCount(0), forwardedOperands(std::move(forwardedOperands)) {
|
|
|
|
|
}
|
2022-04-08 08:17:36 +02:00
|
|
|
|
|
|
|
|
SuccessorOperands::SuccessorOperands(unsigned int producedOperandCount,
|
|
|
|
|
MutableOperandRange forwardedOperands)
|
|
|
|
|
: producedOperandCount(producedOperandCount),
|
|
|
|
|
forwardedOperands(std::move(forwardedOperands)) {}
|
|
|
|
|
|
2020-03-05 12:40:23 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// BranchOpInterface
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
/// Returns the `BlockArgument` corresponding to operand `operandIndex` in some
|
2022-12-04 19:58:32 -08:00
|
|
|
/// successor if 'operandIndex' is within the range of 'operands', or
|
|
|
|
|
/// std::nullopt if `operandIndex` isn't a successor operand index.
|
2022-12-14 11:39:19 +01:00
|
|
|
std::optional<BlockArgument>
|
2022-04-08 08:17:36 +02:00
|
|
|
detail::getBranchSuccessorArgument(const SuccessorOperands &operands,
|
2020-07-15 11:12:38 -07:00
|
|
|
unsigned operandIndex, Block *successor) {
|
2022-04-08 08:17:36 +02:00
|
|
|
OperandRange forwardedOperands = operands.getForwardedOperands();
|
2020-03-05 12:40:23 -08:00
|
|
|
// Check that the operands are valid.
|
2022-04-08 08:17:36 +02:00
|
|
|
if (forwardedOperands.empty())
|
2022-12-03 18:50:27 -08:00
|
|
|
return std::nullopt;
|
2020-03-05 12:40:23 -08:00
|
|
|
|
|
|
|
|
// Check to ensure that this operand is within the range.
|
2022-04-08 08:17:36 +02:00
|
|
|
unsigned operandsStart = forwardedOperands.getBeginOperandIndex();
|
2020-03-05 12:40:23 -08:00
|
|
|
if (operandIndex < operandsStart ||
|
2022-04-08 08:17:36 +02:00
|
|
|
operandIndex >= (operandsStart + forwardedOperands.size()))
|
2022-12-03 18:50:27 -08:00
|
|
|
return std::nullopt;
|
2020-03-05 12:40:23 -08:00
|
|
|
|
|
|
|
|
// Index the successor.
|
2022-04-08 08:17:36 +02:00
|
|
|
unsigned argIndex =
|
|
|
|
|
operands.getProducedOperandCount() + operandIndex - operandsStart;
|
2020-03-05 12:40:23 -08:00
|
|
|
return successor->getArgument(argIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Verify that the given operands match those of the given successor block.
|
|
|
|
|
LogicalResult
|
2020-07-15 11:12:38 -07:00
|
|
|
detail::verifyBranchSuccessorOperands(Operation *op, unsigned succNo,
|
2022-04-08 08:17:36 +02:00
|
|
|
const SuccessorOperands &operands) {
|
2020-03-05 12:40:23 -08:00
|
|
|
// Check the count.
|
2022-04-08 08:17:36 +02:00
|
|
|
unsigned operandCount = operands.size();
|
2020-03-05 12:40:23 -08:00
|
|
|
Block *destBB = op->getSuccessor(succNo);
|
|
|
|
|
if (operandCount != destBB->getNumArguments())
|
|
|
|
|
return op->emitError() << "branch has " << operandCount
|
|
|
|
|
<< " operands for successor #" << succNo
|
|
|
|
|
<< ", but target block has "
|
|
|
|
|
<< destBB->getNumArguments();
|
|
|
|
|
|
|
|
|
|
// Check the types.
|
2022-04-08 08:17:36 +02:00
|
|
|
for (unsigned i = operands.getProducedOperandCount(); i != operandCount;
|
|
|
|
|
++i) {
|
[mlir] Region/BranchOpInterface: Allow implicit type conversions along control-flow edges
RegionBranchOpInterface and BranchOpInterface are allowed to make implicit type conversions along control-flow edges. In effect, this adds an interface method, `areTypesCompatible`, to both interfaces, which should return whether the types of corresponding successor operands and block arguments are compatible. Users of the interfaces, here on forth, must be aware that types may mismatch, although current users (in MLIR core), are not affected by this change. By default, type equality is used.
`async.execute` already has unequal types along control-flow edges (`!async.value<f32>` vs. `f32`), but it opted out of calling `RegionBranchOpInterface::verifyTypes` in its verifier. That method has now been removed and `RegionBranchOpInterface` will verify types along control edges by default in its verifier.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D120790
2022-03-04 20:23:24 +00:00
|
|
|
if (!cast<BranchOpInterface>(op).areTypesCompatible(
|
2022-04-08 08:17:36 +02:00
|
|
|
operands[i].getType(), destBB->getArgument(i).getType()))
|
2020-03-05 12:40:23 -08:00
|
|
|
return op->emitError() << "type mismatch for bb argument #" << i
|
|
|
|
|
<< " of successor #" << succNo;
|
|
|
|
|
}
|
|
|
|
|
return success();
|
|
|
|
|
}
|
2020-07-15 11:12:38 -07:00
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
// RegionBranchOpInterface
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2023-08-30 09:22:34 +02:00
|
|
|
static InFlightDiagnostic &printRegionEdgeName(InFlightDiagnostic &diag,
|
|
|
|
|
RegionBranchPoint sourceNo,
|
|
|
|
|
RegionBranchPoint succRegionNo) {
|
2023-08-09 16:15:17 +02:00
|
|
|
diag << "from ";
|
2023-08-30 09:22:34 +02:00
|
|
|
if (Region *region = sourceNo.getRegionOrNull())
|
|
|
|
|
diag << "Region #" << region->getRegionNumber();
|
2023-08-09 16:15:17 +02:00
|
|
|
else
|
|
|
|
|
diag << "parent operands";
|
|
|
|
|
|
|
|
|
|
diag << " to ";
|
2023-08-30 09:22:34 +02:00
|
|
|
if (Region *region = succRegionNo.getRegionOrNull())
|
|
|
|
|
diag << "Region #" << region->getRegionNumber();
|
2023-08-09 16:15:17 +02:00
|
|
|
else
|
|
|
|
|
diag << "parent results";
|
|
|
|
|
return diag;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 11:12:38 -07:00
|
|
|
/// Verify that types match along all region control flow edges originating from
|
2023-09-21 18:17:14 +02:00
|
|
|
/// `sourcePoint`. `getInputsTypesForRegion` is a function that returns the
|
|
|
|
|
/// types of the inputs that flow to a successor region.
|
2023-08-30 09:22:34 +02:00
|
|
|
static LogicalResult
|
|
|
|
|
verifyTypesAlongAllEdges(Operation *op, RegionBranchPoint sourcePoint,
|
|
|
|
|
function_ref<FailureOr<TypeRange>(RegionBranchPoint)>
|
|
|
|
|
getInputsTypesForRegion) {
|
2020-07-15 11:12:38 -07:00
|
|
|
auto regionInterface = cast<RegionBranchOpInterface>(op);
|
|
|
|
|
|
|
|
|
|
SmallVector<RegionSuccessor, 2> successors;
|
2023-08-30 09:22:34 +02:00
|
|
|
regionInterface.getSuccessorRegions(sourcePoint, successors);
|
2020-07-15 11:12:38 -07:00
|
|
|
|
|
|
|
|
for (RegionSuccessor &succ : successors) {
|
2023-08-30 09:22:34 +02:00
|
|
|
FailureOr<TypeRange> sourceTypes = getInputsTypesForRegion(succ);
|
2023-08-09 16:15:17 +02:00
|
|
|
if (failed(sourceTypes))
|
|
|
|
|
return failure();
|
2020-11-04 09:41:55 +01:00
|
|
|
|
2020-07-15 11:12:38 -07:00
|
|
|
TypeRange succInputsTypes = succ.getSuccessorInputs().getTypes();
|
2020-11-04 09:41:55 +01:00
|
|
|
if (sourceTypes->size() != succInputsTypes.size()) {
|
2020-07-15 11:12:38 -07:00
|
|
|
InFlightDiagnostic diag = op->emitOpError(" region control flow edge ");
|
2023-08-30 09:22:34 +02:00
|
|
|
return printRegionEdgeName(diag, sourcePoint, succ)
|
2023-08-09 16:15:17 +02:00
|
|
|
<< ": source has " << sourceTypes->size()
|
|
|
|
|
<< " operands, but target successor needs "
|
|
|
|
|
<< succInputsTypes.size();
|
2020-07-15 11:12:38 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-02 22:02:14 +00:00
|
|
|
for (const auto &typesIdx :
|
2020-11-04 09:41:55 +01:00
|
|
|
llvm::enumerate(llvm::zip(*sourceTypes, succInputsTypes))) {
|
2020-07-15 11:12:38 -07:00
|
|
|
Type sourceType = std::get<0>(typesIdx.value());
|
|
|
|
|
Type inputType = std::get<1>(typesIdx.value());
|
[mlir] Region/BranchOpInterface: Allow implicit type conversions along control-flow edges
RegionBranchOpInterface and BranchOpInterface are allowed to make implicit type conversions along control-flow edges. In effect, this adds an interface method, `areTypesCompatible`, to both interfaces, which should return whether the types of corresponding successor operands and block arguments are compatible. Users of the interfaces, here on forth, must be aware that types may mismatch, although current users (in MLIR core), are not affected by this change. By default, type equality is used.
`async.execute` already has unequal types along control-flow edges (`!async.value<f32>` vs. `f32`), but it opted out of calling `RegionBranchOpInterface::verifyTypes` in its verifier. That method has now been removed and `RegionBranchOpInterface` will verify types along control edges by default in its verifier.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D120790
2022-03-04 20:23:24 +00:00
|
|
|
if (!regionInterface.areTypesCompatible(sourceType, inputType)) {
|
2020-07-15 11:12:38 -07:00
|
|
|
InFlightDiagnostic diag = op->emitOpError(" along control flow edge ");
|
2023-08-30 09:22:34 +02:00
|
|
|
return printRegionEdgeName(diag, sourcePoint, succ)
|
2020-09-08 15:49:50 -07:00
|
|
|
<< ": source type #" << typesIdx.index() << " " << sourceType
|
|
|
|
|
<< " should match input type #" << typesIdx.index() << " "
|
2020-07-15 11:12:38 -07:00
|
|
|
<< inputType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Verify that types match along control flow edges described the given op.
|
|
|
|
|
LogicalResult detail::verifyTypesAlongControlFlowEdges(Operation *op) {
|
|
|
|
|
auto regionInterface = cast<RegionBranchOpInterface>(op);
|
|
|
|
|
|
2023-09-21 18:17:14 +02:00
|
|
|
auto inputTypesFromParent = [&](RegionBranchPoint point) -> TypeRange {
|
|
|
|
|
return regionInterface.getEntrySuccessorOperands(point).getTypes();
|
2020-07-15 11:12:38 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Verify types along control flow edges originating from the parent.
|
2023-08-30 09:22:34 +02:00
|
|
|
if (failed(verifyTypesAlongAllEdges(op, RegionBranchPoint::parent(),
|
|
|
|
|
inputTypesFromParent)))
|
2020-07-15 11:12:38 -07:00
|
|
|
return failure();
|
|
|
|
|
|
[mlir] Region/BranchOpInterface: Allow implicit type conversions along control-flow edges
RegionBranchOpInterface and BranchOpInterface are allowed to make implicit type conversions along control-flow edges. In effect, this adds an interface method, `areTypesCompatible`, to both interfaces, which should return whether the types of corresponding successor operands and block arguments are compatible. Users of the interfaces, here on forth, must be aware that types may mismatch, although current users (in MLIR core), are not affected by this change. By default, type equality is used.
`async.execute` already has unequal types along control-flow edges (`!async.value<f32>` vs. `f32`), but it opted out of calling `RegionBranchOpInterface::verifyTypes` in its verifier. That method has now been removed and `RegionBranchOpInterface` will verify types along control edges by default in its verifier.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D120790
2022-03-04 20:23:24 +00:00
|
|
|
auto areTypesCompatible = [&](TypeRange lhs, TypeRange rhs) {
|
|
|
|
|
if (lhs.size() != rhs.size())
|
|
|
|
|
return false;
|
|
|
|
|
for (auto types : llvm::zip(lhs, rhs)) {
|
|
|
|
|
if (!regionInterface.areTypesCompatible(std::get<0>(types),
|
|
|
|
|
std::get<1>(types))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-15 11:12:38 -07:00
|
|
|
// Verify types along control flow edges originating from each region.
|
2023-08-30 09:22:34 +02:00
|
|
|
for (Region ®ion : op->getRegions()) {
|
2020-07-15 11:12:38 -07:00
|
|
|
|
2023-08-09 16:15:17 +02:00
|
|
|
// Since there can be multiple terminators implementing the
|
|
|
|
|
// `RegionBranchTerminatorOpInterface`, all should have the same operand
|
|
|
|
|
// types when passing them to the same region.
|
2020-07-15 11:12:38 -07:00
|
|
|
|
2023-08-09 16:15:17 +02:00
|
|
|
SmallVector<RegionBranchTerminatorOpInterface> regionReturnOps;
|
|
|
|
|
for (Block &block : region)
|
2024-01-04 14:43:52 -06:00
|
|
|
if (!block.empty())
|
|
|
|
|
if (auto terminator =
|
|
|
|
|
dyn_cast<RegionBranchTerminatorOpInterface>(block.back()))
|
|
|
|
|
regionReturnOps.push_back(terminator);
|
2020-07-15 11:12:38 -07:00
|
|
|
|
2023-08-09 16:15:17 +02:00
|
|
|
// If there is no return-like terminator, the op itself should verify
|
|
|
|
|
// type consistency.
|
|
|
|
|
if (regionReturnOps.empty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
auto inputTypesForRegion =
|
2023-09-21 18:17:14 +02:00
|
|
|
[&](RegionBranchPoint point) -> FailureOr<TypeRange> {
|
2023-08-09 16:15:17 +02:00
|
|
|
std::optional<OperandRange> regionReturnOperands;
|
|
|
|
|
for (RegionBranchTerminatorOpInterface regionReturnOp : regionReturnOps) {
|
2023-09-21 18:17:14 +02:00
|
|
|
auto terminatorOperands = regionReturnOp.getSuccessorOperands(point);
|
2023-08-09 16:15:17 +02:00
|
|
|
|
|
|
|
|
if (!regionReturnOperands) {
|
|
|
|
|
regionReturnOperands = terminatorOperands;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Found more than one ReturnLike terminator. Make sure the operand
|
|
|
|
|
// types match with the first one.
|
|
|
|
|
if (!areTypesCompatible(regionReturnOperands->getTypes(),
|
|
|
|
|
terminatorOperands.getTypes())) {
|
|
|
|
|
InFlightDiagnostic diag = op->emitOpError(" along control flow edge");
|
2023-09-21 18:17:14 +02:00
|
|
|
return printRegionEdgeName(diag, region, point)
|
2023-08-09 16:15:17 +02:00
|
|
|
<< " operands mismatch between return-like terminators";
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-04 09:41:55 +01:00
|
|
|
|
2021-07-23 11:59:21 +02:00
|
|
|
// All successors get the same set of operand types.
|
|
|
|
|
return TypeRange(regionReturnOperands->getTypes());
|
2020-07-15 11:12:38 -07:00
|
|
|
};
|
|
|
|
|
|
2023-08-30 09:22:34 +02:00
|
|
|
if (failed(verifyTypesAlongAllEdges(op, region, inputTypesForRegion)))
|
2020-07-15 11:12:38 -07:00
|
|
|
return failure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success();
|
|
|
|
|
}
|
2021-07-23 11:59:21 +02:00
|
|
|
|
2024-01-07 13:49:29 +01:00
|
|
|
/// Stop condition for `traverseRegionGraph`. The traversal is interrupted if
|
|
|
|
|
/// this function returns "true" for a successor region. The first parameter is
|
|
|
|
|
/// the successor region. The second parameter indicates all already visited
|
|
|
|
|
/// regions.
|
|
|
|
|
using StopConditionFn = function_ref<bool(Region *, ArrayRef<bool> visited)>;
|
|
|
|
|
|
|
|
|
|
/// Traverse the region graph starting at `begin`. The traversal is interrupted
|
|
|
|
|
/// if `stopCondition` evaluates to "true" for a successor region. In that case,
|
|
|
|
|
/// this function returns "true". Otherwise, if the traversal was not
|
|
|
|
|
/// interrupted, this function returns "false".
|
|
|
|
|
static bool traverseRegionGraph(Region *begin,
|
|
|
|
|
StopConditionFn stopConditionFn) {
|
2022-04-19 16:21:08 +09:00
|
|
|
auto op = cast<RegionBranchOpInterface>(begin->getParentOp());
|
|
|
|
|
SmallVector<bool> visited(op->getNumRegions(), false);
|
|
|
|
|
visited[begin->getRegionNumber()] = true;
|
|
|
|
|
|
|
|
|
|
// Retrieve all successors of the region and enqueue them in the worklist.
|
2023-08-30 09:22:34 +02:00
|
|
|
SmallVector<Region *> worklist;
|
|
|
|
|
auto enqueueAllSuccessors = [&](Region *region) {
|
2022-04-19 16:21:08 +09:00
|
|
|
SmallVector<RegionSuccessor> successors;
|
2023-08-30 09:22:34 +02:00
|
|
|
op.getSuccessorRegions(region, successors);
|
2022-04-19 16:21:08 +09:00
|
|
|
for (RegionSuccessor successor : successors)
|
|
|
|
|
if (!successor.isParent())
|
2023-08-30 09:22:34 +02:00
|
|
|
worklist.push_back(successor.getSuccessor());
|
2022-04-19 16:21:08 +09:00
|
|
|
};
|
2023-08-30 09:22:34 +02:00
|
|
|
enqueueAllSuccessors(begin);
|
2022-04-19 16:21:08 +09:00
|
|
|
|
|
|
|
|
// Process all regions in the worklist via DFS.
|
|
|
|
|
while (!worklist.empty()) {
|
2023-08-30 09:22:34 +02:00
|
|
|
Region *nextRegion = worklist.pop_back_val();
|
2024-01-07 13:49:29 +01:00
|
|
|
if (stopConditionFn(nextRegion, visited))
|
2022-04-19 16:21:08 +09:00
|
|
|
return true;
|
2023-08-30 09:22:34 +02:00
|
|
|
if (visited[nextRegion->getRegionNumber()])
|
2022-04-19 16:21:08 +09:00
|
|
|
continue;
|
2023-08-30 09:22:34 +02:00
|
|
|
visited[nextRegion->getRegionNumber()] = true;
|
2022-04-19 16:21:08 +09:00
|
|
|
enqueueAllSuccessors(nextRegion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-07 13:49:29 +01:00
|
|
|
/// Return `true` if region `r` is reachable from region `begin` according to
|
|
|
|
|
/// the RegionBranchOpInterface (by taking a branch).
|
|
|
|
|
static bool isRegionReachable(Region *begin, Region *r) {
|
|
|
|
|
assert(begin->getParentOp() == r->getParentOp() &&
|
|
|
|
|
"expected that both regions belong to the same op");
|
|
|
|
|
return traverseRegionGraph(begin,
|
|
|
|
|
[&](Region *nextRegion, ArrayRef<bool> visited) {
|
|
|
|
|
// Interrupt traversal if `r` was reached.
|
|
|
|
|
return nextRegion == r;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 17:42:08 +09:00
|
|
|
/// Return `true` if `a` and `b` are in mutually exclusive regions.
|
|
|
|
|
///
|
|
|
|
|
/// 1. Find the first common of `a` and `b` (ancestor) that implements
|
|
|
|
|
/// RegionBranchOpInterface.
|
|
|
|
|
/// 2. Determine the regions `regionA` and `regionB` in which `a` and `b` are
|
|
|
|
|
/// contained.
|
|
|
|
|
/// 3. Check if `regionA` and `regionB` are mutually exclusive. They are
|
|
|
|
|
/// mutually exclusive if they are not reachable from each other as per
|
|
|
|
|
/// RegionBranchOpInterface::getSuccessorRegions.
|
|
|
|
|
bool mlir::insideMutuallyExclusiveRegions(Operation *a, Operation *b) {
|
|
|
|
|
assert(a && "expected non-empty operation");
|
|
|
|
|
assert(b && "expected non-empty operation");
|
|
|
|
|
|
|
|
|
|
auto branchOp = a->getParentOfType<RegionBranchOpInterface>();
|
|
|
|
|
while (branchOp) {
|
|
|
|
|
// Check if b is inside branchOp. (We already know that a is.)
|
|
|
|
|
if (!branchOp->isProperAncestor(b)) {
|
|
|
|
|
// Check next enclosing RegionBranchOpInterface.
|
|
|
|
|
branchOp = branchOp->getParentOfType<RegionBranchOpInterface>();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// b is contained in branchOp. Retrieve the regions in which `a` and `b`
|
|
|
|
|
// are contained.
|
|
|
|
|
Region *regionA = nullptr, *regionB = nullptr;
|
|
|
|
|
for (Region &r : branchOp->getRegions()) {
|
|
|
|
|
if (r.findAncestorOpInRegion(*a)) {
|
|
|
|
|
assert(!regionA && "already found a region for a");
|
|
|
|
|
regionA = &r;
|
|
|
|
|
}
|
|
|
|
|
if (r.findAncestorOpInRegion(*b)) {
|
|
|
|
|
assert(!regionB && "already found a region for b");
|
|
|
|
|
regionB = &r;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert(regionA && regionB && "could not find region of op");
|
|
|
|
|
|
2022-04-19 16:21:08 +09:00
|
|
|
// `a` and `b` are in mutually exclusive regions if both regions are
|
|
|
|
|
// distinct and neither region is reachable from the other region.
|
|
|
|
|
return regionA != regionB && !isRegionReachable(regionA, regionB) &&
|
2021-11-25 17:42:08 +09:00
|
|
|
!isRegionReachable(regionB, regionA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Could not find a common RegionBranchOpInterface among a's and b's
|
|
|
|
|
// ancestors.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 16:12:40 +09:00
|
|
|
bool RegionBranchOpInterface::isRepetitiveRegion(unsigned index) {
|
2022-04-19 16:21:08 +09:00
|
|
|
Region *region = &getOperation()->getRegion(index);
|
|
|
|
|
return isRegionReachable(region, region);
|
2022-04-19 16:12:40 +09:00
|
|
|
}
|
|
|
|
|
|
2024-01-07 13:49:29 +01:00
|
|
|
bool RegionBranchOpInterface::hasLoop() {
|
|
|
|
|
SmallVector<RegionSuccessor> entryRegions;
|
|
|
|
|
getSuccessorRegions(RegionBranchPoint::parent(), entryRegions);
|
|
|
|
|
for (RegionSuccessor successor : entryRegions)
|
|
|
|
|
if (!successor.isParent() &&
|
|
|
|
|
traverseRegionGraph(successor.getSuccessor(),
|
|
|
|
|
[](Region *nextRegion, ArrayRef<bool> visited) {
|
|
|
|
|
// Interrupt traversal if the region was already
|
|
|
|
|
// visited.
|
|
|
|
|
return visited[nextRegion->getRegionNumber()];
|
|
|
|
|
}))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 16:12:40 +09:00
|
|
|
Region *mlir::getEnclosingRepetitiveRegion(Operation *op) {
|
|
|
|
|
while (Region *region = op->getParentRegion()) {
|
|
|
|
|
op = region->getParentOp();
|
|
|
|
|
if (auto branchOp = dyn_cast<RegionBranchOpInterface>(op))
|
|
|
|
|
if (branchOp.isRepetitiveRegion(region->getRegionNumber()))
|
|
|
|
|
return region;
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Region *mlir::getEnclosingRepetitiveRegion(Value value) {
|
|
|
|
|
Region *region = value.getParentRegion();
|
|
|
|
|
while (region) {
|
|
|
|
|
Operation *op = region->getParentOp();
|
|
|
|
|
if (auto branchOp = dyn_cast<RegionBranchOpInterface>(op))
|
|
|
|
|
if (branchOp.isRepetitiveRegion(region->getRegionNumber()))
|
|
|
|
|
return region;
|
|
|
|
|
region = op->getParentRegion();
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|