LTO优化负面影响并找到最佳解决方案
LTO optimizations negative effects and find best solution
我有MCU,闪存在部分断开(像往常一样)。链接器将.struct_init, .struct_init_const, .struct_not_init节放置到属于闪存section20的地址中。它在链接器脚本中被硬编码。
考虑以下测试代码:test.h
typedef struct
{
int val1;
int val2;
} mystruct_t;
test.cpp
#include "test.h"
// each variable is placed in dedicated section
// sections are placed in flash section20
// linker exports symbols with address of eaach section
__attribute__((section(".struct_init")))
mystruct_t struct_init = {
.val1 = 1,.val2 = 2};
__attribute__((section(".struct_init_const")))
extern const mystruct_t struct_init_const = {
.val1 = 1, .val2 = 2};
__attribute__((section(".struct_not_init")))
mystruct_t struct_not_init;
main.cpp
#include <stdint.h>
// This symbols exported by linker
// contains addresses of corresponding sections
extern uintptr_t LNK_STRUCT_INIT_ADDR;
extern uintptr_t LNK_STRUCT_INIT_CONST_ADDR;
extern uintptr_t LNK_STRUCT_NOT_INIT_ADDR;
// Pointers for indirect access to data
mystruct_t* struct_init_ptr = (mystruct_t*)LNK_STRUCT_INIT_ADDR;
const mystruct_t* struct_init_const_ptr = (const mystruct_t*)LNK_STRUCT_INIT_CONST_ADDR;
mystruct_t* struct_not_init_ptr = (mystruct_t*)LNK_STRUCT_NOT_INIT_ADDR;
// Extern variables declarations for DIRECT access data
extern mystruct_t struct_init;
extern const mystruct_t struct_init_const;
extern mystruct_t struct_not_init;
// This is some variables representing config values
// They can be more complex objects(classes) with internal state and logic..
int param1_direct;
int param1_init_const_direct;
int param1_not_init_direct;
int param1_indirect;
int param2_init_const_indirect;
int param1_not_init_indirect;
int main(void)
{
// local variables init with direct access
int param1_direct_local = struct_init.val1;
int param1_init_const_direct_local = struct_init_const.val1;
int param1_not_init_direct_local = struct_not_init.val1;
// local variables init with indirect access
int param1_indirect_local = struct_init_ptr->val1;
int param2_init_const_indirect_local = struct_init_const_ptr->val1;
int param1_not_init_indirect_local = struct_not_init_ptr->val1;
//global variables init direct
param1_direct = struct_init.val1;
param1_init_const_direct = struct_init_const.val1;
param1_not_init_direct = struct_not_init.val1;
//global variables init indirect
param1_indirect = struct_init_ptr->val1;
param2_init_const_indirect = struct_init_const_ptr->val1;
param1_not_init_indirect = struct_not_init_ptr->val1;
while(1){
// use all variables we init above
// usage of variables may also occure in some functions or methods
// directly or indirectly called from this loop
}
}
我想确保param1_变量的初始化将导致从flash中获取数据。因为flash section20中的数据可以使用引导加载程序更改(在主固件未运行的时刻)。
问题是:LTO(和其他优化)是否可以丢弃从flash中获取的数据,而只是替换已知的值,因为它们在链接时由于初始化而已知。哪种方法更好?如果LTO可以替换值,那么应该避免初始化?我知道挥发油有帮助,但在这种情况下真的需要吗?
代码示例显示了访问和初始化数据的不同方法。Not_init版本似乎是最好的,因为编译器不能替代任何东西。但是有一些默认参数是一个好主意,所以如果可以使用的话,我更喜欢init版本。
应该选择什么方法?
目前我使用的是GCC 4.9.3,但这是任何C/c++编译器的普遍问题。
C和c++都有extern
变量,它允许您定义常量而不会立即给出它们的值:
// .h
extern int const param1;
extern char const* const param2;
// ...
一般来说,您将在(单个)源文件中定义它们,这将使它们远离源文件中以外的任何内容。当然,这不是LTO弹性,但如果您可以禁用LTO,这是一个足够简单的策略。
如果不能禁用LTO,另一个解决方案是不定义它们,让LTO生成二进制文件,然后使用脚本将生成的二进制文件中的定义拼接到正确的部分(可以闪现的部分)。
如果该值在LTO时不可用,则可以保证它不会被替换。
对于您提出的解决方案,虽然volatile
确实是一个符合标准的解决方案,但它意味着该值不是恒定的,这阻止了在运行时缓存它。这是否可以接受由您来知道,只是要注意它可能会对性能产生影响,因为您正在使用LTO,我猜您希望避免这种影响。
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- 计算每个节点的树高,帮助我解释这个代码解决方案
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- visual c++,如何获取解决方案目录中的代码
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- 解决难题(最佳解决方案)
- 比较/搜索数组中多个整数的最佳解决方案
- 找出迷宫求解器的最佳解决方案,并具有动画输出
- 从QTcpSocket上的数据流中连续运行复杂算法的最佳Qt线程解决方案是什么
- CPLEX 如何以相同的成本获得所有不同的最佳解决方案
- 我该如何判断k-server动态解决方案的最佳路径以数组成本[i] [j] [k] [t]位于何处
- 暂停和恢复线程的最佳解决方案是什么?
- 需要包含运行任务时间的 2D 矩阵的最佳解决方案
- 为什么通过系统("color YX")更改控制台应用程序中C++颜色不是最佳解决方案?
- 查找内存泄漏的最佳解决方案
- 在c++中绘制(原语,线条等)的最佳通用跨平台解决方案
- LTO优化负面影响并找到最佳解决方案