Search code examples
gccoptimizationconstantsclangpure-function

GCC and Clang: Turn off pure optimizations


I'm running a set of benchmarks comparing different libc string functions. The problem is that GCC and Clang are optimizing out the computations in the loops because the functions are marked "pure" and "const". Is there some way to either turn off that optimization or get around it?


Solution

  • I solved it! The solution was nasty, but it works:

    volatile int x;
    for (...)
    {
        // ...
        x = (int)f(args);
    }
    

    I never use the value of x, so the cast won't be a problem. Better yet, now I don't get errors about not using return value of function declared with pure attribute.