当编译程序在虚拟机中运行时,march 和 mtune 应该设置成什么?

Rus*_*uss 5 linux virtualization gcc compiler-optimization

由于虚拟机从属于主机所提供的任何内容,因此应该向 gcc 提供哪些编译器标志?

我通常认为这-march=native就是您在编译专用盒子时会使用的内容,但是本文-march=native中将指出的详细细节使我对使用它非常谨慎。

那么...在虚拟机内-march设置什么?-mtune


对于一个具体的例子...

我现在的具体情况是在基于 KVM 的“云”主机内的 Linux 客户机中编译 python(以及更多),我无法真正控制主机硬件(除了“简单”的东西,例如 CPU GHz m CPU 计数,以及可用内存)。目前,cpuinfo告诉我我有一个“AMD Opteron(tm) 处理器 6176”,但老实说,我(还)不知道这是否可靠,以及来宾是否可以移动到我身上的不同架构以满足主机的基础设施洗牌需求(听起来很毛茸茸/不太可能)。

我真正能保证的是我的操作系统,它是一个 64 位 Linux 内核,它uname -m产生x86_64.

irr*_*ohn 4

GCC 4.6.3 标准 C++ 库手册第3.17.14 Intel 386 和 AMD x86-64 选项的一些不完整和无序的摘录(我希望是相关的)。

-march=cpu-type
  Generate instructions for the machine type cpu-type.  
  The choices for cpu-type are the same as for -mtune.  
  Moreover, specifying -march=cpu-type implies -mtune=cpu-type. 

-mtune=cpu-type
  Tune to cpu-type everything applicable about the generated code,  
  except for the ABI and the set of available instructions.  
  The choices for cpu-type are:
    generic
      Produce code optimized for the most common IA32/AMD64/EM64T processors. 
    native
      This selects the CPU to tune for at compilation time by determining
      the processor type of the compiling machine. 
      Using -mtune=native will produce code optimized for the local machine
      under the constraints of the selected instruction set.
      Using -march=native will enable all instruction subsets supported by
      the local machine (hence the result might not run on different machines). 
Run Code Online (Sandbox Code Playgroud)

我发现最有趣的是specifying -march=cpu-type implies -mtune=cpu-type。我对其余部分的看法是,如果您同时指定两者 -march,那么-mtune您可能会过于接近调整过度。

我的建议是直接使用-m64,因为你在 x86-64 Linux 中运行,所以你应该足够安全,对吗?

但是,如果您不需要在其他环境中运行并且您感觉幸运且具有容错能力,那么-march=native也可能适合您。

-m32
  The 32-bit environment sets int, long and pointer to 32 bits  
  and generates code that runs on any i386 system.     
-m64
  The 64-bit environment sets int to 32 bits and long and pointer  
  to 64 bits and generates code for AMD's x86-64 architecture.
Run Code Online (Sandbox Code Playgroud)

物有所值 ...

出于好奇,我尝试使用您引用的文章中描述的技术。我在 64 位 Ubuntu 12.04 中测试了 gcc v4.6.3,该版本作为 VMware Player 来宾运行。VMware VM 在使用 Intel Pentium 双核 E6500 CPU 的 Windows 7 桌面上运行。

gcc 选项-m64被替换为 just -march=x86-64 -mtune=generic

然而,编译-march=native导致 gcc 使用下面所有更具体的编译器选项。

-march=core2 -mtune=core2 -mcx16 
-mno-abm -mno-aes -mno-avx -mno-bmi -mno-fma -mno-fma4 -mno-lwp 
-mno-movbe -mno-pclmul -mno-popcnt -mno-sse4.1 -mno-sse4.2 
-mno-tbm -mno-xop -msahf --param l1-cache-line-size=64 
--param l1-cache-size=32 --param l2-cache-size=2048
Run Code Online (Sandbox Code Playgroud)

所以,是的,正如 gcc 文档所述,“使用 -march=native ...结果可能无法在不同的机器上运行”。为了安全起见,您可能应该只使用-m64或者它-march=x86-64 -mtune=generic对于您的编译来说是明显等效的。

我不明白你会遇到什么问题,因为这些编译器选项的目的是 gcc 将生成能够在任何x86-64/amd64 兼容 CPU 上正确运行的代码。(不?)

坦率地说,我对 gcc -march=nativeCPU 选项的具体程度感到惊讶。我不知道如何使用 CPU 的 L1 缓存大小 32k 来微调生成的代码。但显然,如果有办法做到这一点,那么 using-march=native将允许 gcc做到这一点。

我想知道这是否会带来任何明显的性能提升?