Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

Eclipse EDS optimizer ommitted delay loop?

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

8 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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?

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

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

    Check the HAL API Reference.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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");