gpt4 book ai didi

c - GCC -flto 和 inline 关键字

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:43 26 4
gpt4 key购买 nike

我的问题很简单,关键字 inline 对链接时间优化的看法有影响吗?对于链接时间优化,我指的是支持 -flto(链接时间优化)的 GCC 版本。

例如:

ma​​in.c

#include "b.h"

int main() {
print_x(2);
return 0;
}

b.h

extern void print_x(int x);

BC

#include "b.h"
#include "stdio.h"

inline void print_x(int x) {
printf("%d\n", x);
}

b.c 中的 inline 关键字在链接器做 LTO(Link time optimization)时会有什么不同吗?

最佳答案

原则上,编译器可以使用 inline 关键字的存在来改变其启发式。然而,inline 说明符的存在在多大程度上改变了它的启发式是一个实现细节;甚至忽略它(6.7.4.5):

[...] Making a function an inline function suggests that calls to the function be as fast as possible. The extent to which such suggestions are effective is implementation-defined. [121]

[121] For example, an implementation might never perform inline substitution, or might only perform inline substitutions to calls in the scope of an inline declaration.

C 标准没有提到 LTO,所以在这方面没有太多要说的。


现在,当然,编译器可以有不同的启发式方法,并根据是否在 LTO 模式下编译来区别对待 inline 关键字。需要检查手册和/或编译器的实现才能回答该问题,并且可能因版本而异。

特别是对于 GCC,有关于 -flto option 的文档和 LTO internals .然而,问题是 GCC 目前没有在其用户手册中提供很多细节。因此,您不能指望它稳定,即使您可以阅读当前的实现并了解启发式方法。

无论如何,考虑到编译器的内联决策差异很大(供应商、版本、选项等),尝试围绕它调整代码没有多大意义。如果你真的需要改变内联决定,你应该使用编译器提供的特定提示,而不是试图调整其算法的结果。例如,对于 GCC,请尝试使用 __attribute__((always_inline))

相关:Link-time optimization and inline

关于c - GCC -flto 和 inline 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49346228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com