https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99101
Bug ID: 99101 Summary: optimization bug with -ffinite-loops Product: gcc Version: og10 (devel/omp/gcc-10) Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: 251078896 at qq dot com Target Milestone: --- we have a reduced code to show this bug: #include <iostream> class CompactMD { public: void operator()() { while (true) { auto num_job = jobs_; for (auto i = 0; i < jobs_; i++) SendNextMd(); jobs_ -= num_job; } } void SendNextMd() { if (at_eof_) return; if (read_finish_) { at_eof_ = true; std::cout << 1 << std::endl; } } bool read_finish_ = true; int jobs_{1}; bool at_eof_{false}; }; int main() { CompactMD comp_md_; comp_md_(); return 0; } The expected output is only one line of "1", after that the program should be in a "while true" loop forever. With O2 optimization level, the output are infinite lines of "1"s. We can reproduce this in RedHat's devtoolset-10, and Ubuntu 20.10's bundled gcc10. After some tests, I find this bug is related to -ffinite-loops This bug cannot be reproduced with gcc9 or lower version. btw, I cannot further simplify this code for now, any change to it the bug is likely to be gone.