@@ -1442,16 +1442,28 @@ template LogicalResult
14421442mlir::affine::boundCheckLoadOrStoreOp (AffineWriteOpInterface storeOp,
14431443 bool emitError);
14441444
1445+ static inline unsigned getIndexInBlock (
1446+ Operation *op,
1447+ llvm::DenseMap<Block *, llvm::DenseMap<Operation *, unsigned >> &cache) {
1448+ Block *block = op->getBlock ();
1449+ auto &blockMap = cache[block];
1450+ if (blockMap.empty ()) {
1451+ unsigned idx = 0 ;
1452+ for (Operation &it : *block)
1453+ blockMap[&it] = idx++;
1454+ }
1455+ return blockMap.lookup (op);
1456+ }
1457+
14451458// Returns in 'positions' the Block positions of 'op' in each ancestor
14461459// Block from the Block containing operation, stopping at 'limitBlock'.
14471460static void findInstPosition (Operation *op, Block *limitBlock,
14481461 SmallVectorImpl<unsigned > *positions) {
1462+ llvm::DenseMap<Block *, llvm::DenseMap<Operation *, unsigned >> indexCache;
1463+
14491464 Block *block = op->getBlock ();
14501465 while (block != limitBlock) {
1451- // FIXME: This algorithm is unnecessarily O(n) and should be improved to not
1452- // rely on linear scans.
1453- int instPosInBlock = std::distance (block->begin (), op->getIterator ());
1454- positions->push_back (instPosInBlock);
1466+ positions->push_back (getIndexInBlock (op, indexCache));
14551467 op = block->getParentOp ();
14561468 block = op->getBlock ();
14571469 }
0 commit comments