x86-64管道是否会像JMP RAX一样在间接跳转时停顿? [英] Does the x86-64 pipeline stall on an indirect jump like JMP RAX?

查看:102
本文介绍了x86-64管道是否会像JMP RAX一样在间接跳转时停顿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在x86-64中,如果您使用以下汇编代码:

In x86-64, if you use the following assembly code:

MOV RAX, (memory address)
JMP RAX

管道是否在执行分支之前停滞(等待MOV完成RAX处理),还是像条件分支一样冲洗管道?

Does the pipeline stall before executing the branch (to wait for MOV to finish with RAX), or will it flush the pipeline like a conditional branch?

推荐答案

对于大多数现代80x86 CPU;有静态预测(没有历史可用来做出更好的预测)和动态预测(有以前执行的历史可以使用).

For most modern 80x86 CPUs; there's static prediction (no history to use to make a better prediction) and dynamic prediction (where there's history from previous executions that can be used).

对于静态预测,CPU预测 JMP RAX 之后立即执行该指令.我不能完全确定哪个CPU对 JMP RAX 使用动态预测(而不是仅对 Jc 分支使用动态预测);但是对于那些这样做的人,它会覆盖静态预测.

For static prediction CPU predicts that execution will continue at the instruction immediately after the JMP RAX. I'm not entirely sure which CPUs use dynamic prediction for JMP RAX (rather than only for the Jc branches); but for those that do it'd override the static prediction.

一旦CPU具有预测的目标地址,它就会以推测方式执行,直到发现预测的是对还是错.如果它预测正确,它将保留所有工作,并且 JMP RAX 几乎没有成本.

Once the CPU has a predicted target address, it speculatively executes until it finds out if it predicted right/wrong. If it predicted right it keeps all the work it did and the JMP RAX would have little or no cost.

如果CPU预测错误,那么这与其他分支错误预测没有什么不同(丢弃所有以推测方式执行的工作,然后以正确的RIP返回获取/解码).

If CPU predicted wrong then that's no different to any other branch misprediction (discard all the work that was speculatively executed and go back to fetch/decode at the correct RIP).

请注意,如果您的 JMP RAX 无法预测,或者它后面的指令不太可能成为跳转的目标;英特尔建议在跳转之后立即放置 PAUSE UD2 ,以防止不必要的推测执行.在这种情况下,CPU会停止运行(不执行任何操作,直到找到正确的跳转目标为止).

Note that if your JMP RAX is unpredictable or it's too unlikely that the instruction after it is going to be the target of the jump; Intel recommends putting a PAUSE or UD2 immediately after the jump to prevent unnecessary speculative execution. In this case the CPU would stall (do nothing until it finds out the correct jump target).

还请注意,您希望移动 MOV RAX,.. ,以便尽快执行它,以便尽快知道跳转的目标,以便您可以将停顿或投机执行错误的事情的时间减至最少.

Also note that you'd want to move the MOV RAX, .. so that it's executed as soon as possible, so that the target of the jump is known as soon as possible, so that you minimise the time spent stalled or speculatively executing the wrong thing.

这篇关于x86-64管道是否会像JMP RAX一样在间接跳转时停顿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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