[PATCH] D135836: [mlir][MemRef] Add a extract_strided_metadata(extract_strided_metadata) pattern
Quentin Colombet via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 12 17:51:47 PDT 2022
qcolombet created this revision.
qcolombet added reviewers: nicolasvasilache, chelini.
qcolombet added a project: MLIR.
Herald added subscribers: bzcheeseman, sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, antiagainst, shauheen, rriddle, mehdi_amini.
Herald added a project: All.
qcolombet requested review of this revision.
Herald added a subscriber: stephenneuendorffer.
This pattern will be useful to get cleaner code when lowering view like operations.
Note: This patch is part of a series that replaces the lowering of view like operations in `MemRefToLLVM` with composable patterns.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D135836
Files:
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
mlir/test/Dialect/MemRef/simplify-extract-strided-metadata.mlir
Index: mlir/test/Dialect/MemRef/simplify-extract-strided-metadata.mlir
===================================================================
--- mlir/test/Dialect/MemRef/simplify-extract-strided-metadata.mlir
+++ mlir/test/Dialect/MemRef/simplify-extract-strided-metadata.mlir
@@ -844,3 +844,29 @@
return %base, %offset :
memref<i32>, index
}
+
+// -----
+
+// Check that we simplify extract_strided_metadata of
+// extract_strided_metadata.
+//
+// CHECK-LABEL: func @extract_strided_metadata_of_extract_strided_metadata(
+// CHECK-SAME: %[[ARG:.*]]: memref<i32>)
+//
+// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
+// CHECK-DAG: %[[BASE:.*]], %[[OFFSET:.*]] = memref.extract_strided_metadata %[[ARG]]
+//
+// CHECK: return %[[BASE]], %[[C0]]
+func.func @extract_strided_metadata_of_extract_strided_metadata(%arg : memref<i32>)
+ -> (memref<i32>, index) {
+
+ %base, %offset =
+ memref.extract_strided_metadata %arg:memref<i32>
+ -> memref<i32>, index
+ %base2, %offset2 =
+ memref.extract_strided_metadata %base:memref<i32>
+ -> memref<i32>, index
+
+ return %base2, %offset2 :
+ memref<i32>, index
+}
Index: mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
===================================================================
--- mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
+++ mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
@@ -660,6 +660,34 @@
return success();
}
};
+
+/// Replace `base, offset =
+/// extract_strided_metadata(extract_strided_metadata(src)#0)`
+/// With
+/// ```
+/// base, ... = extract_strided_metadata(src)
+/// offset = 0
+/// ```
+class ExtractStridedMetadataOpExtractStridedMetadataFolder
+ : public OpRewritePattern<memref::ExtractStridedMetadataOp> {
+ using OpRewritePattern::OpRewritePattern;
+
+ LogicalResult
+ matchAndRewrite(memref::ExtractStridedMetadataOp extractStridedMetadataOp,
+ PatternRewriter &rewriter) const override {
+ auto sourceExtractStridedMetadataOp =
+ extractStridedMetadataOp.getSource()
+ .getDefiningOp<memref::ExtractStridedMetadataOp>();
+ if (!sourceExtractStridedMetadataOp)
+ return failure();
+ Location loc = extractStridedMetadataOp.getLoc();
+ rewriter.replaceOp(extractStridedMetadataOp,
+ {sourceExtractStridedMetadataOp.getBaseBuffer(),
+ getValueOrCreateConstantIndexOp(
+ rewriter, loc, rewriter.getIndexAttr(0))});
+ return success();
+ }
+};
} // namespace
void memref::populateSimplifyExtractStridedMetadataOpPatterns(
@@ -672,7 +700,8 @@
memref::CollapseShapeOp, getCollapsedSize, getCollapsedStride>,
ExtractStridedMetadataOpAllocFolder<memref::AllocOp>,
ExtractStridedMetadataOpAllocFolder<memref::AllocaOp>,
- RewriteExtractAlignedPointerAsIndexOfViewLikeOp>(
+ RewriteExtractAlignedPointerAsIndexOfViewLikeOp,
+ ExtractStridedMetadataOpExtractStridedMetadataFolder>(
patterns.getContext());
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135836.467323.patch
Type: text/x-patch
Size: 3133 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221013/36651c59/attachment.bin>
More information about the llvm-commits
mailing list