[Mlir-commits] [mlir] [mlir] Fold memref.cast static-to-dynamic to memref.expand_shape (PR #170037)
Matthias Springer
llvmlistbot at llvm.org
Mon Dec 1 01:04:15 PST 2025
================
@@ -2504,11 +2504,89 @@ LogicalResult ExpandShapeOp::verify() {
return success();
}
+struct ExpandShapeOpMemRefCastFolder : public OpRewritePattern<ExpandShapeOp> {
+public:
+ using OpRewritePattern<ExpandShapeOp>::OpRewritePattern;
+
+ LogicalResult matchAndRewrite(ExpandShapeOp op,
+ PatternRewriter &rewriter) const override {
+ auto cast = op.getSrc().getDefiningOp<CastOp>();
+ if (!cast)
+ return failure();
+
+ if (!CastOp::canFoldIntoConsumerOp(cast))
+ return failure();
+
+ auto originalOutputShape = op.getMixedOutputShape();
+ auto newOutputShape = originalOutputShape;
+ SmallVector<int64_t> newOutputShapeSizes;
+ SmallVector<Value> newOperands;
+
+ // Convert output shape dims from dynamic to static where possible.
+ for (auto [dimIdx, dimSize] : enumerate(originalOutputShape)) {
+ auto dimVal = dimSize.dyn_cast<Value>();
+ if (!dimVal) {
+ newOutputShapeSizes.push_back(getConstantIntValue(dimSize).value());
----------------
matthias-springer wrote:
`getConstantIntValue` works on `OpFoldResult`. There's no need to check whether it's a `Value` or `Attribute`.
https://github.com/llvm/llvm-project/pull/170037
More information about the Mlir-commits
mailing list