LTO优化负面影响并找到最佳解决方案

LTO optimizations negative effects and find best solution

本文关键字:最佳 解决方案 影响 优化 LTO      更新时间:2023-10-16

我有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,我猜您希望避免这种影响。