Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12590 Discussions

Eclipse EDS optimizer ommitted delay loop?

Altera_Forum
Honored Contributor II
1,127 Views

Hello! 

 

I was having this code for a simple LED blinking Nios II application based on a counter and some delays: 

 

# include "sys/alt_stdio.h"# include "system.h"# include "altera_avalon_pio_regs.h" int main() { int count = 140; int delay1 = 0; int delay2 = 0; int delay3 = 0; int delay4 = 0; /* Event loop never exits. */ while (1) { IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, count&0x02); delay1 = 0; while(delay1 < 900000) { delay2=0; while(delay2 < 900000) { delay3=0; while(delay3 < 900000) { delay4=0; while(delay4 < 900000) { delay4++; } delay3++; } delay2++; } delay1++; } //alt_putstr("DELAY3\n"); count++; } return 0; }  

 

The optimizer was set at level 2 and I was trying to run the program but for some reason I had no delay!! The LEDs were constantly ON. Then I tried to debug the code using a debug configuration in Eclipse and I saw only the count variable but no delay variables. Then I saw a remark somewhere "Optimized out"! When I switched the optimizer option to Off. I finally could see the difference. 

 

My question is how can the optimizer change the functionality of the code like this? Doesn't the program become unreliable after this? 

 

Regards
0 Kudos
8 Replies
Altera_Forum
Honored Contributor II
332 Views

Check this out..  

 

http://stackoverflow.com/questions/7083482/how-to-prevent-compiler-optimization-on-a-small-piece-of-code 

They offer a work around, but in general it's better to use an os sleep style command that's based on a hardware timer. (and frees up other processes to run). 

 

The optimizer is doing what it's suppose to, optimizing the code. As far as it can tell, there's no need to do the loops, so it removes them.  

 

Pete
0 Kudos
Altera_Forum
Honored Contributor II
332 Views

Thats interesting... 

 

I never thought that the optimizer would change the logic in order to optimize results. I would only expect a warning at compilation time. 

 

Anyway, I will go for the timer. How do I insert it on my Qsys system and how do I use it? Is it based on interrupts?
0 Kudos
Altera_Forum
Honored Contributor II
332 Views

The usleep function should do what you need.

0 Kudos
Altera_Forum
Honored Contributor II
332 Views

Is this based on an operating system or is it part of the HAL. I am not using any operating system. Isn't there a timer IP for Qsys?

0 Kudos
Altera_Forum
Honored Contributor II
332 Views

AFAIK, there's an usleep function in the HAL. 

Check the HAL API Reference.
0 Kudos
Altera_Forum
Honored Contributor II
332 Views

Yes I have already found that and used it. 

 

Does it make any difference with the solution to use a timer block inside the Qsys system? 

 

Also I have not found a tutorial or example that uses an interrupt. 

 

Where can I find such an example? I imagine that the timer uses interrupts to interact with the processor.
0 Kudos
Altera_Forum
Honored Contributor II
332 Views

The debugger reporting a variable as 'optimised out' just means that that the variable is never assigned to a physical memory location, so it could still exist but always be assigned to a register. 

 

The compiler is allowed to remove any calculations where the resultant value isn't used - in this case that collapses back to your entire delay loops being removed. 

 

You could just try declaring the variables as 'volatile int delay1;' that might be enough to stop the loops being (validly) optimised away. 

 

Or add something to the inner loop that the compiler cannot remove. 

eg: define a program scope 'volatile int temp;' and write something to it. 

or: an asm statement that generates no object code: 

asm volatile ("\n");
0 Kudos
Altera_Forum
Honored Contributor II
332 Views

Thank you!

0 Kudos
Reply