[Mlir-commits] [mlir] 26b81c4 - [mlir][memref] Add terminator check to prevent a crash (#141972)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri May 30 22:25:45 PDT 2025
Author: Longsheng Mou
Date: 2025-05-31T13:25:42+08:00
New Revision: 26b81c4300a8efc8e9377dd04b5c78896e96740d
URL: https://github.com/llvm/llvm-project/commit/26b81c4300a8efc8e9377dd04b5c78896e96740d
DIFF: https://github.com/llvm/llvm-project/commit/26b81c4300a8efc8e9377dd04b5c78896e96740d.diff
LOG: [mlir][memref] Add terminator check to prevent a crash (#141972)
This PR adds terminator check to prevent a crash when invoke
`lastNonTerminatorInRegion`. Fixes #137333.
Added:
Modified:
mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
mlir/test/Dialect/MemRef/canonicalize.mlir
Removed:
################################################################################
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..da6faf490653d 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,24 @@ 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
+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]]
More information about the Mlir-commits
mailing list