diff --git a/mlir/lib/Dialect/Vector/VectorOps.cpp b/mlir/lib/Dialect/Vector/VectorOps.cpp --- a/mlir/lib/Dialect/Vector/VectorOps.cpp +++ b/mlir/lib/Dialect/Vector/VectorOps.cpp @@ -3634,6 +3634,20 @@ if (auto otherOp = source().getDefiningOp()) { if (result().getType() == otherOp.source().getType()) return otherOp.source(); + + // Only allows valid transitive folding. + VectorType srcType = otherOp.source().getType().cast(); + VectorType resultType = getResult().getType().cast(); + if (srcType.getRank() < resultType.getRank()) { + if (!isValidShapeCast(srcType.getShape(), resultType.getShape())) + return {}; + } else if (srcType.getRank() > resultType.getRank()) { + if (!isValidShapeCast(resultType.getShape(), srcType.getShape())) + return {}; + } else { + return {}; + } + setOperand(otherOp.source()); return getResult(); } diff --git a/mlir/test/Dialect/Vector/canonicalize.mlir b/mlir/test/Dialect/Vector/canonicalize.mlir --- a/mlir/test/Dialect/Vector/canonicalize.mlir +++ b/mlir/test/Dialect/Vector/canonicalize.mlir @@ -542,6 +542,17 @@ return %r : vector<4x2xf32> } +// ----- + +// CHECK-LABEL: dont_fold_expand_collapse +// CHECK: %[[A:.*]] = vector.shape_cast %{{.*}} : vector<1x1x64xf32> to vector<1x1x8x8xf32> +// CHECK: %[[B:.*]] = vector.shape_cast %{{.*}} : vector<1x1x8x8xf32> to vector<8x8xf32> +// CHECK: return %[[B]] : vector<8x8xf32> +func @dont_fold_expand_collapse(%arg0: vector<1x1x64xf32>) -> vector<8x8xf32> { + %0 = vector.shape_cast %arg0 : vector<1x1x64xf32> to vector<1x1x8x8xf32> + %1 = vector.shape_cast %0 : vector<1x1x8x8xf32> to vector<8x8xf32> + return %1 : vector<8x8xf32> +} // -----