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

    <tr>
        <th>Summary</th>
        <td>
            [mlir][linalg] Make RemoveOutsDependency pattern preserve destination passing style
        </td>
    </tr>

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

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

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

<pre>
    The pattern [RemoveOutsDependency](https://github.com/llvm/llvm-project/blob/f73844d92b36cb6801ac50ea721f4ba29b35d7a9/mlir/lib/Dialect/Linalg/Transforms/ElementwiseOpFusion.cpp#L1802) is meant to do the following

```
%0 = <some operation>
%1 = linalg.generic {
    iterator_types = ["parallel", "parallel"], indexing_maps = [...]}
    ins(....) outs(%0 : tensor<?x?xf32>) {...}
```

The `linalg.generic` is all parallel. So the use of `%0 -> %1` is a "false" dependency cause the actual values of `%0` arent used in `%1`. They are only really present to represent the shape of the output. So it is presumably legal to convert this to 

```
%0 = <some operation>
%empty = tensor.empty(...)
%1 = linalg.generic {
 iterator_types = ["parallel", "parallel"],  indexing_maps = [...]}
 ins(....) outs(%empty : tensor<?x?xf32>) {...}
```

where the dependency is now broken. This has advantages in terms of ability to DCE code and also helps fusion.

This pattern though was added before `DestinationPassingStyleOpInterface`. One thing the above pattern does is break destination passing style. A better approach is to no replace `outs` with `tensor.empty()`, instead restrict this pattern to do the following. If `%0` instead of "some operation" is "some destination passing style operation) 

```
%0 = <some destination passing op> init(%dest : tensor<?x?xf32>) ...
%1 = linalg.generic {
    iterator_types = ["parallel", "parallel"],  indexing_maps = [...]}
    ins(....) outs(%0 : tensor<?x?xf32>) {...}
```

and `%dest` is the operand that is "tied" to `%0`, the pattern above should convert this to

```
%0 = <some destination passing op> init(%dest : tensor<?x?xf32>) ...
%1 = linalg.generic {
    iterator_types = ["parallel", "parallel"],  indexing_maps = [...]}
    ins(....) outs(%dest : tensor<?x?xf32>) {...}
```

This still breaks the false `%0` -> `%1` dependency. It does introduce a `%dest` to `%1` dependency, but if that is also defined by a destination passing style op, then the pattern can further apply, propagating as far up the use-def chain as possible.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVk9v2z4S_TT0ZVBBpqTYOviQxDFQoEUWbe_FSBxJ3NCkQFJO_e0XQ9muk_7ZFAUWe_gBiQyKfKMZvsfHwRB0b4k2oroTUn7EgcLwCQ86DGif0AspRbVd4BQH5zc_TC8ap46bLwPBiDGStyCqu0-0dwd6nGLY0khWkW2PotoKuR5iHIMoboXcCbnrdRymJmvdXsidMYfzz7vRu39TG4XcNcY1Qu66VbEuS1XLprhpm5t1vsS2yglXctmVDcq6KSq1wlrI3d5oz3E047YazRzog7ZoeiF3Xzza0Dm_D0LuHgztycZnHehx3E1BO5u14yhk8WG5zqWQNegAe0IbITpQDuJA0Dlj3LO2vci3Ir89PW_y0988lFUOotiCKO6D2xO4kTxG7awoHi5LlmmJSbllPVnyugWxupsXAADoyDDnv8bjSGGOmJga0aMxZJggeQ-v3vBu34O2ir5p23_d43jBZlnG06vt1UdsEHKd8YyswU2Rh6cKbiGSDc6L4l4Uu2_83xWSi5A1p8qgc6zXe5CeLA5xk78sUtzkvLNoDJzTzuDzvL1TIHAdY1IK70TxALxXZwzX2qEJJKQEdVEYtMhIjoBtnNDAAc1E4SoWR0BPNvI3FGh7muDQGXwZ6MjT4Kw5gic05gijp0Az-54ug4EgDDimPHngpjhOMVWgIyfJK6c9NuYIhno0jG-dPZBntA48_mv10H6Mx7RspihLL2YmhazfqrK_ktjbNPYrgZ0r-GuRPQ_kZ-6vBKEDWPcMjXdPZJlfHWDAAKgOaCP2FFgCkfw-iQQbbXQ8MjXb-wdonSJAqwBNcDCQGQN0s0W8lDfTfTK_OLipH-A5fUSRgoY655P-txSitonDf7Hp2v5zPBp6HN_bSL7DlpIIHy2X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