Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

short program which only works correctly when compiled in debug mode

ZES
Beginner
308 Views
Hi everyone,
we are using the Intel compiler v11 for Linux (11.069 and 11.074) and for Windows (11.061).

Compiling the following short program with
> icc -O0 main.cc
the executable a.out runs in an infinitely loop as expected.
Compiling this programm with
> icc -O1 main.cc
the executable a.out aborts.

This effect appears with all the Intel compilers mentioned above, but does not appear if we use gcc.

[cpp]#include 

int main(void)
{
   int fb[1];
   int z;
   fb[0]=42;
  
   z=*((fb+0)+0); //  This line is essential for the effect.
                  //  If you use  z=*(fb+(0+0)) instead,
                  //  the effect vanishes. 
                 
   std::cout << "The value of fb[0] is " << fb[0] << ".n";
   std::cout << "Lets start the while loop.n" ;

   while( fb[0]==42 )
   {
   }; 
   std::cout << "Running out of the while loop.n";
}[/cpp]

0 Kudos
6 Replies
JenniferJ
Moderator
308 Views
I've filed a bug report on this, the # is "200112239".

Thanks for the posting.

Jennifer
0 Kudos
JenniferJ
Moderator
308 Views
I got the answer from the compiler engineer. The reason the loop gets eliminated is because there's no dependencybetween the loop and the code after. So the loop is considered dead code and is removed.

If you'd like to keep it, you should use needs to define a volatile variable & spin on it.

But the question is why using "z=*(fb+(0+0));" keeps the loop. I don't know but will find out.
0 Kudos
levicki
Valued Contributor I
308 Views
I got the answer from the compiler engineer. The reason the loop gets eliminated is because there's no dependencybetween the loop and the code after. So the loop is considered dead code and is removed.

If you'd like to keep it, you should use needs to define a volatile variable & spin on it.

But the question is why using "z=*(fb+(0+0));" keeps the loop. I don't know but will find out.

Jen, there may be something more to it. I tested with latest beta and changing to z = *(fb + (0 + 0)) didn't stop the compiler from optimizing it out -- on the contrary, it completely removed the code and left only the push 42 instruction before the call std::cout instruction.

0 Kudos
ZES
Beginner
308 Views
I got the answer from the compiler engineer. The reason the loop gets eliminated is because there's no dependencybetween the loop and the code after. So the loop is considered dead code and is removed.

If you'd like to keep it, you should use needs to define a volatile variable & spin on it.

But the question is why using "z=*(fb+(0+0));" keeps the loop. I don't know but will find out.

Hello Jennifer,
in principle I understand the argumentation of the compiler engineer.
But if I write while(1) there is also no dependency between the loop and
the code after, but the loop is not considered dead code in this case and it is not
removed!
That does not sound logical to me. What is the difference (for the
compiler) between the while-loop in our program and while(1)?

0 Kudos
levicki
Valued Contributor I
308 Views

That does not sound logical to me. What is the difference (for the compiler) between the while-loop in our program and while(1)?


Well the difference should be obvious -- 1 is a compile-time constant while (fb[0] == 42) doesn't have to be. It is a reference to a memory location which can change from the "outside". Since the compiler can determine that it doesn't change (i.e. you haven't specified it as a volatile storage), and the value is known to the compiler at compile-time the compiler eliminates the loop.

I am really not sure what to think of it though -- I would personally expect it to keep the loop as well, and optimize it out only if it said while (fb[0] != 42). I believe it is a bug.

0 Kudos
JenniferJ
Moderator
308 Views
The"z=..." should not affect the "while()" loop being optimized away. This is fixed in our new compiler (next release). If you'd like to keep the "while()" loop, please include a "volatile" variable local or in the loop.


Jennifer

0 Kudos
Reply