https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110228
Bug ID: 110228 Summary: [14 Regression] llvm-16 miscompilation on small case switch, minimized Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: slyfox at gcc dot gnu.org Target Milestone: --- I initially observed the failure as an llvm-16 (and older) single test failure when built by r14-1704-gff83d1b47aadcd : - TEST 'LLVM :: ExecutionEngine/JITLink/X86/MachO_weak_references.s' FAILED It is caused by ObjectLinkingLayerJITLinkContext::lookup() miscompilation. I extracted the following minimized example from it: #include <vector> using M = std::vector<unsigned>; static M s_lm{1}; __attribute__((noipa)) static const M & clm(void) { return s_lm; } __attribute__((noipa)) static void bug(unsigned * p) { const M & Symbols = clm(); for (unsigned v : Symbols) { int LookupFlags; switch (v) { // never used case 0: LookupFlags = 0; break; // always executed for a given input case 1: LookupFlags = 1; break; } *p = LookupFlags; } } __attribute__((noipa)) int main() { unsigned r = 42; bug(&r); if (r != 1) __builtin_trap(); } Triggering: $ g++ bug.cc -o bug -O1 -std=c++11 && ./bug Illegal instruction (core dumped) $ g++ bug.cc -o bug -O0 -std=c++11 && ./bug Note: the program traverses vector of 1 element of value 1. Somehow on -O1 it manages to miss `LookupFlags = 1;` store and does something else. $ g++ -v Using built-in specs. COLLECT_GCC=/<<NIX>>/gcc-14.0.0/bin/g++ COLLECT_LTO_WRAPPER=/<<NIX>>/gcc-14.0.0/libexec/gcc/x86_64-unknown-linux-gnu/14.0.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: --with-gmp-include=/0wz52f16y71ywjvc2rs9r7lw2axyl3fw3jma12azg88jia6k1srk/include --with-gmp-lib=/1ri6zpg87f38gjqjg3hxxfwg9i4fk3hwr1d4c8saiic61pkmzvza/lib --with-mpfr-include=/18rzsbmvdk9f7qcy0iynsiabalafwfhjjv9i38q7h5ckvz3z86hz/include --with-mpfr-lib=/1dza8r46l2racn05l78c9iy3ibj1mp02bn45cwrfrg2rxqf05icg/lib --with-mpc=/1pg6d47qa8b4l9mq51cvzkkd1jjjbqbvmlm6c9cyzw3kmsf4a10i --with-native-system-header-dir=/16q46gpr64lcgbhkdnyigap3mc07g762vn2ckl6zqa12c1ww1kmp/include --with-build-sysroot=/ --program-prefix= --enable-lto --disable-libstdcxx-pch --without-included-gettext --with-system-zlib --enable-checking=release --enable-static --enable-languages=c,c++ --disable-multilib --enable-plugin --disable-libcc1 --with-isl=/16g549izckw0akjcgs6x4rps7swlqi0ff8i4xfs4cj36l7kdilpv --disable-bootstrap --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=x86_64-unknown-linux-gnu Thread model: posix Supported LTO compression algorithms: zlib gcc version 14.0.0 99999999 (experimental) (GCC)