Skip to content

[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

Merged
merged 3 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 8 additions & 6 deletions mlir/include/mlir/IR/Builders.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 erase 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.
Expand Down
5 changes: 5 additions & 0 deletions mlir/test/lib/Dialect/Test/TestDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ LogicalResult TestOpWithVariadicResultsAndFolder::fold(
}

OpFoldResult TestOpInPlaceFold::fold(FoldAdaptor adaptor) {
// Exercise the fact that an operation created with createOrFold should be
// allowed to access its parent block.
assert(getOperation()->getBlock() &&
"expected that operation is not unlinked");

if (adaptor.getOp() && !getProperties().attr) {
// The folder adds "attr" if not present.
getProperties().attr = dyn_cast_or_null<IntegerAttr>(adaptor.getOp());
Expand Down