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

    <tr>
        <th>Summary</th>
        <td>
            Volatile load before unreachable is optimized out
        </td>
    </tr>

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

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

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

<pre>
    In the following, the volatile load is optimized out:
```c
void load(volatile int* x) {
    *x;
 __builtin_unreachable();
}
```
```llvm
define void @load(ptr noundef %x) {
  %v = load volatile i32, ptr %x, align 4
 unreachable
}
```

I think that C23/C2x/C2y should disallow this optimization, given, under the definition of undefined behavior:

> Any other behavior during execution of a program is only affected as a direct consequence of the concrete behavior that occurs when encountering the erroneous or non-portable program construct or data. In particular, all observable behavior (5.1.2.4 [or 5.2.2.4 depending on the draft/version]) appears as specified in this document when it happens before an operation with undefined behavior in the execution of the program.

and I believe the volatile load should count under that observable behavior that is to be completed even if UB is known to follow.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VN1u6zYMfhr5hqjhyLGTXPgibRagD7DdFrJE29xRJE8_TnuefpCck_R0xYBAsSj-ffxICu9pNIgda55ZcypEDJN1nfop6CKK3qqP7tVAmBAGq7W9khkZf8mCxWoRSCNoKxSQBzsHutBPVGBjYPWRVSdWHVlbrT-53hdLKpswvr-7IBMYP8I74wdgu-dVEwCA8eM7q38J3t76SDqQeYvGoZCT6DUyvmf8cFdiu9OXwF-uWi-XVaRwIJOAkAK2rW5JzcGBsdEoHIDx5mtOjDcLsPq0wn4gqHkqTDJejV5AaBoNbG-GnzP-_0Tz-QphIvMDwiQCvPCa8fMLf8_nB_jJRq1AkReJlKR6L78IZE0KP9KC-SNBcZmyDJiSAtghy1MBFPQ4iYWse5C2nvUfcDQfYMOE7q4EKjoyI-A7yvjLl4DZ2dGJS24Eoz9ADAPKgAqEBwGKHMoA0hqP_0Q0EpNVyklaIx0GfPjPkK2U0Xm4TmgAjbTRBMxhkw06Zw3a6MEmrszTbF1Ilb1nkQIFF2VIGkoEUcKrgVm4QDJq4VZ-NNjeo1uy6T0-4_um3JS83AJrnq2DpuT5pnBGo1ISdp0J5cQQGD8v6HyqenNKzSLmGYXzCbifUdJAqIDMypKyMl7QhBUZBZiSuvHQ42AdgjBgZ3SZRbhSmL6haXWGvzOQBDf05WcShVHwCj1qwgW_GdxbL-UK31slEfBNZfIDeQgW-sTcZdaYKMYlYRngz-f0-sPYq0k668ooC9XV6lAfRIHdZsf5Yb_hdVtMXbvHbdP0da9UUzVtXym16dta7hTnmxoPBXW84ttqX7V809RNW26God22-7rdNbttKwTbVngRpMs01KV1Y0HeR-w2Fef1rtCiR-3zbuPc4BXyK-M8rTrXJaOnPo4-DT_54B9uAgWN3V-_VerG0KdB_s_WK6LT3RTC7NMo8TPj55HCFPtS2gvj57x71r-n2dm_Uab2yVl5xs-3tJeO_xsAAP__rHvVAA">