1

GCC/Clang supports compilation options -march and -mcpu.

  1. What exactly is architecture in the context of -march ?
  2. What exactly is processor (cpu) in the context of option -mcpu ?
  3. What is the difference between architecture and processor(cpu) ?

Thanks.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Pendyala
  • 585
  • 1
  • 6
  • 17
  • My understanding is that it's mostly relevant for ISAs like ARM that have official ISA-version numbers, separate from CPU model numbers. Like `-mcpu=cortex-m3` to set the relevant arch= option, and set tune=cortex-m3. On x86-64, we only ever use `-march`, not `-mcpu`, but with names like `-march=skylake` that are named after a CPU. – Peter Cordes Oct 13 '21 at 09:55
  • 3
    Does this answer your question? [GCC: mtune vs march vs mcpu](https://stackoverflow.com/questions/42718572) – n. m. could be an AI Oct 18 '21 at 11:47

1 Answers1

4

for the flags:

  • -march flag specifies the target architecture version, i.e. a set of ISA extensions. -march tells the compiler that it is allowed to generate special instructions to use the specific hardware features of a given architecture. (On x86, it also implies -mtune= the same thing)

  • -mtune flag specifies the target microarchitecture (specific CPU) to tune for, in terms of choices like loop unrolling thresholds or instruction choices that don't affect compatibility. -mtune without any -march tells the compiler to generate a binary with a bare-minimum, generic instruction set but also tune the resulting binary code for the specified target. The -mtune flag does not enable the compiler to use the special hardware features of the target, but can be used with it.

    It can be used with -march to set a baseline CPU feature level, but also to tune for a specific CPU, which is hopefully representative of the CPUs you care most about actually running on. Even if that's newer than the oldest CPUs that match the -march setting, i.e. which could run the binary.

  • -mcpu flag on ARM specifies the target processor to tune for, and to use any extension it supports. GCC uses this name to derive the name of the target ARM architecture (as if specified by -march), such as -mcpu=cortex-a53 implying -march= and -mtune= settings.

GCC and LLVM agree on the meanings of these flags, e.g. both treating -mcpu as a synonym for -mtune on x86-64, but being different on ARM.

example:

  • -mcpu=cortex-a8 will perform specific optimisations for the Cortex-A8 such as instruction scheduling and will produce better performing code on that core, as well as using instructions it supports but older CPUs don't.

  • -march=armv7-a just selects the ARMv7-a architecture which tells the compiler that it can use the instructions in ARMv7-a, but it will leave the performance tuning / instruction scheduling heuristics at their default values, aimed at running well across a range of CPUs of the overall architecture. (e.g. ARM in general, not restricted to ones new enough to support the selected -march, unfortunately).

  • ‑mtune=thunderx2t99, the compiler will generate a binary that is optimized for the ThunderX2 microarchitecture. This binary will not take full advantage of all the ThunderX2’s hardware features; But the binary may be somewhat optimized for the ThunderX2 and it will be more portable than a binary compiled with ‑march=armv8.1‑a for instance.

The x86 convention is to let -march imply -mtune as stated here in the docs

Links:

For further reference maybe these arm docs help

Deprecation of -mcpu as synonym of -mtune

Docs for x86 options

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
bjornaer
  • 336
  • 3
  • 9
  • 1
    Note that this question was tagged [intel], where `-mcpu` is deprecated and works differently from on ARM. (I added the [x86] tag because that's the proper tag for the architecture, rather than the vendor.) If you add something about that, this would make the answer better. (Also probably good to mention the `-mtune=` option that's also implied by `-mcpu` on ARM, or by `-march=` on x86, like this earlier duplicate does: [GCC: mtune vs march vs mcpu](https://stackoverflow.com/q/42718572) - or maybe we should just close this question as a dup and refund the bounty.) – Peter Cordes Oct 25 '21 at 09:40
  • Good addition, but your example would benefit from saying that for ARM, `-mcpu=thunderx2t99` is a concise way to write `‑mtune=thunderx2t99 -march=armv8.1-a` (If I have that correct). While for x86-64, `-mcpu=skylake` is just `-mtune=skylake` without setting `-march`. Your bolded sentence, *When GNU or LLVM compilers use the same flags on x86, the -mcpu flag is just a deprecated synonym for -mtune* is not clear. That use of "when" would imply that it's sometimes not the case on x86. (I'm guessing English might not be your native language; I'll edit.) – Peter Cordes Oct 26 '21 at 10:51
  • Thanks for the edit Peter, indeed English is not my native language haha, will revisit later today to try and improve the answer – bjornaer Oct 26 '21 at 12:06
  • I think it's pretty reasonable at this point, but certainly feel free to rephrase or reorganize some points, or add more. – Peter Cordes Oct 26 '21 at 12:12