Monday, January 22, 2007

GCC: -march and -mtune

Some might've noticed that the check-cpu script in the MySQL BUILD directory does not always succeed in detecting the right processor:

roland@roland-laptop:/opt/mysql/5.1/bitkeeper/BUILD$ ./check-cpu
BUILD/check-cpu: Oops, could not find out what kind of cpu this machine is using.

When you dig a little deeper into that script, you will notice that the name of you particular processor (in my case: Intel(R) Core(TM)2 CPU T5600 @ 1.83GHz) is indeed not there and thus not checked. Finally you may find that the model and name of your processor are passed to the gcc compiler.

As far as I cans see the information is used to set the values of the --mtune and --march compiler options. What those are? Hehe! Ok, here's a partial result from man gcc:

-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.

Mmm, not sure why -mtune=cpu-type is there in the first place then...


(PS: use /proc/cpuinfo to find out what kind of processor you really have....)


Anonymous said...

gcc 4.3 adds "core2" as arch. I've resisted the temptation to add that quite yet as pretty much nobody has 4.3 yet.
-- Azundris

Anonymous said...

I'm not 100% sure, but I think -mcpu doesn't break compatability with other CPUs but -march (-mtune) does...

Anonymous said...

The manual (info gcc) is your friend:

-mtune (not --mtune): Tune to CPU-TYPE everything applicable about the generated code, except for the ABI and the set of available instructions.

-march: Generate instructions for the machine type CPU-TYPE. Specifying -march=CPU-TYPE implies -mtune=CPU-TYPE.

-mcpu: A deprecated synonym for -mtune.

Unfortunately the manual for GCC 4.1 will not tell you what CPU-TYPE to use for core2. Dunno about 4.2, though.

//Carl T

Anonymous said...

-mtune is used predominantly for instruction scheduling, gcc uses it when it's reordering instructions to favour a specific micro-architecture over others. Code which is tuned for one microarchitecture will run on another chip of the same architecture, but the reordering may hurt performance on these chips.

-mcpu tells it to generate code SPECIFICALLY for a certain chip, which obviously includes instruction reordering (thus why -mcpu implies -mtune), but will also cause the compiler to emit instructions specific to the target processor. This does not guarantee binary compatibility with other chips based on a different microarchitecture.

Pádraig Brady said...

Here is a script that finds the
optimum gcc cpu settings for your
cpu in conjuntion with your currently
installed gcc.

Anonymous said...

This is why I hate blogs / wiki answers. People post answers when they don't really know all the facts, even *after* the issue has been correctly addressed by a previous comment! I'm concluding that these type of people just like to here themselves type. :|

-march={cpu} : Generate a binary *only* for the specific cpu type (and later, compatible models [most later cpu's support earlier optimizations, or gracefully bypass them]). This flag should generate the faster code on x86 architectures. Fall back to the most recent (mainstream) model in your vendors line if yours is not yet available.

-mtune={cpu} : Tunes the binary for given cpu on x86 architecture. This means that the binary will still execute on a 386 (if it did in the first place), while also including optimization for the cpu supplied to the '-mtune=' switch.

-mcpu={cpu} : Deprecated (phased out) for the x86 architecture.

Year-to-Date on Synapse Analytics 5: Using Window Functions

For one of our Just-BI customers we implemented a Year-to-Date calculation in a Azure Synapse Backend. We encountered a couple of approache...