<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/68884>68884</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] `createOrFold`-created ops has null block when accessing folder
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mortbopet
      </td>
    </tr>
</table>

<pre>
    Submitting as an issue, since this seems like a bug to me;

When an operation is created through `createOrFold`, the operation will
1. first be created
2. attempted folded
3. inserted into the block

https://github.com/llvm/llvm-project/blob/f89b7a9ee205acbd6c180cc56ea5a74f8602d269/mlir/include/mlir/IR/Builders.h#L515-L518

One may have a folder which also looks at the `mlir::Block`s where e.g. the operands of the newly created operation resides, and compare this wrt. the `mlir::Block` that the newly created operation resides in. This - to me - shouldn't violate any existing constraints (e.g. IsolatedFromAbove, local transformation, ...) since we're just looking across blocks. 

However, this may result in a folder not applying, seeing as, in step 2, when an op is created through `createOrFold`, the `block` of an op is still a `nullptr` when the fold method is run - given that the `block` pointer of an op is updated in step 3, after folding failed.

Another hypothetical issue along the lines of this is if a folder for some reason has the op block iterator as load bearing (e.g. relative position wrt. other ops); this, as well, is not assigned until after the op is inserted.

Solutions could be that , `createOrFold` pre-emptively assign the `block` pointer the op in-between step 1 and 2 (wouldn't fix the block iterator issue) or that we "fully" create an operation (e.g. insert it into the block) before applying the folder method (which then may remove it from the block again).


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU2P2zYQ_TX0ZWBBoiXbOviw28BogBQBkgI9U9JIYkJxBA5lxf--ICWvvG2DIpdd80NvZt68eVTMurOIF1G8iuLDTk2-J3cZyPmKRvS7ipr75etUDdp7bTtQDMqCZp5QyN-Ata0RfK8ZGHFgMPo7goJq6sATDCgOryL9INKX5e9fPdoAQCM65TUFKKgdKo8N-N7R1PUg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