[MLIR] Inserting Regions Into an Operation Instance

The core of what I’m trying to do is a “raising” pass that will generate regions out of an operation’s block list and the basic blocks them selfs. To do so I intended to modify an operation in place using the class methods provided. However, I was unable to find a way to insert regions into an operation through such methods.

I’ve seen the MutableArrayRef of regions that can be retrieved with getRegions(), but this does not seem to serve the purpose of inserting new regions into an operation. The only way I’ve found to do this is by allocating a new operation while passing the existing information of the current operation into the constructor and also setting the number of regions to the desired amount. Only then I would be able to modify these regions with the MutableArrayRef<Region> object.

It’s likely that I’ve overlooked something, but I haven’t found another method.
Is there any easier way to do this?

You haven’t overlooked anything :slight_smile:
You need to create a new operation with regions, and replace the existing one with the newly created one. You can’t mutate in-place an operation this way.
I’d love to have wrapper though, at some point I proposed a way to populate an OperationState from an existing operation (so that adding a region is trivial), but @River707 had objections at the time.

1 Like