Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.

bad loop optimization

Bert_Jonson
Beginner
266 Views

[cpp]

void foo(int a) {
    while(a);
}

int main() {
    foo(1);
}[/cpp]

Compiler removes empty loop and there is no endless loop that i expect. MSVC and GCC works correctly.

Windows, ICC 13.1.

0 Kudos
5 Replies
SergeyKostrov
Valued Contributor II
266 Views
With Intel C++ compiler this is a well know issue but it is Not a problem. What optimization options did you use, /O1, /O2 or /O3?
Bert_Jonson
Beginner
266 Views

With any level of optimization.

SergeyKostrov
Valued Contributor II
266 Views
>>...Compiler removes empty loop and there is no endless loop that i expect... Do you need to use a similar processing in a real application?
JenniferJ
Moderator
266 Views

Adding "#pragma intel optimization_level 0" before the function "foo()" will keep the loop. 

Or introduce a volatile variable like below: 

//#pragma intel optimization_level 0
void foo(int a) {
    volatile int b=a;
    while(a)
    ;
}

Bert_Jonson
Beginner
266 Views

Okay, i forgot about volatile, thanks.

Reply