forked from intelxed/xed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chipmodel.py
executable file
·367 lines (311 loc) · 12.2 KB
/
chipmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python
# -*- python -*-
#BEGIN_LEGAL
#
#Copyright (c) 2019 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#END_LEGAL
from __future__ import print_function
import sys
import os
import re
import enum_txt_writer
import codegen
import genutil
def _die(s):
genutil.die(s)
def filter_comments(lines):
n = []
for line in lines:
t = re.sub('#.*','',line)
t = t.strip()
if t:
n.append(t)
return n
all_of_pattern = re.compile(r'ALL_OF[(](?P<chip>[A-Z0-9a-z_]+)[)]')
not_pattern = re.compile(r'NOT[(](?P<ext>[A-Z0-9a-z_]+)[)]')
common_subset_pattern = re.compile(r'COMMON_SUBSET[(](?P<chip1>[A-Z0-9a-z_]+),(?P<chip2>[A-Z0-9a-z_]+)[)]')
def uniquify_list(l):
d = {}
for a in l:
d[a]=True
return list(d.keys())
def parse_lines(input_file_name, lines): # returns a dictionary
"""Return a list of chips and a dictionary indexed by chip containing
lists of isa-sets """
d = {}
chips = []
for line in lines:
if line.find(':') == -1:
_die("reading file {}: missing colon in line: {}".format(
input_file_name, line))
try:
(chip, extensions) = line.split(':')
except:
_die("Bad line: {}".format(line))
chip = chip.strip()
chips.append(chip)
extensions = extensions.split()
if chip in d:
_die("Duplicate definition of %s in %s" %
(chip, input_file_name))
if chip == 'ALL':
_die("Cannot define a chip named 'ALL'." +
" That name is reserved.")
d[chip] = extensions
return (chips,d)
def _feature_index(all_features, f):
try:
return all_features.index(f)
except:
_die("Did not find isa set %s in list\n" % (f))
def expand_chip(chip, d):
new_ext = []
# ALL_OF(chip)
for ext in d[chip]:
all_of_match = all_of_pattern.match(ext)
if all_of_match:
sub_chip = all_of_match.group('chip')
expand_chip(sub_chip, d)
new_ext.extend(d[sub_chip])
# COMMON_SUBSET(chip1,chip2)
for ext in d[chip]:
common_subset_match = common_subset_pattern.match(ext)
if common_subset_match:
sub_chip1 = common_subset_match.group('chip1')
sub_chip2 = common_subset_match.group('chip2')
expand_chip(sub_chip1, d)
expand_chip(sub_chip2, d)
exts1 = set(d[sub_chip1])
exts2 = set(d[sub_chip2])
common = exts1.intersection(exts2)
new_ext.extend(list(common))
# NOT(ext)
for ext in d[chip]:
not_ext_match = not_pattern.match(ext)
if not_ext_match:
ext = not_ext_match.group('ext')
while ext in new_ext:
new_ext.remove(ext) # remove from the incoming stuff
# add all the conventional extensions
for ext in d[chip]:
if (all_of_pattern.match(ext) or
common_subset_pattern.match(ext) or
not_pattern.match(ext) ):
pass
else:
new_ext.append(ext)
d[chip] = uniquify_list(new_ext)
def recursive_expand(d):
'''d is a dict of lists. The lists contain extension names, and 3
operators: ALL_OF(chip), NOT(extension), and
COMMON_SUBSET(chip1,chip2). Before inserting the extensions
from an ALL_OF(x) reference we must remove the all of the
NOT(y) in the extensions of x. This is because chip z might
re-add y and we don't want the NOT(y) from x to mess that up.
'''
for chip in d.keys():
expand_chip(chip,d)
def read_database(filename):
lines = open(filename,'r').readlines()
lines = filter_comments(lines)
lines = genutil.process_continuations(lines)
# returns a list and a dictionary
(chips,chip_features_dict) = parse_lines(filename,lines)
recursive_expand(chip_features_dict)
return (chips,chip_features_dict)
def _format_names(lst):
cols = 4
lines = ('\t'.join(lst[i:i+cols]) for i in range(0,len(lst),cols))
return '\n\t'.join(lines)
def dump_chip_hierarchy(arg, chips, chip_features_dict):
fe = codegen.xed_file_emitter_t(arg.xeddir,
arg.gendir,
'cdata.txt',
shell_file=True)
fe.start(full_header=False)
for c in chips:
fl = chip_features_dict[c]
fl.sort()
s = "{} :\n".format(c)
s = s + '\t' + _format_names(fl) + '\n'
fe.write(s)
fe.close()
return fe.full_file_name
def add_all_chip(d):
# the XED_ISA_SET_ enum
isa_set = set()
for vl in list(d.values()):
for v in vl:
isa_set.add(v.upper())
isa_set = list(isa_set)
isa_set.sort()
d['ALL'] = isa_set
isa_set = ['INVALID'] + isa_set
return isa_set
def _check_in_chip_hierarchy_not_instructions(isaset_ch, isaset_inst):
genutil.msgb("FROM CHIP MODEL", isaset_ch)
genutil.msgb("FROM INSTRUCTIONS ", isaset_inst)
missing = []
for v in isaset_ch: # stuff from the chip hierarchy model
if v in ['INVALID']:
continue
if v not in isaset_inst: # stuff from the instructions
missing.append(v)
genutil.warn("isa_set referenced by chip model hierarchy, " +
"but not used by any instructions: {}".format(v))
return missing
def _check_in_instructions_not_chip_hierarchy(isaset_ch, isaset_inst):
missing = []
for v in isaset_inst: # stuff from the instructions
if v in ['INVALID']:
continue
if v not in isaset_ch: # stuff from the chip hierarchy model
missing.append(v)
genutil.warn("isa_set referenced by instructions, " +
"but not part of any chip: {}".format(v))
return missing
def work(arg):
(chips,chip_features_dict) = read_database(arg.input_file_name)
isa_set_per_chip_fn = dump_chip_hierarchy(arg, chips, chip_features_dict)
# the XED_CHIP_ enum
chips.append("ALL")
chip_enum = enum_txt_writer.enum_info_t(['INVALID'] + chips,
arg.xeddir,
arg.gendir,
'xed-chip',
'xed_chip_enum_t',
'XED_CHIP_',
cplusplus=False)
chip_enum.print_enum()
chip_enum.run_enumer()
# Add the "ALL" chip
# the XED_ISA_SET_ enum
isa_set = add_all_chip(chip_features_dict)
# missing1 is extra stuff in chip model hierarchy that usually
# needs to be deleted.
missing1=_check_in_chip_hierarchy_not_instructions(isa_set, arg.isa_sets_from_instr)
# missing2 is extra stuff in the instructions, not in chip
# hierarchy that usually needs to be part of some chip.
missing2=_check_in_instructions_not_chip_hierarchy(isa_set, arg.isa_sets_from_instr)
if missing2:
if arg.add_orphans_to_future:
# add missing2 to "FUTURE" and "ALL" chips.
chip_features_dict['ALL'].extend(missing2)
if 'FUTURE' in chip_features_dict:
genutil.warn("Adding {} to FUTURE chip".format(missing2))
chip_features_dict['FUTURE'].extend(missing2)
isa_set.extend(missing2)
else:
_die("These need to be part of some chip: {}".format(missing2))
isa_set_enum = enum_txt_writer.enum_info_t(isa_set,
arg.xeddir,
arg.gendir,
'xed-isa-set',
'xed_isa_set_enum_t',
'XED_ISA_SET_',
cplusplus=False)
isa_set_enum.print_enum()
isa_set_enum.run_enumer()
# the initialization file and header
chip_features_cfn = 'xed-chip-features-table.c'
chip_features_hfn = 'xed-chip-features-table.h'
cfe = codegen.xed_file_emitter_t(arg.xeddir,
arg.gendir,
chip_features_cfn,
shell_file=False)
private_gendir = os.path.join(arg.gendir,'include-private')
hfe = codegen.xed_file_emitter_t(arg.xeddir,
private_gendir,
chip_features_hfn,
shell_file=False)
for header in [ 'xed-isa-set-enum.h', 'xed-chip-enum.h' ]:
cfe.add_header(header)
hfe.add_header(header)
cfe.start()
hfe.start()
cfe.write("xed_uint64_t xed_chip_features[XED_CHIP_LAST][5];\n")
hfe.write("extern xed_uint64_t xed_chip_features[XED_CHIP_LAST][5];\n")
fo = codegen.function_object_t('xed_init_chip_model_info', 'void')
fo.add_code_eol("const xed_uint64_t one=1")
# make a set for each machine name
spacing = "\n |"
for c in chips:
s0 = ['0']
s1 = ['0']
s2 = ['0']
s3 = ['0']
s4 = ['0']
# loop over the features
for f in chip_features_dict[c]:
feature_index = _feature_index(isa_set,f)
if feature_index < 64:
s0.append('(one<<XED_ISA_SET_%s)' % (f))
elif feature_index < 128:
s1.append('(one<<(XED_ISA_SET_%s-64))' % (f))
elif feature_index < 192:
s2.append('(one<<(XED_ISA_SET_%s-128))' % (f))
elif feature_index < 256:
s3.append('(one<<(XED_ISA_SET_%s-192))' % (f))
elif feature_index < 320:
s4.append('(one<<(XED_ISA_SET_%s-256))' % (f))
else:
_die("Feature index > 320. Need another features array")
s0s = spacing.join(s0)
s1s = spacing.join(s1)
s2s = spacing.join(s2)
s3s = spacing.join(s3)
s4s = spacing.join(s4)
for i,x in enumerate([s0s, s1s, s2s,s3s, s4s]):
fo.add_code_eol("xed_chip_features[XED_CHIP_{}][{}] = {}".format(c,i,x) )
# figure out which chips support AVX512 for ILD evex processing
cfe.write("xed_bool_t xed_chip_supports_avx512[XED_CHIP_LAST];\n")
hfe.write("extern xed_bool_t xed_chip_supports_avx512[XED_CHIP_LAST];\n")
for c in chips:
supports_avx512=False
for f in chip_features_dict[c]:
if 'AVX512' in f:
supports_avx512=True
break
v = '1' if supports_avx512 else '0'
fo.add_code_eol('xed_chip_supports_avx512[XED_CHIP_{}]={}'.
format(c, v))
cfe.write(fo.emit())
hfe.write(fo.emit_header())
cfe.close()
hfe.close()
return ( [ isa_set_per_chip_fn,
chip_enum.hdr_full_file_name,
chip_enum.src_full_file_name,
isa_set_enum.hdr_full_file_name,
isa_set_enum.src_full_file_name,
hfe.full_file_name,
cfe.full_file_name],
chips, isa_set)
class args_t(object):
def __init__(self):
self.input_file_name = None
self.xeddir = None
self.gendir = None
self.add_orphans_to_future = False
self.isa_sets_from_instr = None
if __name__ == '__main__':
arg = args_t()
arg.input_file_name = 'datafiles/xed-chips.txt'
arg.xeddir = '.'
arg.gendir = 'obj'
files_created,chips,isa_set = work(arg)
print("Created files: %s" % (" ".join(files_created)))
sys.exit(0)