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/EK-TM4C129EXL: ARM v16.9.2.LTS compiler at optimization level 2 unexpectedly eliminates function where only side-effect is use of the RTS assert macro

Part Number: EK-TM4C129EXL

Tool/software: TI C/C++ Compiler

The following function was written to verify that a fixed test-pattern stored in flash could be read correctly:

static void verify_flash_contant_data (void)
{
    uint32_t seed = FLASH_DATA_INITIAL_SEED;
    uint32_t word_index;

    for (word_index = 0; word_index < FLASH_DATA_NUM_WORDS; word_index++)
    {
        assert (flash_constant_data[word_index] == seed);
        seed = RAND_GENERATOR (seed);
    }
}

The flash_constant_data array is declared to the compiler as a constant array:

#define FLASH_DATA_NUM_WORDS 235929
extern const uint32_t flash_constant_data[FLASH_DATA_NUM_WORDS];

Thus, the only side effect of the function is the use of the assert macro from the RTS assert.h include file to abort the program if the data read from flash_constant_data isn't the expected test pattern.

When the program was compiled with optimization level two the optimizer has eliminated the entire verify_flash_contant_data function. Looking at the RTS assert.h, the assert macro is implemented using the _assert builtin. Is the problem that at optimization level two the optimizer incorrectly eliminates functions for which the only side effect is use of the _assert builtin?

I made the compiler keep the generated assembly file, and confirmed that when compiled at optimization level two other functions which did use assert were not eliminated, and a call to _abort_msg() is made if the assertion fails.

I have attached a CCS 7.1 project which contains the code which demonstrates the problem.

The Debug configuration has the optimization level set to "off", and the flash_constant_data function is shown in the generated assembly file Debug\TM4C129_self_test.asm

The Release configuration has the optimization level set to "2 - Global Optimizations", and the flash_constant_data function is NOT shown in the Release\TM4C129_self_test.asm

2185.TM4C129_selftest.zip