[mlir][memref] Simplify memref.copy canonicalization#149506
Merged
linuxlonelyeagle merged 2 commits intollvm:mainfrom Jul 18, 2025
Merged
[mlir][memref] Simplify memref.copy canonicalization#149506linuxlonelyeagle merged 2 commits intollvm:mainfrom
linuxlonelyeagle merged 2 commits intollvm:mainfrom
Conversation
Member
|
@llvm/pr-subscribers-mlir-memref @llvm/pr-subscribers-mlir Author: lonely eagle (linuxlonelyeagle) ChangesFoldCopyOfCast has both a pattern implementation and a folder implementation. This PR removes the pattern implementation. Full diff: https://github.com/llvm/llvm-project/pull/149506.diff 1 Files Affected:
diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index d1a9920aa66c5..4b868ed5b08fc 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -715,51 +715,6 @@ OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
//===----------------------------------------------------------------------===//
namespace {
-/// If the source/target of a CopyOp is a CastOp that does not modify the shape
-/// and element type, the cast can be skipped. Such CastOps only cast the layout
-/// of the type.
-struct FoldCopyOfCast : public OpRewritePattern<CopyOp> {
- using OpRewritePattern<CopyOp>::OpRewritePattern;
-
- LogicalResult matchAndRewrite(CopyOp copyOp,
- PatternRewriter &rewriter) const override {
- bool modified = false;
-
- // Check source.
- if (auto castOp = copyOp.getSource().getDefiningOp<CastOp>()) {
- auto fromType = llvm::dyn_cast<MemRefType>(castOp.getSource().getType());
- auto toType = llvm::dyn_cast<MemRefType>(castOp.getSource().getType());
-
- if (fromType && toType) {
- if (fromType.getShape() == toType.getShape() &&
- fromType.getElementType() == toType.getElementType()) {
- rewriter.modifyOpInPlace(copyOp, [&] {
- copyOp.getSourceMutable().assign(castOp.getSource());
- });
- modified = true;
- }
- }
- }
-
- // Check target.
- if (auto castOp = copyOp.getTarget().getDefiningOp<CastOp>()) {
- auto fromType = llvm::dyn_cast<MemRefType>(castOp.getSource().getType());
- auto toType = llvm::dyn_cast<MemRefType>(castOp.getSource().getType());
-
- if (fromType && toType) {
- if (fromType.getShape() == toType.getShape() &&
- fromType.getElementType() == toType.getElementType()) {
- rewriter.modifyOpInPlace(copyOp, [&] {
- copyOp.getTargetMutable().assign(castOp.getSource());
- });
- modified = true;
- }
- }
- }
-
- return success(modified);
- }
-};
/// Fold memref.copy(%x, %x).
struct FoldSelfCopy : public OpRewritePattern<CopyOp> {
@@ -797,22 +752,28 @@ struct FoldEmptyCopy final : public OpRewritePattern<CopyOp> {
void CopyOp::getCanonicalizationPatterns(RewritePatternSet &results,
MLIRContext *context) {
- results.add<FoldCopyOfCast, FoldEmptyCopy, FoldSelfCopy>(context);
+ results.add<FoldEmptyCopy, FoldSelfCopy>(context);
}
-LogicalResult CopyOp::fold(FoldAdaptor adaptor,
- SmallVectorImpl<OpFoldResult> &results) {
- /// copy(memrefcast) -> copy
- bool folded = false;
- Operation *op = *this;
+/// If the source/target of a CopyOp is a CastOp that does not modify the shape
+/// and element type, the cast can be skipped. Such CastOps only cast the layout
+/// of the type.
+LogicalResult FoldCopyOfCast(CopyOp op) {
for (OpOperand &operand : op->getOpOperands()) {
auto castOp = operand.get().getDefiningOp<memref::CastOp>();
if (castOp && memref::CastOp::canFoldIntoConsumerOp(castOp)) {
operand.set(castOp.getOperand());
- folded = true;
+ return success();
}
}
- return success(folded);
+ return failure();
+}
+
+LogicalResult CopyOp::fold(FoldAdaptor adaptor,
+ SmallVectorImpl<OpFoldResult> &results) {
+
+ /// copy(memrefcast) -> copy
+ return FoldCopyOfCast(*this);
}
//===----------------------------------------------------------------------===//
|
This was referenced Jul 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FoldCopyOfCast has both a OpRewritePattern implementation and a folder implementation. This PR removes the OpRewritePattern implementation.