[Mlir-commits] [mlir] [MLIR] Fix incorrect memref::DimOp canonicalization, add tensor::DimOp canonicalization (PR #84225)
Sayan Saha
llvmlistbot at llvm.org
Fri Mar 8 12:37:43 PST 2024
================
@@ -1069,6 +1069,52 @@ OpFoldResult DimOp::fold(FoldAdaptor adaptor) {
return {};
}
+namespace {
+/// Fold dim of a memref reshape operation to a load into the reshape's shape
+/// operand.
+struct DimOfMemRefReshape : public OpRewritePattern<DimOp> {
+ using OpRewritePattern<DimOp>::OpRewritePattern;
+
+ LogicalResult matchAndRewrite(DimOp dim,
+ PatternRewriter &rewriter) const override {
+ auto reshape = dim.getSource().getDefiningOp<ReshapeOp>();
+
+ if (!reshape)
+ return rewriter.notifyMatchFailure(
+ dim, "Dim op is not defined by a reshape op.");
+
+ if (dim.getIndex().getParentBlock() == reshape->getBlock()) {
+ if (auto *definingOp = dim.getIndex().getDefiningOp()) {
+ if (reshape->isBeforeInBlock(definingOp))
+ return rewriter.notifyMatchFailure(
+ dim,
+ "dim.getIndex is not defined before reshape in the same block.");
+ } // else dim.getIndex is a block argument to reshape->getBlock
+ } else if (!dim.getIndex().getParentRegion()->isProperAncestor(
----------------
sahas3 wrote:
I am not following how this check will help. Can you elaborate?
https://github.com/llvm/llvm-project/pull/84225
More information about the Mlir-commits
mailing list