ARM Cortex M4 中是否可以安全地省略 DMB 指令

Can DMB instructions be safely omitted in ARM Cortex M4

我正在检查由 GCC 为 ARM Cortex M4 生成的程序集,并注意到 atomic_compare_exchange_weak 在条件周围插入了两个 DMB 指令(使用 [=13= 使用 GCC 4.9 编译) ]):

// if (atomic_compare_exchange_weak(&address, &x, y))
dmb      sy
ldrex    r0, [r3]
cmp      r0, r2
itt      eq
strexeq  lr, r1, [r3]
cmpeq.w  lr, #0
dmb      sy
bne.n    ...

由于 programming guide to barrier instructions for ARM Cortex M4 指出:

Omitting the DMB or DSB instruction in the examples in Figure 41 and Figure 42 would not cause any error because the Cortex-M processors:

  • do not re-order memory transfers
  • do not permit two write transfers to be overlapped.

在针对 Cortex M 时,是否有任何原因无法删除这些指令?

我不知道 Cortex M4 是否可以用于 multi-cpu/multi-core 配置,但总的来说:

  1. 在单核系统中永远不需要内存屏障(总是可以省略)。
  2. 内存屏障在多核系统中始终是必需的(永远不能省略),其中 threads/processes 在同一内存上运行可能 运行 在不同的内核上运行。

是否在硬件级别重新排序内存写入是无关紧要的。

当然,我希望 DMB 指令在不支持 SMP 的芯片上基本上是免费的,所以我不确定您为什么要尝试破解它。

请注意,根据问题引用编译器为原子内在函数生成的代码,我假设上下文是用于同步原子以使其符合高级规范,而不是其他用途,如 IO 屏障对于 MMIO,上述 "never" 不应被理解为适用于此(无关)用途(尽管我怀疑,出于您已经引用的原因,它不适用于 Cortex M4)。