Skip to content

Commit bf3a981

Browse files
authored
[MLIR] Properly add operations to blocks during createOrFold (#70010)
Fixes #68884.
1 parent 9ae11a5 commit bf3a981

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

mlir/include/mlir/IR/Builders.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,18 +504,20 @@ class OpBuilder : public Builder {
504504
template <typename OpTy, typename... Args>
505505
void createOrFold(SmallVectorImpl<Value> &results, Location location,
506506
Args &&...args) {
507-
// Create the operation without using 'create' as we don't want to
508-
// insert it yet.
507+
// Create the operation without using 'create' as we want to control when
508+
// the listener is notified.
509509
OperationState state(location,
510510
getCheckRegisteredInfo<OpTy>(location.getContext()));
511511
OpTy::build(*this, state, std::forward<Args>(args)...);
512512
Operation *op = Operation::create(state);
513+
if (block)
514+
block->getOperations().insert(insertPoint, op);
513515

514-
// Fold the operation. If successful destroy it, otherwise insert it.
516+
// Fold the operation. If successful erase it, otherwise notify.
515517
if (succeeded(tryFold(op, results)))
516-
op->destroy();
517-
else
518-
insert(op);
518+
op->erase();
519+
else if (listener)
520+
listener->notifyOperationInserted(op);
519521
}
520522

521523
/// Overload to create or fold a single result operation.

mlir/test/lib/Dialect/Test/TestDialect.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,11 @@ LogicalResult TestOpWithVariadicResultsAndFolder::fold(
529529
}
530530

531531
OpFoldResult TestOpInPlaceFold::fold(FoldAdaptor adaptor) {
532+
// Exercise the fact that an operation created with createOrFold should be
533+
// allowed to access its parent block.
534+
assert(getOperation()->getBlock() &&
535+
"expected that operation is not unlinked");
536+
532537
if (adaptor.getOp() && !getProperties().attr) {
533538
// The folder adds "attr" if not present.
534539
getProperties().attr = dyn_cast_or_null<IntegerAttr>(adaptor.getOp());

0 commit comments

Comments
 (0)