On Wed, Oct 11, 2017 at 11:54 PM, Gabriele Svelto <gsve...@mozilla.com> wrote: > On 09/10/2017 13:47, Henri Sivonen wrote: >> I omitted volatile, because the GCC manual said it has no effect on >> basic asm. However, it turns out it still has an effect on extended >> asm, which is what this is. Oops. > > Yes, volatile implies the statement has side effects and so it cannot be > moved around or eliminated (even though GCC might prove that it is > indeed useless). So in your example https://godbolt.org/g/iTBXYW > you're telling GCC you're doing something with the contents of ptr, > which indeed forces it to emit separate adds for every iteration. In the > case with volatile https://godbolt.org/g/35xcCL you're telling it you're > doing something with ptr and you're doing something else it doesn't know > about but which may touch other variables or memory locations. The > latter is a pretty big hammer, it works like a barrier in the code so if > GCC has promoted some stuff from memory to registers it will be forced > to spill them and reload them, statements will not be moved around it, > etc... If the only thing that interests you is pretending that the > output of the function is being consumed then the former non-volatile > option is preferable as it will not have such a negative impact on code > generation.
I think it's of interest to pretend that all memory has been touched so that the compiler isn't allowed to assume that the input function being benchmarked hasn't changed between iterations (even though it really hasn't). This is now available as T* mozilla::BlackBox(T*) in https://searchfox.org/mozilla-central/source/testing/gtest/benchmark/BlackBox.h . That is, the function returns the T* that it took as argument. -- Henri Sivonen hsivo...@hsivonen.fi https://hsivonen.fi/ _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform