Skip to content

Fold memref.dim into memref.expand_shape #88423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,11 +1125,67 @@ struct DimOfMemRefReshape : public OpRewritePattern<DimOp> {
}
};

int64_t getCorrespondingSourceDim(ExpandShapeOp expandShapeOp,
int64_t resultDim) {
assert(resultDim >= 0 &&
resultDim < expandShapeOp.getResultType().getRank() &&
"invalid resultDim");
for (const auto &it :
llvm::enumerate(expandShapeOp.getReassociationIndices()))
if (llvm::is_contained(it.value(), resultDim))
return it.index();
assert(false && "could not find reassociation group");
return 0;
}

struct FoldDimOfExpandShape : public OpRewritePattern<DimOp> {
using OpRewritePattern<DimOp>::OpRewritePattern;

LogicalResult matchAndRewrite(DimOp dimOp,
PatternRewriter &rewriter) const override {
auto expandShapeOp = dimOp.getSource().getDefiningOp<ExpandShapeOp>();
if (!expandShapeOp)
return failure();

// Only constant dimension values are supported.
std::optional<int64_t> dim = dimOp.getConstantIndex();
if (!dim.has_value())
return failure();

// Skip static dims. These are folded to constant ops.
MemRefType resultType = expandShapeOp.getResultType();
if (!resultType.isDynamicDim(*dim))
return failure();

// Find reassociation group that contains this result dimension.
int64_t srcDim = getCorrespondingSourceDim(expandShapeOp, *dim);

// `dim` is the only dynamic dimension in `group`. (Otherwise, the
// ExpandShapeOp would be ambiguous.)
int64_t product = 1;
ReassociationIndices grp = expandShapeOp.getReassociationIndices()[srcDim];
for (int64_t d : grp) {
if (d != dim) {
assert(!resultType.isDynamicDim(d) && "expected static dim");
product *= resultType.getDimSize(d);
}
}

// result dim size = src dim size / (product(other dims in reassoc group))
Value srcDimSz =
rewriter.create<DimOp>(dimOp.getLoc(), expandShapeOp.getSrc(), srcDim);
rewriter.replaceOpWithNewOp<arith::FloorDivSIOp>(
dimOp, srcDimSz,
rewriter.create<arith::ConstantIndexOp>(dimOp.getLoc(), product));
return success();
}
};

} // namespace

void DimOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
results.add<DimOfMemRefReshape>(context);
results.add<DimOfMemRefReshape, FoldDimOfExpandShape>(context);
}

// ---------------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions mlir/test/Dialect/MemRef/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ func.func @dim_of_memref_reshape_i32(%arg0: memref<*xf32>, %arg1: memref<?xi32>)

// -----

// Test case: Folding of memref.dim(memref.expand_shape)
// CHECK-LABEL: func @dim_of_memref_expand_shape(
// CHECK-SAME: %[[MEM:[0-9a-z]+]]: memref<?x8xi32>
// CHECK-NEXT: %[[IDX:.*]] = arith.constant 0
// CHECK-NEXT: %[[DIM:.*]] = memref.dim %[[MEM]], %[[IDX]] : memref<?x8xi32>
// CHECK: return %[[DIM]] : index
func.func @dim_of_memref_expand_shape(%arg0: memref<?x8xi32>)
-> index {
%c1 = arith.constant 1 : index
%0 = memref.expand_shape %arg0 [[0, 1], [2, 3]]: memref<?x8xi32> into memref<1x?x2x4xi32>
%1 = memref.dim %0, %c1 : memref<1x?x2x4xi32>
return %1 : index
}

// -----

// Test case: memref.dim(memref.reshape %v %shp, %idx) -> memref.load %shp[%idx]
// CHECK-LABEL: func @dim_of_memref_reshape_block_arg_index(
// CHECK-SAME: %[[MEM:[0-9a-z]+]]: memref<*xf32>,
Expand Down