Skip to content

[MLIR][IR] Add assert to Operation::moveBefore (NFC) #137772

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
Apr 29, 2025

Conversation

Dinistro
Copy link
Contributor

This commit adds an assert to Operation::moveBefore which ensures that moving of operations without a parent block is detected early on. This case otherwise runs into a segfault, as it's assumed that there is an parent block.

This commit adds an assert to `Operation::moveBefore` which ensures that
moving of operations without a parent block is detected early on.
This case otherwise runs into a segfault, as it's assumed that there is
an parent block.
@Dinistro Dinistro requested review from joker-eph and gysit April 29, 2025 08:59
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Apr 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Christian Ulmann (Dinistro)

Changes

This commit adds an assert to Operation::moveBefore which ensures that moving of operations without a parent block is detected early on. This case otherwise runs into a segfault, as it's assumed that there is an parent block.


Full diff: https://github.com/llvm/llvm-project/pull/137772.diff

1 Files Affected:

  • (modified) mlir/lib/IR/Operation.cpp (+4-2)
diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index fe0fee0f8db2c..a479ff97f3f16 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -322,8 +322,8 @@ void Operation::setAttrs(DictionaryAttr newAttrs) {
 }
 void Operation::setAttrs(ArrayRef<NamedAttribute> newAttrs) {
   if (getPropertiesStorageSize()) {
-    // We're spliting the providing array of attributes by removing the inherentAttr
-    // which will be stored in the properties.
+    // We're spliting the providing array of attributes by removing the
+    // inherentAttr which will be stored in the properties.
     SmallVector<NamedAttribute> discardableAttrs;
     discardableAttrs.reserve(newAttrs.size());
     for (NamedAttribute attr : newAttrs) {
@@ -560,6 +560,8 @@ void Operation::moveBefore(Operation *existingOp) {
 /// before `iterator` in the specified basic block.
 void Operation::moveBefore(Block *block,
                            llvm::iplist<Operation>::iterator iterator) {
+  assert(getBlock() &&
+         "cannot move an operation that isn't contained in a block");
   block->getOperations().splice(iterator, getBlock()->getOperations(),
                                 getIterator());
 }

Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Dinistro Dinistro merged commit 89c5c3b into main Apr 29, 2025
14 checks passed
@Dinistro Dinistro deleted the users/dinistro/add-assert-to-block-move branch April 29, 2025 10:38
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
This commit adds an assert to `Operation::moveBefore` which ensures that
moving of operations without a parent block is detected early on. This
case otherwise runs into a segfault, as it's assumed that there is an
parent block.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
This commit adds an assert to `Operation::moveBefore` which ensures that
moving of operations without a parent block is detected early on. This
case otherwise runs into a segfault, as it's assumed that there is an
parent block.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
This commit adds an assert to `Operation::moveBefore` which ensures that
moving of operations without a parent block is detected early on. This
case otherwise runs into a segfault, as it's assumed that there is an
parent block.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
This commit adds an assert to `Operation::moveBefore` which ensures that
moving of operations without a parent block is detected early on. This
case otherwise runs into a segfault, as it's assumed that there is an
parent block.
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
This commit adds an assert to `Operation::moveBefore` which ensures that
moving of operations without a parent block is detected early on. This
case otherwise runs into a segfault, as it's assumed that there is an
parent block.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants