Skip to content

Commit e9088b0

Browse files
committed
improved error message when rank change is not correct
1 parent 3684e45 commit e9088b0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,14 @@ LogicalResult ExpandShapeOp::verify() {
22242224
MemRefType srcType = getSrcType();
22252225
MemRefType resultType = getResultType();
22262226

2227+
if (srcType.getRank() > resultType.getRank()) {
2228+
auto r0 = srcType.getRank();
2229+
auto r1 = resultType.getRank();
2230+
return emitOpError("has source rank ")
2231+
<< r0 << " and result rank " << r1 << ". This is not an expansion ("
2232+
<< r0 << " > " << r1 << ").";
2233+
}
2234+
22272235
// Verify result shape.
22282236
if (failed(verifyCollapsedShape(getOperation(), srcType.getShape(),
22292237
resultType.getShape(),
@@ -2374,6 +2382,14 @@ LogicalResult CollapseShapeOp::verify() {
23742382
MemRefType srcType = getSrcType();
23752383
MemRefType resultType = getResultType();
23762384

2385+
if (srcType.getRank() < resultType.getRank()) {
2386+
auto r0 = srcType.getRank();
2387+
auto r1 = resultType.getRank();
2388+
return emitOpError("has source rank ")
2389+
<< r0 << " and result rank " << r1 << ". This is not a collapse ("
2390+
<< r0 << " < " << r1 << ").";
2391+
}
2392+
23772393
// Verify result shape.
23782394
if (failed(verifyCollapsedShape(getOperation(), resultType.getShape(),
23792395
srcType.getShape(), getReassociationIndices(),

mlir/test/Dialect/MemRef/invalid.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ func.func @collapse_shape_invalid_reassociation(%arg0: memref<?x?x?xf32>) {
451451
// An (invalid) attempt at using collapse_shape to increase the rank might look
452452
// like this. Verify that a sensible error is emitted in this case.
453453
func.func @collapse_shape_invalid_reassociation_expansion(%arg0: memref<?xf32>) {
454-
// expected-error @+1 {{reassociation indices must be contiguous}}
454+
// expected-error @+1 {{'memref.collapse_shape' op has source rank 1 and result rank 2. This is not a collapse (1 < 2)}}
455455
%0 = memref.collapse_shape %arg0 [[0], [0]] :
456456
memref<?xf32> into memref<?x?xf32>
457457
}
@@ -461,7 +461,7 @@ func.func @collapse_shape_invalid_reassociation_expansion(%arg0: memref<?xf32>)
461461
// An (invalid) attempt at using expand_shape to reduce the rank might look
462462
// like this. Verify that a sensible error is emitted in this case.
463463
func.func @expand_shape_invalid_reassociation(%arg0: memref<2x3x1xf32>) {
464-
// expected-error @+1 {{reassociation indices must be contiguous}}
464+
// expected-error @+1 {{'memref.expand_shape' op has source rank 3 and result rank 2. This is not an expansion (3 > 2)}}
465465
%0 = memref.expand_shape %arg0 [[0], [1], [1]] :
466466
memref<2x3x1xf32> into memref<2x3xf32>
467467
}

0 commit comments

Comments
 (0)