Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions mlir/lib/Dialect/Affine/Analysis/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,16 +1442,28 @@ template LogicalResult
mlir::affine::boundCheckLoadOrStoreOp(AffineWriteOpInterface storeOp,
bool emitError);

static inline unsigned getIndexInBlock(
Operation *op,
llvm::DenseMap<Block *, llvm::DenseMap<Operation *, unsigned>> &cache) {
Block *block = op->getBlock();
auto &blockMap = cache[block];
if (blockMap.empty()) {
unsigned idx = 0;
for (Operation &it : *block)
blockMap[&it] = idx++;
}
return blockMap.lookup(op);
}

// Returns in 'positions' the Block positions of 'op' in each ancestor
// Block from the Block containing operation, stopping at 'limitBlock'.
static void findInstPosition(Operation *op, Block *limitBlock,
SmallVectorImpl<unsigned> *positions) {
llvm::DenseMap<Block *, llvm::DenseMap<Operation *, unsigned>> indexCache;

Block *block = op->getBlock();
while (block != limitBlock) {
// FIXME: This algorithm is unnecessarily O(n) and should be improved to not
// rely on linear scans.
int instPosInBlock = std::distance(block->begin(), op->getIterator());
positions->push_back(instPosInBlock);
positions->push_back(getIndexInBlock(op, indexCache));
op = block->getParentOp();
block = op->getBlock();
}
Expand Down