Daniel P. Berrangé berrange@redhat.com writes:
Alternatively adding __attribute__((noinline)) to "time_function" fixes it - nb noinline on "bench_nothing" does NOT fix it.
I've now deleted the problematic code.
I would guess the reason the compiler thinks it can optimize away the entire loop in time_function is that after inlining, it finds that the loop body, f(arg), has no side effects. It will hopefully not do the same with any other functions in the benchmark.
Alternatively adding assert(ncalls != 0); in the loop in time_function fixes it, because causing GCC to stop inlining it. That's largely luck, but it probably makes sense to have that assert() added regardless as this loop is inherantly susceptible to this wraparound problem as written.
It would be more robust to check if ncalls is about to overflow, and in that case let time_function return 0.0. I'll consider that if any similar problems reappear.
Regards, /Niels