Skip to content

Commit 1d4aa69

Browse files
[mlir] Optimize const values AffineMap::compose
The original implementation will create two intermediate AffineMap in the context, calling this compose function with different values multiple times will occupy a lot of memory. To improve the performance, we can call the AffineExpr::replace directly, so we don't need to store all combinations of values in the context.
1 parent 851da60 commit 1d4aa69

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

mlir/lib/IR/AffineMap.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,15 +580,13 @@ AffineMap AffineMap::compose(AffineMap map) const {
580580
SmallVector<int64_t, 4> AffineMap::compose(ArrayRef<int64_t> values) const {
581581
assert(getNumSymbols() == 0 && "Expected symbol-less map");
582582
SmallVector<AffineExpr, 4> exprs;
583-
exprs.reserve(values.size());
584583
MLIRContext *ctx = getContext();
585-
for (auto v : values)
586-
exprs.push_back(getAffineConstantExpr(v, ctx));
587-
auto resMap = compose(AffineMap::get(0, 0, exprs, ctx));
584+
for (int64_t value : values)
585+
exprs.push_back(getAffineConstantExpr(value, ctx));
588586
SmallVector<int64_t, 4> res;
589-
res.reserve(resMap.getNumResults());
590-
for (auto e : resMap.getResults())
591-
res.push_back(cast<AffineConstantExpr>(e).getValue());
587+
res.reserve(getNumResults());
588+
for (auto e : getResults())
589+
res.push_back(cast<AffineConstantExpr>(e.replaceDims(exprs)).getValue());
592590
return res;
593591
}
594592

0 commit comments

Comments
 (0)