【发布时间】:2014-12-05 09:00:09
【问题描述】:
我最近遇到了一个奇怪的反优化(或者说错过了优化机会)。
考虑使用此函数将 3 位整数数组高效解包为 8 位整数。它在每次循环迭代中解压 16 个整数:
void unpack3bit(uint8_t* target, char* source, int size) {
while(size > 0){
uint64_t t = *reinterpret_cast<uint64_t*>(source);
target[0] = t & 0x7;
target[1] = (t >> 3) & 0x7;
target[2] = (t >> 6) & 0x7;
target[3] = (t >> 9) & 0x7;
target[4] = (t >> 12) & 0x7;
target[5] = (t >> 15) & 0x7;
target[6] = (t >> 18) & 0x7;
target[7] = (t >> 21) & 0x7;
target[8] = (t >> 24) & 0x7;
target[9] = (t >> 27) & 0x7;
target[10] = (t >> 30) & 0x7;
target[11] = (t >> 33) & 0x7;
target[12] = (t >> 36) & 0x7;
target[13] = (t >> 39) & 0x7;
target[14] = (t >> 42) & 0x7;
target[15] = (t >> 45) & 0x7;
source+=6;
size-=6;
target+=16;
}
}
这是为部分代码生成的程序集:
...
367: 48 89 c1 mov rcx,rax
36a: 48 c1 e9 09 shr rcx,0x9
36e: 83 e1 07 and ecx,0x7
371: 48 89 4f 18 mov QWORD PTR [rdi+0x18],rcx
375: 48 89 c1 mov rcx,rax
378: 48 c1 e9 0c shr rcx,0xc
37c: 83 e1 07 and ecx,0x7
37f: 48 89 4f 20 mov QWORD PTR [rdi+0x20],rcx
383: 48 89 c1 mov rcx,rax
386: 48 c1 e9 0f shr rcx,0xf
38a: 83 e1 07 and ecx,0x7
38d: 48 89 4f 28 mov QWORD PTR [rdi+0x28],rcx
391: 48 89 c1 mov rcx,rax
394: 48 c1 e9 12 shr rcx,0x12
398: 83 e1 07 and ecx,0x7
39b: 48 89 4f 30 mov QWORD PTR [rdi+0x30],rcx
...
它看起来很有效。只需一个shift right,后跟一个and,然后是一个store 到target 缓冲区。但是现在,看看当我将函数更改为结构中的方法时会发生什么:
struct T{
uint8_t* target;
char* source;
void unpack3bit( int size);
};
void T::unpack3bit(int size) {
while(size > 0){
uint64_t t = *reinterpret_cast<uint64_t*>(source);
target[0] = t & 0x7;
target[1] = (t >> 3) & 0x7;
target[2] = (t >> 6) & 0x7;
target[3] = (t >> 9) & 0x7;
target[4] = (t >> 12) & 0x7;
target[5] = (t >> 15) & 0x7;
target[6] = (t >> 18) & 0x7;
target[7] = (t >> 21) & 0x7;
target[8] = (t >> 24) & 0x7;
target[9] = (t >> 27) & 0x7;
target[10] = (t >> 30) & 0x7;
target[11] = (t >> 33) & 0x7;
target[12] = (t >> 36) & 0x7;
target[13] = (t >> 39) & 0x7;
target[14] = (t >> 42) & 0x7;
target[15] = (t >> 45) & 0x7;
source+=6;
size-=6;
target+=16;
}
}
我认为生成的程序集应该完全相同,但事实并非如此。这是其中的一部分:
...
2b3: 48 c1 e9 15 shr rcx,0x15
2b7: 83 e1 07 and ecx,0x7
2ba: 88 4a 07 mov BYTE PTR [rdx+0x7],cl
2bd: 48 89 c1 mov rcx,rax
2c0: 48 8b 17 mov rdx,QWORD PTR [rdi] // Load, BAD!
2c3: 48 c1 e9 18 shr rcx,0x18
2c7: 83 e1 07 and ecx,0x7
2ca: 88 4a 08 mov BYTE PTR [rdx+0x8],cl
2cd: 48 89 c1 mov rcx,rax
2d0: 48 8b 17 mov rdx,QWORD PTR [rdi] // Load, BAD!
2d3: 48 c1 e9 1b shr rcx,0x1b
2d7: 83 e1 07 and ecx,0x7
2da: 88 4a 09 mov BYTE PTR [rdx+0x9],cl
2dd: 48 89 c1 mov rcx,rax
2e0: 48 8b 17 mov rdx,QWORD PTR [rdi] // Load, BAD!
2e3: 48 c1 e9 1e shr rcx,0x1e
2e7: 83 e1 07 and ecx,0x7
2ea: 88 4a 0a mov BYTE PTR [rdx+0xa],cl
2ed: 48 89 c1 mov rcx,rax
2f0: 48 8b 17 mov rdx,QWORD PTR [rdi] // Load, BAD!
...
如您所见,我们在每个班次之前从内存中引入了额外的冗余load (mov rdx,QWORD PTR [rdi])。似乎target 指针(现在是成员而不是局部变量)在存储到它之前必须始终重新加载。 这大大减慢了代码速度(在我的测量中约为 15%)。
首先我认为 C++ 内存模型可能强制成员指针不能存储在寄存器中,但必须重新加载,但这似乎是一个尴尬的选择,因为它会使许多可行的优化变得不可能。所以我很惊讶编译器没有将target 存储在此处的寄存器中。
我尝试自己将成员指针缓存到局部变量中:
void T::unpack3bit(int size) {
while(size > 0){
uint64_t t = *reinterpret_cast<uint64_t*>(source);
uint8_t* target = this->target; // << ptr cached in local variable
target[0] = t & 0x7;
target[1] = (t >> 3) & 0x7;
target[2] = (t >> 6) & 0x7;
target[3] = (t >> 9) & 0x7;
target[4] = (t >> 12) & 0x7;
target[5] = (t >> 15) & 0x7;
target[6] = (t >> 18) & 0x7;
target[7] = (t >> 21) & 0x7;
target[8] = (t >> 24) & 0x7;
target[9] = (t >> 27) & 0x7;
target[10] = (t >> 30) & 0x7;
target[11] = (t >> 33) & 0x7;
target[12] = (t >> 36) & 0x7;
target[13] = (t >> 39) & 0x7;
target[14] = (t >> 42) & 0x7;
target[15] = (t >> 45) & 0x7;
source+=6;
size-=6;
this->target+=16;
}
}
此代码还可以生成“好的”汇编程序,而无需额外的存储。所以我的猜测是:编译器不允许提升结构的成员指针的负载,所以这样的“热指针”应该始终存储在局部变量中。
- 那么,为什么编译器无法优化这些负载?
- 是 C++ 内存模型禁止这样做吗?还是只是我的编译器的一个缺点?
- 我的猜测是否正确或无法执行优化的确切原因是什么?
使用的编译器是 g++ 4.8.2-19ubuntu1 和 -O3 优化。我还尝试了clang++ 3.4-1ubuntu3,得到了类似的结果:Clang 甚至能够使用本地 target 指针对方法进行矢量化。但是,使用 this->target 指针会产生相同的结果:在每次存储之前额外加载指针。
我检查了一些类似方法的汇编程序,结果是相同的:似乎this 的成员总是必须在存储之前重新加载,即使这样的加载可以简单地提升到循环之外。我将不得不重写大量代码来摆脱这些额外的存储,主要是通过自己将指针缓存到在热代码上方声明的局部变量中。 但我一直认为,在编译器变得如此聪明的今天,摆弄诸如在局部变量中缓存指针之类的细节肯定有资格过早优化。但这里似乎我错了。在热循环中缓存成员指针似乎是一种必要的手动优化技术。
【问题讨论】:
-
不知道为什么这会被否决 - 这是一个有趣的问题。 FWIW 我已经看到与非指针成员变量类似的优化问题,其中解决方案相似,即在方法的生命周期内将成员变量缓存在局部变量中。我猜这与别名规则有关?
-
看起来编译器没有优化,因为他无法确保不通过某些“外部”代码访问该成员。所以如果成员可以在外面修改,那么每次访问都应该重新加载。似乎被认为是一种不稳定的...
-
不使用
this->只是语法糖。问题与变量(本地与成员)的性质以及编译器从这一事实中推断出的东西有关。 -
与指针别名有关吗?
-
从语义上讲,“过早优化”仅适用于过早的优化,即在分析发现它成为问题之前。在这种情况下,您努力分析和反编译并找到问题的根源,并制定和分析解决方案。应用该解决方案绝对不会“为时过早”。
标签: c++ c++11 optimization compiler-optimization strict-aliasing