Gentoo Archives: gentoo-dev

From: Francesco R <vivo75@×××××.com>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] Re: Move x86/amd64 CPU extensions USE flags to a new USE_EXPAND variable
Date: Mon, 13 Dec 2010 18:08:05
Message-Id: AANLkTi=h3ZXq=mhCXhf4sy01hUQ9C7oHNnuxPMQRV8G_@mail.gmail.com
In Reply to: Re: [gentoo-dev] Re: Move x86/amd64 CPU extensions USE flags to a new USE_EXPAND variable by Konstantin Tokarev
1 2010/12/13 Konstantin Tokarev <annulen@××××××.ru>
2
3 >
4 >
5 > 13.12.2010, 18:53, "Francesco R" <vivo75@×××××.com>:
6 > > 2010/12/13 Ryan Hill <dirtyepic@g.o>
7 > >> On Sun, 12 Dec 2010 09:01:13 -0400
8 > >> "Sergio D. Rodríguez Inclan" <srinclan@×××××.com> wrote:
9 > >>
10 > >>> El 12/12/2010 02:46 a.m., Ryan Hill escribió:
11 > >>> > I think the fewer sources of magic USE flags the better. Maybe we
12 > could
13 > >>> > document how to figure out what instruction sets a processor supports
14 > in the
15 > >>> > handbook instead.
16 > >>
17 > >>> A good manual would be greatly appreciated :)
18 > >>
19 > >> I wrote a guide a couple weeks ago that might be a good starting point.
20 > >>
21 > >> http://en.gentoo-wiki.com/wiki/Hardware_CFLAGS
22 > >
23 > > if I read correctly the article on the wiki it does circa what the script
24 > reported below does.
25 > > would be possible to adopt something similar for automatic C*FLAGS
26 > selection if someone step in willing to take the pain to mantain it.
27 > >
28 > > #!/usr/bin/python
29 > > # Copyright 2010 Gentoo Foundation
30 > > # Distributed under the terms of the GNU General Public License v2
31 > > # Author: Francesco Riosa
32 > > # extrapolated from http://en.gentoo-wiki.com/wiki/Hardware_CFLAGS,
33 > errors are mine
34 > >
35 > > # kate: encoding utf-8; eol unix
36 > > # kate: indent-width 4; mixedindent off; replace-tabs on;
37 > > # kate: remove-trailing-space on; space-indent on
38 > >
39 > > # echo "int main() { return 0; }" | gcc -march=native -v -E - 2>&1 | grep
40 > march
41 > > # echo "int main() { return 0; }" | gcc -march=core2 -v -Q -x c - 2>&1
42 > >
43 > > """
44 > > example output:
45 > > ./hw-cflags.py
46 > > extrapolating flags for gcc-4.4.5
47 > > useful flags: -march=core2 -msse4.1 --param l1-cache-size=32 --param
48 > l1-cache-line-size=64 --param l2-cache-size=2048 -mtune=generic
49 > > redundant: -mcx16 -msahf
50 > >
51 > > extrapolating flags for gcc-4.5.1
52 > > useful flags: -march=core2 -msse4.1 --param l1-cache-size=32 --param
53 > l1-cache-line-size=64 --param l2-cache-size=2048 -mtune=core2
54 > > redundant: -mcx16 -msahf
55 > > """
56 > >
57 > > import os
58 > > import time
59 > > import fnmatch
60 > > from subprocess import Popen, PIPE
61 > >
62 > > GCC_PATH = '/usr/bin/'
63 > > GCC_LIST = fnmatch.filter(os.listdir(GCC_PATH), 'gcc-[0-9].*')
64 > > GCC_LIST.sort()
65 > >
66 > > def extract_flags(gcccmd, header):
67 > > # get output from gcc
68 > > buf = ''
69 > > devnul = open('/dev/null', 'w')
70 > > p = Popen(gcccmd, stdin=PIPE, stdout=devnul, stderr=PIPE)
71 > > p.stdin.write("""int main() { return 0; }""")
72 > > p.stdin.close()
73 > > while p.poll() is None:
74 > > t = p.stderr.read()
75 > > buf = "buf%s" % t
76 > > time.sleep(0.01)
77 > > p.stderr.close()
78 > > devnul.close()
79 > >
80 > > # parse it
81 > > flags = []
82 > > add = False
83 > > for line in buf.split('\n'):
84 > > if line.startswith(header):
85 > > add = True
86 > > flags += line.strip().split(' ')
87 > > continue
88 > > if add:
89 > > if line.startswith(' '):
90 > > flags += line.strip().split(' ')
91 > > else:
92 > > break
93 > >
94 > > # extract flags we are interested in
95 > > t = []
96 > > march = ''
97 > > mtune = '-mtune=generic'
98 > > for i in xrange(len(flags)):
99 > > if flags[i].startswith('-m'):
100 > > if flags[i].startswith('-mtune'):
101 > > mtune = flags[i]
102 > > elif flags[i].startswith('-march'):
103 > > march = flags[i]
104 > > else:
105 > > t.append(flags[i])
106 > > elif flags[i] == '--param':
107 > > t.append("%s %s" % (flags[i], flags[i+1]))
108 > > flags = t
109 > >
110 > > return march, mtune, flags
111 > >
112 > > for gcc in GCC_LIST:
113 > >
114 > > print "extrapolating flags for %s" % gcc
115 > >
116 > > gcccmd = [ GCC_PATH + gcc, '-march=native', '-v', '-E', '-', ]
117 > > header='COLLECT_GCC_OPTIONS'
118 > > march, mtune, flags_native = extract_flags(gcccmd, header)
119 > >
120 > > gcccmd = [ GCC_PATH + gcc, march, '-v', '-Q', '-x', 'c', '-', ]
121 > > header='options enabled:'
122 > > t, t, flags_enabled = extract_flags(gcccmd, header)
123 > >
124 > > redundant_flags = []
125 > > useful_flags = []
126 > >
127 > > for x in flags_native:
128 > > if x in flags_enabled:
129 > > redundant_flags.append(x)
130 > > else:
131 > > useful_flags.append(x)
132 > >
133 > > if gcc < "gcc-4.5.0":
134 > > mtune = '-mtune=generic'
135 > >
136 > > print " useful flags: %s %s %s " % (march, " ".join(useful_flags),
137 > mtune)
138 > > print " redundant: %s" % " ".join(redundant_flags)
139 > > print
140 >
141 > Note that for some architectures (e.g, PowerPC) you need to add "-mcpu"
142 > case
143 > (-mcpu has the same meaning as -march on x86)
144 >
145 >
146 > --
147 > Regards,
148 > Konstantin
149 >
150 >
151 Assuming there is some possibility of Gentoo using some automated method of
152 choice of CFLAGS the arch maintainers should really step in and tell what
153 works on their cpus and be able to modify/disable it.
154
155 There are other shortcomings in the script, for example it does not work
156 with <gcc-4.2 because it use -march=native.
157 Also it should at least consider what's in /etc/make.conf C*FLAGS/CHOST.
158 It's really a proof of concept

Replies