In mcuXpresso SDK 2.10 for the i.MX-RT 1064, the CMSIS file core_cm7.h, causes an issue with optimization level. This is testable in mcuXpresso IDE 11.4.
To test, simply create a hello_world project with SDK 2.10 in IDE 11.4. Set the project optimization level to "O0".
In main(), define a local variable but do not reference it anywhere (i.e. int test_o_level = 1;) In Og this variable will be optimized out, in O0 it will not be. The variable is always optimized out even when the project setting is at O0.
This because of an issue core_cm7.h. Around line 2320, the SCB_ cache functions are defined, which are surrounded by GCC pragmas to temporarily change the optimization level to "Og".
/*
* Optimize the Data Cache functions, for the endless loop issue.
* More details, see https://github.com/ARM-software/CMSIS_5/issues/620
*/
#if (defined(__GNUC__) && !defined(__OPTIMIZE__))
#pragma GCC push_options
#pragma GCC optimize ("Og")
#endif
... CACHE FUNCTIONS HERE ...
#if (defined(__GNUC__) && !defined(__OPTIMIZE__))
#pragma GCC pop_options
#endif
This looks like 100% valid GCC pragmas to set and then restore the optimization level to me. Except, it doesn't actually work. (FYI - that github link relates to an issue that required the Og level in the first place - but that is ostensibly a different bug.)
I prefer to debug with "O0" level. For some reason, the "Og" level set by core_cm7.h is not properly popped, and the "O0" level set via the command line is not restored. This effectively forces "Og" on my whole project.
Adding this to core_cm7.h (after #pragma GCC pop_options) works to properly restore the level:
#pragma GCC reset_options
I found no other way to fix it. This looks like a GCC bug. However, since this impacts the SDK I think you should provide a workaround in the SDK includes.
Solved! Go to Solution.
Hello steve_n,
I will escalate this to the SDK team, thank you for pointing out this issue!
Regards,
Gustavo
Would someone from NXP care to comment on this?
The file core_cm7.h breaks the optimization level setting - will this be addressed in future releases?
Hello steve_n,
I will escalate this to the SDK team, thank you for pointing out this issue!
Regards,
Gustavo
Thank you!
Of course I figure this out right after my initial post...
The issue is the !defined(__OPTIMIZE__) qualifier of the #if.
The first set of pragmas that pushes on a new set of optimization options works. In doing so, it defines __OPTIMIZE__ as part of the process!
Therefore, the second half never gets run, to pop off the "Og" option.
#if (defined(__GNUC__) && !defined(__OPTIMIZE__))
#pragma GCC pop_options
#endi
Once "Og" is set, __OPTIMIZE__ is defined.
So, this is not a GCC bug, it is a CMSIS issue.
EDIT: From what I can tell, the extra pragmas were added by NXP and they are not a part of the native CMSIS.