-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][memref] Add terminator check to prevent a crash #141972
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
Conversation
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-memref Author: Longsheng Mou (CoTinker) ChangesThis PR adds terminator check to prevent a crash when invoke Full diff: https://github.com/llvm/llvm-project/pull/141972.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index cab0ab8d15d5d..aa9587510670c 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -398,8 +398,9 @@ static bool isOpItselfPotentialAutomaticAllocation(Operation *op) {
/// and is only followed by a terminator. This prevents
/// extending the lifetime of allocations.
static bool lastNonTerminatorInRegion(Operation *op) {
- return op->getNextNode() == op->getBlock()->getTerminator() &&
- llvm::hasSingleElement(op->getParentRegion()->getBlocks());
+ return op->getBlock()->mightHaveTerminator() &&
+ op->getNextNode() == op->getBlock()->getTerminator() &&
+ op->getParentRegion()->hasOneBlock();
}
/// Inline an AllocaScopeOp if either the direct parent is an allocation scope
@@ -2011,7 +2012,7 @@ struct ReinterpretCastOpExtractStridedMetadataFolder
// Second, check the sizes.
if (!llvm::equal(extractStridedMetadata.getConstifiedMixedSizes(),
op.getConstifiedMixedSizes()))
- return false;
+ return false;
// Finally, check the offset.
assert(op.getMixedOffsets().size() == 1 &&
diff --git a/mlir/test/Dialect/MemRef/canonicalize.mlir b/mlir/test/Dialect/MemRef/canonicalize.mlir
index e7cee7cd85426..6f17caad3fd6a 100644
--- a/mlir/test/Dialect/MemRef/canonicalize.mlir
+++ b/mlir/test/Dialect/MemRef/canonicalize.mlir
@@ -739,6 +739,8 @@ func.func @scopeMerge() {
// CHECK: "test.use"(%[[alloc]]) : (memref<?xi64>) -> ()
// CHECK: return
+// -----
+
func.func @scopeMerge2() {
"test.region"() ({
memref.alloca_scope {
@@ -763,6 +765,8 @@ func.func @scopeMerge2() {
// CHECK: return
// CHECK: }
+// -----
+
func.func @scopeMerge3() {
%cnt = "test.count"() : () -> index
"test.region"() ({
@@ -787,6 +791,8 @@ func.func @scopeMerge3() {
// CHECK: return
// CHECK: }
+// -----
+
func.func @scopeMerge4() {
%cnt = "test.count"() : () -> index
"test.region"() ({
@@ -813,6 +819,8 @@ func.func @scopeMerge4() {
// CHECK: return
// CHECK: }
+// -----
+
func.func @scopeMerge5() {
"test.region"() ({
memref.alloca_scope {
@@ -839,6 +847,8 @@ func.func @scopeMerge5() {
// CHECK: return
// CHECK: }
+// -----
+
func.func @scopeInline(%arg : memref<index>) {
%cnt = "test.count"() : () -> index
"test.region"() ({
@@ -855,6 +865,31 @@ func.func @scopeInline(%arg : memref<index>) {
// -----
+// Ensure this case not crash.
+
+// CHECK-LABEL: func.func @scope_merge_without_terminator() {
+// CHECK: "test.region"() ({
+// CHECK: memref.alloca_scope {
+// CHECK: %[[cnt:.*]] = "test.count"() : () -> index
+// CHECK: %[[alloc:.*]] = memref.alloca(%[[cnt]]) : memref<?xi64>
+// CHECK: "test.use"(%[[alloc]]) : (memref<?xi64>) -> ()
+// CHECK: }
+// CHECK: }) : () -> ()
+// CHECK: return
+// CHECK: }
+func.func @scope_merge_without_terminator() {
+ "test.region"() ({
+ memref.alloca_scope {
+ %cnt = "test.count"() : () -> index
+ %a = memref.alloca(%cnt) : memref<?xi64>
+ "test.use"(%a) : (memref<?xi64>) -> ()
+ }
+ }) : () -> ()
+ return
+}
+
+// -----
+
// CHECK-LABEL: func @reinterpret_noop
// CHECK-SAME: (%[[ARG:.*]]: memref<2x3x4xf32>)
// CHECK-NEXT: return %[[ARG]]
|
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.
LG, but please reduce the test.
This PR adds terminator check to prevent a crash when invoke `lastNonTerminatorInRegion`.
This PR adds terminator check to prevent a crash when invoke
lastNonTerminatorInRegion
. Fixes #137333.