A thorough examination of the performance effects of using undefined behaviour in compiler optimizations.

Method:

  1. Modifying clang to not use UB where this is possible
  2. Run a large suite of benchmarks on different architectures, compare results for modified and unmodified clang
  3. Do statistics on the results
  4. Examine performance deviations
  5. Discuss factors which could bias results.

Very good science!

Result in short:

Only on ARM and if no link-time optimization is used, a systematic small positive performance effect can be seen. For Intel and AMD CPUs, there are no systematic improvements.

Average effects are typically below 2%, which is the typical effect of system and measurement noise. Often, effects are even negative. In some cases, benchmarks show large differences, and many of these can be fixed by simple modifications to the compiler or program.

    • nous@programming.dev
      link
      fedilink
      English
      arrow-up
      2
      ·
      8 hours ago

      In a way that seems to be what the paper is showing evidence for. Basically they looked at what happens if you disable the optimizations the LLVM compiler does when it knows something is undefined (and thus should not appear in a well written program). And they claim to have found minimal performance regressions of which can largely be mitigated in other ways.

      And that has been the biggest argument for having UB in C/C++ - to let the compilers optimize things in ways that you cannot do if everything was well defined. This might have been true in the past when we had a lot more variation in CPU designs but this paper seems to conclude that is no longer the case. Thus raises the question as to why do we need so much UB in C/C++ any more if performance is not a bit issue for modern programs using modern CPUs.