Skip to content

[mlir][memref] Simplify memref.copy canonicalization #149506

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

Merged
Merged
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
67 changes: 14 additions & 53 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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.
static 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);
}

//===----------------------------------------------------------------------===//
Expand Down