forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathcommon.py
266 lines (225 loc) · 9.72 KB
/
common.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
from __future__ importprint_function
importre
importstring
importsubprocess
importsys
importcopy
ifsys.version_info[0] >2:
classstring:
expandtabs=str.expandtabs
else:
importstring
##### Common utilities for update_*test_checks.py
defshould_add_line_to_output(input_line, prefix_set):
# Skip any blank comment lines in the IR.
ifinput_line.strip() ==';':
returnFalse
# Skip any blank lines in the IR.
#if input_line.strip() == '':
# return False
# And skip any CHECK lines. We're building our own.
m=CHECK_RE.match(input_line)
ifmandm.group(1) inprefix_set:
returnFalse
returnTrue
# Invoke the tool that is being tested.
definvoke_tool(exe, cmd_args, ir):
withopen(ir) asir_file:
# TODO Remove the str form which is used by update_test_checks.py and
# update_llc_test_checks.py
# The safer list form is used by update_cc_test_checks.py
ifisinstance(cmd_args, list):
stdout=subprocess.check_output([exe] +cmd_args, stdin=ir_file)
else:
stdout=subprocess.check_output(exe+' '+cmd_args,
shell=True, stdin=ir_file)
ifsys.version_info[0] >2:
stdout=stdout.decode()
# Fix line endings to unix CR style.
returnstdout.replace('\r\n', '\n')
##### LLVM IR parser
RUN_LINE_RE=re.compile('^\s*[;#]\s*RUN:\s*(.*)$')
CHECK_PREFIX_RE=re.compile('--?check-prefix(?:es)?[= ](\S+)')
CHECK_RE=re.compile(r'^\s*[;#]\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
OPT_FUNCTION_RE=re.compile(
r'^\s*define\s+(?:internal\s+)?[^@]*@(?P<func>[\w-]+?)\s*\('
r'(\s+)?[^)]*[^{]*\{\n(?P<body>.*?)^\}$',
flags=(re.M|re.S))
ANALYZE_FUNCTION_RE=re.compile(
r'^\s*\'(?P<analysis>[\w\s-]+?)\'\s+for\s+function\s+\'(?P<func>[\w-]+?)\':'
r'\s*\n(?P<body>.*)$',
flags=(re.X|re.S))
IR_FUNCTION_RE=re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
TRIPLE_IR_RE=re.compile(r'^\s*target\s+triple\s*=\s*"([^"]+)"$')
TRIPLE_ARG_RE=re.compile(r'-mtriple[= ]([^ ]+)')
MARCH_ARG_RE=re.compile(r'-march[= ]([^ ]+)')
SCRUB_LEADING_WHITESPACE_RE=re.compile(r'^(\s+)')
SCRUB_WHITESPACE_RE=re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
SCRUB_TRAILING_WHITESPACE_RE=re.compile(r'[ \t]+$', flags=re.M)
SCRUB_KILL_COMMENT_RE=re.compile(r'^ *#+ +kill:.*\n')
SCRUB_LOOP_COMMENT_RE=re.compile(
r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
defscrub_body(body):
# Scrub runs of whitespace out of the assembly, but leave the leading
# whitespace in place.
body=SCRUB_WHITESPACE_RE.sub(r' ', body)
# Expand the tabs used for indentation.
body=string.expandtabs(body, 2)
# Strip trailing whitespace.
body=SCRUB_TRAILING_WHITESPACE_RE.sub(r'', body)
returnbody
defdo_scrub(body, scrubber, scrubber_args, extra):
ifscrubber_args:
local_args=copy.deepcopy(scrubber_args)
local_args[0].extra_scrub=extra
returnscrubber(body, *local_args)
returnscrubber(body, *scrubber_args)
# Build up a dictionary of all the function bodies.
classfunction_body(object):
def__init__(self, string, extra):
self.scrub=string
self.extrascrub=extra
def__str__(self):
returnself.scrub
defbuild_function_body_dictionary(function_re, scrubber, scrubber_args, raw_tool_output, prefixes, func_dict, verbose):
forminfunction_re.finditer(raw_tool_output):
ifnotm:
continue
func=m.group('func')
body=m.group('body')
scrubbed_body=do_scrub(body, scrubber, scrubber_args, extra=False)
scrubbed_extra=do_scrub(body, scrubber, scrubber_args, extra=True)
if'analysis'inm.groupdict():
analysis=m.group('analysis')
ifanalysis.lower() !='cost model analysis':
print('WARNING: Unsupported analysis mode: %r!'% (analysis,), file=sys.stderr)
iffunc.startswith('stress'):
# We only use the last line of the function body for stress tests.
scrubbed_body='\n'.join(scrubbed_body.splitlines()[-1:])
ifverbose:
print('Processing function: '+func, file=sys.stderr)
forlinscrubbed_body.splitlines():
print(' '+l, file=sys.stderr)
forprefixinprefixes:
iffuncinfunc_dict[prefix] andstr(func_dict[prefix][func]) !=scrubbed_body:
iffunc_dict[prefix][func] andfunc_dict[prefix][func].extrascrub==scrubbed_extra:
func_dict[prefix][func].scrub=scrubbed_extra
continue
else:
ifprefix==prefixes[-1]:
print('WARNING: Found conflicting asm under the '
'same prefix: %r!'% (prefix,), file=sys.stderr)
else:
func_dict[prefix][func] =None
continue
func_dict[prefix][func] =function_body(scrubbed_body, scrubbed_extra)
##### Generator of LLVM IR CHECK lines
SCRUB_IR_COMMENT_RE=re.compile(r'\s*;.*')
# Match things that look at identifiers, but only if they are followed by
# spaces, commas, paren, or end of the string
IR_VALUE_RE=re.compile(r'(\s+)%([\w\.\-]+?)([,\s\(\)]|\Z)')
# Create a FileCheck variable name based on an IR name.
defget_value_name(var):
ifvar.isdigit():
var='TMP'+var
var=var.replace('.', '_')
var=var.replace('-', '_')
returnvar.upper()
# Create a FileCheck variable from regex.
defget_value_definition(var):
return'[['+get_value_name(var) +':%.*]]'
# Use a FileCheck variable.
defget_value_use(var):
return'[['+get_value_name(var) +']]'
# Replace IR value defs and uses with FileCheck variables.
defgenericize_check_lines(lines, is_analyze):
# This gets called for each match that occurs in
# a line. We transform variables we haven't seen
# into defs, and variables we have seen into uses.
deftransform_line_vars(match):
var=match.group(2)
ifvarinvars_seen:
rv=get_value_use(var)
else:
vars_seen.add(var)
rv=get_value_definition(var)
# re.sub replaces the entire regex match
# with whatever you return, so we have
# to make sure to hand it back everything
# including the commas and spaces.
returnmatch.group(1) +rv+match.group(3)
vars_seen=set()
lines_with_def= []
fori, lineinenumerate(lines):
# An IR variable named '%.' matches the FileCheck regex string.
line=line.replace('%.', '%dot')
# Ignore any comments, since the check lines will too.
scrubbed_line=SCRUB_IR_COMMENT_RE.sub(r'', line)
ifis_analyze==False:
lines[i] =IR_VALUE_RE.sub(transform_line_vars, scrubbed_line)
else:
lines[i] =scrubbed_line
returnlines
defadd_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, is_asm, is_analyze):
printed_prefixes= []
forpinprefix_list:
checkprefixes=p[0]
forcheckprefixincheckprefixes:
ifcheckprefixinprinted_prefixes:
break
# TODO func_dict[checkprefix] may be None, '' or not exist.
# Fix the call sites.
iffunc_namenotinfunc_dict[checkprefix] ornotfunc_dict[checkprefix][func_name]:
continue
# Add some space between different check prefixes, but not after the last
# check line (before the test code).
ifis_asm==True:
iflen(printed_prefixes) !=0:
output_lines.append(comment_marker)
printed_prefixes.append(checkprefix)
output_lines.append(check_label_format% (checkprefix, func_name))
func_body=str(func_dict[checkprefix][func_name]).splitlines()
# For ASM output, just emit the check lines.
ifis_asm==True:
output_lines.append('%s %s: %s'% (comment_marker, checkprefix, func_body[0]))
forfunc_lineinfunc_body[1:]:
output_lines.append('%s %s-NEXT: %s'% (comment_marker, checkprefix, func_line))
break
# For IR output, change all defs to FileCheck variables, so we're immune
# to variable naming fashions.
func_body=genericize_check_lines(func_body, is_analyze)
# This could be selectively enabled with an optional invocation argument.
# Disabled for now: better to check everything. Be safe rather than sorry.
# Handle the first line of the function body as a special case because
# it's often just noise (a useless asm comment or entry label).
#if func_body[0].startswith("#") or func_body[0].startswith("entry:"):
# is_blank_line = True
#else:
# output_lines.append('%s %s: %s' % (comment_marker, checkprefix, func_body[0]))
# is_blank_line = False
is_blank_line=False
forfunc_lineinfunc_body:
iffunc_line.strip() =='':
is_blank_line=True
continue
# Do not waste time checking IR comments.
func_line=SCRUB_IR_COMMENT_RE.sub(r'', func_line)
# Skip blank lines instead of checking them.
ifis_blank_line==True:
output_lines.append('{} {}: {}'.format(
comment_marker, checkprefix, func_line))
else:
output_lines.append('{} {}-NEXT: {}'.format(
comment_marker, checkprefix, func_line))
is_blank_line=False
# Add space between different check prefixes and also before the first
# line of code in the test function.
output_lines.append(comment_marker)
break
defadd_ir_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
# Label format is based on IR string.
check_label_format='{} %s-LABEL: @%s('.format(comment_marker)
add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, False, False)
defadd_analyze_checks(output_lines, comment_marker, prefix_list, func_dict, func_name):
check_label_format='{} %s-LABEL: \'%s\''.format(comment_marker)
add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, False, True)