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.

Compiler/TMS320C6748: Data memory corruption when activating optimization level 2 (-O2)

Part Number: TMS320C6748

Tool/software: TI C/C++ Compiler

I am currently optimizing a software that is apparently executing fine when no optimization is activated. As soon as I activate the -O2 optimization, I can observe memory corruption at execution.

Here is a piece of code that reproduces the problem:

Data declaration (global to a file)

INT16 const TAB_BEEP_HI_1[AUDIO_BUFFER_SIZE] = // 1KHz tone
{
0, 3212, 6393, 9512, 12539, 15446, 18204, 20787, 23170, 25329, 27245, 28898,
30273, 31356, 32137, 32609, 32767, 32609, 32137, 31356, 30273, 28898, 27245,
25329, 23170, 20787, 18204, 15446, 12539, 9512, 6393, 3212, 0, -3212, -6392,
-9512, -12539, -15446, -18204, -20787, -23170, -25329, -27245, -28898, -30273,
-31356, -32137, -32609, -32767, -32609, -32137, -31356, -30273, -28898, -27245,
-25329, -23170, -20787, -18205, -15446, -12540, -9512, -6393, -3212, 0, 3212,
6392, 9512, 12539, 15446, 18204, 20787, 23170, 25329, 27245, 28898, 30273,
31356, 32137, 32609, 32767, 32609, 32137, 31356, 30273, 28898, 27245, 25329,
23170, 20787, 18205, 15446, 12540, 9512, 6393, 3212
};

Loop (located in a function of the same file) that reproduces the issue:

#pragma MUST_ITERATE(AUDIO_BUFFER_SIZE,AUDIO_BUFFER_SIZE);
for(i = 0; i < AUDIO_BUFFER_SIZE; i++)
{
    beep_buff1[i] = (UINT16)(TAB_BEEP_HI_1[i] * l_volume) + DC_OFFSET;
    beep_buff2[i] = (UINT16)(TAB_BEEP_HI_2[i] * l_volume) + DC_OFFSET;
    beep_buff3[i] = (UINT16)(TAB_BEEP_HI_1[i] * l_volume) + DC_OFFSET;
    beep_buff4[i] = (UINT16)(TAB_BEEP_HI_2[i] * l_volume) + DC_OFFSET;
}

Despite the loop only reads the constant table, its execution results in writing some random values inside it. Note that I have similar issue on other pieces of code.

That's why I am wondering if I misuse the optimization command or if the compiler is behaving wrong. Is it a known issue on TI side?

I am using the Code Composer C6000 compiler V8.3.7 and running the software on a C6748 DSP.

Thanks in advance for your support.

  • Some background in the topic of software pipelined loops is needed.  Please read through the first few chapters of the application note Hand-Tuning Loops and Control Code on the TMS320C6000.  Focus on the first part of the chapter titled Tuning Software-Pipelined Loops.

    Emmanuel Caestecker said:
    As soon as I activate the -O2 optimization, I can observe memory corruption at execution.

    It is likely that there is a problem present in your code all the time, but this problem is only uncovered when you increase optimization.  In your case, using no optimization means no loops are software-pipelined.  Changing to optimization level 2 means all the loops are subject to being software-pipelined.  This often means a significant difference in the order in which instructions execute.  

    The C6000 compiler has software-pipelined loops like this one ...

    Emmanuel Caestecker said:
    #pragma MUST_ITERATE(AUDIO_BUFFER_SIZE,AUDIO_BUFFER_SIZE);
    for(i = 0; i < AUDIO_BUFFER_SIZE; i++)
    {
        beep_buff1[i] = (UINT16)(TAB_BEEP_HI_1[i] * l_volume) + DC_OFFSET;
        beep_buff2[i] = (UINT16)(TAB_BEEP_HI_2[i] * l_volume) + DC_OFFSET;
        beep_buff3[i] = (UINT16)(TAB_BEEP_HI_1[i] * l_volume) + DC_OFFSET;
        beep_buff4[i] = (UINT16)(TAB_BEEP_HI_2[i] * l_volume) + DC_OFFSET;
    }

    ... for a very long time.  While anything is possible, it is very unlikely the code generated by the compiler contains a write to the array TAB_BEEP_HI_1.  All the same, you are welcome to send me a test case, and I will check.  To send a test case, for the source file which contains this loop, please follow the directions in the article How to Submit a Compiler Test Case.

    Thanks and regards,

    -George