- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The usleep function should do what you need.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
AFAIK, there's an usleep function in the HAL.
Check the HAL API Reference.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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");- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page