Skip to content

Commit b0c4aaf

Browse files
committed
Allow only valid vector.shape_cast transitive folding
When folding A->B->C => A->C only accept A->C that is valid shape cast Reviewed By: ThomasRaoux, nicolasvasilache Differential Revision: https://reviews.llvm.org/D111473
1 parent f193bcc commit b0c4aaf

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

mlir/lib/Dialect/Vector/VectorOps.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3634,6 +3634,20 @@ OpFoldResult ShapeCastOp::fold(ArrayRef<Attribute> operands) {
36343634
if (auto otherOp = source().getDefiningOp<ShapeCastOp>()) {
36353635
if (result().getType() == otherOp.source().getType())
36363636
return otherOp.source();
3637+
3638+
// Only allows valid transitive folding.
3639+
VectorType srcType = otherOp.source().getType().cast<VectorType>();
3640+
VectorType resultType = getResult().getType().cast<VectorType>();
3641+
if (srcType.getRank() < resultType.getRank()) {
3642+
if (!isValidShapeCast(srcType.getShape(), resultType.getShape()))
3643+
return {};
3644+
} else if (srcType.getRank() > resultType.getRank()) {
3645+
if (!isValidShapeCast(resultType.getShape(), srcType.getShape()))
3646+
return {};
3647+
} else {
3648+
return {};
3649+
}
3650+
36373651
setOperand(otherOp.source());
36383652
return getResult();
36393653
}

mlir/test/Dialect/Vector/canonicalize.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,17 @@ func @fold_extract_shapecast_negative(%arg0 : vector<16xf32>,
542542
return %r : vector<4x2xf32>
543543
}
544544

545+
// -----
546+
547+
// CHECK-LABEL: dont_fold_expand_collapse
548+
// CHECK: %[[A:.*]] = vector.shape_cast %{{.*}} : vector<1x1x64xf32> to vector<1x1x8x8xf32>
549+
// CHECK: %[[B:.*]] = vector.shape_cast %{{.*}} : vector<1x1x8x8xf32> to vector<8x8xf32>
550+
// CHECK: return %[[B]] : vector<8x8xf32>
551+
func @dont_fold_expand_collapse(%arg0: vector<1x1x64xf32>) -> vector<8x8xf32> {
552+
%0 = vector.shape_cast %arg0 : vector<1x1x64xf32> to vector<1x1x8x8xf32>
553+
%1 = vector.shape_cast %0 : vector<1x1x8x8xf32> to vector<8x8xf32>
554+
return %1 : vector<8x8xf32>
555+
}
545556

546557
// -----
547558

0 commit comments

Comments
 (0)