Nowadays compilers are extremely capable of elimiting 'dead' code. But in benchmarking we need some seemingly 'useless' code to do the work we want. For example, to do computation in registers in order to measure instruction latency and throughput. In these cases, we need a mechanism to temporarily disable optimization.
This document keep an up-to-date collections of techniques to temporarily disable compiler optimizations. These techniques are tested on a recent gcc or icc compiler.
Using volatile
volatile
makes compilers be extremely careful (or conservative) on memory
load and store of a variable. Most compilers tend to do no optimizations at
all (even for proven dead code). So the volatile
technique becomes the top 1
used.