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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR][BUG] MLIR Pattern Rewriter may cause core dump in this situation 
        </td>
    </tr>

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

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

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

<pre>
    Hi there, i have mlir code like this:
``` c++
    %176 = "tf.GatherV2"(%arg2, %175, %cst_12) {batch_dims = 0 : i64, device = "", "tf-op-name" = "GatherV2_3"} : (tensor<?x2xi64>, tensor<?xi64>, tensor<i32>) -> tensor<?x2xi64>
    %177 = "tf.GatherV2"(%arg7, %175, %cst_12) {batch_dims = 0 : i64, device = "", "tf-op-name" = "GatherV2_4"} : (tensor<?xi64>, tensor<?xi64>, tensor<i32>) -> tensor<?xi64>
```

And i gave pdll like this:
```c++
 let op_196 = op<tf.GatherV2>(arg_2, op_195.0, op_13.0) {device = attr<"\"\"">, batch_dims = attr<"0 : i64">};
        let op_197 = op<tf.GatherV2>(arg_7, op_195.0, op_13.0) {device = attr<"\"\"">, batch_dims = attr<"0 : i64">};

```

But when i try to rewriter this pattern, i met core dump like:
```c++
terminate called after throwing an instance of 'std::length_error'
  what():  SmallVector unable to grow. Requested capacity (4294967296) is larger than maximum value for size type (4294967295)
TensorFlow crashed, please file a bug on https://github.com/tensorflow/tensorflow/issues with the trace below.

```
when i gave `-debug-only=greedy-rewriter`, i got log as below. Why the `Replace : 'tf.GatherV2'` phase is same? And the second **Replace** is after **Erase** ,this seems a BUG?
```c++
Processing operation : 'tf.GatherV2'(0x55870f3f4230) {
  %197 = "tf.GatherV2"(%arg7, %195, %13) {batch_dims = 0 : i64, device = "", "tf-op-name" = "GatherV2_4"} : (tensor<?xi64>, tensor<?xi64>, tensor<i32>) -> tensor<?xi64>


  * Pattern  : 'tf.GatherV2 -> (mhlo_disc.custom_call_v3)' {
    ** Insert  : 'mhlo_disc.custom_call_v3'(0x55870f462b80)
    ** Replace : 'tf.GatherV2'(0x55870f3f4230)
    ** Modified: 'func.return'(0x55870f3f6be0)
    ** Erase   : 'tf.GatherV2'(0x55870f3f4230)
    ** Replace : 'tf.GatherV2'(0x55870f3f4230)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcVlGPozYQ_jXOyygRGUOAhzxkN5frST3ptG2vj5ExE_DVYGqbzaa_vrJJ9rLXvdypqlqpESJghs_jb775jHBONT3RmmV3LNvOxOhbY9efrKNKOK9EP6tMfVr_oMC3ZInhPShoxSNBp5UFaWoCrX4j8K1yjG9YsmXJhq2S6QDJ8C4ccRgAgGG2zFfA-BYYoj8s3oqA_BEZIsOCYSZsg2GeGJmdr6Tz-yUyLIHld5Xwst3XqnMRJwHGN6BWaYit6VFJuuDH436aaW6GeS86YoiXx5e59zwE5tsIxLDw1DtjGb9nfPeETwGavwlALx68Mqw4xqES5oy_gddxXnKRf4OL_N_mIr3BxT_DxDUPz1I538bzpq9BQRNkNtRa31LYS4Fp8mCG_bKcFGYGxu-veQ0ZFcI2-0hFjMwWyeWah8vI6hVzwvuYNyLLrs6I5xV_UYDP4VeliLH5lvGrRgi_53Tzb6Sb_6fp3qjU3ejh2FIPCrw9gTdg6WiVJxvrBYPwnmw_-UZHHqSxBPXYDbGo36inJ9upXngCKbSmGsRhQrbmqPoGRA-qd170ksAcgGHufB0w-UZT3_h2T9YayzC_8H5shY-tVYbVwk-d0PojSW8sjL2oNIUlNNYcF_BAv4_kPNUgxSCk8qfQDimWabnKsVwF7pUDLWwTcxI9dOJJdWMHj0KPBAdjwak_CPxpoBfvZmH-mNHPsS922hxBWuFaqgNVgybhCA5KEwioxgZMD633Q2wA3DHcNcq3Y7WQpmO4m7rroM3xyxvl3EgOjsq3wcHBWyEJKtLmuLhR3HNJYweyVTKvqRqbu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