diff --git a/mlir/include/mlir/IR/Operation.h b/mlir/include/mlir/IR/Operation.h --- a/mlir/include/mlir/IR/Operation.h +++ b/mlir/include/mlir/IR/Operation.h @@ -85,7 +85,10 @@ /// original one, but they will be left empty. /// Operands are remapped using `mapper` (if present), and `mapper` is updated /// to contain the results. - Operation *cloneWithoutRegions(BlockAndValueMapping &mapper); + /// The `mapResults` argument specifies whether the results of the operation + /// should also be mapped. + Operation *cloneWithoutRegions(BlockAndValueMapping &mapper, + bool mapResults = true); /// Create a partial copy of this operation without traversing into attached /// regions. The new operation will have the same number of regions as the diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp --- a/mlir/lib/IR/Operation.cpp +++ b/mlir/lib/IR/Operation.cpp @@ -526,7 +526,8 @@ /// 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) { +Operation *Operation::cloneWithoutRegions(BlockAndValueMapping &mapper, + bool mapResults) { SmallVector operands; SmallVector successors; @@ -545,8 +546,10 @@ successors, getNumRegions()); // Remember the mapping of any results. - for (unsigned i = 0, e = getNumResults(); i != e; ++i) - mapper.map(getResult(i), newOp->getResult(i)); + if (mapResults) { + for (unsigned i = 0, e = getNumResults(); i != e; ++i) + mapper.map(getResult(i), newOp->getResult(i)); + } return newOp; } @@ -562,12 +565,15 @@ /// sub-operations to the corresponding operation that is copied, and adds /// those mappings to the map. Operation *Operation::clone(BlockAndValueMapping &mapper) { - auto *newOp = cloneWithoutRegions(mapper); + auto *newOp = cloneWithoutRegions(mapper, /*mapResults=*/false); // Clone the regions. for (unsigned i = 0; i != numRegions; ++i) getRegion(i).cloneInto(&newOp->getRegion(i), mapper); + for (unsigned i = 0, e = getNumResults(); i != e; ++i) + mapper.map(getResult(i), newOp->getResult(i)); + return newOp; }