Skip to content

clang >=13.0.0 seems provide invalid optimize for C++, empty while(true) loop and -O1 #97211

Open
@yyjdelete

Description

@yyjdelete

I notice this when try to convert an do-while loop in my code to an for loop, but forget to remove the while(true);

The code looks like the below, compile it as C++ with -O1 or -O3, it should output aaa for 100 times and do a busy loop forever, but infact, it repeat output aaa forever(looks like an do-while loop outside the for loop)
Tested with clang-18 in Ubuntu 24.04 and godbolt.org

#include <stdio.h>
#include <stdbool.h>
 
int main() {
    for (int i = 0; i < 100; ++i)
    {
        printf("aaa\n");
    }
    while(true);
  return 0;
}

https://godbolt.org/z/r5W1vf6Pc

It works with -O0
It works when compile the code as C code instead of C++
It also works with gcc/g++
According to godbolt.org, it used to works with clang <=12.0.1, and not work for >=13.0.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions