2019-03-15 01:38:44 +08:00
|
|
|
//===- Block.cpp - MLIR Block and Region Classes --------------------------===//
|
2018-07-04 08:51:28 +08:00
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
2018-12-29 05:07:39 +08:00
|
|
|
#include "mlir/IR/Block.h"
|
2019-01-25 04:25:30 +08:00
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
2018-12-28 03:07:34 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
2019-03-27 05:45:38 +08:00
|
|
|
#include "mlir/IR/Operation.h"
|
2018-07-04 08:51:28 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
2019-01-10 16:52:40 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BlockArgument
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Returns the number of this argument.
|
2019-03-24 06:09:06 +08:00
|
|
|
unsigned BlockArgument::getArgNumber() {
|
2019-01-10 16:52:40 +08:00
|
|
|
// Arguments are not stored in place, so we have to find it within the list.
|
|
|
|
auto argList = getOwner()->getArguments();
|
|
|
|
return std::distance(argList.begin(), llvm::find(argList, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Block
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-29 05:07:39 +08:00
|
|
|
Block::~Block() {
|
2019-03-27 08:05:09 +08:00
|
|
|
assert(!verifyInstOrder() && "Expected valid operation ordering.");
|
2018-12-25 10:01:01 +08:00
|
|
|
clear();
|
|
|
|
|
|
|
|
llvm::DeleteContainerPointers(arguments);
|
|
|
|
}
|
2018-07-04 08:51:28 +08:00
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// Returns the closest surrounding operation that contains this block or
|
|
|
|
/// nullptr if this is a top-level operation block.
|
|
|
|
Operation *Block::getContainingOp() {
|
|
|
|
return getParent() ? getParent()->getContainingOp() : nullptr;
|
2018-07-15 07:44:22 +08:00
|
|
|
}
|
|
|
|
|
2019-03-22 08:53:00 +08:00
|
|
|
Function *Block::getFunction() {
|
|
|
|
Block *block = this;
|
2019-03-27 08:05:09 +08:00
|
|
|
while (auto *op = block->getContainingOp()) {
|
|
|
|
block = op->getBlock();
|
2018-08-04 02:12:34 +08:00
|
|
|
if (!block)
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-27 07:31:54 +08:00
|
|
|
if (auto *list = block->getParent())
|
|
|
|
return list->getContainingFunction();
|
|
|
|
return nullptr;
|
2018-07-04 08:51:28 +08:00
|
|
|
}
|
2018-10-19 02:14:26 +08:00
|
|
|
|
2018-12-31 08:22:50 +08:00
|
|
|
/// Insert this block (which must not already be in a function) right before
|
|
|
|
/// the specified block.
|
|
|
|
void Block::insertBefore(Block *block) {
|
|
|
|
assert(!getParent() && "already inserted into a block!");
|
|
|
|
assert(block->getParent() && "cannot insert before a block without a parent");
|
2019-03-15 01:38:44 +08:00
|
|
|
block->getParent()->getBlocks().insert(Region::iterator(block), this);
|
2018-12-31 08:22:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unlink this Block from its Function and delete it.
|
|
|
|
void Block::eraseFromFunction() {
|
|
|
|
assert(getFunction() && "Block has no parent");
|
|
|
|
getFunction()->getBlocks().erase(this);
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// Returns 'op' if 'op' lies in this block, or otherwise finds the
|
|
|
|
/// ancestor operation of 'op' that lies in this block. Returns nullptr if
|
2018-12-29 05:07:39 +08:00
|
|
|
/// the latter fails.
|
2019-03-27 08:05:09 +08:00
|
|
|
Operation *Block::findAncestorInstInBlock(Operation &op) {
|
|
|
|
// Traverse up the operation hierarchy starting from the owner of operand to
|
|
|
|
// find the ancestor operation that resides in the block of 'forInst'.
|
|
|
|
auto *currInst = &op;
|
2018-12-29 05:07:39 +08:00
|
|
|
while (currInst->getBlock() != this) {
|
2019-03-27 23:55:17 +08:00
|
|
|
currInst = currInst->getParentOp();
|
2018-12-29 05:07:39 +08:00
|
|
|
if (!currInst)
|
2018-10-19 02:14:26 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-12-29 05:07:39 +08:00
|
|
|
return currInst;
|
2018-10-19 02:14:26 +08:00
|
|
|
}
|
2018-12-25 10:01:01 +08:00
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// This drops all operand uses from operations within this block, which is
|
2019-01-15 02:30:20 +08:00
|
|
|
/// an essential step in breaking cyclic dependences between references when
|
|
|
|
/// they are to be deleted.
|
|
|
|
void Block::dropAllReferences() {
|
2019-03-27 08:05:09 +08:00
|
|
|
for (Operation &i : *this)
|
2019-01-15 02:30:20 +08:00
|
|
|
i.dropAllReferences();
|
|
|
|
}
|
|
|
|
|
Allow creating standalone Regions
Currently, regions can only be constructed by passing in a `Function` or an
`Instruction` pointer referencing the parent object, unlike `Function`s or
`Instruction`s themselves that can be created without a parent. It leads to a
rather complex flow in operation construction where one has to create the
operation first before being able to work with its regions. It may be
necessary to work with the regions before the operation is created. In
particular, in `build` and `parse` functions that are executed _before_ the
operation is created in cases where boilerplate region manipulation is required
(for example, inserting the hypothetical default terminator in affine regions).
Allow creating standalone regions. Such regions are meant to own a list of
blocks and transfer them to other regions on demand.
Each instruction stores a fixed number of regions as trailing objects and has
ownership of them. This decreases the size of the Instruction object for the
common case of instructions without regions. Keep this behavior intact. To
allow some flexibility in construction, make OperationState store an owning
vector of regions. When the Builder creates an Instruction from
OperationState, the bodies of the regions are transferred into the
instruction-owned regions to minimize copying. Thus, it becomes possible to
fill standalone regions with blocks and move them to an operation when it is
constructed, or move blocks from a region to an operation region, e.g., for
inlining.
PiperOrigin-RevId: 240368183
2019-03-27 00:55:06 +08:00
|
|
|
void Block::dropAllDefinedValueUses() {
|
|
|
|
for (auto *arg : getArguments())
|
|
|
|
arg->dropAllUses();
|
2019-03-27 08:05:09 +08:00
|
|
|
for (auto &op : *this)
|
|
|
|
op.dropAllDefinedValueUses();
|
Allow creating standalone Regions
Currently, regions can only be constructed by passing in a `Function` or an
`Instruction` pointer referencing the parent object, unlike `Function`s or
`Instruction`s themselves that can be created without a parent. It leads to a
rather complex flow in operation construction where one has to create the
operation first before being able to work with its regions. It may be
necessary to work with the regions before the operation is created. In
particular, in `build` and `parse` functions that are executed _before_ the
operation is created in cases where boilerplate region manipulation is required
(for example, inserting the hypothetical default terminator in affine regions).
Allow creating standalone regions. Such regions are meant to own a list of
blocks and transfer them to other regions on demand.
Each instruction stores a fixed number of regions as trailing objects and has
ownership of them. This decreases the size of the Instruction object for the
common case of instructions without regions. Keep this behavior intact. To
allow some flexibility in construction, make OperationState store an owning
vector of regions. When the Builder creates an Instruction from
OperationState, the bodies of the regions are transferred into the
instruction-owned regions to minimize copying. Thus, it becomes possible to
fill standalone regions with blocks and move them to an operation when it is
constructed, or move blocks from a region to an operation region, e.g., for
inlining.
PiperOrigin-RevId: 240368183
2019-03-27 00:55:06 +08:00
|
|
|
dropAllUses();
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// Verifies the current ordering of child operations. Returns false if the
|
2019-01-25 09:16:30 +08:00
|
|
|
/// order is valid, true otherwise.
|
2019-03-22 08:53:00 +08:00
|
|
|
bool Block::verifyInstOrder() {
|
2019-01-25 09:16:30 +08:00
|
|
|
// The order is already known to be invalid.
|
|
|
|
if (!isInstOrderValid())
|
|
|
|
return false;
|
2019-03-27 08:05:09 +08:00
|
|
|
// The order is valid if there are less than 2 operations.
|
|
|
|
if (operations.empty() || std::next(operations.begin()) == operations.end())
|
2019-01-25 09:16:30 +08:00
|
|
|
return false;
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
Operation *prev = nullptr;
|
2019-01-25 09:16:30 +08:00
|
|
|
for (auto &i : *this) {
|
2019-03-27 08:05:09 +08:00
|
|
|
// The previous operation must have a smaller order index than the next as
|
2019-01-25 09:16:30 +08:00
|
|
|
// it appears earlier in the list.
|
|
|
|
if (prev && prev->orderIndex >= i.orderIndex)
|
|
|
|
return true;
|
|
|
|
prev = &i;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// Recomputes the ordering of child operations within the block.
|
2019-01-25 09:16:30 +08:00
|
|
|
void Block::recomputeInstOrder() {
|
|
|
|
parentValidInstOrderPair.setInt(true);
|
|
|
|
|
|
|
|
// TODO(riverriddle) Have non-congruent indices to reduce the number of times
|
|
|
|
// an insert invalidates the list.
|
|
|
|
unsigned orderIndex = 0;
|
2019-03-27 08:05:09 +08:00
|
|
|
for (auto &op : *this)
|
|
|
|
op.orderIndex = orderIndex++;
|
2019-01-25 09:16:30 +08:00
|
|
|
}
|
|
|
|
|
2019-03-22 08:53:00 +08:00
|
|
|
Block *PredecessorIterator::operator*() const {
|
|
|
|
// The use iterator points to an operand of a terminator. The predecessor
|
|
|
|
// we return is the block that the terminator is embedded into.
|
|
|
|
return bbUseIterator.getUser()->getBlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the successor number in the predecessor terminator.
|
|
|
|
unsigned PredecessorIterator::getSuccessorIndex() const {
|
|
|
|
return bbUseIterator->getOperandNumber();
|
|
|
|
}
|
|
|
|
|
2018-12-25 10:01:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Argument list management.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-29 05:07:39 +08:00
|
|
|
BlockArgument *Block::addArgument(Type type) {
|
2018-12-25 10:01:01 +08:00
|
|
|
auto *arg = new BlockArgument(type, this);
|
|
|
|
arguments.push_back(arg);
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add one argument to the argument list for each type specified in the list.
|
2018-12-29 05:07:39 +08:00
|
|
|
auto Block::addArguments(ArrayRef<Type> types)
|
2018-12-25 10:01:01 +08:00
|
|
|
-> llvm::iterator_range<args_iterator> {
|
|
|
|
arguments.reserve(arguments.size() + types.size());
|
|
|
|
auto initialSize = arguments.size();
|
|
|
|
for (auto type : types) {
|
|
|
|
addArgument(type);
|
|
|
|
}
|
|
|
|
return {arguments.data() + initialSize, arguments.data() + arguments.size()};
|
|
|
|
}
|
|
|
|
|
2018-12-29 05:07:39 +08:00
|
|
|
void Block::eraseArgument(unsigned index) {
|
2018-12-25 10:01:01 +08:00
|
|
|
assert(index < arguments.size());
|
|
|
|
|
|
|
|
// Delete the argument.
|
|
|
|
delete arguments[index];
|
|
|
|
arguments.erase(arguments.begin() + index);
|
|
|
|
|
|
|
|
// Erase this argument from each of the predecessor's terminator.
|
|
|
|
for (auto predIt = pred_begin(), predE = pred_end(); predIt != predE;
|
|
|
|
++predIt) {
|
|
|
|
auto *predTerminator = (*predIt)->getTerminator();
|
|
|
|
predTerminator->eraseSuccessorOperand(predIt.getSuccessorIndex(), index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Terminator management
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// Get the terminator operation of this block. This function asserts that
|
|
|
|
/// the block has a valid terminator operation.
|
|
|
|
Operation *Block::getTerminator() {
|
2019-02-09 01:52:26 +08:00
|
|
|
assert(!empty() && !back().isKnownNonTerminator());
|
|
|
|
return &back();
|
2018-12-25 10:01:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if this block has no predecessors.
|
2019-03-22 08:53:00 +08:00
|
|
|
bool Block::hasNoPredecessors() { return pred_begin() == pred_end(); }
|
2018-12-25 10:01:01 +08:00
|
|
|
|
|
|
|
// Indexed successor access.
|
2019-03-22 08:53:00 +08:00
|
|
|
unsigned Block::getNumSuccessors() {
|
2019-02-09 01:52:26 +08:00
|
|
|
return empty() ? 0 : back().getNumSuccessors();
|
2018-12-25 10:01:01 +08:00
|
|
|
}
|
|
|
|
|
2018-12-29 05:07:39 +08:00
|
|
|
Block *Block::getSuccessor(unsigned i) {
|
2019-02-09 01:52:26 +08:00
|
|
|
assert(i < getNumSuccessors());
|
2018-12-25 10:01:01 +08:00
|
|
|
return getTerminator()->getSuccessor(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If this block has exactly one predecessor, return it. Otherwise, return
|
|
|
|
/// null.
|
|
|
|
///
|
|
|
|
/// Note that multiple edges from a single block (e.g. if you have a cond
|
|
|
|
/// branch with the same block as the true/false destinations) is not
|
|
|
|
/// considered to be a single predecessor.
|
2018-12-29 05:07:39 +08:00
|
|
|
Block *Block::getSinglePredecessor() {
|
2018-12-25 10:01:01 +08:00
|
|
|
auto it = pred_begin();
|
|
|
|
if (it == pred_end())
|
|
|
|
return nullptr;
|
|
|
|
auto *firstPred = *it;
|
|
|
|
++it;
|
|
|
|
return it == pred_end() ? firstPred : nullptr;
|
|
|
|
}
|
2018-12-27 07:31:54 +08:00
|
|
|
|
2019-02-05 08:24:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-27 08:05:09 +08:00
|
|
|
// Operation Walkers
|
2019-02-05 08:24:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
void Block::walk(const std::function<void(Operation *)> &callback) {
|
2019-02-05 08:24:44 +08:00
|
|
|
walk(begin(), end(), callback);
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:13:02 +08:00
|
|
|
/// Walk the operations in the specified [begin, end) range of this block,
|
|
|
|
/// calling the callback for each operation.
|
2019-02-05 08:24:44 +08:00
|
|
|
void Block::walk(Block::iterator begin, Block::iterator end,
|
2019-03-27 08:05:09 +08:00
|
|
|
const std::function<void(Operation *)> &callback) {
|
|
|
|
for (auto &op : llvm::make_early_inc_range(llvm::make_range(begin, end)))
|
|
|
|
op.walk(callback);
|
2019-02-05 08:24:44 +08:00
|
|
|
}
|
|
|
|
|
2018-12-28 03:07:34 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// Split the block into two blocks before the specified operation or
|
2018-12-31 08:22:50 +08:00
|
|
|
/// iterator.
|
2018-12-28 03:07:34 +08:00
|
|
|
///
|
2019-03-27 08:05:09 +08:00
|
|
|
/// Note that all operations BEFORE the specified iterator stay as part of
|
|
|
|
/// the original basic block, and the rest of the operations in the original
|
2018-12-31 08:22:50 +08:00
|
|
|
/// block are moved to the new block, including the old terminator. The
|
|
|
|
/// original block is left without a terminator.
|
2018-12-28 03:07:34 +08:00
|
|
|
///
|
2018-12-31 08:22:50 +08:00
|
|
|
/// The newly formed Block is returned, and the specified iterator is
|
|
|
|
/// invalidated.
|
2018-12-29 05:07:39 +08:00
|
|
|
Block *Block::splitBlock(iterator splitBefore) {
|
2018-12-28 03:07:34 +08:00
|
|
|
// Start by creating a new basic block, and insert it immediate after this
|
|
|
|
// one in the containing function.
|
2018-12-29 05:07:39 +08:00
|
|
|
auto newBB = new Block();
|
2018-12-29 00:48:09 +08:00
|
|
|
getFunction()->getBlocks().insert(++Function::iterator(this), newBB);
|
2018-12-28 03:07:34 +08:00
|
|
|
|
|
|
|
// Move all of the operations from the split point to the end of the function
|
|
|
|
// into the new block.
|
2019-03-27 08:05:09 +08:00
|
|
|
newBB->getOperations().splice(newBB->end(), getOperations(), splitBefore,
|
|
|
|
end());
|
2018-12-28 03:07:34 +08:00
|
|
|
return newBB;
|
|
|
|
}
|
|
|
|
|
2018-12-27 07:31:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-15 01:38:44 +08:00
|
|
|
// Region
|
2018-12-27 07:31:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-15 01:38:44 +08:00
|
|
|
Region::Region(Function *container) : container(container) {}
|
2018-12-27 07:31:54 +08:00
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
Region::Region(Operation *container) : container(container) {}
|
2018-12-27 07:31:54 +08:00
|
|
|
|
Allow creating standalone Regions
Currently, regions can only be constructed by passing in a `Function` or an
`Instruction` pointer referencing the parent object, unlike `Function`s or
`Instruction`s themselves that can be created without a parent. It leads to a
rather complex flow in operation construction where one has to create the
operation first before being able to work with its regions. It may be
necessary to work with the regions before the operation is created. In
particular, in `build` and `parse` functions that are executed _before_ the
operation is created in cases where boilerplate region manipulation is required
(for example, inserting the hypothetical default terminator in affine regions).
Allow creating standalone regions. Such regions are meant to own a list of
blocks and transfer them to other regions on demand.
Each instruction stores a fixed number of regions as trailing objects and has
ownership of them. This decreases the size of the Instruction object for the
common case of instructions without regions. Keep this behavior intact. To
allow some flexibility in construction, make OperationState store an owning
vector of regions. When the Builder creates an Instruction from
OperationState, the bodies of the regions are transferred into the
instruction-owned regions to minimize copying. Thus, it becomes possible to
fill standalone regions with blocks and move them to an operation when it is
constructed, or move blocks from a region to an operation region, e.g., for
inlining.
PiperOrigin-RevId: 240368183
2019-03-27 00:55:06 +08:00
|
|
|
Region::~Region() {
|
2019-03-27 08:05:09 +08:00
|
|
|
// Operations may have cyclic references, which need to be dropped before we
|
Allow creating standalone Regions
Currently, regions can only be constructed by passing in a `Function` or an
`Instruction` pointer referencing the parent object, unlike `Function`s or
`Instruction`s themselves that can be created without a parent. It leads to a
rather complex flow in operation construction where one has to create the
operation first before being able to work with its regions. It may be
necessary to work with the regions before the operation is created. In
particular, in `build` and `parse` functions that are executed _before_ the
operation is created in cases where boilerplate region manipulation is required
(for example, inserting the hypothetical default terminator in affine regions).
Allow creating standalone regions. Such regions are meant to own a list of
blocks and transfer them to other regions on demand.
Each instruction stores a fixed number of regions as trailing objects and has
ownership of them. This decreases the size of the Instruction object for the
common case of instructions without regions. Keep this behavior intact. To
allow some flexibility in construction, make OperationState store an owning
vector of regions. When the Builder creates an Instruction from
OperationState, the bodies of the regions are transferred into the
instruction-owned regions to minimize copying. Thus, it becomes possible to
fill standalone regions with blocks and move them to an operation when it is
constructed, or move blocks from a region to an operation region, e.g., for
inlining.
PiperOrigin-RevId: 240368183
2019-03-27 00:55:06 +08:00
|
|
|
// can start deleting them.
|
|
|
|
for (auto &bb : *this)
|
|
|
|
bb.dropAllReferences();
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:24:57 +08:00
|
|
|
Region *Region::getContainingRegion() {
|
|
|
|
if (auto *inst = getContainingOp())
|
|
|
|
return inst->getContainingRegion();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
Operation *Region::getContainingOp() {
|
|
|
|
return container.dyn_cast<Operation *>();
|
2018-12-27 07:31:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-22 02:57:14 +08:00
|
|
|
Function *Region::getContainingFunction() {
|
2018-12-28 03:07:34 +08:00
|
|
|
return container.dyn_cast<Function *>();
|
2018-12-27 07:31:54 +08:00
|
|
|
}
|
|
|
|
|
2019-04-25 00:24:57 +08:00
|
|
|
bool Region::isProperAncestor(Region *other) {
|
|
|
|
if (this == other)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while ((other = other->getContainingRegion())) {
|
|
|
|
if (this == other)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-15 01:38:44 +08:00
|
|
|
/// Clone the internal blocks from this region into `dest`. Any
|
2019-01-25 04:25:30 +08:00
|
|
|
/// cloned blocks are appended to the back of dest.
|
2019-03-15 01:38:44 +08:00
|
|
|
void Region::cloneInto(Region *dest, BlockAndValueMapping &mapper,
|
2019-03-22 02:57:14 +08:00
|
|
|
MLIRContext *context) {
|
2019-03-15 01:38:44 +08:00
|
|
|
assert(dest && "expected valid region to clone into");
|
2019-01-25 04:25:30 +08:00
|
|
|
|
|
|
|
// If the list is empty there is nothing to clone.
|
|
|
|
if (empty())
|
|
|
|
return;
|
|
|
|
|
2019-01-26 04:48:25 +08:00
|
|
|
iterator lastOldBlock = --dest->end();
|
2019-03-22 08:53:00 +08:00
|
|
|
for (Block &block : *this) {
|
2019-01-25 04:25:30 +08:00
|
|
|
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-03-24 06:09:06 +08:00
|
|
|
for (auto *arg : block.getArguments())
|
2019-01-25 04:25:30 +08:00
|
|
|
if (!mapper.contains(arg))
|
|
|
|
mapper.map(arg, newBlock->addArgument(arg->getType()));
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
// Clone and remap the operations within this block.
|
|
|
|
for (auto &op : block)
|
|
|
|
newBlock->push_back(op.clone(mapper, context));
|
2019-01-25 04:25:30 +08:00
|
|
|
|
|
|
|
dest->push_back(newBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that each of the blocks have been cloned, go through and remap the
|
2019-03-27 08:05:09 +08:00
|
|
|
// operands of each of the operations.
|
|
|
|
auto remapOperands = [&](Operation *op) {
|
2019-03-29 02:25:19 +08:00
|
|
|
for (auto &operand : op->getOpOperands())
|
|
|
|
if (auto *mappedOp = mapper.lookupOrNull(operand.get()))
|
|
|
|
operand.set(mappedOp);
|
2019-03-27 08:05:09 +08:00
|
|
|
for (auto &succOp : op->getBlockOperands())
|
2019-02-09 01:52:26 +08:00
|
|
|
if (auto *mappedOp = mapper.lookupOrNull(succOp.get()))
|
|
|
|
succOp.set(mappedOp);
|
2019-01-25 04:25:30 +08:00
|
|
|
};
|
|
|
|
|
2019-01-26 04:48:25 +08:00
|
|
|
for (auto it = std::next(lastOldBlock), e = dest->end(); it != e; ++it)
|
2019-02-05 08:24:44 +08:00
|
|
|
it->walk(remapOperands);
|
2019-01-25 04:25:30 +08:00
|
|
|
}
|
|
|
|
|
2019-05-04 02:40:22 +08:00
|
|
|
/// Check that the given `region` does not use any value defined outside its
|
|
|
|
/// ancestor region `limit`. That is, given `A{B{C{}}}` with limit `B`, `C` is
|
|
|
|
/// allowed to use values defined in `B` but not those defined in `A`.
|
|
|
|
/// Emit errors if `noteLoc` is provided; this location is used to point to
|
|
|
|
/// the operation containing the region, the actual error is reported at the
|
|
|
|
/// operation with an offending use.
|
|
|
|
static bool isRegionIsolatedAbove(Region ®ion, Region &limit,
|
|
|
|
llvm::Optional<Location> noteLoc) {
|
2019-04-29 18:00:25 +08:00
|
|
|
assert(limit.isAncestor(®ion) &&
|
|
|
|
"expected isolation limit to be an ancestor of the given region");
|
|
|
|
|
|
|
|
// List of regions to analyze. Each region is processed independently, with
|
|
|
|
// respect to the common `limit` region, so we can look at them in any order.
|
|
|
|
// Therefore, use a simple vector and push/pop back the current region.
|
|
|
|
SmallVector<Region *, 8> pendingRegions;
|
|
|
|
pendingRegions.push_back(®ion);
|
|
|
|
|
|
|
|
// Traverse all operations in the region.
|
|
|
|
while (!pendingRegions.empty()) {
|
|
|
|
for (Block &block : *pendingRegions.pop_back_val()) {
|
|
|
|
for (Operation &op : block) {
|
|
|
|
for (Value *operand : op.getOperands()) {
|
|
|
|
// Check that any value that is used by an operation is defined in the
|
|
|
|
// same region as either an operation result or a block argument.
|
|
|
|
if (operand->getContainingRegion()->isProperAncestor(&limit)) {
|
2019-05-04 02:40:22 +08:00
|
|
|
if (noteLoc) {
|
|
|
|
op.emitOpError("using value defined outside the region")
|
|
|
|
.attachNote(noteLoc)
|
|
|
|
<< "required by region isolation constraints";
|
2019-04-29 18:00:25 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Schedule any regions the operations contain for further checking.
|
|
|
|
pendingRegions.reserve(pendingRegions.size() + op.getNumRegions());
|
|
|
|
for (Region &subRegion : op.getRegions())
|
|
|
|
pendingRegions.push_back(&subRegion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-04 02:40:22 +08:00
|
|
|
bool Region::isIsolatedAbove(llvm::Optional<Location> noteLoc) {
|
|
|
|
return isRegionIsolatedAbove(*this, *this, noteLoc);
|
2019-04-29 18:00:25 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:38:44 +08:00
|
|
|
Region *llvm::ilist_traits<::mlir::Block>::getContainingRegion() {
|
2018-12-29 05:07:39 +08:00
|
|
|
size_t Offset(
|
2019-03-15 01:38:44 +08:00
|
|
|
size_t(&((Region *)nullptr->*Region::getSublistAccess(nullptr))));
|
2018-12-29 05:07:39 +08:00
|
|
|
iplist<Block> *Anchor(static_cast<iplist<Block> *>(this));
|
2019-03-15 01:38:44 +08:00
|
|
|
return reinterpret_cast<Region *>(reinterpret_cast<char *>(Anchor) - Offset);
|
2018-12-27 07:31:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 01:38:44 +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.
|
2018-12-29 05:07:39 +08:00
|
|
|
void llvm::ilist_traits<::mlir::Block>::addNodeToList(Block *block) {
|
2019-03-15 01:38:44 +08:00
|
|
|
assert(!block->getParent() && "already in a region!");
|
|
|
|
block->parentValidInstOrderPair.setPointer(getContainingRegion());
|
2018-12-27 07:31:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// This is a trait method invoked when an operation is removed from a
|
2019-03-15 01:38:44 +08:00
|
|
|
/// region. We keep the region pointer up to date.
|
2018-12-29 05:07:39 +08:00
|
|
|
void llvm::ilist_traits<::mlir::Block>::removeNodeFromList(Block *block) {
|
2019-03-15 01:38:44 +08:00
|
|
|
assert(block->getParent() && "not already in a region!");
|
2019-01-25 09:16:30 +08:00
|
|
|
block->parentValidInstOrderPair.setPointer(nullptr);
|
2018-12-27 07:31:54 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
/// This is a trait method invoked when an operation is moved from one block
|
2018-12-27 07:31:54 +08:00
|
|
|
/// to another. We keep the block pointer up to date.
|
2018-12-29 05:07:39 +08:00
|
|
|
void llvm::ilist_traits<::mlir::Block>::transferNodesFromList(
|
|
|
|
ilist_traits<Block> &otherList, block_iterator first, block_iterator last) {
|
2019-03-27 08:05:09 +08:00
|
|
|
// If we are transferring operations within the same function, the parent
|
2018-12-27 07:31:54 +08:00
|
|
|
// pointer doesn't need to be updated.
|
2019-03-15 01:38:44 +08:00
|
|
|
auto *curParent = getContainingRegion();
|
|
|
|
if (curParent == otherList.getContainingRegion())
|
2018-12-27 07:31:54 +08:00
|
|
|
return;
|
|
|
|
|
2018-12-29 05:07:39 +08:00
|
|
|
// Update the 'parent' member of each Block.
|
2018-12-27 07:31:54 +08:00
|
|
|
for (; first != last; ++first)
|
2019-01-25 09:16:30 +08:00
|
|
|
first->parentValidInstOrderPair.setPointer(curParent);
|
2018-12-27 07:31:54 +08:00
|
|
|
}
|