<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63739>63739</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Program is miscompile with '-O1' option
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Yvan-xy
</td>
</tr>
</table>
<pre>
My clang version is:
```
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: x86_64-apple-darwin21.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
```
And I compiled the following program with `clang -O1 -o a a.cpp`:
```c++
#include <mutex>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
#include <iostream>
#include <cassert>
std::mutex l;
void f(std::size_t id) {
while (true) {
{
std::lock_guard<std::mutex> guard(l);
std::cout << "thread " << id << " acquired\n";
}
}
}
int main() {
for (std::size_t i = 0; i < 256; i++) {
std::thread t(f, i);
t.detach();
std::cout << i << std::endl;
}
while(1);
}
```
The program would keep running the `for` loop rather than stop when `i == 256`.
![Screen Shot 2023-07-07 at 15 28 57](https://github.com/llvm/llvm-project/assets/25425110/0c784e6d-cf4d-4d22-8e82-5b0272a69ede)
I suppose that, without special treatment for `while(1)`, the exit on the control flow graph (CFG) will not post-dominant the preceding nodes. As a result, passes that rely on post-dominators for optimization will not work correctly.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU2P4jgT_jXmUkrkVD45cOiG4dVI82pWmr7saWTignjHsbO20zTz61cOaaBZ-rBSBLafqnLVUx8W3quDIVqx8pmVm4UYQ2fd6s9XYZK302Jn5Wn1_xO0WpgDvJLzyhpQnuVPjG8Yf2IVn79p-zQMmu6kszzN0goYNtN5kuVZlfIUsxTTkuHyrPki3IECy5_gral-VkUioqlECndUBrO0SOcrXjpHQkJvJekoPliv3s7QV-OD0JrkRrkIMdx-Uzsn3InhdkOvpO1A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