Skip to content

Commit 20c3386

Browse files
limo1996ftynse
authored andcommitted
[mlir][Linalg] emitLoopRanges and emitLoopRangesWithSymbols merged into one
Right now there is a branching for 2 functions based on whether target map has symbols or not. In this commit these functions are merged into one. Furthermore, emitting does not require inverse and map applying as it computes the correct Range in a single step and thus reduces unnecessary overhead. Differential Revision: https://reviews.llvm.org/D83756
1 parent 919922b commit 20c3386

File tree

1 file changed

+12
-41
lines changed

1 file changed

+12
-41
lines changed

mlir/lib/Dialect/Linalg/Transforms/Loops.cpp

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,17 @@ static SmallVector<Value, 4> permuteIvs(ArrayRef<Value> ivs,
5858
: SmallVector<Value, 4>(ivs.begin(), ivs.end());
5959
}
6060

61-
// Creates a number of ranges equal to the number of results in `map`.
62-
// The returned ranges correspond to the loop ranges, in the proper order, for
63-
// which new loops will be created.
64-
static SmallVector<SubViewOp::Range, 4>
65-
emitLoopRanges(OpBuilder &b, Location loc, AffineMap map,
66-
ArrayRef<Value> allViewSizes) {
67-
// Apply `map` to get view sizes in loop order.
68-
auto sizes = applyMapToValues(b, loc, map, allViewSizes);
69-
// Create a new range with the applied tile sizes.
70-
ScopedContext scope(b, loc);
71-
SmallVector<SubViewOp::Range, 4> res;
72-
for (unsigned idx = 0, e = map.getNumResults(); idx < e; ++idx) {
73-
res.push_back(SubViewOp::Range{std_constant_index(0), sizes[idx],
74-
std_constant_index(1)});
75-
}
76-
return res;
77-
}
78-
7961
/// Creates a number of ranges equal to the number of dimensions in the `map`.
80-
/// The function supports for now only limited number of expressions inside
81-
/// map results. It expects a non-inverted, concatenated map and last values in
82-
/// viewSizes will be applied to the symbols in the map.
83-
static SmallVector<SubViewOp::Range, 4>
84-
emitLoopRangesWithSymbols(OpBuilder &b, Location loc, AffineMap map,
85-
ValueRange viewSizes) {
62+
/// The returned ranges correspond to the loop ranges, in the proper order, for
63+
/// which new loops will be created.
64+
/// The function supports only maps that are invertible and have results of type
65+
/// DimExpr or (DimExpr + DimExpr - SymbolExpr floordiv ConstExpr).
66+
/// It expects a non-inverted, concatenated map and last values in
67+
/// allViewSizes will be applied to the symbols in the map if it contains any.
68+
static SmallVector<SubViewOp::Range, 4> emitLoopRanges(OpBuilder &b,
69+
Location loc,
70+
AffineMap map,
71+
ValueRange viewSizes) {
8672
unsigned numDims = map.getNumDims(), numRes = map.getNumResults();
8773
unsigned numSym = map.getNumSymbols();
8874
assert(viewSizes.size() == numRes + numSym &&
@@ -537,23 +523,8 @@ Optional<LinalgLoops> linalgOpToLoopsImpl(Operation *op, OpBuilder &builder) {
537523
llvm::map_range(mapsRange, [](AffineMapAttr a) { return a.getValue(); }));
538524
SmallVector<Value, 8> sizes = getViewSizes(builder, linalgOp);
539525
AffineMap map = concatAffineMaps(maps);
540-
SmallVector<SubViewOp::Range, 4> loopRanges;
541-
542-
if (map.getNumSymbols()) {
543-
loopRanges = emitLoopRangesWithSymbols(scope.getBuilderRef(),
544-
scope.getLocation(), map, sizes);
545-
} else {
546-
AffineMap invertedMap = inversePermutation(map);
547-
if (!invertedMap)
548-
return {};
549-
if (invertedMap.isEmpty()) {
550-
emitScalarImplementation<IndexedValueTy>({}, linalgOp);
551-
return LinalgLoops();
552-
}
553-
554-
loopRanges = emitLoopRanges(scope.getBuilderRef(), scope.getLocation(),
555-
invertedMap, sizes);
556-
}
526+
auto loopRanges = emitLoopRanges(scope.getBuilderRef(), scope.getLocation(),
527+
map, getViewSizes(builder, linalgOp));
557528
SmallVector<Value, 4> allIvs;
558529
GenerateLoopNest<LoopTy>::doit(
559530
loopRanges, linalgOp.iterator_types().getValue(), [&](ValueRange ivs) {

0 commit comments

Comments
 (0)