我们可以在堆内存上使用非临时 mov 指令吗?

Can we use non-temporal mov instructions on heap memory?

在 Agner Fog 的 "Optimizing subroutines in assembly language - section 11.8 Cache control instructions," 中,他说:"Memory writes are more expensive than reads when cache misses occur in a write-back cache. A whole cache line has to be read from memory, modified, and written back in case of a cache miss. This can be avoided by using the non-temporal write instructions MOVNTI, MOVNTQ, MOVNTDQ, MOVNTPD, MOVNTPS. These instructions should be used when writing to a memory location that is unlikely to be cached and unlikely to be read from again before the would-be cache line is evicted. As a rule of thumb, it can be recommended to use non-temporal writes only when writing a memory block that is bigger than half the size of the largest-level cache."

来自 "Intel 64 and IA-32 Architectures Software Developer's Manual Combined Volumes Oct 2019" - "These SSE and SSE2 non-temporal store instructions minimize cache pollution by treating the memory being accessed as the write combining (WC) type. If a program specifies a non-temporal store with one of these instructions and the memory type of the destination region is write back (WB), write through (WT), or write combining (WC), the processor will do the following . . . "

我认为写组合内存只存在于显卡中,而不存在于通用堆内存中——推而广之,上面列出的指令只在这种情况下有用。如果那是真的,为什么 Agner Fog 会推荐这些说明?英特尔手册似乎表明它仅适用于 WB、WT 或 WC 内存,但随后他们说正在访问的内存 将被视为 WC。

如果这些指令实际上可以用于普通的堆内存写入,是否有任何限制?如何分配写组合内存?

您可以在普通 WB 内存(即堆)上使用像 movntps 这样的 NT 存储。 另请参阅 了解更多关于 NT 存储与 NT 存储的信息。普通商店。

尽管 MTRR and/or PAT 已将其设置为正常 WB,但它 将其 视为用于那些 NT 商店的 WC。

英特尔文档告诉您 NT 在 WB、WT 和 WC 内存 上存储"work"。 (但不是强序UC不可缓存内存,当然也不是WP写保护内存)。


您是正确的,通常只有视频 RAM(或其他类似的设备内存区域)被映射到 WC。不,在像 Linux 这样的正常 OS 下,您不能轻易地在用户 space 进程中分配 WC 内存,但您通常不想这样做。

您只能在 WC 内存上使用 SSE4 NT 负载(否则当前的 CPU 会忽略 NT 提示),但是负载的一些缓存污染对于硬件预取和缓存工作来说是一个很小的代价。您可以使用 WB 内存中的 NT prefetchreduce 某些级别缓存中的污染,例如绕过L2。但这很难调整。

IIRC,normal 商店像 mov 在 WC 内存上具有您从 NT 商店获得的商店合并行为。但是你不需要使用 WC 内存来让 NT 商店工作。