-
Notifications
You must be signed in to change notification settings - Fork 13.4k
clang-12 strangely optimize a loop into recursive call to main function #54528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
That loop is an infinite loop with no side-effects, and it's condition is not one of those that are explicitly allowed |
Thanks for response, UB is first part in my posted issue (I have mentioned it), the second part is that, when compile it with -O2, the gdb will fail to locate main function, this may trouble programmers when debugging it. |
gdb is in fact finding the main function; otherwise you wouldn't be able to set a breakpoint. It's just zero bytes long. It might be worth taking a slight codesize penalty to make what's happening here more clear, though. Feel free to open a new bug with a clean description for that. |
Very thanks for your explanation, it solved my confusion. |
I guess the zero-length function issue is #32380. |
the code:
the above code has an infinite loop in function test, however when compile it with
clang-12 -O1
, the produced executable will immediately segfault.I disassembled the a.out and find that:
clang-12 optimize the above code into a strange recursive call of main function and finally cause stack overflow.
I know that unterminated loop is undefined behavior, theoretically compiler can do anything, but such optimization is still counterintuitive.
Not only this, while the loop condition
counter < 1
is changed into1
, this two versions should be identical, but clang-12 reasonably optimize it into infinite loop.A much more strange case is that, when enable
-O2
for clang-12, clang-12 will directly optimize out the whole main function, this cause gdb failed to break on main function:The text was updated successfully, but these errors were encountered: