BOOST_LIKELY和__builtin_expect是否仍然相关? [英] Are BOOST_LIKELY and __builtin_expect still relevant?

查看:122
本文介绍了BOOST_LIKELY和__builtin_expect是否仍然相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解解释的内容

I understand what is explained here as well as these would include hints to CPU for static branch prediction.

我想知道这些与Intel CPU的相关性如何,因为Intel CPU放弃了对静态预测提示的支持,如所述

I was wondering how relevant are these on Intel CPUs now that Intel CPUs have dropped support for static prediction hints as mentioned here. Also if I understand how it works now, the number of branch instructions in the path would be the only thing that the compiler can control and which branch path is predicted, fetched and decoded is decided at runtime.

鉴于此,在任何情况下,代码中的分支提示仍可用于针对最新英特尔处理器的软件,例如使用条件返回或避免在嵌套if/else语句的情况下避免关键路径中分支指令的数量吗?

Given this, are there any scenarios where branch hints in code are still useful for software targeting recent Intel processors, perhaps using conditional return or for avoiding the number of branch instructions in the critical path in case of nested if/else statements?

此外,如果这些内容仍然有意义,则欢迎使用gcc和其他流行的编译器的任何细节.

Also, if these are still relevant, any specifics on gcc and other popular compilers are appreciated.

P.S.我不是要过早的优化,也不是要用这些宏添加代码,但是我对这个话题很感兴趣,因为我正在处理一些时间紧迫的代码,并且仍然希望尽可能减少代码混乱.

P.S. I am not for premature optimization or for peppering the code with these macros, but I am interested in the topic as I am working with some time critical code and still like to reduce code clutter where possible.

谢谢

推荐答案

正如您对问题的评论部分中所述,您可以正确地找出这一点:

As in the comments section for your question you correctly figure out that:

  1. 在Intel x86 CPU上,操作码映射中不再有静态分支预测提示;
  2. 冷"条件跳跃的动态分支预测趋向于预测下降路径;
  3. 编译器可以使用__builtin_expect来重新排列if-then-else构造的路径,作为生成组件中的失败案例.
  1. There are no static branch prediction hints in opcode map anymore on Intel x86 CPUs;
  2. Dynamic branch prediction for "cold" conditional jumps tend to predict the fallthrough path;
  3. The compiler can use __builtin_expect to reorder what path of the if-then-else construct will be placed as a fallthrough case in generated assembly.

现在,考虑为多个目标体系结构而不仅仅是英特尔x86编译的代码库.它们中的许多确实具有静态分支提示,复杂性不同的动态分支预测器,或两者都有.

Now, consider a code base being compiled for multiple target architectures, not just Intel x86. A lot of them do have either static branch hints, dynamic branch predictors of different complexity, or both.

例如,Intel Itanium架构确实为所有类型的指令提供了广泛的预测提示系统:控制流,加载/存储等.Itanium旨在通过编译器对所有这些静态分配的代码进行广泛优化.捆绑说明和提示中的说明槽.

As an example, Intel Itanium architecture does offer an extensive system of prediction hints for all types of instructions: control flow, load/store etc. And Itanium was designed to have code being extensively optimized by a compiler with all these statically assigned instructions slots in a bundle and hints.

因此,__builtin_expect仍然与(罕见)情况有关:1)正确的分支预测信息太难由编译器自动推导,并且2)至少一种目标体系结构上的底层硬件也已为无法可靠地动态预测它们.考虑到某些低功耗处理器包括原始分支预测器,这些预测器不跟踪分支历史,但始终选择故障转移路径,因此它开始看起来很有用.对于现代的Intel x86硬件,不是那么多.

Therefore, __builtin_expect is still relevant for (rare) cases when 1) correct branch prediction information was too hard to deduce automatically by a compiler, and 2) the underlying hardware on at least one of target architectures was also known to be unable to reliably predict them dynamically. Given that certain low-power processors include primitive branch predictors that do not track branch history but always choose the fallthrough path, it starts to look beneficial. For modern Intel x86 hardware, not so much.

这篇关于BOOST_LIKELY和__builtin_expect是否仍然相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆