gpt4 book ai didi

multithreading - ARM STLR 内存排序语义

转载 作者:行者123 更新时间:2023-12-03 12:46:09 25 4
gpt4 key购买 nike

我正在为 ARM STLR 的确切语义而苦苦挣扎。
根据文档,它具有发布语义。所以如果你有 STLR 商店,你会得到:

[StoreStore][LoadStore]
X=r1
X是内存和 r1是一些寄存器。
问题是发布存储和获取加载,无法提供顺序一致性:
[StoreStore][LoadStore]
X=r1
r2=Y
[LoadLoad][LoadStore]
在上述情况下,允许重新排序 X=r1 和 r2=Y。为了使这个顺序一致,需要添加一个 [StoreLoad]:
[StoreStore][LoadStore]
X=r1
[StoreLoad]
r2=Y
[LoadLoad][LoadStore]
你通常在商店里这样做,因为加载更频繁。
在 X86 上,普通存储是发布存储,普通负载是获取负载。 [StoreLoad] 可以通过 MFENCE 或使用 LOCK ADDL %(RSP),0 来实现。就像在 Hotspot JVM 中所做的那样。
查看 ARM 文档时,似乎 LDAR 已获得语义;所以这将是 [LoadLoad][LoadStore]。
但是 STLR 的语义是模糊的。当我使用 memory_order_seq_cst 编译 C++ atomic 时,只有一个 STLR;没有 DMB。因此,STLR 似乎比发布存储具有更强的内存排序保证。在我看来,在栅栏级别上,STLR 等效于:
 [StoreStore][LoadStore]
X=r1
[StoreLoad]
有人可以对此有所了解吗?

最佳答案

我只是在学习这些东西,所以请谨慎对待。但我的理解是,在 ARMv8/AArch64 中,STLR/LDAR确实提供超出通常的发布/获取定义的额外语义,但不如您的建议那么强。即,一个发布商店STLR确实与获取负载具有顺序一致性 LDAR按程序顺序跟随它,但不是普通的 LDR负载。
来自 ARMv8 Architecture Reference Manual, B2.3.7, "Load-Acquire, Load-AcquirePC, and Store-Release":

Where a Load-Acquire appears in program order after a Store-Release, the memory access generated by theStore-Release instruction is Observed-by each PE to the extent that PE is required to observe the access coherently,before the memory access generated by the Load-Acquire instruction is Observed-by that PE, to the extent that thePE is required to observe the access coherently.


从 B2.3.2,“订购关系”:

A read or a write RW1 is Barrier-ordered-before a read or a write RW2 from thesame Observer if and only if RW1 appears in program order before RW2 and any of the followingcases apply: [...] RW1 is a write W1 generated by an instruction with Release semantics and RW2 is a read R2generated by an instruction with Acquire semantics.


作为测试,我借了 a C++ implementation of Peterson's locking algorithm by LWimsey .与 clang 11.0 on godbolt ,可以看到即使请求顺序一致性,编译器还是会生成 STLR, LDAR拿锁(程序集的第 18-19 行),没有 DMB .我运行了一段时间(Raspberry Pi 4B、Cortex A72、4 核)并且没有违规。
然而,与你的想法相反, STLR仍然可以相对于它后面的普通(非获取)加载重新排序,因此它没有隐式地具有完整的 StoreLoad 栅栏。我修改了 LWimsey 的程序以使用 STLR, LDR相反,在添加了一些额外的垃圾来引发竞争之后,我能够看到锁违规。
同样, LDAR可以相对于它之前的普通(非发布)商店重新排序。我同样能够通过 STR, LDAR 获得锁违规。在测试程序中。

关于multithreading - ARM STLR 内存排序语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65466840/

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