`isync` 会阻止 CPU PowerPC 上的 Store-Load 重新排序吗?

Does `isync` prevent Store-Load reordering on CPU PowerPC?

众所周知,PowerPC 具有弱内存模型,允许任何推测性重新排序:Store-Store、Load-Store、Store-Load、Load-Load。

至少有 3 个栅栏:

比如在这段代码中可以重新排序Store-stwcx.和Load-lwz?:https://godbolt.org/g/84t5jM

    lwarx 9,0,10
    addi 9,9,2
    stwcx. 9,0,10
    bne- 0,.L2
    isync
    lwz 9,8(1)

众所周知,isync 防止重新排序 lwarxbne <--> any following instructions

但是 isync 会阻止重新排序 stwcx.,bne <--> any following instructions?

即Store-stwcx.能不能比后面的Load-lwz开始得早,比Load-lwz晚执行完?

即Store-stwcx. 可以先于 Load-lwz 开始存储到 Store-Buffer,但是实际存储到对所有 CPU-core 可见的缓存发生晚于加载-lwz完成了吗?

正如我们从以下文档、文章和书籍中看到的那样:

题主,初始:a=0,b=0

那么Core-0可以看到[b]==1,Core-1可以看到[a]==0吗?


还有:

  1. https://www.ibm.com/developerworks/systems/articles/powerpc.html

The isync prevents speculative execution from accessing the data block before the flag has been set. And in conjunction with the preceding load, compare, and conditional branch instructions, the isync guarantees that the load on which the branch depends (the load of the flag) is performed prior to any loads that occur subsequent to the isync (loads from the shared block). isync is not a memory barrier instruction, but the load-compare-conditional branch-isync sequence can provide this ordering property.

  1. http://www.nxp.com/assets/documents/data/en/application-notes/AN2540.pdf

Unlike isync, sync forces all external accesses to complete with respect to other processors and mechanisms that access memory.

  1. PowerPC 中的存储 Janice M. Stone,Robert P. Fitzgerald,1995:http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.47.4033&rep=rep1&type=pdf

Unlike sync , isync does not wait for all other processors to detect storage accesses. isync is a less conservative fence than sync because it does not delay until all processors detect previous loads and stores.

  1. http://open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2745.html

bc;isync: this is a very low-overhead and very weak form of memory fence. A specific set of preceding loads on which the bc (branch conditional) instruction depends are guaranteed to have completed before any subsequent instruction begins execution. However, store-buffer and cache-state effects can nevertheless make it appear that subsequent loads occur before the preceding loads upon which the twi instruction depends. That said, the PowerPC architecture does not permit stores to be executed speculatively, so any store following the twi;isync instruction is guaranteed to happen after any of the loads on which the bc depends.

  1. https://books.google.ru/books?id=TKOfDQAAQBAJ&pg=PA264&lpg=PA264&dq=isync+store+load&source=bl&ots=-4FyWvxTwg&sig=r1fitaG-Q3GHOxvSMTgLJMBVGUU&hl=ru&sa=X&ved=0ahUKEwiKjYK97urTAhUJ_iwKHbfMA58Q6AEIOjAC#v=onepage&q=isync%20store%20load&f=false

  1. https://books.google.ru/books?id=gZZgAQAAQBAJ&pg=PA71&lpg=PA71&dq=isync+store+load&source=bl&ots=bo6nTLdzEZ&sig=vCjoDmUWhn0buN_uMf8XgbDzCf4&hl=ru&sa=X&ved=0ahUKEwiKjYK97urTAhUJ_iwKHbfMA58Q6AEIcTAJ#v=onepage&q=isync%20store%20load&f=false

  1. https://books.google.ru/books?id=G2fmCgAAQBAJ&pg=PA321&lpg=PA321&dq=isync+store+load&source=bl&ots=YS4mE-4f_F&sig=OVwaJYE-SNnor-KtKrjlkOd6AOs&hl=ru&sa=X&ved=0ahUKEwiKjYK97urTAhUJ_iwKHbfMA58Q6AEIYjAH#v=onepage&q&f=false

  1. http://www.nxp.com/assets/documents/data/en/application-notes/AN3441.pdf

Note that isync does not affect data accesses and does not wait for all stores to be performed.

  1. 第 77 页:https://www.setphaserstostun.org/power8/POWER8_UM_v1.3_16MAR2016_pub.pdf

3.5.7.2 Instruction Cache Block Invalidate (icbi)

As a result of this and other implementation-specific design optimizations, instead of requiring the instruction sequence specified by the Power ISA to be executed on a per cache-line basis, software must only execute a single sequence of three instructions to make any previous code modifications become visible: sync, icbi (to any address), isync.


答案:

所以,isync 不保证存储加载顺序,因为“isync 不是内存屏障指令”,那么 isync 不不保证在下一个指令完成之前,任何先前的存储对其他 CPU-Core(使用顺序一致性)都是可见的。指令同步命令isync只保证指令开始的顺序,不保证指令完成的顺序,即不保证它们对其他CPU-Core可见效果的顺序。 isync 允许在此代码 stwcx. [a]=1; bne-; isync; lwz [b].

中重新排序 Store-Load 的可见效果

正如您所猜测的那样,并且您的大部分优秀资源都暗示,这里涉及内存访问的两个属性:

能见度

如果其他处理器可以阻止内存访问。
使用特定于处理器的缓冲区或高速缓存可以使存储在处理器上完整,但对其他处理器不可见。

订购

当针对同一处理器上的其他指令执行内存访问时


排序是内存访问的处理器内方面,它控制处理器的乱序能力。
不能根据其他处理器的指令进行排序。

可见性是处理器间方面,它确保内存访问的副作用对其他处理器(或一般情况下,对其他代理)可见。
存储的主要副作用是更改内存位置。

通过控制这两个方面,可以强制执行进程间排序,即其他处理器看到内存访问序列的顺序。
不为人知的是,"ordering" 这个词通常指的是第二种含义,除非在没有其他代理人存在的上下文中使用。
诚然,这是一个令人困惑的术语。


请注意,我对 PowerPC 体系结构没有信心,我只是在网上找到的一些官方文档和您提供的引述的帮助下应用理论。

isync,就像scrfiContext-Synchronizing instructions一样,它们的主要目的是保证后面的指令在前面的指令建立的上下文中执行。 例如,执行系统调用会改变上下文,我们不希望特权代码在非特权上下文中执行,反之亦然。

这些指令等待所有先前调度的指令完成但不可见

All previously issued instructions have completed, at least to a point where they can no longer cause an exception.
However, memory accesses that these instructions cause need not have completed with respect to other processors and mechanisms.

因此,根据重新排序的含义,isync 会或不会阻止加载-加载、加载-存储等重新排序。
确实 从执行它的处理器的角度防止任何此类重新排序(进程内重新排序) - 所有先前的加载和存储都在 isync 完成之前完成,但它们不一定可见。
不会阻止从其他处理器的角度重新排序(进程间重新排序),因为它不能确保先前指令的可见性。


But does isync prevent reordering stwcx.,bne <--> any following instructions?

仅进程内重新排序。

I.e. can Store-stwcx. begins earlier than the following Load-lwz, and finishes performed later than Load-lwz?

不是从执行它们的处理器的角度来看,stwcx.lwz 开始时 已完成 但是,使用英特尔术语,它是 在本地完成的 - 其他处理器可能在 lwz 开始时看不到它已完成。

I.e. can Store-stwcx. preforms Store to the Store-Buffer earlier than the following Load-lwz begun, but the actual Store to the cache that visible for all CPU-cores occurs later than the Load-lwz finished?

是的,完全正确。