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

    <tr>
        <th>Summary</th>
        <td>
            [WebAssembly] Problems of forcing mem2reg in reference-types
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    I recently noticed that we force mem2reg optimization to run unconditionally, even in `-O0`, when reference-types feature is enabled:
https://github.com/llvm/llvm-project/blob/2dd52046816ac706a25d588efac77705793d8778/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp#L479-L482
This was added in https://github.com/llvm/llvm-project/commit/890146b19206827bc48ee1ae1dc1534ff2ff18d7.

I think this can have some problems:

1. This means we run some optimizations even when we are not supposed to. We will handle debug value differently and debug info can be lost even in `-O0`, if reference-types is enabled. And the newest EH spec requires reference-types to be enabled, so this means enabling EH means running some optimizations even in `-O0`. Also, as it happens, we've been even discussing enabling reference-types by default. (#80923) It'd be good if there's a way to promote only reference type values to registers.

2. When Wasm EH is used, we [demote](https://github.com/llvm/llvm-project/blob/85e6e71eb09cb9e75bbd475e5f54f876653c3f16/llvm/lib/CodeGen/WinEHPrepare.cpp#L858-L887) certain PHIs from registers to memory in `WinEHPrepare`. (Wasm EH uses WinEHPrepare pass, because it uses WinEH IR) Specifically, we cannot have a PHI before a `catchswitch`. PHIs in `catchswitch` BBs have to be removed (= demoted) because `catchswitch`s are removed in ISel and `catchswitch` BBs are removed as well, so they can't have other instructions. But when reference-types is enabled, we [force](https://github.com/llvm/llvm-project/blob/2dd52046816ac706a25d588efac77705793d8778/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp#L479-L482) to run mem2reg in `addISelPrepare`, which runs after `WinEHPrepare`, re-promoting the demoted PHIs in `WinEHPrepare`, which crashes the compiler. This is a problem even if we have a way to promote only reference types because they can be before a `catchswitch` too.

Any thoughts or ideas? 
cc @pmatos
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVl1v27gS_TX0yyCCRH0_-CFpmtsAvbjFbYE8U-TIYlcitRwqhvfXL0ayE7dNsejuw77I5tdwZs45M1RE9uAQ96K8E-X9Ti1x8GGvBsSv1u06b077Rwio0cXxBM5Hq9FAHFSEI0Lvg0aYcJIBD-DnaCf7h4rWO4gewuJgcdo7Y3lKjeNJyHeAz-jAOhBVevO_VFQpTx4HdBCwx4BO4008zUjQo4pLQLAE6FQ3ohH5rUjvRXo7xDgTj-SDkA8HG4elS7SfhHwYx-fLz80c_FfUUciHbvSdkA_SmFKmRdVkldJ1WilZmrJpsFe6ruu0rNvcNHXdXNmxfO6LCgdkO0_Y3RLh1HEw16Ntx3-VHqzDRM-zkPnHom5vPhaN3Jz-MliCoyJQxqDhHPxyGNpPk-U_TZtmRdVlrUyrRtadLhrETGFmdFbmRd_Lvs8aUyfb1dv3EeJg3W_8JdDKwaCeEchPCHPw3YgTvWR4-2YJrF5PqBwx5AzqeuAabdpAXUE8IqiATBWgZZ49MV98Ak8IRzuOMChnRgSD3XKAZzUuCMb2K_BMMeXMec263q9Odgijp_g2cWz_A21e6ZLArWO2Ijg8IkV4_wFoRg0Bf19sQPrhbPR83YVt8h2Q37K1JWBdsO7AhraZsDjHEz_LybW_CdyO5NmqIrARBjXP6GjlPwpZPyN0iG47aCzphYhtv9z6vbfdCQz2ahljAkI2QuZN2spcyBYeo5C14WAO3htOUxww8C0ECo7qxKHOwU8-Ing3nl6NAxvfoFkTEvBgKWKgb7gkE3hivJ8UTZwOS7DQlrMjgijvDLJtUd4L2fxduTYlVlhn2KWt7lqsy64zRV1i2ZdF39RVVeY677Pqe7m-8wb_g44Vat37D58Cziq8qLIpm5uPTVNznjSGqKyDTx8eCfrgp9dwOfYJJx9OZxSvba1wCtlcwl8ICa43wKxohbZDrRZCBvx1Ezz-n2__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