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.

Weird errors on code generation with -o2 optimization flag



Hi,

I am using the TI compiler version 7.4.12 with the following flags set (I removed the include file paths):

-mv6740 --abi=coffabi -O1 -ms1 -g --include_path="***" --float_operations_allowed=all --gcc --define="_DEBUG" --define=omapl137 --display_error_number --issue_remarks --diag_wrap=off --remove_hooks_when_inlining --auto_inline=64 --c_extension=.C


I am compiling a project consisting of several C modules, and I got some problems with a relatively large C module (about 2'700 lines of code).

If I enable the optimisation flag -O2, the compiler generates the wrong code, e.g. in this code snippet:

        
        static unsigned int uiIdxPrec;
        // the variable uiIdx comes from the function parameters 
        ...       
        if (ui_calculating_PLC_move)
        {
            sppc.pPrecMovWait = p_get_PLC_move_queue_scratchpad();
        }
        else
        {
            uiIdxPrec = uiIdx;
            sppc.pPrecMovWait = pGetPPrecMW(&uiIdxPrec);
        }
        {
            static int idx_axis_preset;
            for (idx_axis_preset = 0; idx_axis_preset < defMaxAxis; idx_axis_preset++)
            {
            	static volatile double my_dbl_aux;
            	my_dbl_aux = dbl_get_actual_position(idx_axis_preset);
            	sppc.pPrecMovWait->fPosIdealeFinale[idx_axis_preset] = my_dbl_aux;
            	my_dbl_aux = 0;
            	sppc.pPrecMovWait->fVelIdealeFinale[idx_axis_preset] = my_dbl_aux;
            }
        }

the sppc.pPrecMovWait gets the wrong value, e.g. NULL and the sppc.pPrecMovWait->fPosIdealeFinale[] values are wrong.

BTW, even the uiIdxPrec=uiIdx; is wrong, i.e. the  uiIdxPrec doesn't get the correct value

I check the called function and they are correct, but the register the calling routine looks for the result isn't the one used by the called one (eg A0 vs B0) so the code is messed up.

If I set the optimisation flag back to -O1, the code works just fine.

I have the same behaviour on small and big functions in this module.

This modules' code is located in external SDRAM while the called routines with the buggy behaviour are located in both external or internal RAM, this doesn't seem important.

The only difference I found  from my standard C modules is that this one contains a function with approx 1'500 lines of code, because of a wide switch(), while normally the function are relatively small.

The same C module works OK when compiled for an old TMS320C32 platform, which is where it has been taken from.

Any suggestions?

  • The C6x returns values in register A4. At -O1, you don't get function inlining, so you should see the return value in A4. At -O2 or higher, you do get inlining. Perhaps one or the other of the functions p_get_PLC_move_queue_scratchpad or pGetPPrecMW was inlined at -O2? This might explain why you are seeing the return value in a different register. If this is the case, we'd need to see a lot more of the program, particularly the inlined functions. Even if not, it would be better to see a complete, compilable test case that demonstrates the problem. Please run the C preprocessor on that source file (compiler option --preproc_with_comment) and post that.
  • Many thanks Archaeologist for your fast reply!

    Today I am out of office, I hope in the next days to get some standalone example I can forward you: I wrote the email in a hurry so sorry if it seemed a little bit frantic!

    About the -O2/-O1 mixup... right now, the working code has -O1 set just only in the very big module, while all of the others are set to -O2.

    If I set -O1 an all of the modules including the big one, the code would crash.

    But again, better to get a small reproducible case before!

  • Michele Sponchiado said:
    If I set -O1 an all of the modules including the big one, the code would crash.

    Frankly, that's a red flag.  This may be a symptom of a bug in one of the other modules.  Does the program crash with optimization turned off?

  • Oops sorry man, what I really meant was: "If I set -O2 an all of the modules including the big one, the code would crash."