-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR] Properly add operations to blocks during createOrFold
#70010
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
[MLIR] Properly add operations to blocks during createOrFold
#70010
Conversation
@llvm/pr-subscribers-mlir-core @llvm/pr-subscribers-mlir Author: Morten Borup Petersen (mortbopet) ChangesFixes #68884. Full diff: https://github.com/llvm/llvm-project/pull/70010.diff 2 Files Affected:
diff --git a/mlir/include/mlir/IR/Builders.h b/mlir/include/mlir/IR/Builders.h
index 5e54d4ea49e8251..11c89273e70ea31 100644
--- a/mlir/include/mlir/IR/Builders.h
+++ b/mlir/include/mlir/IR/Builders.h
@@ -504,18 +504,20 @@ class OpBuilder : public Builder {
template <typename OpTy, typename... Args>
void createOrFold(SmallVectorImpl<Value> &results, Location location,
Args &&...args) {
- // Create the operation without using 'create' as we don't want to
- // insert it yet.
+ // Create the operation without using 'create' as we want to control when
+ // the listener is notified.
OperationState state(location,
getCheckRegisteredInfo<OpTy>(location.getContext()));
OpTy::build(*this, state, std::forward<Args>(args)...);
Operation *op = Operation::create(state);
+ if (block)
+ block->getOperations().insert(insertPoint, op);
- // Fold the operation. If successful destroy it, otherwise insert it.
+ // Fold the operation. If successful destroy it, otherwise notify.
if (succeeded(tryFold(op, results)))
- op->destroy();
- else
- insert(op);
+ op->erase();
+ else if (listener)
+ listener->notifyOperationInserted(op);
}
/// Overload to create or fold a single result operation.
diff --git a/mlir/test/lib/Dialect/Test/TestDialect.cpp b/mlir/test/lib/Dialect/Test/TestDialect.cpp
index 8aa7774d2bf008f..fff2b15ca641327 100644
--- a/mlir/test/lib/Dialect/Test/TestDialect.cpp
+++ b/mlir/test/lib/Dialect/Test/TestDialect.cpp
@@ -529,6 +529,10 @@ LogicalResult TestOpWithVariadicResultsAndFolder::fold(
}
OpFoldResult TestOpInPlaceFold::fold(FoldAdaptor adaptor) {
+ // Excercise the fact that an operation created during createOrFold should be
+ // allowed to enable its block.
+ assert(getBlock() && "expected block to be assigned");
+
if (adaptor.getOp() && !getProperties().attr) {
// The folder adds "attr" if not present.
getProperties().attr = dyn_cast_or_null<IntegerAttr>(adaptor.getOp());
|
@@ -529,6 +529,10 @@ LogicalResult TestOpWithVariadicResultsAndFolder::fold( | |||
} | |||
|
|||
OpFoldResult TestOpInPlaceFold::fold(FoldAdaptor adaptor) { | |||
// Excercise the fact that an operation created with createOrFold should be | |||
// allowed to enable its block. |
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.
What does it mean to "enable a block"?
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.
Typo, meant 'access' its block.
Fixes #68884.