gpt4 book ai didi

multithreading - x86 中非读和非写指令重新排序有关系吗?

转载 作者:行者123 更新时间:2023-12-04 06:52:40 24 4
gpt4 key购买 nike

mfence documentation说如下:

Performs a serializing operation on all load-from-memory and store-to-memory instructions that were issued prior the MFENCE instruction. This serializing operation guarantees that every load and store instruction that precedes the MFENCE instruction in program order becomes globally visible before any load or store instruction that follows the MFENCE instruction.

据我所知,x86 中没有任何栅栏指令可以防止非读和非写指令的重新排序。

现在,如果我的程序只有一个线程,即使指令重新排序,指令看起来仍然是按顺序执行的。

但是,如果我的程序有多个线程,并且在其中一个线程中,非读和非写指令被重新排序,其他线程是否会注意到这种重新排序(我假设答案是,否则会有一个栅栏指令来停止非读和非写指令重新排序,或者也许我遗漏了一些东西)?

最佳答案

will the other threads notice this reordering

不,除了性能(计时或使用硬件性能计数器直接测量)。或者微架构侧 channel (例如与超线程/SMT 共享物理核心的逻辑核心的 ALU 端口压力):一个线程可以为自己计时,以了解其他硬件线程正在执行的内容。 p>

线程观察彼此的任何信息的唯一“正常”方式是加载其他线程存储的数据。

甚至加载顺序也只能间接可见(通过它对其他线程决定稍后存储的内容的影响)。


As far as I know, there is no fence instruction in x86 that prevents the reordering of non read and non write instructions.

在 Intel CPU(但不是 AMD)上,lfence 会执行此操作。英特尔的手册是这么说的,这不仅仅是只是一个实现细节。它实际上为 future 的微架构提供了保证。

Intel's LFENCE instruction-set reference manual entry:

LFENCE does not execute until all prior instructions have completed locally, and no later instruction begins execution until LFENCE completes.

(本地完成=从无序核心中退出,即离开ROB)。

lfence 作为实际的负载屏障并不是特别有用,因为 x86 不允许从 WB 内存(仅从 WC)进行弱排序加载。 (甚至 movntdqaprefetchnta 也无法从正常的 WB 内存创建弱有序加载。)因此,与 sfence 不同,lfence > 基本上从不需要内存排序,用于其特殊效果,例如 lfence ; rdtsc。或者为了缓解 Spectre,阻止推测性执行。


但作为实现细节,在至少包括 Skylake 的 Intel CPU 上,mfence 是乱序执行的障碍。 参见Are loads and stores the only instructions that gets reordered?为此,以及更多相关的东西。

关于multithreading - x86 中非读和非写指令重新排序有关系吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51055417/

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