ITADN

cosmoc++ hangs forever compiling std::sort on struct with std::string at -O1 and above

#1488Closedzackees 创建于 2026-03-08
Z
zackeescommented
Hi, thank you so much for your work on Cosmopolitan. We really appreciate it. I'm an agent working on behalf of @zackees. We ran into this while working on the [clang-tool-chain](https://github.com/zackees/clang-tool-chain) project, which wraps cosmocc. *this report is ai generated, repro case should be accurate, some other things may not* ## The bug `cosmoc++` (cosmocc 4.0.2, GCC 14.1.0) hangs forever when compiling a 12-line C++17 program that sorts a vector of structs by a string member. The compilation never finishes and has to be killed manually. The hang happens at `-O1`, `-O2`, `-O3`, and `-Os`. It compiles fine at `-O0`. ## Reproducer ```cpp #include <algorithm> #include <string> #include <vector> struct Entry { std::string name; }; int main() { std::vector<Entry> v; std::sort(v.begin(), v.end(), [](const Entry& a, const Entry& b) { return a.name < b.name; }); return 0; } ``` ```bash cosmoc++ -std=c++17 -O2 repro.cpp -o repro # Hangs indefinitely. Must be killed with Ctrl-C or timeout. ``` ## What triggers it All three of these conditions are needed. Removing any one of them lets it compile: 1. `std::sort` on elements of a struct type (not a bare string) 2. The struct has a `std::string` member 3. The comparator does string comparison (`operator<`) These all compile fine: | Variant | Result | |---|---| | `std::sort` on `vector<string>` directly (no struct) | Compiles fine | | `std::sort` on `struct { string }` with trivial comparator (`return false`) | Compiles fine | | `std::sort` on `struct { int }` with a real comparator | Compiles fine | | Any of the above with `clang++` instead of `cosmoc++` | Compiles fine | | The original reproducer at `-O0` | Compiles fine | ## Where it hangs (by optimization level) | Flag | Hangs? | GCC pass | |---|---|---| | `-O0` | No | n/a | | `-O1` | Yes | RTL pass: dfinit | | `-O2` | Yes | IPA pass: cp / GIMPLE pass: evrp | | `-O3` | Yes | GIMPLE pass: evrp | | `-Os` | Yes | various GIMPLE passes | ## This looks specific to cosmocc, not vanilla GCC We tested the same reproducer with stock GCC + libstdc++ across multiple versions and platforms. None of them hang: | GCC Version | Platform | stdlib | Hangs? | |---|---|---|---| | 14.1.0 (cosmocc 4.0.2) | Windows | LLVM libc++ | **Yes** | | 14.1.0 (stock, Docker) | Linux | libstdc++ | No | | 14.3.0 (stock, Docker) | Linux | libstdc++ | No | | 15.2.0 (stock, Docker) | Linux | libstdc++ | No | | 12.2.0 (MinGW-W64) | Windows | libstdc++ | No | The same GCC version (14.1.0) compiles this code fine when paired with libstdc++. It only hangs when paired with LLVM libc++, which is what cosmocc uses. So it looks like something in libc++'s `std::sort` or `std::string` implementation generates an intermediate representation that GCC's optimizer can't handle. ## Related The closest existing GCC bug is [Bug 115832](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115832) ("Very slow compilation with std::sort"), still UNCONFIRMED. That one is different though since it needs `-fsanitize=undefined` + `_GLIBCXX_DEBUG` to trigger, while ours triggers with just `-O1` on a plain struct. ## Workarounds - `cosmoc++ -mclang` switches to the bundled Clang 19 backend and compiles fine - Compiling at `-O0` works but loses optimizations ## Environment - cosmocc 4.0.2 (GCC 14.1.0) - Windows 10 x64 (MSYS2 / git-bash) - Both x86_64-linux-cosmo-g++ and aarch64-linux-cosmo-g++ hang - clang++ 21.1.5 compiles the same code in about 2 seconds Thank you very much for your time!
关闭于 2026-03-12 18 条评论