return lookupOrValue(from, from);
}
+ /// Lookup a mapped value within the map. This asserts the provided value
+ /// exists within the map.
+ template <typename T> T *lookup(T *from) const {
+ auto *result = lookupOrNull(from);
+ assert(result && "expected 'from' to be contained within the map");
+ return result;
+ }
+
/// Clears all mappings held by the mapper.
void clear() { valueMap.clear(); }
/// cloned sub-operations to the corresponding operation that is copied,
/// and adds those mappings to the map.
Operation *clone(Operation &op, BlockAndValueMapping &mapper) {
- Operation *cloneOp = op.clone(mapper, getContext());
+ Operation *cloneOp = op.clone(mapper);
block->getOperations().insert(insertPoint, cloneOp);
return cloneOp;
}
Operation *clone(Operation &op) {
- Operation *cloneOp = op.clone(getContext());
+ Operation *cloneOp = op.clone();
block->getOperations().insert(insertPoint, cloneOp);
return cloneOp;
}
/// empty. Operands are remapped using `mapper` (if present), and `mapper` is
/// updated to contain the results.
Operation *cloneWithoutRegions(Operation &op, BlockAndValueMapping &mapper) {
- Operation *cloneOp = op.cloneWithoutRegions(mapper, getContext());
+ Operation *cloneOp = op.cloneWithoutRegions(mapper);
block->getOperations().insert(insertPoint, cloneOp);
return cloneOp;
}
Operation *cloneWithoutRegions(Operation &op) {
- Operation *cloneOp = op.cloneWithoutRegions(getContext());
+ Operation *cloneOp = op.cloneWithoutRegions();
block->getOperations().insert(insertPoint, cloneOp);
return cloneOp;
}
/// them alone if no entry is present). Replaces references to cloned
/// sub-operations to the corresponding operation that is copied, and adds
/// those mappings to the map.
- Operation *clone(BlockAndValueMapping &mapper, MLIRContext *context);
- Operation *clone(MLIRContext *context);
+ Operation *clone(BlockAndValueMapping &mapper);
+ Operation *clone();
/// Create a deep copy of this operation but keep the operation regions empty.
/// Operands are remapped using `mapper` (if present), and `mapper` is updated
/// to contain the results.
- Operation *cloneWithoutRegions(BlockAndValueMapping &mapper,
- MLIRContext *context);
- Operation *cloneWithoutRegions(MLIRContext *context);
+ Operation *cloneWithoutRegions(BlockAndValueMapping &mapper);
+ Operation *cloneWithoutRegions();
/// Returns the operation block that contains this operation.
Block *getBlock() { return block; }
/// cloned blocks are appended to the back of dest. If the mapper
/// contains entries for block arguments, these arguments are not included
/// in the respective cloned block.
- void cloneInto(Region *dest, BlockAndValueMapping &mapper,
- MLIRContext *context);
+ void cloneInto(Region *dest, BlockAndValueMapping &mapper);
+ /// Clone this region into 'dest' before the given position in 'dest'.
+ void cloneInto(Region *dest, Region::iterator destPos,
+ BlockAndValueMapping &mapper);
/// Takes body of another region (that region will have no body after this
/// operation completes). The current body of this region is cleared.
dest->setAttrs(newAttrs.takeVector());
// Clone the body.
- body.cloneInto(&dest->body, mapper, getContext());
+ body.cloneInto(&dest->body, mapper);
}
/// Create a deep copy of this function and all of its blocks, remapping
/// Create a deep copy of this operation but keep the operation regions empty.
/// Operands are remapped using `mapper` (if present), and `mapper` is updated
/// to contain the results.
-Operation *Operation::cloneWithoutRegions(BlockAndValueMapping &mapper,
- MLIRContext *context) {
+Operation *Operation::cloneWithoutRegions(BlockAndValueMapping &mapper) {
SmallVector<Value *, 8> operands;
SmallVector<Block *, 2> successors;
unsigned numRegions = getNumRegions();
auto *newOp = Operation::create(getLoc(), getName(), operands, resultTypes,
attrs, successors, numRegions,
- hasResizableOperandsList(), context);
+ hasResizableOperandsList(), getContext());
// Remember the mapping of any results.
for (unsigned i = 0, e = getNumResults(); i != e; ++i)
return newOp;
}
-Operation *Operation::cloneWithoutRegions(MLIRContext *context) {
+Operation *Operation::cloneWithoutRegions() {
BlockAndValueMapping mapper;
- return cloneWithoutRegions(mapper, context);
+ return cloneWithoutRegions(mapper);
}
/// Create a deep copy of this operation, remapping any operands that use
/// them alone if no entry is present). Replaces references to cloned
/// sub-operations to the corresponding operation that is copied, and adds
/// those mappings to the map.
-Operation *Operation::clone(BlockAndValueMapping &mapper,
- MLIRContext *context) {
- auto *newOp = cloneWithoutRegions(mapper, context);
+Operation *Operation::clone(BlockAndValueMapping &mapper) {
+ auto *newOp = cloneWithoutRegions(mapper);
// Clone the regions.
for (unsigned i = 0; i != numRegions; ++i)
- getRegion(i).cloneInto(&newOp->getRegion(i), mapper, context);
+ getRegion(i).cloneInto(&newOp->getRegion(i), mapper);
return newOp;
}
-Operation *Operation::clone(MLIRContext *context) {
+Operation *Operation::clone() {
BlockAndValueMapping mapper;
- return clone(mapper, context);
+ return clone(mapper);
}
//===----------------------------------------------------------------------===//
/// Clone the internal blocks from this region into `dest`. Any
/// cloned blocks are appended to the back of dest.
-void Region::cloneInto(Region *dest, BlockAndValueMapping &mapper,
- MLIRContext *context) {
+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) {
assert(dest && "expected valid region to clone into");
// If the list is empty there is nothing to clone.
if (empty())
return;
- iterator lastOldBlock = --dest->end();
for (Block &block : *this) {
Block *newBlock = new Block();
mapper.map(&block, newBlock);
// Clone and remap the operations within this block.
for (auto &op : block)
- newBlock->push_back(op.clone(mapper, context));
+ newBlock->push_back(op.clone(mapper));
- dest->push_back(newBlock);
+ dest->getBlocks().insert(destPos, newBlock);
}
// Now that each of the blocks have been cloned, go through and remap the
succOp.set(mappedOp);
};
- for (auto it = std::next(lastOldBlock), e = dest->end(); it != e; ++it)
+ for (iterator it(mapper.lookup(&front())); it != destPos; ++it)
it->walk(remapOperands);
}