2

I would like to disable dead code elimination optimization in c++ compilation. Is there a way to disable this particular optimization by keeping all other -O optimization. I tried with -fnodce but its not working.

Update (copied from a comment): I have something like

timer t;
t.start();
for(int i=1;i<=1000;++i)
    object t;
t.stop();

I want to measure object t construction time and do nothing with it. I dont want to do this by creating an array of 1000 objects. Is there a way to solve this?

Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
user1167158
  • 31
  • 1
  • 2
  • 1
    That sounds like you split some problem in two parts, solved the easy one and are now struggling with the hard one. Why do you want to do such a thing? – Voo Jan 24 '12 at 14:19
  • I have something like timer t; t.start(); for(int i=1;i<=1000;++i) object t; t.stop(); I want to measure object t construction time and do nothing with it. I dont want to do this by creating an array of 1000 objects. Is there a way to solve this? – user1167158 Jan 24 '12 at 14:28
  • If you do measuring, do you need optimization at all? Or do you need the measuring even in the release version? – Some programmer dude Jan 24 '12 at 14:38
  • 1
    First of all your loop doesn't measure object creation time, but object creation + destruction time, so I'm not sure that's what you want. Second point: how is your measuring going to be correct if you disable certain optimizations (assuming you need the times for optimized builds)? Afterall it is quite possible that the compiler would do some dead code and dead store eliminations inside constructors/destructors (particulary if there is code in the constructor body instead of doing everything on initialization) – Grizzly Jan 24 '12 at 14:56
  • for my measurement i don't need other optimization, but i don't like to have -g option, wherein it adds debug symbols which i dont want. let me ask different question, is it possible to not have optimizations without any debug symbols by any compiler option? I am looking something in between -g and -O options, – user1167158 Jan 24 '12 at 14:57
  • 1) I dont have any specific destructor, I am ok to include destruction cost. 2) Ideally i want all other optimizations without deadcode elimination. this is just for test purpose. we will have proper builds in production. 3) i dont have anything in constructor body, everything is part of the initialization list. – user1167158 Jan 24 '12 at 15:00

2 Answers2

0

Add "volatile" qualifier on constructed objects, this tells the compiler to assume that there are side-effects to construction thus preventing optimizing it away. That is:

timer t; 
t.start(); 
for(int i=1;i<=1000;++i) 
  volatile object t; 
t.stop(); 
bronekk
  • 2,051
  • 1
  • 16
  • 18
  • That will most certainly disable other possible optimizations by the compiler as well though. – Voo Jan 24 '12 at 15:31
  • Thanks for your suggestion. this is working. but do we have any additional cost with volatile except for the immediate flush to memory e.t.c – user1167158 Jan 24 '12 at 20:41
  • if you can afford to create dummy or change type of one of the fields in the class, you can make it volatile instead. One subobject will be enough to prevent compiler from optimizing away side effects of its construction. – bronekk Jan 24 '12 at 21:09
0

Well if you just want to measure the initialization time of your objects why try to coerce the compiler to avoid DCE and whatnot and not just write it in such a way as to avoid the problem in the first place?

object *arr = new object[100];   // allocate that outside the function and pass it into it
for (int i = 0; i < 100; i++) {
    arr[i] = new object;
}

If the function is large enough to avoid inlining that should do the trick just fine - otherwise you can export the function and call it from another compilation module to avoid unwanted optimizations. Simple, no tricks with some compiler flags that may have unintended consequences and the only overhead is an array store - if THAT measurably influences your timing you're measuring the wrong things anyhow.

Or if you really want some compiler specific flags - gcc has a noinline attribute..

Voo
  • 29,040
  • 11
  • 82
  • 156
  • thanks for your response. I thought of testing stack allocation of the object but above one uses heap. Actually constructor is declared in header file and I don't have privileges to change the header file. Anyhow the volatile stuff is working fine. I appreciate your help – user1167158 Jan 24 '12 at 20:44