I'm encountering a bug with Xcode 16's clang's code generation. It seems to be generating incorrect optimized code (-O2
setting) when functions are inlined and then optimized. This is a behavior that didn't exist in Xcode 15, and also doesn't happen in open source clang (I tested open source clang 17/18/19 on my M1 Max MacBook Pro).
The entire code snippet is slightly too long to post here so I'm including a link to the godbolt compiler explorer instead: https://godbolt.org/z/KhG3x7E1d . This piece of code attempts to find a sequence of illegal UTF-8 characters and report the index of the character in a string.
Not that in godbolt, the program works fine and finishes correctly. When I test it in Xcode 16 though (with -O2
), it doesn't, and utf_find_illegal
returns 4 instead of 3, which is incorrect. Digging through the disassembly it seems to be doing some complicated optimizations by inlining both utf_ptr2len
and utf_ptr2char
together but it doesn't perform correctly and jumped to the wrong place when the illegal character was found.
I did try to see if there are some undefined behaviors or something which caused the optimizer to go to town with the code. Funnily when I use UBSAN (by compiling the code with -O2 -fsanitize=undefined
) the code works just fine and the bug doesn't happen.
Wonder if other people have seen similar issues? Code generation bugs seem really dangerous considering that people rely on the compiler to… work. I tried to scrub the code to see if there are anything that could suggest the compiler to behave incorrectly but not having any luck as well.
I have also tested Xcode 16.1 beta and it doesn't seem to help.
Note: Yes, I know I'm supposed to use the Feedback Assistant but I have never received any responses on it even when filing legit bugs and their statuses are still open, with their bugs unfixed. Pardon me for not trusting it too much.