3

Is there a way to tell the Intel compiler to not optimize-away un-used variables? I am trying to time some code and I currently prevent the optimization by using a cout statement on the variables.

Ideally the solution would tell the compiler to not remove the variable via a pragma/hint, otherwise I would have to use a program-wise argument?

user997112
  • 29,025
  • 43
  • 182
  • 361
  • 1
    When I waste time doing micro-benchmarking, I find that `volatile` works sufficiently well. – Cody Gray - on strike May 13 '14 at 22:46
  • What value does a benchmark with unused variables have? Even if they are not optimized away, they'll just be some wasted space on the stack (which is free, it's just matter of moving ESP a bit further). Once you start *really* using these variables the compiler may change the register allocations and the likes, thus changing the program behavior. – Matteo Italia May 13 '14 at 22:50
  • @CodyGray who said I was micro-benchmarking? Pretty amazing remark considering I haven't shown you the size of the code.... – user997112 May 13 '14 at 22:50
  • 2
    First of all, by micro-benchmarking, I meant benchmarking code "out of context", so to speak. That's the only time it would matter if an unused variable was optimized out. If you were profiling an entire application, you'd want the optimizer to be fully engaged. Second, I did say when *I* did that. I didn't mean to imply anything about what you were doing. – Cody Gray - on strike May 13 '14 at 22:52
  • If the variables are unused, then there's no code that uses them. What are you timing? ... trying to keep a value available for a debugger? – jthill May 13 '14 at 22:54

1 Answers1

3

Use the volatile keyword when assigning your variable to let the compiler know not to optimize it. As far as I know, this is a C/C++ standard so it should work on any compiler. See the MSDN link for more info.

Elias
  • 1,367
  • 11
  • 25
  • Here is a link to a different thread covering this topic: [link](http://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword-in-c). From c++ standard ($7.1.5.1/8) `[..] volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation.[...]` – Elias May 13 '14 at 22:58
  • @MatteoItalia: Please see 1.9 Program Execution § 8: `The least requirements on a conforming implementation are: — Access to volatile objects are evaluated strictly according to the rules of the abstract machine.[...]` – Deduplicator May 13 '14 at 22:59
  • @Deduplicator: which means nothing in terms of optimization, since the as-if rule always holds. As far as the observable behavior is the same, the compiler is free to completely optimize away the variable. The part that is relevant to the optimization is the one quoted by Elias, which anyhow is just a hint (normally followed by the compilers, but not specified in detail since it's obviously very implementation-specific). – Matteo Italia May 13 '14 at 23:06
  • @MatteoItalia: That is part of the restrictions on the as-if rule. My quote goes on with: `These collectively are referred to as the observable behavior of the program. [ Note: More stringent correspondences between abstract and actual semantics may be defined by each implementation. —end note ]` – Deduplicator May 13 '14 at 23:07
  • 1
    Those parts of the standard are essentially meaningless since they're platform independent and there's no platform-independent notion of "access" or what it would mean to "observe" an access. I have yet to hear anyone coherently explain what "rules of the abstract machine" are either. – David Schwartz May 13 '14 at 23:29
  • Marking variables with volatile keyword has the disadvantage that it will have an impact on benchmark results. The compiler will apply less aggressive optimizations for variables marked as volatile. I usually summarize variables that shouldn't optimized away and printing them out or use them as return parameter. – Alexander Weggerle May 14 '14 at 11:43
  • This should not be enough to prevent optimizing-out an unused variable. volatile is meaningless if no reads and no writes are made. – einpoklum Sep 10 '16 at 18:39