GCC 有时不内联 std::array::operator[]
GCC sometimes doesn't inline std::array::operator[]
我有一个复杂的程序,它使用 std::array<double, N>
来获取较小的 N 值。它使用 operator[]
从这些数组中获取值。
我发现带有 -O2
或 -O3
的 GCC 6.1 不会内联这些调用,导致这些 C++ 数组比它们的 C 等效数组慢。
这是生成的程序集:
340 <std::array<double, 8ul>::operator[](unsigned long) const>:
340: 48 8d 04 f7 lea (%rdi,%rsi,8),%rax
344: c3 retq
345: 90 nop
346: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
34d: 00 00 00
为每个大小的数组发出完全相同的代码(因为没有边界检查)。
对此类数组的循环如下所示:
4c0: e8 7b fe ff ff callq 340 <std::array<double, 8ul>::operator[](unsigned long) const>
4c5: be 07 00 00 00 mov [=12=]x7,%esi
4ca: 4c 89 f7 mov %r14,%rdi
4cd: 48 89 44 24 78 mov %rax,0x78(%rsp)
...6 more copies of this...
4d2: e8 69 fe ff ff callq 340 <std::array<double, 8ul>::operator[](unsigned long) const>
4d7: 48 89 44 24 70 mov %rax,0x70(%rsp)
4dc: 31 f6 xor %esi,%esi
4de: 4c 89 ef mov %r13,%rdi
这显然很糟糕。问题是小型测试程序不会引发这种行为。
所以我的问题是:如何让 GCC 告诉我为什么它不内联这些单指令调用,and/or 让它内联它们?显然我不能修改<array>
头文件来添加__attribute__((inline))
.
GCC 5 和 6 的优化器中似乎存在错误,当 __attribute__(("unroll-loops"))
与 -ffast-math
或相关选项结合使用时会出现该错误。
您可以在此处查看实际效果:https://godbolt.org/g/ZBGCDB
如果您使用 -O3 -ffast-math
进行编译,此代码会重现错误:
#include <array>
typedef std::array<double, 2> Array;
void foo(Array& a) __attribute__((optimize("unroll-loops")));
void foo(Array& a)
{
for (size_t ii = 0; ii < a.size(); ++ii)
a[ii] = 1.0;
}
它在没有 -ffast-math
的情况下按预期工作,或者如果使用 GCC 4.9、GCC 7 或更高版本或 Clang 编译。
我有一个复杂的程序,它使用 std::array<double, N>
来获取较小的 N 值。它使用 operator[]
从这些数组中获取值。
我发现带有 -O2
或 -O3
的 GCC 6.1 不会内联这些调用,导致这些 C++ 数组比它们的 C 等效数组慢。
这是生成的程序集:
340 <std::array<double, 8ul>::operator[](unsigned long) const>:
340: 48 8d 04 f7 lea (%rdi,%rsi,8),%rax
344: c3 retq
345: 90 nop
346: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
34d: 00 00 00
为每个大小的数组发出完全相同的代码(因为没有边界检查)。
对此类数组的循环如下所示:
4c0: e8 7b fe ff ff callq 340 <std::array<double, 8ul>::operator[](unsigned long) const>
4c5: be 07 00 00 00 mov [=12=]x7,%esi
4ca: 4c 89 f7 mov %r14,%rdi
4cd: 48 89 44 24 78 mov %rax,0x78(%rsp)
...6 more copies of this...
4d2: e8 69 fe ff ff callq 340 <std::array<double, 8ul>::operator[](unsigned long) const>
4d7: 48 89 44 24 70 mov %rax,0x70(%rsp)
4dc: 31 f6 xor %esi,%esi
4de: 4c 89 ef mov %r13,%rdi
这显然很糟糕。问题是小型测试程序不会引发这种行为。
所以我的问题是:如何让 GCC 告诉我为什么它不内联这些单指令调用,and/or 让它内联它们?显然我不能修改<array>
头文件来添加__attribute__((inline))
.
GCC 5 和 6 的优化器中似乎存在错误,当 __attribute__(("unroll-loops"))
与 -ffast-math
或相关选项结合使用时会出现该错误。
您可以在此处查看实际效果:https://godbolt.org/g/ZBGCDB
如果您使用 -O3 -ffast-math
进行编译,此代码会重现错误:
#include <array>
typedef std::array<double, 2> Array;
void foo(Array& a) __attribute__((optimize("unroll-loops")));
void foo(Array& a)
{
for (size_t ii = 0; ii < a.size(); ++ii)
a[ii] = 1.0;
}
它在没有 -ffast-math
的情况下按预期工作,或者如果使用 GCC 4.9、GCC 7 或更高版本或 Clang 编译。