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

bad loop optimization

Bert_Jonson
Beginner
517 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
517 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?
0 Kudos
Bert_Jonson
Beginner
517 Views

With any level of optimization.

0 Kudos
SergeyKostrov
Valued Contributor II
517 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?
0 Kudos
JenniferJ
Moderator
517 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)
    ;
}

0 Kudos
Bert_Jonson
Beginner
517 Views

Okay, i forgot about volatile, thanks.

0 Kudos
Reply