【发布时间】:2019-10-05 12:40:03
【问题描述】:
我开始使用 SSE/AVX 内部函数已经有一段时间了。我最近开始为矩阵转置编写标题。我使用了很多if constexpr 分支,以便编译器始终根据一些模板参数选择最佳指令集。现在我想通过objdump查看本地反汇编来检查一切是否按预期工作。使用 Clang 时,我得到一个清晰的输出,它基本上只包含与使用的内在函数相对应的汇编指令。但是,如果我使用 GCC,反汇编会非常臃肿,并带有额外的指令。对 Godbolt 的快速检查表明,GCC 反汇编中的那些额外指令不应该存在。
这是一个小例子:
#include <x86intrin.h>
#include <array>
std::array<__m256, 1> Test(std::array<__m256, 1> a)
{
std::array<__m256, 1> b;
b[0] = _mm256_unpacklo_ps(a[0], a[0]);
return b;
}
我用-march=native -Wall -Wextra -Wpedantic -pthread -O3 -DNDEBUG -std=gnu++1z 编译。然后我在目标文件上使用objdump -S -Mintel libassembly.a > libassembly.dump。对于 Clang (6.0.0),结果是:
In archive libassembly.a:
libAssembly.cpp.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z4TestSt5arrayIDv8_fLm1EE>:
0: c4 e3 7d 04 c0 50 vpermilps ymm0,ymm0,0x50
6: c3 ret
与Godbolt一样返回:Godbolt - Clang 6.0.0
对于 GCC (7.4),输出为
In archive libassembly.a:
libAssembly.cpp.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_Z4TestSt5arrayIDv8_fLm1EE>:
0: 4c 8d 54 24 08 lea r10,[rsp+0x8]
5: 48 83 e4 e0 and rsp,0xffffffffffffffe0
9: c5 fc 14 c0 vunpcklps ymm0,ymm0,ymm0
d: 41 ff 72 f8 push QWORD PTR [r10-0x8]
11: 55 push rbp
12: 48 89 e5 mov rbp,rsp
15: 41 52 push r10
17: 48 83 ec 28 sub rsp,0x28
1b: 64 48 8b 04 25 28 00 mov rax,QWORD PTR fs:0x28
22: 00 00
24: 48 89 45 e8 mov QWORD PTR [rbp-0x18],rax
28: 31 c0 xor eax,eax
2a: 48 8b 45 e8 mov rax,QWORD PTR [rbp-0x18]
2e: 64 48 33 04 25 28 00 xor rax,QWORD PTR fs:0x28
35: 00 00
37: 75 0c jne 45 <_Z4TestSt5arrayIDv8_fLm1EE+0x45>
39: 48 83 c4 28 add rsp,0x28
3d: 41 5a pop r10
3f: 5d pop rbp
40: 49 8d 62 f8 lea rsp,[r10-0x8]
44: c3 ret
45: c5 f8 77 vzeroupper
48: e8 00 00 00 00 call 4d <_Z4TestSt5arrayIDv8_fLm1EE+0x4d>
如您所见,还有很多附加说明。与此相反,Godbolt 不包含所有这些额外的指令:Godbolt - GCC 7.4
那么这里发生了什么?我刚刚开始学习汇编,所以对于有汇编经验的人来说可能完全清楚,但我有点困惑为什么 GCC 在我的机器上创建这些额外的指令。
提前问候并感谢您。
编辑
为避免进一步混淆,我只是使用以下代码编译:
gcc-7 -I/usr/local/include -O3 -march=native -Wall -Wextra -Wpedantic -pthread -std=gnu++1z -o test.o -c /<PathToFolder>/libAssembly.cpp
输出保持不变。我不确定这是否相关,但它会生成警告:
warning: ignoring attributes on template argument ‘__m256 {aka __vector(8) float}’ [-Wignored-attributes]
通常我会忽略这个警告,这应该不是问题:
Implication of GCC warning: ignoring attributes on template argument (-Wignored-attributes)
处理器是Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
这里是gcc -v:
gcc-7 -v
Using built-in specs.
COLLECT_GCC=gcc-7
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
【问题讨论】:
-
我无法使用本地 GCC 安装重现它。
-
why GCC- 等等,哪个 gcc?您发布了一个指向 gcc 7.4 的 Godbolt 链接,该链接生成vunpcklps ymm0, ymm0, ymm0。那么你呈现的输出是什么?适合你的机器吗?你用-march=native,你的本地机器支持SSE/AVX吗? -
在“坏”的例子中,你确定你编译时启用了优化吗?您在问题中提到了
-O3,但在我看来,这就像未优化的输出。 -
@JasonR:很确定。我正在使用 CMake,详细输出告诉我该文件是使用
-march=native -Wall -Wextra -Wpedantic -pthread -O3 -DNDEBUG -std=gnu++1z -o CMakeFiles/assembly.dir/libAssembly.cpp.o构建的 -
@KamilCuk 输出是在我的机器上使用 gcc 7.4 生成的,我的机器支持 AVX2 指令。
标签: c++ gcc intrinsics avx