partition function into cold and hot function by using __builtin_expect

陈云星 chen.yunxing@me.com
Thu May 6 09:08:16 GMT 2021


GCC already has function attribute *cold*, which hint compiler to separate cold and hot part of a caller function into different functions, and here is a example:  https://godbolt.org/z/j7sK4hd48 <https://godbolt.org/z/j7sK4hd48>
"
cold

The cold attribute on functions is used to inform the compiler that the function is unlikely to be executed. The function is optimized for size rather than speed and on many targets it is placed into a special subsection of the text section so all cold functions appear close together, improving code locality of non-cold parts of program. The paths leading to calls of cold functions within code are marked as unlikely by the branch prediction mechanism. It is thus useful to mark functions used to handle unlikely conditions, such as perror, as cold to improve optimization of hot functions that do call marked functions in rare occasions.
When profile feedback is available, via -fprofile-use, cold functions are automatically detected and this attribute is ignored.

“

Which I want was the builtin function __builtin_expect can also do such hint for compiler, and result in split expected and unexpected part of a function into two function (named cold and hot).

Is this a plan for gcc in the future ? 



More information about the Gcc-help mailing list