在上一篇博文《海光处理器与AMD Zen1的指令差异-CSDN博客》中发现,海光相比AMD,缺失了一些指令集。
那么在AMD或Intel上编译出来的程序,可以跑在海光上吗?
这个问题的关键,在于编译器默认使用哪些指令来编译程序。以CentOS8.4为例,它自带的gcc版本为gcc version 8.4.1 20200928:
[root@CentOS8 ~]# gcc -v Using built-in specs. COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) |
gcc在编译的时候,可通过-march=来指定要使用的指令范围,默认值为-march=x86-64,这个默认值是构建gcc自身时指定的,即上表中的--with-arch_32=x86-64。只要指定了--with-arch_32和--with-arch_64其中一个并且处理器支持64位,另外一个变量都会被指定为x86-64。
接下来看一下-march=x86-64代表的指令范围。
[root@CentOS8 ~]# gcc -march=x86-64 -c -Q --help=target | grep -i enabled -m128bit-long-double [enabled] -m64 [enabled] -m80387 [enabled] -malign-stringops [enabled] -mavx256-split-unaligned-load [enabled] -mavx256-split-unaligned-store [enabled] -mfancy-math-387 [enabled] -mfp-ret-in-387 [enabled] -mfxsr [enabled] -mglibc [enabled] -mhard-float [enabled] -mieee-fp [enabled] -mlong-double-80 [enabled] -mmmx [enabled] -mno-sse4 [enabled] -mpush-args [enabled] -mred-zone [enabled] -msse [enabled] -msse2 [enabled] -mstv [enabled] -mtls-direct-seg-refs [enabled] -mvzeroupper [enabled] |
这些指令代表什么意思呢?
-march=x86-64 of GCC8.4.1(CentOS8.4) | x86-64 | Instructions |
m128bit-long-double | [enabled] | sizeof(long double) is 16. |
m64 | [enabled] | Generate 64bit x86-64 code. |
m80387 | [enabled] | Use hardware fp. |
malign-stringops | [enabled] | Align destination of the string operations. |
march= | x86-64 | Generate code for given CPU. |
mavx256-split-unaligned-load | [enabled] | d Split 32-byte AVX unaligned load. |
mavx256-split-unaligned-store | [enabled] | re Split 32-byte AVX unaligned store. |
mfancy-math-387 | [enabled] | Generate sin, cos, sqrt for FPU. |
mfp-ret-in-387 | [enabled] | Return values of functions in FPU registers. |
mfxsr | [enabled] | Support FXSAVE and FXRSTOR instructions. |
mglibc | [enabled] | Use GNU C library. |
mhard-float | [enabled] | Use hardware fp. |
mieee-fp | [enabled] | Use IEEE math for fp comparisons. |
mlong-double-80 | [enabled] | Use 80-bit long double. |
mmmx | [enabled] | Support MMX built-in functions. |
mno-sse4 | [enabled] | Do not support SSE4.1 and SSE4.2 built-in functions and code generation. |
mpush-args | [enabled] | Use push instructions to save outgoing arguments. |
mred-zone | [enabled] | Use red-zone in the x86-64 code. |
msse | [enabled] | Support MMX and SSE built-in functions and code generation. |
msse2 | [enabled] | Support MMX, SSE and SSE2 built-in functions and code generation. |
mstv | [enabled] | Disable Scalar to Vector optimization pass transforming 64-bit integer computations into a vector ones. |
mtls-direct-seg-refs | [enabled] | Use direct references against %gs when accessing tls data. |
mvzeroupper | [enabled] | Generate vzeroupper instruction before a transfer of control flow out of the function. |
很明显,这个-march=x86-64代表的指令范围很小,海光全部都支持。所以,默认情况下,在AMD上编译的程序,依然可以跑在海光上。
如果gcc编译的时候指定-march=znver1,那么海光可以运行吗?答案是否定的,因为至少有2个指令集海光是不支持的:aes和pclmul。
只有一种特例:那就是程序本身并不涉及加解密,编译之后的汇编代码不包括aes和pclmul相关指令,即使编译的时候指定-march=znver1,那么这个程序还是可以跑在海光上的。
-march=??? of GCC8.4.1(CentOS8.4) | x86-64 | znver1 |
m128bit-long-double | [enabled] | [enabled] |
m64 | [enabled] | [enabled] |
m80387 | [enabled] | [enabled] |
mabm | [disabled] | [enabled] |
madx | [disabled] | [enabled] |
maes | [disabled] | [enabled] |
malign-stringops | [enabled] | [enabled] |
march= | x86-64 | znver1 |
mavx | [disabled] | [enabled] |
mavx2 | [disabled] | [enabled] |
mavx256-split-unaligned-store | [enabled] | [enabled] |
mbmi | [disabled] | [enabled] |
mbmi2 | [disabled] | [enabled] |
mclflushopt | [disabled] | [enabled] |
mclzero | [disabled] | [enabled] |
mcx16 | [disabled] | [enabled] |
mf16c | [disabled] | [enabled] |
mfancy-math-387 | [enabled] | [enabled] |
mfma | [disabled] | [enabled] |
mfp-ret-in-387 | [enabled] | [enabled] |
mfsgsbase | [disabled] | [enabled] |
mfxsr | [enabled] | [enabled] |
mglibc | [enabled] | [enabled] |
mhard-float | [enabled] | [enabled] |
mieee-fp | [enabled] | [enabled] |
mlong-double-80 | [enabled] | [enabled] |
mlzcnt | [disabled] | [enabled] |
mmmx | [enabled] | [enabled] |
mmovbe | [disabled] | [enabled] |
mmwaitx | [disabled] | [enabled] |
mpclmul | [disabled] | [enabled] |
mpopcnt | [disabled] | [enabled] |
mprfchw | [disabled] | [enabled] |
mpush-args | [enabled] | [enabled] |
mrdrnd | [disabled] | [enabled] |
mrdseed | [disabled] | [enabled] |
mred-zone | [enabled] | [enabled] |
msahf | [disabled] | [enabled] |
msha | [disabled] | [enabled] |
msse | [enabled] | [enabled] |
msse2 | [enabled] | [enabled] |
msse3 | [disabled] | [enabled] |
msse4 | [disabled] | [enabled] |
msse4.1 | [disabled] | [enabled] |
msse4.2 | [disabled] | [enabled] |
msse4a | [disabled] | [enabled] |
mssse3 | [disabled] | [enabled] |
mstv | [enabled] | [enabled] |
mtls-direct-seg-refs | [enabled] | [enabled] |
mtune= | generic | znver1 |
mvzeroupper | [enabled] | [enabled] |
mxsave | [disabled] | [enabled] |
mxsavec | [disabled] | [enabled] |
mxsaveopt | [disabled] | [enabled] |
mxsaves | [disabled] | [enabled] |
如果你觉得有用,请点赞收藏哈!