20

Is there a way to get gcc to output the available -march=arch options? I'm getting build errors (tried -march=x86_64) and I don't know what my options are.

The compiler I'm using is a proprietary wrapper around gcc that doesn't seem to like -march=skylake. The flags should be the same so I assume whatever options I'd send to gcc to dump architectures would be the same for this wrapper.

I managed to cause gcc to error with a bogus parameter and it dumped a list, but I'm not seeing that now that I'm going through a wrapper.

How can I get gcc to tell me what it supports?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Brydon Gibson
  • 1,179
  • 3
  • 11
  • 22
  • 1
    I mentioned in the question the wrapper doesn't pass this through - it's actually jumping down to `cc` which just throws an error. It's a cross compile so native won't work (however I don't really care about optimizations since it's just a benchmark test) – Brydon Gibson Nov 05 '18 at 15:05
  • 1
    You just wrote, that it doesn't like `-march=skylake`, nothing more. Is it a binary or a script file? You could use `strings` and search for things that look promising. – hellow Nov 05 '18 at 15:07
  • 1
    It's a binary - I'll trudge through it when it becomes more of a priority. I'm doing two benchmarks on different OS's on the same CPU, so for the moment optimization isn't critical, it's just that the dhrystone makefile recommends it – Brydon Gibson Nov 05 '18 at 15:20
  • 1
    Optimization like `-O2` at least is critical for benchmarking, preferably `-O3 -fprofile-use -march=native` (after a run with `-fprofile-generate`). If you meant that target-specific tuning isn't critical, then sure. – Peter Cordes Nov 06 '18 at 02:22
  • 1
    Related: https://lemire.me/blog/2018/07/25/it-is-more-complicated-than-i-thought-mtune-march-in-gcc/ (from a link-only answer) – Peter Cordes May 03 '22 at 06:27
  • [How can I determine what architectures gcc supports?](https://stackoverflow.com/q/47299458/995714) – phuclv Aug 10 '23 at 04:41

2 Answers2

21

Use gcc --target-help

-march=CPU[,+EXTENSION...]
                      generate code for CPU and EXTENSION, CPU is one of:
                       generic32, generic64, i386, i486, i586, i686,
                       pentium, pentiumpro, pentiumii, pentiumiii, pentium4,
                       prescott, nocona, core, core2, corei7, l1om, k1om,
                       iamcu, k6, k6_2, athlon, opteron, k8, amdfam10,
                       bdver1, bdver2, bdver3, bdver4, znver1, znver2,
                       btver1, btver2
...

It's often not the general architecture like x86 or x86-64 but the specific microarchitectures. But there's x86-64 (not x86_64) for a generic x86 CPU with 64-bit extensions. The full list for each architecture can be found on GCC's -march manual. For x86:

  • -march=cpu-type

    Generate instructions for the machine type cpu-type. In contrast to -mtune=cpu-type, which merely tunes the generated code for the specified cpu-type, -march=cpu-type allows GCC to generate code that may not run at all on processors other than the one indicated. Specifying -march=cpu-type implies -mtune=cpu-type.

...

https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html#index-march-13


While the baseline version of -march is -march=x86-64, the baseline / default tune option is -mtune=generic. That aims to not be terrible anywhere, avoiding performance pitfalls even at the cost of extra instructions or code size.


-march=native will pick the right arch and tune settings for the machine the compiler is running on, or tune=generic if the compiler doesn't recognize the specific model of CPU it's running on.

(e.g. old gcc on a Skylake, will still enable -mavx2 -mpopcnt -mbmi2 and so on, but will set -mtune=generic instead of something closer to appropriate.)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 2
    I don't think this actually works well - it seems to show an incomplete list, and appears under the "assembler options" heading, whereas you'd generally be looking for the options accepted by the compiler. E.g., where is sandybridge, haswell, skylake, etc in your list? On my system `g++8` gives a similarly incomplete list. – BeeOnRope Nov 03 '19 at 23:17
  • 2
    @BeeOnRope: oh no, apparently GCC does not construct this string on the fly from the same list it uses to check `arch=` options and print help for it. That's unfortunate and could be considered a bug that they're out of sync, or a design bug that they're separate at all. – Peter Cordes Nov 04 '19 at 03:29
  • 1
    @PeterCordes - well it also appears under "assembler options", so it is possible that somehow the assembler accepts a smaller list of arches? Dunno. Otherwise yes it seems like a bug. – BeeOnRope Nov 04 '19 at 03:43
  • 1
    @BeeOnRope: oh, yes GAS itself takes `-march` options. I'm not sure the GCC front-end even passes on `-march` to `as` though, at least for `.c` sources. Maybe asm source? – Peter Cordes Nov 04 '19 at 03:45
  • What is the difference between general architekture and microarchitekture ? Is is a standard? – Adam Jul 17 '21 at 14:36
  • 2
    @Adam it's defined by the CPU manufacturer. See [What is difference between architecture and microarchitecture?](https://cs.stackexchange.com/q/29460/9489), [Architecture and microarchitecture](https://stackoverflow.com/q/35200938/995714), https://en.wikichip.org/wiki/microarchitecture, https://en.wikipedia.org/wiki/Microarchitecture, https://www.quora.com/What-is-CPU-microarchitecture – phuclv Jul 17 '21 at 16:13
15

Using gcc --target-help seems like it might be the right idea, but gives an incomplete list.

One workaround on modern gcc versions is just to pass a bogus value to -march:

$ gcc --target-help -march=foo
cc1: error: bad value (‘foo’) for ‘-march=’ switch
cc1: note: valid arguments to ‘-march=’ switch are: nocona core2 nehalem corei7 westmere sandybridge corei7-avx ivybridge core-avx-i haswell core-avx2 broadwell skylake skylake-avx512 cannonlake icelake-client icelake-server bonnell atom silvermont slm knl knm x86-64 eden-x2 nano nano-1000 nano-2000 nano-3000 nano-x2 eden-x4 nano-x4 k8 k8-sse3 opteron opteron-sse3 athlon64 athlon64-sse3 athlon-fx amdfam10 barcelona bdver1 bdver2 bdver3 bdver4 znver1 btver1 btver2 native
...

Note how there are many more options compared to the output from --target-help.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386