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

    <tr>
        <th>Summary</th>
        <td>
            [mlir] Implement a canonicalization pattern for memref.alloc to remove unused / "dead" memrefs
        </td>
    </tr>

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

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

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

<pre>
    I would like to remove unused memrefs as a cleanup pass in my program. I was hoping `--affine-scalrep` would do this, but it only does this for memrefs that are dead *after* it forwarded its stores to loads. Note this comment here:

https://github.com/llvm/llvm-project/blob/d3bc9cc99b3d45e1fb8d65a57e308e899439fe26/mlir/lib/Dialect/Affine/Utils/Utils.cpp#L1060-L1063

I was planning to implement this functionality as a canonicalizer for `memref.alloc` (to ensure that the memref is locally allocated) - it will simply check if there are only writes and dealloc users, and if so, remove the operation.

There's still the open question of other "unused" memrefs which could be canonicalized away (e.g. ones that weren't locally allocated with `memref.alloc` but rather returned from a function call). I'm a little unsure of where that canonicalization pattern would go or anchor on.

Let me know! 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUlEuO4zYQhk9DbwoWKMqWrIUWnTQMNDDIKjlAiSxZTFMshaRGcE4fkPb0dDLJIhsJetSD3_9XYYz25okGcf5JnF8PuKWZw4AxIB5GNvfhDXbenAFn3wkSQ6CFvxJsfotkYKEl0BQBIyBoR-i3FVaMEayH5Q5r4FvApYI32DHCzKv1NxCtPB5xmqynY9ToAq2ilc9ChiHNNgr1M4xbApuAvbuDYYrlA0wcPuqmGRNgIDCEBoR6wSlREOolx00cdgyGDNgUISYOOQWDYzSxgl840SOj5mUhn2CmQKJ5EfJVyOd1TmmN-Z26CnW92TRvY6V5Eerq3Ndvt-Ma-HfSSajr6HgU6mqaUfda9_3YmNOZ6mm8mPaM544aeaFL35-afiLVCnVdnA05j81xrxbdI9FL4SPU9bdkXfx2r_S6CtV8qWUrj_nafO72QXl16H3mnBjssjoqh3uw27xOlj06m-5P1dCztxqd_ZNCgSta-eBboXOsszRCXRID-bgFekBPMz1VABvBsUbn7lACMJERqodjFmG3zkHMbdxBz6TfwU45OFDRrWi7B5soAnqTdcwpYIsUigfySztB5PzwNF-uzSsFzEepPgP4tUiouix3Lvz808MfG8X8N_AEnMuDUOphYqHUh5_22eoZdDHiSH9jYwB3vGcSVN0qYE9P--0UyAvVpR8pwG7T_G88s7MDlj4CpS14MjAFXgA_JIKcS6i-gjehuvzF2ZRcHr2iAk-wF4yliU-dFiqwYkoU_HOobgwcAL2eOcA_mH2hBAvBu-ddqBoOZmhM3_R4oKHu6rpTZyW7wzwYeaL6rCWNcmxrbNUou8tpUnTRSho9HuygpDrJRvX1uW7Uqaqnuj1NkuoLtlJejDhJWtC6Ks9MxeF2sDFuNFw62XUHhyO5WDaRUo-hUHknhaGM2LjdojhJZ2OK3xMkm1zZXiXg_ApvH4bH_4byfYU8JPlxsQl1zQ7Ja-WTPw5bcMP_XgnlkHmCyzn_CgAA__9iKdRf">