[Mlir-commits] [mlir] [mlir][PartialReductionTilingInterface] Add support for `ReductionTilingStrategy::PartialReductionOuterParallel` in `tileUsingSCF`. (PR #143988)
Kunwar Grover
llvmlistbot at llvm.org
Mon Jun 23 07:09:35 PDT 2025
================
@@ -348,28 +359,79 @@ getPartialResultAffineMaps(LinalgOp linalgOp,
return partialReductionMaps;
}
-/// Return the slice of the `initValue` to use as input to the partial reduction
-/// op generated.
-static Operation *getInitSliceForOuterReduction(
- OpBuilder &b, Location loc, Value initValue, ArrayRef<OpFoldResult> offsets,
+struct InitSliceInfo {
+ SmallVector<int64_t> resultShape;
+ SmallVector<OpFoldResult> offsets;
+ SmallVector<OpFoldResult> sizes;
+ SmallVector<OpFoldResult> strides;
+};
+
+/// Return the result type, offsets, sizes and strides of the slice of the
+/// `initValue` to use as input to the partial reduction op generated with
+/// outer reduction strategy.
+static InitSliceInfo getInitSliceInfoForOuterReduction(
+ MLIRContext *context, ArrayRef<OpFoldResult> offsets,
ArrayRef<OpFoldResult> sizes, const SetVector<unsigned> &reductionDims,
AffineMap partialReductionMap) {
int64_t initRank = partialReductionMap.getNumResults();
SmallVector<OpFoldResult> initOffsets, initSizes;
- SmallVector<OpFoldResult> initStrides(initRank, b.getIndexAttr(1));
+ Attribute zero = IntegerAttr::get(IndexType::get(context), 0);
+ Attribute one = IntegerAttr::get(IndexType::get(context), 1);
+ SmallVector<OpFoldResult> initStrides(initRank, one);
for (AffineExpr dimExpr : partialReductionMap.getResults()) {
unsigned dim = cast<AffineDimExpr>(dimExpr).getPosition();
if (reductionDims.contains(dim)) {
- initOffsets.push_back(b.getIndexAttr(0));
+ initOffsets.push_back(zero);
} else {
initOffsets.push_back(offsets[dim]);
}
initSizes.push_back(sizes[dim]);
}
- // TODO: Use SubsetExtractOpInterface here once available.
- auto extractSlice = b.create<tensor::ExtractSliceOp>(
- loc, initValue, initOffsets, initSizes, initStrides);
- return extractSlice;
+ SmallVector<int64_t> resultShape;
+ std::tie(resultShape, std::ignore) = decomposeMixedValues(initSizes);
+ return {resultShape, initOffsets, initSizes, initStrides};
+}
+
+/// Return the result type, offsets, sizes and strides of the slice of the
+/// `initValue` to use as input to the partial reduction op generated with
+/// outer parallel strategy.
+static InitSliceInfo getInitSliceInfoForOuterParallel(
+ MLIRContext *context, ValueRange ivs, ArrayRef<OpFoldResult> offsets,
+ ArrayRef<OpFoldResult> sizes, const SetVector<unsigned> &reductionDims,
+ AffineMap partialReductionMap) {
+ int64_t initRank = partialReductionMap.getNumResults();
+ SmallVector<OpFoldResult> initOffsets, initSizes;
+ Attribute one = IntegerAttr::get(IndexType::get(context), 1);
+ SmallVector<OpFoldResult> initStrides(initRank, one);
+ SmallVector<OpFoldResult> resultShape;
+ for (AffineExpr dimExpr : partialReductionMap.getResults()) {
+ unsigned dim = cast<AffineDimExpr>(dimExpr).getPosition();
+ if (std::optional<int> dimPos = getPositionIn(reductionDims, dim)) {
+ initOffsets.push_back(ivs[dimPos.value()]);
----------------
Groverkss wrote:
On further thought, passing the ivs makes sense.
Passing num_threads is a seperate discussion, i personally think it's useful in cases where you want your grid offsets to be distributed with specific strides, but i don't want to discuss that here.
https://github.com/llvm/llvm-project/pull/143988
More information about the Mlir-commits
mailing list