This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

-o3 compiler option gives wrong output

Hi,

When I use -o3/-o2 option during compilation, I get wrong output. If I do not use any optimization provide by compiler (no -o option) i get correct output. Problem is I need  -o3option as I need to enable software pipelining and optimization. I am working on C6000 floating point processor. Any suggestions to overcome the problem?

  • Abhishek,

     

    The compiler performs various optimization steps when you enable -o option. See: http://focus.ti.com/lit/ug/spru187o/spru187o.pdf chapter 3 for details. I'll list couple common coding issues that can cause -o3 generated code to not give correct output

    • Have a global variable that is modified outside the scope of the file. Define the variable with volatile keyword
    • Make sure dependent memory pointers are not declared as 'restrict/const'. This can cause compiler to assume no dependency between pointers and produce incorrect result

     

    I don't know how big your application is, but below is what you can try:

    • Build the entire pjt with no -o option. Ensure all is working well
    • Selectively enable -o option for individual file. You can do it using CCS Pjt option of adding file specific compiler options
    • If you are lucky, you will identify the one file that has issue
    • Split the file and try and come to the one loop that has the issue (if you are lucky!)
    • Review loop carefully for logic that can go wrong due to assumptions made by compiler when you enable -o option.

     

    Gagan

  • I cant get this "You can do it using CCS Pjt option of adding file specific compiler options ". Can you explain a bit more about enabling -o3 option for one file at a time. Can you post ur e-mail id too?

  • Thanks a lot for the suggestion Gagan!

    I ran into a similar issue and I fixed mine by selective optimization of the files and identifying the file which needed volatile variables. 

    Thanks,

    Arun

  • I use the C6748,CCS5.3,my software is below ,when I use the -o2 or -O3 ,the calculate result c1 is wrong while I use the -o1 the result is right ,is there any wrong in my software ? How can I deal with?

    //****************************************************************************

     float DSPF_dp_sp_dotprod( float * x,   float * y,  int nx)
    {
         int i;
         float sum = 0;

        _nassert(nx >= 0);
        _nassert(nx % 4 == 0);

        for(i=0;i<nx;i++)
        {
         sum += x[i]*y[i];
        }

        return (sum);
    }

    float a[600],b[600];
    volatile float c1=0,c2=0,yt1,yt2,yt;

    void main()
    {
     int i;
     for(i=0;i<600;i++)
     {
      a[i]=1.0/(i+1.0);
      b[i]=i*2.1;
     }

     c1=DSPF_dp_sp_dot(a,b,600);
    }

  • I tried your test code. I get 1245.352417 for O=2 and  1245.352539 for O=3. Moving either the array init code or the DSPF_dp_sp_dotprod() to another file seems to fix it. Or making arrays a and b volatile. I would guess it is a bug with file level optimizations. You should submit a complete test case to the "Development Tools"->"TI C/C++ Compiler " forum.