Instruction set selection in Sun Studio and gcc (Grudge match: -xarch versus -fast)
By Chris Quenelle on Jul 17, 2006
The right way to control compiler code generation has proven to be a bit a little tricky. Every compiler needs a way for the user say what kind of machines the program should run on. Different machines have different chips, different chips support different instructions. Closely related to this is a way to say which machines the program should run best on.
SPARC spoiled us. We were used to a list of instructions sets that was almost linear in its availability. Version N of SPARC always included all the instructions from version N-1. (Apart from a 10 year old anecdote which I omitted. You can thank me later. ;-))
Solaris spoiled us. When 64-bit Solaris came out, the transition was so compatible that the compiler group just treated it as another set of instructions they could use. So -xarch=v8 means 32-bit code and -xarch=v9 mean 64-bit code. Nobody thought 64-bits was a whole different universe.
Something we invented called “macro options” messed us up a little bit.
The Sun compilers in Sun Studio 11 have a general option called -xtarget which we call a “macro option”. This option “expands” into settings for -xarch, -xchip and -xcache. In theory this helps the user because they don't have to remember the details of every Sun box. In practice I'm not sure how many people use it, but we feel compelled never to remove anything. To see all the expansions of -xtarget in Sun Studio 11 check the docs. I think we smartened up after a while and stopped building the marketing name of every Sun system into the compiler, it got to be a maintenance nightmare.
The group of related options I'm talking about goes like this:
- -xarch is used to control 32-bit versus 64-bits
- -xarch is also used to control instruction set selection (x86 SSE, sparc VIS, etc)
- -xchip is used to control the timing details of code scheduling
- -xcache tells the compiler what kind of level 1, 2, and 3 caches should be assumed
Compared to gcc which uses these options:
- -m32 / -m64 controls 32-bit or 64-bit ABI
- -mtune=(pentium2,pentium3,...) controls timing and scheduling (also called -mcpu)
- -march=(same choices as -mtune) controls instruction selection
- -mmmx, -msse, -m3dnow ... These are also used to control instruction selection
- no explicit way to control cache sizes (x86 probably rolls these into chip definitions)
It turns out that having one option (-xarch) control both instruction set selection and the 32/64 ABIness of your program wasn't a good long-term solution. When people used -xarch=v9 to select a 64-bit program, we ran into glitches. Because most of our options will silently override a previous instance of the option, our macros options caused us grief. Users were saying “cc -xarch=v9 -xtarget=foo” and getting a 32-bit program. More common was to use “-xarch=v9 -fast” and get a 32-bit program. Which of course gives different results than “-fast -xarch=v9”. The next release of the Sun Studio compilers will have -m32 and -m64 options to control the data model of the program, and with luck we'll have something like a -xisa option to select groups of instructions.
Another related quirk is the need for an option to select hybrid data models. As long as there are 32-bit and 64-bit programs, each with their own strengths, someone will want to get the best of both worlds. If you want to use SPARC 64-bit registers, but want to follow the 32-bit ABI, you can use what we called the “v8plus” architecture. Seongbae wrote about it in his blog, with a link to the specification. I couldn't find a better description of it, so maybe I'll summarize it one day. To support this kind of hybrid, we ended up turning our -xarch option into a bit of a cross-product. We now have -xarch=v8a/v8b/v8plusa/v8plusb etc, etc. The proper place to control this would have been to define an option like -m32plus. I remember hearing hallway gossip about something like this on intel, so don't think sparc is the only architecture with this kind of mess. ;-)
You can also read the list of -xarch values in the -xarch documentation in the Sun Studio 11 compiler docs.