Wen*_*ang 2 c x86 sse2 cpu-cache clflush
我开始使用_mm_clflush、_mm_clflushopt和 等函数_mm_clwb。
现在说我已经定义了一个结构体名称 mystruct ,它的大小是 256 字节。我的缓存行大小是 64 字节。现在我想刷新包含 mystruct 变量的缓存行。以下哪种方法是正确的方法?
_mm_clflush(&mystruct)
Run Code Online (Sandbox Code Playgroud)
或者
for (int i = 0; i < sizeof(mystruct)/64; i++) {
_mm_clflush( ((char *)&mystruct) + i*64)
}
Run Code Online (Sandbox Code Playgroud)
CPUclflush指令不知道结构体的大小;它只刷新一个缓存行,该缓存行包含指针操作数指向的字节。(C 内在函数将其公开为 a const void*,但char*也有意义,特别是考虑到asm 文档将其描述为 8 位内存操作数。)
您需要 4 次刷新,间隔 64 个字节,或者如果您的结构不是这样,则可能需要 5 次alignas(64)刷新,这样它可以在 5 个不同的行中包含部分。(您可以无条件刷新结构的最后一个字节,而不是使用更复杂的逻辑来检查它是否在尚未刷新的缓存行中,具体取决于clflush与更多逻辑的相对成本和可能的分支错误预测。)
您的原始循环在结构体的开头对 4 个相邻字节进行了 4 次刷新。
使用指针增量可能是最简单的,因此转换不会与关键逻辑混淆。
// first attempt, a bit clunky:
const int LINESIZE = 64;
const char *lastbyte = (const char *)(&mystruct+1) - 1;
for (const char *p = (const char *)&mystruct; p <= lastbyte ; p+=LINESIZE) {
_mm_clflush( p );
}
// if mystruct is guaranteed aligned by 64, you're done. Otherwise not:
// check if next line to maybe flush contains the last byte of the struct; if not then it was already flushed.
if( ((uintptr_t)p ^ (uintptr_t)lastbyte) & -LINESIZE == 0 )
_mm_clflush( lastbyte );
Run Code Online (Sandbox Code Playgroud)
x^y在它们不同的位位置上为 1。 x & -LINESIZE丢弃地址的行内偏移位,仅保留行号位。因此,我们可以仅使用 XOR 和 TEST 指令来查看 2 个地址是否位于同一高速缓存行中。(或者 clang 将其优化为更短的cmp指令)。
或者将其重写为单个循环,使用 if 逻辑作为终止条件:
我使用了 C++struct foo &var参考,因此我可以遵循您的&var语法,但仍然可以看到它如何针对采用指针 arg 的函数进行编译。适应 C 语言很简单。
/* I think this version is best:
* compact setup / small code-size
* with no extra latency for the initial pointer
* doesn't need to peel a final iteration
*/
inline
void flush_structfoo(struct foo &mystruct) {
const int LINESIZE = 64;
const char *p = (const char *)&mystruct;
uintptr_t endline = ((uintptr_t)&mystruct + sizeof(mystruct) - 1) | (LINESIZE-1);
// set the offset-within-line address bits to get the last byte
// of the cacheline containing the end of the struct.
do { // flush while p is in a cache line that contains any of the struct
_mm_clflush( p );
p += LINESIZE;
} while(p <= (const char*)endline);
}
Run Code Online (Sandbox Code Playgroud)
使用适用于 x86-64 的 GCC10.2 -O3,可以很好地编译 (Godbolt)
flush_v3(foo&):
lea rax, [rdi+255]
or rax, 63
.L11:
clflush [rdi]
add rdi, 64
cmp rdi, rax
jbe .L11
ret
Run Code Online (Sandbox Code Playgroud)
如果您不幸使用,GCC 不会展开,也不会进行任何更好的优化alignas(64) struct foo{...};。您可以使用if (alignof(mystruct) >= 64) { ... }检查是否需要特殊处理来让 GCC 更好地优化,否则只需使用end = p + sizeof(mystruct);orend = (const char*)(&mystruct+1) - 1;或类似的。
(在 C 中,#include <stdalign.h>对于 #define foralignas()和alignof()C++ 等,而不是 ISO C11_Alignas和_Alignof关键字。)
另一种选择是这样,但它更笨重并且需要更多的设置工作。
const int LINESIZE = 64;
uintptr_t line = (uintptr_t)&mystruct & -LINESIZE;
uintptr_t lastline = ((uintptr_t)&mystruct + sizeof(mystruct) - 1) & -LINESIZE;
do { // always at least one flush; works on small structs
_mm_clflush( (void*)line );
line += LINESIZE;
} while(line < lastline);
Run Code Online (Sandbox Code Playgroud)
257 字节的结构总是会恰好接触 5 个缓存行,无需检查。或者一个已知按 4.IDK 对齐的 260 字节结构,如果我们能让 GCC 优化基于该结构的检查。