multithreading - `isync` 是否会阻止 CPU PowerPC 上的存储加载重新排序?

标签 multithreading assembly concurrency powerpc

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

至少有 3 个栅栏:

  • hwsyncsync - 完整的内存屏障,防止任何重新排序
  • lwsync - 阻止重新排序的内存屏障:加载-加载、存储-存储、加载-存储
  • isync - 指令屏障 :https://www.ibm.com/support/knowledgecenter/en/ssw_aix_71/com.ibm.aix.alangref/idalangref_isync_ics_instrs.htm

  • 例如,可以重新排序 Store- stwcx.和负载- 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防止重新排序 lwarx , bne <--> any following instructions .

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

    IE。 can Store- stwcx.开始时间早于以下负载- lwz , 并且完成时间晚于 Load- lwz ?

    IE。 can Store- stwcx. preforms Store 到 Store-Buffer 的时间早于以下 Load- lwz开始,但对所有 CPU 核心可见的缓存的实际存储发生晚于 Load-lwz完成的?

    正如我们从以下文件、文章和书籍中看到的:
  • isync不是内存栅栏,而只是指令栅栏。
  • isync相对于访问内存的其他处理器和机制,不会强制完成所有外部访问。
  • isync不等待所有其他处理器检测到存储访问
  • isync是一个非常低的开销并且非常弱(低于 lwsynchwsync)
  • isync不保证以前和将来的存储将被本地发布的顺序中的其他处理器感知 - 这需要同步指令之一。
  • isync是获取屏障,但正如我们所知,获取只能应用于加载操作,不适用于存储 ( stwcx. )
  • isync不影响数据访问和不等待所有存储执行完毕 .

  • 主要问题 , 最初: a=0, b=0
  • 如果 CPU-Core-0 执行:stwcx. [a]=1 bne- isync lwz [b] .
  • 而 CPU-Core-1 做:hwsync stw [b]=1 hwsync lwz [a] hwsync .

  • 然后Core-0可以看到[b]==1和 Core-1 见 [a]==0 ?

    还:
  • 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.


  • 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.


  • 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.


  • 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.


  • 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

  • enter image description here
  • 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

  • enter image description here
  • 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

  • enter image description here
  • 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.


  • 第 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 核心(使用顺序一致性)可以看到任何先前的存储。指令同步命令isync只保证启动指令的顺序,但不保证指令完成的顺序,即不保证它们对其他 CPU-Core 的可见效果的顺序。那些,isync允许在此代码中重新排序 Store-Load 的可见效果 stwcx. [a]=1; bne-; isync; lwz [b] .

    最佳答案

    正如您已经猜到的以及您的大多数优秀资料所暗示的那样,这里涉及内存访问的两个属性:

    能见度

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

    订购

    当执行内存访问时考虑到同一处理器上的其他指令。

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

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

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

    请注意,我对 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不会或不会阻止 Load-Load、Load-Store 等重新排序。
    从执行它的处理器的角度来看,它确实阻止了任何此类重新排序(进程内重新排序)——所有先前的加载和存储都在 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开始,但使用 Intel 术语,它是在本地完成的 - 其他处理器可能看不到它在 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?



    对,就是这样。

    关于multithreading - `isync` 是否会阻止 CPU PowerPC 上的存储加载重新排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43944411/

    相关文章:

    c - 用于上下文切换的 gcc 内联汇编

    assembly - x86 "ret"指令相当于什么?

    multithreading - Flutter 插件异步和线程

    php - 如何在Codeigniter中进行工作(线程或恶魔)

    Python 太多打开的文件、eventlet 和 multiprocessing.dummy

    java - 如何关闭正在运行的套接字线程?

    assembly - 在哪里可以找到使用 AT&T 语法学习 C 语言内联汇编的资源?

    multithreading - 卡在Rust中的 channel 接收器迭代器上?

    python - 如何将具有多个参数的函数传递给 python concurrent.futures.ProcessPoolExecutor.map()?

    java - 将 volatile 关键字与包装类一起使用