Hi ,
Here we using LPC55s16 for the project. After completion of 80% memory, tried to optimize the project with optimize most (-O3) and by optimize for size (-Os). After the optimization of the project, while executing the code half of the project get collapsed and not works well as like before.
We observed that some global variables, which are works well without optimization and after optimization those variable not get updated in case using in multiple files. But if we use the variable with the declaration of volatile its words well.
Mainly while optimization, project size get reduced by the 20-25%. So we have reduce the size. What actually happens, while optimization of the project. Kindly resolve this issue.
Hi @Dhaya
This table shows the optimization strategies corresponding to different optimization levels.
Optimization level | Strategy Explanation |
-O0 |
No optimization is performed (if no optimization level is specified, it is set by default). |
-O1 | On the premise of not affecting compilation speed, try to use some optimization algorithms to reduce code size and improve the running speed of executable code. -This level has executed 45 strategy sub items. |
-O2 | Sacrifice some compilation speed and adopt optimization algorithms supported by almost all target configurations to improve the running speed of the target code. -This level adds 48 strategy sub items above all optimization strategy sub items in - O1. |
-O3 | Adopting many vectorization algorithms to improve the parallel execution level of code, such as utilizing pipelines, cache, etc. in modern CPUs, the goal is to increase the size of the target code rather than desperately improve the running speed. -This level adds 16 strategy sub items above all optimization strategy sub items in - O2. |
-Os | There is a similarity between - O3 and - O3, but their goals are different. This level is to minimize the size of the target code, which is very important for devices with small storage capacity. -This level subtracted 6 strategy sub items from all optimization strategy sub items of - O2, and then enabled the - inline functions strategy. |
-Og |
Provide reasonable optimization levels while maintaining fast compilation and a good debugging experience. |
BR
Hang
Hi @Harry_Zhang
Thanks for the detailed info. Is any changes in the functionality in the program during different optimizations.
Note : This doubt raised because of that volatile and non-volatile variables functioning differently during non-optimized and optimized projects
Hi @Dhaya
In theory, functionality will not be affected by optimization. As you said, the functionality has been affected due to the possibility of variables in the function being optimized.
BR
Hang
I think you have a bigger problem, called 'reentrancy'. See https://en.wikipedia.org/wiki/Reentrancy_(computing) or other articles. The concepts around reentrancy are well understood and should be part of any software engineering curriculum.
'Volatile' is intended to tell the compiler that read/write accesses have side effects, e.g. because they are hardware registers. If you have to use 'volatile' as a workaround as it seems here, the problem is that your code is wrong and not reentrant (or not thread-safe or not interrupt-safe).
I hope this helps.