<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60462>60462</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir] Can't use `GreedyPatternRewriteDriver` on `mlir::ModuleOp`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mikeurbach
</td>
</tr>
</table>
<pre>
I'm working on pulling in the latest changes to MLIR in a downstream project, and I believe I've hit a snag with the recent change here: https://github.com/llvm/llvm-project/commit/977cddb95eac67a6dc6680a7d0fadee81114de11.
Specifically, we have this call to `applyOpPatternsAndFold`, where `getOperation()` returns an `mlir::ModuleOp`: https://github.com/llvm/circt/blob/f80e0f6e67bd915ff5f1ddb90905a20f609a9ef1/lib/Conversion/SCFToCalyx/SCFToCalyx.cpp#L1230-L1234
`applyOpPatternsAndFold` will look for a `scope` within which to apply the rewrite: https://github.com/llvm/llvm-project/blob/ffa2a647c6a9dd2fab13a5e1bd4832b5b200d908/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp#L595-L598
`findCommonAncestor` goes through the fast path, since we only have one op to rewrite: https://github.com/llvm/llvm-project/blob/ffa2a647c6a9dd2fab13a5e1bd4832b5b200d908/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp#L561-L563
However, for our case, the `mlir::ModuleOp` is the outermost level of the IR, so this returns null. Note that in the non-fast path, `findCommonAncestor` asserts that it found a `Region`, but in this path, null can be returned.
This is all existing logic, which seemed to work fine. But the above patch to the `GreedyPatternRewriteDriver` also added this assertion, which now fires for the code I linked from CIRCT: https://github.com/llvm/llvm-project/blob/ffa2a647c6a9dd2fab13a5e1bd4832b5b200d908/mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp#L127
I'm curious what the path forward should be. Should we remove the assertion? Is there a better way to handle this? It seems like using the existing drivers to run patterns on the outermost op(s) in the IR won't work.
For this specific pass in CIRCT, we can probably adjust how we are rewriting the IR, but in general this seems to pose a restriction that perhaps we don't need.
cc @matthias-springer
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVsFu4zYQ_Rr5MohBUZZsHXzIOnBrINstsukHUORI4oYiBZKy1n9fDGWnyQIptr3txZZEcjjvzXtDihB0ZxH3WfkpKx9WYoq98_tBv-DkGyH7VePUZX_K-HaA2fkXbTtwFsbJGHrUFmKPYETEEEH2wnYYIDr4_Hh6olEBys02RI9igNG7byhjxg8grIITNGg0nhEo_hmh1xEEBCs6mHXsU2iPEu0tNPToMSvuoY9xDFlxn_Fjxo-djv3UrKUbMn405nz7u3vd8CjdMGh6qLdbqVRTlyhktRWVklW1Y2KrWCsU4i7P843CPF9n7CFj98vv1xGlbrUUxlwo-xmhF2eE2OsA9JUgZxUT42guX8Y_RYzobbi36uiMyiqWFlHyNKvD-GVEL6J2NuO7jNdZxcBjnLwNICzNGYz2hK-4_-zUZPDLSFF-CrnUPkFujGsyfmx3DFlbYbVtVJ2XbVu2ORHAalYKztqK1aLGNqcAmhYcnD2jDym549fD8dkdhLl8f_eyluOY8eIx5wW7o9_NW7r-jQmYtTFgnHuB1nkQBDZIN-IyFnttYe617InSFOUqg9nr-P9qfyOiFVxUm62sRK0Ub0WTF6LEvFGbXcGbsuGMqZrtMn5M7N_4ePbChtb5IWT8-FfUhv5_84jqcoX3tCT34PUZ_Y2asi7vHst69wMxrbbq4IbB2XsrMUTnCXjnyDa9d1O3yL4VIcIoYk_KCdpKJNE5ay6L8pxFcCNx9AsyU-V3j2VVvGXmdzfjGT2hJVm4yYMUAemd6PjIEaBDGndTRD-4EMHgGQ24Nn0-PSX63GLUm8PsZMwa_nCRDCzirYlZZ-_e0f5htUQI6GO4ro7QusmqRcpP2JFxFsc30zW4Dq9BaXOQwkKD14RQves1zzRbB6Cugt91iNRnjeu0XJoIWSMgDqio-tSTodUW1_BpigmHaNwZab_FQ1f6Pi5LQmSCA6EUB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