-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[mlir][bufferization] Refine tensor-buffer compatibility checks #167705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3744279
f390e90
ae43acc
1eceefe
07e040f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -127,3 +127,63 @@ func.func @invalid_manual_deallocation() { | |
| // expected-error @below{{op attribute 'bufferization.manual_deallocation' can be used only on ops that have an allocation and/or free side effect}} | ||
| arith.constant {bufferization.manual_deallocation} 0 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @matthias-springer I have extended these tests with rank / shape / element type checks for builtin types. now, this does "confirm" that ensureToBufferOpIsValid() removed in ::getBuffer() is indeed unnecessary. |
||
|
|
||
| func.func @invalid_rank_to_buffer(%t: tensor<1x2x3x4xf32>) { | ||
| // expected-error @below{{'bufferization.to_buffer' op failed to verify that specified tensor and buffer types match}} | ||
| // expected-error @below{{shapes do not match}} | ||
| %b = bufferization.to_buffer %t | ||
| : tensor<1x2x3x4xf32> to memref<1x2x3xf32> | ||
| return | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| func.func @invalid_rank_to_tensor(%b: memref<1x2x3xf32>) { | ||
| // expected-error @below{{'bufferization.to_tensor' op failed to verify that specified tensor and buffer types match}} | ||
| // expected-error @below{{shapes do not match}} | ||
| %t = bufferization.to_tensor %b | ||
| : memref<1x2x3xf32> to tensor<1x2x3x4xf32> | ||
| return | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| func.func @invalid_shape_to_buffer(%t: tensor<1x2x3x4xf32>) { | ||
| // expected-error @below{{'bufferization.to_buffer' op failed to verify that specified tensor and buffer types match}} | ||
| // expected-error @below{{shapes do not match}} | ||
| %b = bufferization.to_buffer %t | ||
| : tensor<1x2x3x4xf32> to memref<1x2x4x3xf32> | ||
| return | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| func.func @invalid_shape_to_tensor(%b: memref<1x2x4x3xf32>) { | ||
| // expected-error @below{{'bufferization.to_tensor' op failed to verify that specified tensor and buffer types match}} | ||
| // expected-error @below{{shapes do not match}} | ||
| %t = bufferization.to_tensor %b | ||
| : memref<1x2x4x3xf32> to tensor<1x2x3x4xf32> | ||
| return | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| func.func @invalid_type_to_buffer(%t: tensor<1x2x3x4xf32>) { | ||
| // expected-error @below{{'bufferization.to_buffer' op failed to verify that specified tensor and buffer types match}} | ||
| // expected-error @below{{element types do not match}} | ||
| %b = bufferization.to_buffer %t | ||
| : tensor<1x2x3x4xf32> to memref<1x2x3x4xf16> | ||
| return | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| func.func @invalid_type_to_tensor(%b: memref<1x2x3x4xf16>) { | ||
| // expected-error @below{{'bufferization.to_tensor' op failed to verify that specified tensor and buffer types match}} | ||
| // expected-error @below{{element types do not match}} | ||
| %t2 = bufferization.to_tensor %b | ||
| : memref<1x2x3x4xf16> to tensor<1x2x3x4xf32> | ||
| return | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,3 +83,40 @@ func.func @test_dealloc_op(%arg0: memref<2xf32>, %arg1: memref<4xi32>, | |
| bufferization.dealloc | ||
| return %0#0, %0#1 : i1, i1 | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: moved the tests that were originally in tensorlike-bufferlike.mlir to ops.mlir. this seems to be a better place since the tests essentially check what operations can do. |
||
| // CHECK: func.func @test_builtin_custom_builtin_type_conversion | ||
| // CHECK-SAME: (%[[t:.*]]: tensor<42xf32>) -> tensor<42xf32> | ||
| func.func @test_builtin_custom_builtin_type_conversion(%t: tensor<42xf32>) | ||
| -> tensor<42xf32> { | ||
| // CHECK: %[[buffer:.*]] = bufferization.to_buffer %[[t]] | ||
| // CHECK-SAME: to !test.test_memref<[42], f32> | ||
| %buffer = bufferization.to_buffer %t | ||
| : tensor<42xf32> to !test.test_memref<[42], f32> | ||
|
|
||
| // CHECK: %[[tensor:.*]] = bufferization.to_tensor %[[buffer]] | ||
| // CHECK-SAME: to tensor<42xf32> | ||
| %tensor = bufferization.to_tensor %buffer | ||
| : !test.test_memref<[42], f32> to tensor<42xf32> | ||
|
|
||
| // CHECK: return %[[tensor]] | ||
| return %tensor : tensor<42xf32> | ||
| } | ||
|
|
||
| // CHECK: func.func @test_custom_builtin_custom_type_conversion | ||
| // CHECK-SAME: (%[[t:.*]]: !test.test_tensor<[42], f32>) | ||
| // CHECK-SAME: -> !test.test_tensor<[42], f32> | ||
| func.func @test_custom_builtin_custom_type_conversion(%t: !test.test_tensor<[42], f32>) | ||
| -> !test.test_tensor<[42], f32> { | ||
| // CHECK: %[[buffer:.*]] = bufferization.to_buffer %[[t]] | ||
| // CHECK-SAME: to memref<42xf32> | ||
| %buffer = bufferization.to_buffer %t | ||
| : !test.test_tensor<[42], f32> to memref<42xf32> | ||
|
|
||
| // CHECK: %[[tensor:.*]] = bufferization.to_tensor %[[buffer]] | ||
| // CHECK-SAME: to !test.test_tensor<[42], f32> | ||
| %tensor = bufferization.to_tensor %buffer | ||
| : memref<42xf32> to !test.test_tensor<[42], f32> | ||
|
|
||
| // CHECK: return %[[tensor]] | ||
| return %tensor : !test.test_tensor<[42], f32> | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -569,11 +569,17 @@ TestTensorType::getBufferType( | |
| ::mlir::LogicalResult TestTensorType::verifyCompatibleBufferType( | ||
| ::mlir::bufferization::BufferLikeType bufferType, | ||
| ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) { | ||
| auto testMemref = dyn_cast<TestMemrefType>(bufferType); | ||
| if (!testMemref) | ||
| return emitError() << "expected TestMemrefType"; | ||
| if (auto testMemref = dyn_cast<TestMemrefType>(bufferType)) { | ||
| const bool valid = getShape() == testMemref.getShape() && | ||
| getElementType() == testMemref.getElementType(); | ||
| return mlir::success(valid); | ||
| } | ||
|
|
||
| if (auto builtinMemref = dyn_cast<MemRefType>(bufferType)) { | ||
| const bool valid = getShape() == builtinMemref.getShape() && | ||
| getElementType() == builtinMemref.getElementType(); | ||
| return mlir::success(valid); | ||
| } | ||
|
|
||
| const bool valid = getShape() == testMemref.getShape() && | ||
| getElementType() == testMemref.getElementType(); | ||
| return mlir::success(valid); | ||
| return emitError() << "expected MemRefType or TestMemrefType"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to trigger this in the test above?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this specifically - not really. |
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this actually blocked our downstream to switch to
mlir::bufferization::getBuffer()implementation. from what I can tell, this check is superseded a long time ago by ToBufferOp's verifier (that checks, for builtins, both shape - and thus rank - and element type).unfortunately, testing this specifically is a pain: i need a fairly large setup to showcase how this fails. I hope this is fine that I just delete it and provide some general tests that exercise op's verifiers instead.