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

    <tr>
        <th>Summary</th>
        <td>
            possible miscompilation under -O3 since clang 15.0.0
        </td>
    </tr>

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

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

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

<pre>
    Hi all

I'm at the end of my knowledge.

I have the feeling, that I am triggering a very serious bug in the optimizer. But of course, I cannot really be sure (hello undefined behavior)

One out of a 1000 unittests in our codebase triggers a bug that only happens on the situation described below. It does not happen under asan, tsan and ubsan. Only on release -O3.
I invested dozens of hours to make the sample as small as possible.
I have trimmed down the reproducer to a sadly not so small reproducer (gtest, boost.asio, etc. involved).

I managed to strip it down to:
- self built libc++
  - -DLIBCXX_USE_COMPILER_RT=ON
  - -DLIBCXX_ABI_UNSTABLE=ON
  - -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON
- static linking against libc++ and libc++abi
- I removed tons of code, removing/changing more almost immediately makes the error go away:
  - removing struct members
  - removing one of two test asserts
  - changing to -O2
  - moving empty functions from one compilation unit into another
  - DLIBCXX_ABI_UNSTABLE=OFF
  - ...

I create a small Dockerfile which installs clang-15, compiles libc++ and runs the testcase:

[repro.tar.gz](https://github.com/llvm/llvm-project/files/9602767/repro.tar.gz)
My own code is under src/async, all other code is framework code (boost, gtest, ...)

```
FROM ubuntu:22.04

RUN \
  apt-get update && \
  apt-get -y install cmake sudo lsb-release wget software-properties-common gnupg && \
  echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-15 main" | sudo tee /etc/apt/sources.list.d/llvm.list && \
  (wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -) && \
  sudo apt-get -y update && \
  sudo apt-get -y install cmake python3 gzip git ninja-build libedit-dev swig zlib1g-dev && \
  sudo apt-get -y install clang-15 lld-15 && \
  sudo rm -rf /var/lib/apt/lists/*

RUN git clone https://github.com/llvm/llvm-project.git
WORKDIR /llvm-project
RUN git checkout llvmorg-15.0.0

ENV CXX=clang++-15
ENV CC=clang-15
RUN cmake -G Ninja -S runtimes -B build \
        -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
        -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON \
        -DLIBCXX_CXX_ABI=libcxxabi \
        -DLIBCXX_ABI_UNSTABLE=ON

RUN ninja -C build cxx cxxabi unwind
RUN ninja -C build install-cxx install-cxxabi install-unwind

COPY repro /root/repro

WORKDIR /root/repro
```
output:
```
$ docker build . -t test && docker run -it test ./build.sh
... compile omitted ...
+ out/app
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from ApiTest
[ RUN      ] ApiTest.caching
etag matches
etag matches
./build.sh: line 64:   111 Segmentation fault      (core dumped) out/app

```

The exact same reproducer with clang-14 does not trigger any crash.:
```
+ out/app
... compile omitted ...
Running main() from gmock_main.cc
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from ApiTest
[ RUN      ] ApiTest.caching
etag matches
etag matches
test done
[       OK ] ApiTest.caching (1 ms)
[----------] 1 test from ApiTest (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[  PASSED  ] 1 test.

```

Any help would be appreciated. I am also willing to help of course. LLVM is a very important project to me.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWFtz4rgS_jXmpcsuMJeEBx6AhD3UTsJUktmz-5SSbWFrY0suSYYhv3675QuXA7Mz74dygSW1-vL1RS0ilRxm_xHA8tzrP3j9ef299sK7ApgFm3HgMgG1heIAH1Ltc56kPDgjhoztuCPdcp4LmXrhEoe4fQ2sAKtFmnKN88Bgx_UBDI5UZSCqUhDS7VSlFY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