8/31/2005
Stupidity in CFLAGS
Ok people (that means Gentoo users and some of your developers out there… you know who you are), quit being retarded about CFLAGS.
Example one:
CFLAGS="-march=pentium4 -mtune=pentium4 -O3 -mmmx -msse -msse2 -pipe -ffast-math"
Is equivalent to the following:
CFLAGS="-march=pentium4 -O3 -pipe -ffast-math"
Why?
Let me explain a few things. march stands for set minimum architecture to X so you’re setting the minimum architecture to produce code for to a Pentium 4. Well guess what, GCC knows that Pentium 4s have MMX, SSE, and SSE2 which means it will automatically use those technologies. Then lastly mtune stands for use whatever set minimum architecture but add in optimized code paths for this architecture. Well then if all your code is going to come out optimized for Pentium 4, what’s the point in asking GCC to provide code branches that are optimized for a Pentium 4!? The only purpose that does if you compile with the following CFLAGS.
CFLAGS="-march=i386 -mtune=pentium2 -pipe"
So now you’re code will support ALL x86 based processors but will take advantage of the power CMOV instruction available to the Pentium and higher. This CFLAG will actually make a difference.
So please, quit with the redundant CFLAGS. They’re stupid and pointless.
12 Responses to “Stupidity in CFLAGS”
Leave a Reply
Filed under:
August 31st, 2005 at 5:17 pm
Your point is well made, but I wanted to know - do any ebuilds filter out -march, and not -mtune? I was under the impression (very possibly false) that this is the case, and having both of them in your CFLAGS doesn’t hurt, and would help optimize the package in this case. Again, could be totally wrong here.
August 31st, 2005 at 5:23 pm
Well, the reason why some people started to do the “-march={some arch} -mtune={some arch}” thing was because of a suggestion by someone on the forum a long while back that when asked why he had a “-march={some arch} -mcpu={some arch}”, he replied that some ebuilds filter out the -march flag and he wanted code for his cpu, so the -mcpu flag would still be there instead of the default -mcpu=i386 flag. I guess that thought caught on and is still around today. I think that was in the cflags suggestion thread. Oh well.
As far as the other redundant flags, I guess people don’t know what flags are included by default. I was thinking of an idea for a program like ufed/profuse, but for cflags instead. It would take away all redundant flags, and list all that are not included in the selected -0x, well, I guess if a person wants to, they could not select a -0x and select every flag they wanted.
I would hate to see the size of that make.conf file. hehe.
August 31st, 2005 at 5:48 pm
Take this program and compile it with four different command-line
switches. You will get four different ‘S’ files.
double div_d(register double n)
{
register double a = 0.0;
register double i;
register double s = 1.0;
for(i = 0.0; i < n; i+=1.0)
{
s *= -1.0;
a += s * (n / i);
}
return a;
}
$ gcc -S -O3 -fomit-frame-pointer dd.c -o dd.1.S
$ gcc -S -O3 -fomit-frame-pointer -march=pentium4 dd.c -o dd.2.S
$ gcc -S -O3 -fomit-frame-pointer -march=pentium2 dd.c -o dd.3.S
$ gcc -S -O3 -fomit-frame-pointer -march=pentium4 -mfpmath=sse dd.c -o dd.4.S
There is serious reason for complex GCC optimization flags.
P.S. From GCC documentation:
`-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’.
August 31st, 2005 at 8:41 pm
Sadly, most of this is wrong.
August 31st, 2005 at 11:40 pm
@ Sadly, most of this is wrong.
Uh, which parts exactly?
Can I fix my CFLAGS according to the original entry?
September 1st, 2005 at 1:41 am
stupid but harmless.
well except for the morons that actually use the “i use -m{tune|cpu} because sometimes -march is filtered” excuse.
ps. -march doesn’t specify minimum architecture. it’s supposed to be used to name a specific cpu type of the architecture. the -m prefix stands for machine.
September 1st, 2005 at 7:10 pm
dirtyepic, I’d like to know why that makes us morons - you haven’t said why I’m incorrect. Maybe I’m wrong about there being ebuilds that filter -march and not -m{cpu,tune}, but calling us morons without quantifying the argument isn’t exactly fair.
September 1st, 2005 at 11:33 pm
I’ve grepped the entire tree. I can’t find one instance where march is entirely removed and not replaced by a safe value. (i.e. march=pentium4 is replaced by march=pentium3) or it’s not replaced by a corresponding mtune.
So the arguement that you need mtune and march because march is filtered is false.
September 2nd, 2005 at 12:41 am
when -march is filtered, it’s filtered for a good reason. like doug says, it’s always replaced with a sane alternative. trying to get around that because you’re worried you’re not getting the full “power” out of your processor is silly, because the alternative is a broken package. if you have some other reason why you think you need -mtune then consider yourself excluded from my moron comment. i’m talking about people who freak when they see that their -march=pentium4 got replaced with pentium3 because that’ll make their l33t box so so much slower.
September 2nd, 2005 at 5:55 pm
Cardoe - that’s fair enough, I’ll remove the -mtune from my CFLAGS now and point it out to anyone I see using both. I just like to know when there’s substance backing an argument.
dirtyepic - I think you’re going against two different groups there - those with the absurdly huge CFLAGS that break packages, and those with modest ones. I know that the difference between a simple -march/-O2/-pipe and the 30 some-odd flags you see in bugzilla might be 1-2% and result in many breakages. However, my only point was that -mtune is a pretty safe optimization that does help a fair bit more than the others, and if there were packages that filtered -march without putting that in it might be worthwhile.
I’ve been proven wrong, so there’s nothing more to argue.
September 3rd, 2005 at 12:48 pm
@dirtyepic:
“when -march is filtered, it’s filtered for a good reason.”
Not exactly. -march is nothing by itself. -march=pentium4 is just a short way of saying -msse2 -mmmx -mtune=pentium4 and probably a few others. If an ebuild breaks with -march=pentium4 then one of the implied flags is breaking it and AFAIK this is never the -mtune. Sadly, ebuilds filter the entire -march, not just the breaking flag.
@Cardoe:
“i.e. march=pentium4 is replaced by march=pentium3″
And what if I have a pentium4 ? Then I want instruction scheduling for P4, not P3.
The correct replacement will be -march=pentium3 -mtune=pentium4. I have not grepped the entire tree to see if all ebuilds replace -march correctly, I just dupe it in my CFLAGS to be sure. I don’t know in how many cases it helps, it surely won’t hurt anything. Besides, unlike most ueber-secret-1337-opt1m1zat10nz -mtune matters a lot.
@Doug:
I and many others prefer to add -mtune to CFLAGS instead of checking a huge ebuild tree with know QA issues to see if everything is replaced properly. This hardly makes anyone “retarded”.
“march stands for set minimum architecture to X”
No. It stands for set architecture _exactly_ to X. The fact that all Intel x86 CPUs are backward comaptible is a different story.
“mtune stands for use whatever set minimum architecture but add in optimized code paths for this architecture.”
I think that a more correct wording will be: mtune=X stands for align instructions properly to fit in the X CPU cache line, etc.
AFAIK this is called instruction scheduling.
-mtune is independent of -march.
“the only purpose that does if you compile with the following CFLAGS. CFLAGS=”-march=i386 -mtune=pentium2 -pipe”"
EXACTLY ! Which is precisely what will happen if an ebuild replaces -march=pentium4 with -march=pentium3 or -march=i386 ( the default if no -march is specified ). You will get -march=i386 -mtune=pentium4. If you did not dupe -mtune you would have gotten -march=i386 -mtune=i386 which will hurt performance and won’t affect stability of the code at all !
September 4th, 2005 at 12:47 pm
Hey Ciaran, you’re an asshole!