optimization - GCC热属性语义

标签 optimization gcc

假设我有一个由三个函数 A、B 和 C 组成的编译单元。A 从编译单元外部的函数调用一次(例如,它是入口点或回调); B 被 A 多次调用(例如,它在紧密循环中被调用);每次调用 B 时都会调用 C 一次(例如,它是一个库函数)。

通过 A 的整个路径(通过 B 和 C)对性能至关重要,但 A 本身的性能并不重要(因为大部分时间都花在 B 和 C 上)。

应使用 __attribute__ ((hot)) 注释以实现对该路径更积极的优化的最小函数集是什么?假设我们无法使用-fprofile-generate

等效地:__attribute__ ((hot)) 是否意味着“优化此函数的主体”、“优化对此函数的调用”、“优化此函数进行的所有后代调用”或某种组合其中?

GCC 信息页面没有明确解决这些问题。

最佳答案

Official documentation :

hot The hot attribute on a function is used to inform the compiler that the function is a hot spot of the compiled program. The function is optimized more aggressively and on many target it is placed into special subsection of the text section so all hot functions appears close together improving locality. When profile feedback is available, via -fprofile-use, hot functions are automatically detected and this attribute is ignored.

The hot attribute on functions is not implemented in GCC versions earlier than 4.3.

The hot attribute on a label is used to inform the compiler that path following the label are more likely than paths that are not so annotated. This attribute is used in cases where __builtin_expect cannot be used, for instance with computed goto or asm goto.

The hot attribute on labels is not implemented in GCC versions earlier than 4.8.

2007 :

 __attribute__((hot))   

Hint that the marked function is "hot" and should be optimized more aggresively and/or placed near other "hot" functions (for cache locality).

Gilad Ben-Yossef :

As their name suggests, these function attributes are used to hint the compiler that the corresponding functions are called often in your code (hot) or seldom called (cold).

The compiler can then order the code in branches, such as if statements, to favour branches that call these hot functions and disfavour functions cold functions, under the assumption that it is more likely that that the branch that will be taken will call a hot function and less likely to call a cold one.

In addition, the compiler can choose to group together functions marked as hot in a special section in the generated binary, on the premise that since data and instruction caches work based on locality, or the relative distance of related code and data, putting all the often called function together will result in better caching of their code for the entire application.

Good candidates for the hot attribute are core functions which are called very often in your code base. Good candidates for the cold attribute are internal error handling functions which are called only in case of errors.

因此,根据这些来源,__attribute__ ((hot)) 的意思是:

  • 优化对此函数的调用
  • 优化该函数的主体
  • 将此函数的主体放入 .hot 部分(将所有热代码分组到一个位置)

经过源代码分析,我们可以说“hot”属性是通过 (lookup_attribute ("hot", DECL_ATTRIBUTES (current_function_decl)) 检查的;当它为 true 时,函数的 节点->频率设置为NODE_FREQUENCY_HOT (predict.c, compute_function_frequency())。

如果函数的频率为NODE_FREQUENCY_HOT

  • 如果分支上没有个人资料信息且没有可能/不可能maybe_hot_frequency_p将为函数返回 true(==“...频率 FREQ 被认为是热的。”)。这将值变为 maybe_hot_bb_p对于函数中的所有基本 block (BB) 为 true(“BB 可能是 CPU 密集型的,应该优化以获得最大性能。”)和 maybe_hot_edge_p对于函数中的所有边都为 true。反过来,在非-Os-模式中,这些 BB 和边缘以及循环将针对速度而不是尺寸进行优化。

  • 对于此函数的所有出站调用边缘,cgraph_maybe_hot_edge_p将返回true(“如果调用可以热则返回true。”)。该标志用于 IPA(ipa-inline.c、ipa-cp.c、ipa-inline-analysis.c)并影响内联和克隆决策

关于optimization - GCC热属性语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15028990/

相关文章:

regex - 正则表达式如何在幕后工作(在 CPU 级别)?

c - 更快(优化)的图像抽取解决方案 (C++)

ios - Swift - 优化使用非常长的数组

c - "__G"是什么意思?

了解 C 真的会损害您用高级语言编写的代码吗?

c++ - 密码破解代码优化;

c++ - Codeblocks 16.01 中警告被视为错误

c - 针对 PowerPC 的 GCC C/C++ 交叉分析实现问题

C++:链接时gcc找不到静态成员

gcc - 无法在 EC2 上使用 GCC 进行编译