2019-06-16 11:19:36 +08:00
|
|
|
//===- Region.cpp - MLIR Region Class -------------------------------------===//
|
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-06-16 11:19:36 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-06-16 11:19:36 +08:00
|
|
|
|
|
|
|
#include "mlir/IR/Region.h"
|
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
|
|
|
#include "mlir/IR/Operation.h"
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
Region::Region(Operation *container) : container(container) {}
|
|
|
|
|
|
|
|
Region::~Region() {
|
|
|
|
// Operations may have cyclic references, which need to be dropped before we
|
|
|
|
// can start deleting them.
|
2019-07-12 01:00:54 +08:00
|
|
|
dropAllReferences();
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the context this region is inserted in. The region must have a valid
|
|
|
|
/// parent container.
|
|
|
|
MLIRContext *Region::getContext() {
|
2019-07-04 04:21:24 +08:00
|
|
|
assert(container && "region is not attached to a container");
|
|
|
|
return container->getContext();
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a location for this region. This is the location attached to the
|
|
|
|
/// parent container. The region must have a valid parent container.
|
|
|
|
Location Region::getLoc() {
|
2019-07-04 04:21:24 +08:00
|
|
|
assert(container && "region is not attached to a container");
|
|
|
|
return container->getLoc();
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
2020-07-29 06:26:36 +08:00
|
|
|
auto Region::getArgumentTypes() -> ValueTypeRange<BlockArgListType> {
|
|
|
|
return ValueTypeRange<BlockArgListType>(getArguments());
|
|
|
|
}
|
|
|
|
|
2022-01-19 10:28:51 +08:00
|
|
|
iterator_range<Region::args_iterator>
|
|
|
|
Region::addArguments(TypeRange types, ArrayRef<Location> locs) {
|
|
|
|
return front().addArguments(types, locs);
|
2020-07-11 08:07:29 +08:00
|
|
|
}
|
|
|
|
|
2019-08-10 11:07:25 +08:00
|
|
|
Region *Region::getParentRegion() {
|
2019-07-04 04:21:24 +08:00
|
|
|
assert(container && "region is not attached to a container");
|
2019-08-10 11:07:25 +08:00
|
|
|
return container->getParentRegion();
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Region::isProperAncestor(Region *other) {
|
|
|
|
if (this == other)
|
|
|
|
return false;
|
|
|
|
|
2019-08-10 11:07:25 +08:00
|
|
|
while ((other = other->getParentRegion())) {
|
2019-06-16 11:19:36 +08:00
|
|
|
if (this == other)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-18 05:45:53 +08:00
|
|
|
/// Return the number of this region in the parent operation.
|
|
|
|
unsigned Region::getRegionNumber() {
|
|
|
|
// Regions are always stored consecutively, so use pointer subtraction to
|
|
|
|
// figure out what number this is.
|
2019-08-10 11:07:25 +08:00
|
|
|
return this - &getParentOp()->getRegions()[0];
|
2019-07-18 05:45:53 +08:00
|
|
|
}
|
|
|
|
|
2019-06-16 11:19:36 +08:00
|
|
|
/// Clone the internal blocks from this region into `dest`. Any
|
|
|
|
/// cloned blocks are appended to the back of dest.
|
2019-06-28 07:42:50 +08:00
|
|
|
void Region::cloneInto(Region *dest, BlockAndValueMapping &mapper) {
|
|
|
|
assert(dest && "expected valid region to clone into");
|
|
|
|
cloneInto(dest, dest->end(), mapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clone this region into 'dest' before the given position in 'dest'.
|
|
|
|
void Region::cloneInto(Region *dest, Region::iterator destPos,
|
|
|
|
BlockAndValueMapping &mapper) {
|
2019-06-16 11:19:36 +08:00
|
|
|
assert(dest && "expected valid region to clone into");
|
2019-10-09 17:43:18 +08:00
|
|
|
assert(this != dest && "cannot clone region into itself");
|
2019-06-16 11:19:36 +08:00
|
|
|
|
|
|
|
// If the list is empty there is nothing to clone.
|
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
[mlir] Make `Regions`s `cloneInto` multithread-readable
Prior to this patch, `cloneInto` would do a simple walk over the blocks and contained operations and clone and map them as it encounters them. As finishing touch it then remaps any successor and operands it has remapped during that process.
This is generally fine, but sadly leads to a lot of uses of both operations and blocks from the source region, in the cloned operations in the target region. Those uses lead to writes in the use-def list of the operations, making `cloneInto` never thread safe.
This patch reimplements `cloneInto` in three steps to avoid ever creating any extra uses on elements in the source region:
* It first creates the mapping of all blocks and block operands
* It then clones all operations to create the mapping of all operation results, but does not yet clone any regions or set the operands
* After all operation results have been mapped, it now sets the operations operands and clones their regions.
That way it is now possible to call `cloneInto` from multiple threads if the Region or Operation is isolated-from-above. This allows creating copies of functions or to use `mlir::inlineCall` with the same source region from multiple threads. In the general case, the method is thread-safe if through cloning, no new uses of `Value`s from outside the cloned Operation/Region are created. This can be ensured by mapping any outside operands via the `BlockAndValueMapping` to `Value`s owned by the caller thread.
While I was at it, I also reworked the `clone` method of `Operation` a little bit and added a proper options class to avoid having a `cloneWithoutRegionsAndOperands` method, and be more extensible in the future. `cloneWithoutRegions` is now also a simple wrapper that calls `clone` with the proper options set. That way all the operation cloning code is now contained solely within `clone`.
Differential Revision: https://reviews.llvm.org/D123917
2022-04-21 19:43:00 +08:00
|
|
|
// The below clone implementation takes special care to be read only for the
|
|
|
|
// sake of multi threading. That essentially means not adding any uses to any
|
|
|
|
// of the blocks or operation results contained within this region as that
|
|
|
|
// would lead to a write in their use-def list. This is unavoidable for
|
|
|
|
// 'Value's from outside the region however, in which case it is not read
|
|
|
|
// only. Using the BlockAndValueMapper it is possible to remap such 'Value's
|
|
|
|
// to ones owned by the calling thread however, making it read only once
|
|
|
|
// again.
|
|
|
|
|
|
|
|
// First clone all the blocks and block arguments and map them, but don't yet
|
|
|
|
// clone the operations, as they may otherwise add a use to a block that has
|
|
|
|
// not yet been mapped
|
2019-06-16 11:19:36 +08:00
|
|
|
for (Block &block : *this) {
|
|
|
|
Block *newBlock = new Block();
|
|
|
|
mapper.map(&block, newBlock);
|
|
|
|
|
|
|
|
// Clone the block arguments. The user might be deleting arguments to the
|
|
|
|
// block by specifying them in the mapper. If so, we don't add the
|
|
|
|
// argument to the cloned block.
|
2019-12-23 13:59:55 +08:00
|
|
|
for (auto arg : block.getArguments())
|
2019-06-16 11:19:36 +08:00
|
|
|
if (!mapper.contains(arg))
|
2022-01-17 05:16:57 +08:00
|
|
|
mapper.map(arg, newBlock->addArgument(arg.getType(), arg.getLoc()));
|
2019-06-16 11:19:36 +08:00
|
|
|
|
2019-06-28 07:42:50 +08:00
|
|
|
dest->getBlocks().insert(destPos, newBlock);
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
[mlir] Make `Regions`s `cloneInto` multithread-readable
Prior to this patch, `cloneInto` would do a simple walk over the blocks and contained operations and clone and map them as it encounters them. As finishing touch it then remaps any successor and operands it has remapped during that process.
This is generally fine, but sadly leads to a lot of uses of both operations and blocks from the source region, in the cloned operations in the target region. Those uses lead to writes in the use-def list of the operations, making `cloneInto` never thread safe.
This patch reimplements `cloneInto` in three steps to avoid ever creating any extra uses on elements in the source region:
* It first creates the mapping of all blocks and block operands
* It then clones all operations to create the mapping of all operation results, but does not yet clone any regions or set the operands
* After all operation results have been mapped, it now sets the operations operands and clones their regions.
That way it is now possible to call `cloneInto` from multiple threads if the Region or Operation is isolated-from-above. This allows creating copies of functions or to use `mlir::inlineCall` with the same source region from multiple threads. In the general case, the method is thread-safe if through cloning, no new uses of `Value`s from outside the cloned Operation/Region are created. This can be ensured by mapping any outside operands via the `BlockAndValueMapping` to `Value`s owned by the caller thread.
While I was at it, I also reworked the `clone` method of `Operation` a little bit and added a proper options class to avoid having a `cloneWithoutRegionsAndOperands` method, and be more extensible in the future. `cloneWithoutRegions` is now also a simple wrapper that calls `clone` with the proper options set. That way all the operation cloning code is now contained solely within `clone`.
Differential Revision: https://reviews.llvm.org/D123917
2022-04-21 19:43:00 +08:00
|
|
|
auto newBlocksRange =
|
|
|
|
llvm::make_range(Region::iterator(mapper.lookup(&front())), destPos);
|
|
|
|
|
|
|
|
// Now follow up with creating the operations, but don't yet clone their
|
|
|
|
// regions, nor set their operands. Setting the successors is safe as all have
|
|
|
|
// already been mapped. We are essentially just creating the operation results
|
|
|
|
// to be able to map them.
|
|
|
|
// Cloning the operands and region as well would lead to uses of operations
|
|
|
|
// not yet mapped.
|
|
|
|
auto cloneOptions =
|
|
|
|
Operation::CloneOptions::all().cloneRegions(false).cloneOperands(false);
|
|
|
|
for (auto zippedBlocks : llvm::zip(*this, newBlocksRange)) {
|
|
|
|
Block &sourceBlock = std::get<0>(zippedBlocks);
|
|
|
|
Block &clonedBlock = std::get<1>(zippedBlocks);
|
|
|
|
// Clone and remap the operations within this block.
|
|
|
|
for (Operation &op : sourceBlock)
|
|
|
|
clonedBlock.push_back(op.clone(mapper, cloneOptions));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally now that all operation results have been mapped, set the operands
|
|
|
|
// and clone the regions.
|
|
|
|
SmallVector<Value> operands;
|
|
|
|
for (auto zippedBlocks : llvm::zip(*this, newBlocksRange)) {
|
|
|
|
for (auto ops :
|
|
|
|
llvm::zip(std::get<0>(zippedBlocks), std::get<1>(zippedBlocks))) {
|
|
|
|
Operation &source = std::get<0>(ops);
|
|
|
|
Operation &clone = std::get<1>(ops);
|
|
|
|
|
|
|
|
operands.resize(source.getNumOperands());
|
|
|
|
llvm::transform(
|
|
|
|
source.getOperands(), operands.begin(),
|
|
|
|
[&](Value operand) { return mapper.lookupOrDefault(operand); });
|
|
|
|
clone.setOperands(operands);
|
|
|
|
|
|
|
|
for (auto regions : llvm::zip(source.getRegions(), clone.getRegions()))
|
|
|
|
std::get<0>(regions).cloneInto(&std::get<1>(regions), mapper);
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:31:18 +08:00
|
|
|
/// Returns 'block' if 'block' lies in this region, or otherwise finds the
|
|
|
|
/// ancestor of 'block' that lies in this region. Returns nullptr if the latter
|
|
|
|
/// fails.
|
|
|
|
Block *Region::findAncestorBlockInRegion(Block &block) {
|
2021-05-31 09:02:51 +08:00
|
|
|
Block *currBlock = █
|
2020-04-08 16:31:18 +08:00
|
|
|
while (currBlock->getParent() != this) {
|
|
|
|
Operation *parentOp = currBlock->getParentOp();
|
|
|
|
if (!parentOp || !parentOp->getBlock())
|
|
|
|
return nullptr;
|
|
|
|
currBlock = parentOp->getBlock();
|
|
|
|
}
|
|
|
|
return currBlock;
|
|
|
|
}
|
|
|
|
|
2021-05-31 09:02:51 +08:00
|
|
|
/// Returns 'op' if 'op' lies in this region, or otherwise finds the
|
|
|
|
/// ancestor of 'op' that lies in this region. Returns nullptr if the
|
|
|
|
/// latter fails.
|
|
|
|
Operation *Region::findAncestorOpInRegion(Operation &op) {
|
|
|
|
Operation *curOp = &op;
|
|
|
|
while (Region *opRegion = curOp->getParentRegion()) {
|
|
|
|
if (opRegion == this)
|
|
|
|
return curOp;
|
|
|
|
|
|
|
|
curOp = opRegion->getParentOp();
|
|
|
|
if (!curOp)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-07-12 01:00:54 +08:00
|
|
|
void Region::dropAllReferences() {
|
|
|
|
for (Block &b : *this)
|
|
|
|
b.dropAllReferences();
|
|
|
|
}
|
|
|
|
|
2019-08-10 11:07:25 +08:00
|
|
|
Region *llvm::ilist_traits<::mlir::Block>::getParentRegion() {
|
2021-12-21 03:45:05 +08:00
|
|
|
size_t offset(
|
2019-06-16 11:19:36 +08:00
|
|
|
size_t(&((Region *)nullptr->*Region::getSublistAccess(nullptr))));
|
2021-12-21 03:45:05 +08:00
|
|
|
iplist<Block> *anchor(static_cast<iplist<Block> *>(this));
|
|
|
|
return reinterpret_cast<Region *>(reinterpret_cast<char *>(anchor) - offset);
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when a basic block is added to a region.
|
|
|
|
/// We keep the region pointer up to date.
|
|
|
|
void llvm::ilist_traits<::mlir::Block>::addNodeToList(Block *block) {
|
|
|
|
assert(!block->getParent() && "already in a region!");
|
2019-11-07 08:08:51 +08:00
|
|
|
block->parentValidOpOrderPair.setPointer(getParentRegion());
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when an operation is removed from a
|
|
|
|
/// region. We keep the region pointer up to date.
|
|
|
|
void llvm::ilist_traits<::mlir::Block>::removeNodeFromList(Block *block) {
|
|
|
|
assert(block->getParent() && "not already in a region!");
|
2019-11-07 08:08:51 +08:00
|
|
|
block->parentValidOpOrderPair.setPointer(nullptr);
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when an operation is moved from one block
|
|
|
|
/// to another. We keep the block pointer up to date.
|
|
|
|
void llvm::ilist_traits<::mlir::Block>::transferNodesFromList(
|
|
|
|
ilist_traits<Block> &otherList, block_iterator first, block_iterator last) {
|
|
|
|
// If we are transferring operations within the same function, the parent
|
|
|
|
// pointer doesn't need to be updated.
|
2019-08-10 11:07:25 +08:00
|
|
|
auto *curParent = getParentRegion();
|
|
|
|
if (curParent == otherList.getParentRegion())
|
2019-06-16 11:19:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Update the 'parent' member of each Block.
|
|
|
|
for (; first != last; ++first)
|
2019-11-07 08:08:51 +08:00
|
|
|
first->parentValidOpOrderPair.setPointer(curParent);
|
2019-06-16 11:19:36 +08:00
|
|
|
}
|
2019-12-10 00:57:27 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2020-05-05 08:46:06 +08:00
|
|
|
// Region::OpIterator
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Region::OpIterator::OpIterator(Region *region, bool end)
|
|
|
|
: region(region), block(end ? region->end() : region->begin()) {
|
|
|
|
if (!region->empty())
|
|
|
|
skipOverBlocksWithNoOps();
|
|
|
|
}
|
|
|
|
|
|
|
|
Region::OpIterator &Region::OpIterator::operator++() {
|
|
|
|
// We increment over operations, if we reach the last use then move to next
|
|
|
|
// block.
|
|
|
|
if (operation != block->end())
|
|
|
|
++operation;
|
|
|
|
if (operation == block->end()) {
|
|
|
|
++block;
|
|
|
|
skipOverBlocksWithNoOps();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Region::OpIterator::skipOverBlocksWithNoOps() {
|
|
|
|
while (block != region->end() && block->empty())
|
|
|
|
++block;
|
|
|
|
|
|
|
|
// If we are at the last block, then set the operation to first operation of
|
|
|
|
// next block (sentinel value used for end).
|
|
|
|
if (block == region->end())
|
|
|
|
operation = {};
|
|
|
|
else
|
|
|
|
operation = block->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-12-10 00:57:27 +08:00
|
|
|
// RegionRange
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-12-10 04:55:05 +08:00
|
|
|
|
2019-12-10 00:57:27 +08:00
|
|
|
RegionRange::RegionRange(MutableArrayRef<Region> regions)
|
2019-12-10 04:55:05 +08:00
|
|
|
: RegionRange(regions.data(), regions.size()) {}
|
2019-12-10 00:57:27 +08:00
|
|
|
RegionRange::RegionRange(ArrayRef<std::unique_ptr<Region>> regions)
|
2019-12-10 04:55:05 +08:00
|
|
|
: RegionRange(regions.data(), regions.size()) {}
|
2022-03-26 06:44:34 +08:00
|
|
|
RegionRange::RegionRange(ArrayRef<Region *> regions)
|
|
|
|
: RegionRange(const_cast<Region **>(regions.data()), regions.size()) {}
|
2019-12-10 04:55:05 +08:00
|
|
|
|
2020-04-15 05:53:07 +08:00
|
|
|
/// See `llvm::detail::indexed_accessor_range_base` for details.
|
2019-12-10 04:55:05 +08:00
|
|
|
RegionRange::OwnerT RegionRange::offset_base(const OwnerT &owner,
|
|
|
|
ptrdiff_t index) {
|
2022-03-26 06:44:34 +08:00
|
|
|
if (auto *region = owner.dyn_cast<const std::unique_ptr<Region> *>())
|
|
|
|
return region + index;
|
|
|
|
if (auto **region = owner.dyn_cast<Region **>())
|
|
|
|
return region + index;
|
2019-12-10 04:55:05 +08:00
|
|
|
return &owner.get<Region *>()[index];
|
|
|
|
}
|
2020-04-15 05:53:07 +08:00
|
|
|
/// See `llvm::detail::indexed_accessor_range_base` for details.
|
2019-12-10 04:55:05 +08:00
|
|
|
Region *RegionRange::dereference_iterator(const OwnerT &owner,
|
|
|
|
ptrdiff_t index) {
|
2022-03-26 06:44:34 +08:00
|
|
|
if (auto *region = owner.dyn_cast<const std::unique_ptr<Region> *>())
|
|
|
|
return region[index].get();
|
|
|
|
if (auto **region = owner.dyn_cast<Region **>())
|
|
|
|
return region[index];
|
2019-12-10 04:55:05 +08:00
|
|
|
return &owner.get<Region *>()[index];
|
2019-12-10 00:57:27 +08:00
|
|
|
}
|