4
votes

I know that it's generally recommended to use -march=native (if you're compiling for the machine you're on), so that gcc determines your arch and cputype and generates the most machine-specific code, but how does it do that?

Does it use cpuid (on either arm or x86)? What techniques are used on platforms without a cpuid-like instruction?

1

1 Answers

4
votes

Good question.

My intuition was that it would go check /proc/cpuinfo. Actually it depends on the architecture it has been compiled to run on. It seems like host_detect_local_cpu is the function responsible for that. Its job is to replace -march=native by either the good -march=<...> or a set of flags (-mmmx, -mno-avx, etc) that match as closely as possible the current cpu. Examples for i386 and arm. i386 uses cpuid directly to check for each and every possible feature. arm checks in /proc/cpuinfo for the CPU part line and has a table that maps from a CPU part value to a generation of the architecture and use this directly in -march=<...>.

Just for fun I check on other architectures (I am not familiar with them).

  • solaris on sparc: uses the kstat interface
  • linux on sparc: uses /proc/cpuinfo
  • alpha: uses the implver instruction
  • darwin on rs6000: uses the hw.cpusubtype system call
  • freebsd on rs6000: uses hardcoded powerpc
  • linux on rs6000: checks in /proc/self/auxv for the platform value in the elf interpreter of its own process
  • aix on rs6000: uses _system_configuration (apparently a global structure)