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

    <tr>
        <th>Summary</th>
        <td>
            [LoopOptimizer][LICM][Bug] Protobuf loop
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          kyulee-com
      </td>
    </tr>
</table>

<pre>
    We encountered a runtime crash bug due to miscompilation on a loop for utf8ToString in the SwiftProtobuf library built with thinlto (`-lto=llvm-thin`). The relevant source can be found  https://github.com/apple/swift-protobuf/blob/main/Sources/SwiftProtobuf/StringUtils.swift#L93 and the reduced repro are https://gist.github.com/kyulee-com/df8a81ceabbf3ad4997d11b3ab71edcd

The bug can be isolated by only a single line difference in a callee's linkage type, `$ss7UnicodeO4UTF8O13ForwardParserVs10_UTFParserssAGP7_buffers11_UIntBufferVy8Encoding_8CodeUnitQZGvMTW`.
        • `fail.ll` (before opt), the linkage type is `internal` from the ThinLTO internalization. The IR appears valid. https://gist.github.com/kyulee-com/df8a81ceabbf3ad4997d11b3ab71edcd#file-fail-ll-L257
        • `pass.ll` (before opt), the linkage type is `linkonce_odr`, the same as the pre-link case. The IR also appears valid. https://gist.github.com/kyulee-com/df8a81ceabbf3ad4997d11b3ab71edcd#file-pass-ll-L257
        
The `opt` hoists/sinks some critical memory operations in the failing case.
        • `opt -Oz -S fail.ll -o failopt.ll`, the load is hoisted out of the loop, providing an invalid value to `%12` and `%17` to block 16. https://gist.github.com/kyulee-com/df8a81ceabbf3ad4997d11b3ab71edcd#file-failopt-ll-L29.
        •  `opt -Oz -S pass.ll -o passopt.ll`, the load is not hoisted, correctly reading the`i8` value that was just written from the previous store. https://gist.github.com/kyulee-com/df8a81ceabbf3ad4997d11b3ab71edcd#file-passopt-ll-L192

The resulting object also shows the difference.
        • `clang -target arm64-apple-ios15.2 -Oz -S -c fail.ll -o failopt.s`, the stored i8 value (`w8`) in `%bb.9` is incorrectly overridden by `x28`, providing an invalid `w8` value to `LBB0_3`.  https://gist.github.com/kyulee-com/df8a81ceabbf3ad4997d11b3ab71edcd#file-failopt-s-L47
        •  `clang -target arm64-apple-ios15.2 -Oz -S -c pass.ll -o passopt.s`, the stored `i8` value (`w8`) is directly used for comparison in `LBB0_2`, which is correct. https://gist.github.com/kyulee-com/df8a81ceabbf3ad4997d11b3ab71edcd#file-passopt-s-L169-L170

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8lsFu2zwSx5-GvgxkSJQsWwcfkqYuCrhId5O0wF4CihxZbCiOQFL2uk-_oGTXbeouUKD9LjYpUtTMb_4zHOG93lnENVvcssXdTAyhJbd-OQ4GMZHUzWpSx_VnBLSSBhvQoQIBbrBBdwjSCd9CPexADQiBoNNeUtdrI4ImC2RBgCHqoSEHQ2hWj_QQnLY70BZCi_Bw0E346ChQPTRgdO2EO0I9aBPgoEMLodXWBALGV6xMExOI5XfG7LskrrAyZbyaw2OL4NDgXtgAngYnEaSwUCM0NFgF0IbQe5bfML5hfLPToR3quaSO8Y3oe4OMb3y0JelPxjC-qQ3VjG86oS3jm4fxWB9H3xsd56NLT0EbPx8PYTzfVjkIq0YnHapBogKHvSMQDn-yxof5DyZdIsD4RjUrscokirpucqGKqlqqLKtzUS8zVFKx9I6lN9NvBBHjcXJeezIioIL6CGTNEQR4bXcGwWiLoHTToEMrMcZDgBTGIDK-9HH9RewQwrFHxt_ASLrwfvlktSSF98XT42Z1n-Ubcgfh1EfhPLpPPkufnx4308z7m3cfl8_1EL_is-z56b0Nt-Ps03H11kpS2u6eV29I4ZPV4V__ebf_8PiZlen87FPF3nK2StkNjxY0Qpu5MaxMox5qbMghUB8Yr6KNkfX3doP28S0ddWvF-FrjqBv3Pbbabh_v4byov46SnaT0_t8g-h6F87AXRqv5Hw4YzxttMInuJMYkW75YXne4F97_vsPxEVmJz6TcGLdpp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