Skip to content

Commit 9535e8b

Browse files
committed
Amortize cost of block op index lookups
1 parent 199d3d7 commit 9535e8b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

mlir/lib/Dialect/Affine/Analysis/Utils.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,16 +1442,28 @@ template LogicalResult
14421442
mlir::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'.
14471460
static 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

Comments
 (0)