Skip to content

[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

Merged
merged 1 commit into from
May 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
@@ -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 &&
28 changes: 28 additions & 0 deletions mlir/test/Dialect/MemRef/canonicalize.mlir
Original file line number Diff line number Diff line change
@@ -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]]
Loading
Oops, something went wrong.