【问题标题】:LTO optimizations negative effects and find best solutionLTO 优化负面影响并找到最佳解决方案
【发布时间】:2015-07-10 18:43:20
【问题描述】:

我有带闪存的 MCU(像往常一样)。 链接器将 .struct_init、.struct_init_const、.struct_not_init 段放置到属于闪存段 20 的地址。它在链接描述文件中被硬编码。

考虑以下测试代码: 测试.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(和其他优化)能否丢弃从闪存中获取的数据,而只替换已知值,因为它们在链接时由于初始化而已知。 什么方法更好? 如果 LTO 可以替换值 - 那么应该避免初始化吗? 我知道 volatile 可以提供帮助,但在这种情况下真的需要它吗?

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

应该选择什么方法?

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

【问题讨论】:

  • 没有语言“C/C++”。请选择一个。特别是const,它们之间存在差异。
  • 有没有使用正确类型的原因?
  • 语言是 C++。你什么意思 ? “您是否有理由不使用正确的类型”。代码示例只是说明了访问闪存中数据的不同方法。直接和间接。带 const 和不带...
  • 只是想知道为什么你投了那么多而且没有使用正确的类型?
  • 链接器导出带有节地址的 int。我需要来自 int 的指针。这只是一种可能的解决方案的示例。使用 & 运算符肯定更好。 int初始化指针的想法来自这个线程stackoverflow.com/questions/31322646/…

标签: c++ embedded bootloader lto


【解决方案1】:

C 和 C++ 都具有 extern 变量,它允许您定义常量而无需立即泄露它们的值:

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

一般来说,您会在(单个)源文件中定义它们,这会将它们隐藏在该源文件中非的任何内容之外。当然,这不是 LTO 弹性,但如果您可以禁用 LTO,这是一个非常简单的策略。

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

由于该值在 LTO 时不可用,您可以保证它不会被替换。


至于您提出的解决方案,虽然volatile 确实是符合标准的解决方案,但它意味着该值不是恒定的,这会阻止在运行时对其进行缓存。这是否可以接受由您自己了解,请注意它可能会对性能产生影响,当您使用 LTO 时,我推测您希望避免这种影响。

【讨论】:

  • 非常感谢!我可能会在刷机前使用未初始化的带有二进制修改的版本。
猜你喜欢
  • 2018-04-06
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2020-04-12
相关资源
最近更新 更多