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

LTO optimizations negative effects and find best solution

我有带闪存的 MCU(像往常一样)。 链接器将 .struct_init、.struct_init_const、.struct_not_init 部分放置到属于闪存部分 20 的地址。它被硬编码在 linker 脚本中。

考虑以下测试代码: 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 section20中的数据可以使用bootloader来改变(此时主固件不是运行)。

问题是:LTO(和其他优化)是否可以丢弃从闪存中获取的数据并仅替换已知值,因为它们在 link 时间因初始化而已知。 什么方法更好? 如果 LTO 可以替换值——那么应该避免初始化吗? 我知道 volatile 可以提供帮助,但在这种情况下真的需要它吗?

代码示例展示了访问和初始化数据的不同方法。 not_init 版本似乎是最好的,因为编译器无法替代任何东西。但是有一些默认参数是个好主意,所以如果可以使用我更喜欢init版本。

应该选择什么方法?

目前我使用的是 GCC 4.9.3,但这是关于任何 C/C++ 编译器的一般问题。

C 和 C++ 都具有 extern 变量,这使您可以定义常量而不立即放弃它们的值:

// .h
extern int const param1;
extern char const* const param2;
// ...

通常,您会在(单个)源文件中定义它们,这会将它们隐藏起来,远离此源文件中的任何 not。当然,这不是 LTO 弹性,但如果您可以禁用 LTO,这是一个非常简单的策略。

如果禁用 LTO 不是一个选项,另一种解决方案是不定义它们,让 LTO 生成一个二进制文件,然后使用脚本将生成的二进制文件中的定义拼接在正确的部分(可以是闪烁)。

由于LTO时间不可用的值,保证不会被替换。


至于您提供的解决方案,虽然 volatile 确实是一个符合标准的解决方案,但它意味着该值不是常量,这会阻止在 运行 时间内缓存它。这是否可以接受由您自己决定,请注意它可能会对性能产生影响,而当您使用 LTO 时,我想您希望避免这种情况。