blob: 1085996081261c6d4ac88065dd21ab5adef8b341 [file] [log] [blame]
Fangrui Song3823fc42018-02-02 16:41:07 +00001from __future__ import print_function
Fangrui Songee4e2e72018-01-30 00:40:05 +00002import re
Fangrui Song4f0f4262018-02-10 05:01:33 +00003import string
Fangrui Songee4e2e72018-01-30 00:40:05 +00004import subprocess
Fangrui Song3823fc42018-02-02 16:41:07 +00005import sys
Fangrui Songee4e2e72018-01-30 00:40:05 +00006
Fangrui Song4f0f4262018-02-10 05:01:33 +00007if sys.version_info[0] > 2:
8 class string:
9 expandtabs = str.expandtabs
10else:
11 import string
Fangrui Songee4e2e72018-01-30 00:40:05 +000012
Fangrui Song4f0f4262018-02-10 05:01:33 +000013##### Common utilities for update_*test_checks.py
Fangrui Songee4e2e72018-01-30 00:40:05 +000014
15def should_add_line_to_output(input_line, prefix_set):
16 # Skip any blank comment lines in the IR.
17 if input_line.strip() == ';':
18 return False
19 # Skip any blank lines in the IR.
20 #if input_line.strip() == '':
21 # return False
22 # And skip any CHECK lines. We're building our own.
23 m = CHECK_RE.match(input_line)
24 if m and m.group(1) in prefix_set:
25 return False
26
27 return True
28
29# Invoke the tool that is being tested.
30def invoke_tool(exe, cmd_args, ir):
31 with open(ir) as ir_file:
Fangrui Song0a301a12018-03-02 17:37:04 +000032 # TODO Remove the str form which is used by update_test_checks.py and
33 # update_llc_test_checks.py
34 # The safer list form is used by update_cc_test_checks.py
35 if isinstance(cmd_args, list):
36 stdout = subprocess.check_output([exe] + cmd_args, stdin=ir_file)
37 else:
38 stdout = subprocess.check_output(exe + ' ' + cmd_args,
39 shell=True, stdin=ir_file)
Fangrui Song3823fc42018-02-02 16:41:07 +000040 if sys.version_info[0] > 2:
41 stdout = stdout.decode()
Fangrui Songee4e2e72018-01-30 00:40:05 +000042 # Fix line endings to unix CR style.
Fangrui Song3823fc42018-02-02 16:41:07 +000043 return stdout.replace('\r\n', '\n')
Fangrui Songee4e2e72018-01-30 00:40:05 +000044
Fangrui Song4f0f4262018-02-10 05:01:33 +000045##### LLVM IR parser
46
Justin Bogner35a9d1b2018-02-28 00:56:24 +000047RUN_LINE_RE = re.compile('^\s*[;#]\s*RUN:\s*(.*)$')
48CHECK_PREFIX_RE = re.compile('--?check-prefix(?:es)?[= ](\S+)')
49CHECK_RE = re.compile(r'^\s*[;#]\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
Fangrui Song4f0f4262018-02-10 05:01:33 +000050
51OPT_FUNCTION_RE = re.compile(
52 r'^\s*define\s+(?:internal\s+)?[^@]*@(?P<func>[\w-]+?)\s*\('
53 r'(\s+)?[^)]*[^{]*\{\n(?P<body>.*?)^\}$',
54 flags=(re.M | re.S))
55
56IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
Justin Bogner35a9d1b2018-02-28 00:56:24 +000057TRIPLE_IR_RE = re.compile(r'^\s*target\s+triple\s*=\s*"([^"]+)"$')
58TRIPLE_ARG_RE = re.compile(r'-mtriple[= ]([^ ]+)')
59MARCH_ARG_RE = re.compile(r'-march[= ]([^ ]+)')
Fangrui Song4f0f4262018-02-10 05:01:33 +000060
61SCRUB_LEADING_WHITESPACE_RE = re.compile(r'^(\s+)')
62SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
63SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
64SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
65SCRUB_LOOP_COMMENT_RE = re.compile(
66 r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
67
68def scrub_body(body):
69 # Scrub runs of whitespace out of the assembly, but leave the leading
70 # whitespace in place.
71 body = SCRUB_WHITESPACE_RE.sub(r' ', body)
72 # Expand the tabs used for indentation.
73 body = string.expandtabs(body, 2)
74 # Strip trailing whitespace.
75 body = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', body)
76 return body
77
Fangrui Songee4e2e72018-01-30 00:40:05 +000078# Build up a dictionary of all the function bodies.
79def build_function_body_dictionary(function_re, scrubber, scrubber_args, raw_tool_output, prefixes, func_dict, verbose):
80 for m in function_re.finditer(raw_tool_output):
81 if not m:
82 continue
83 func = m.group('func')
84 scrubbed_body = scrubber(m.group('body'), *scrubber_args)
85 if func.startswith('stress'):
86 # We only use the last line of the function body for stress tests.
87 scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
88 if verbose:
Fangrui Song3823fc42018-02-02 16:41:07 +000089 print('Processing function: ' + func, file=sys.stderr)
Fangrui Songee4e2e72018-01-30 00:40:05 +000090 for l in scrubbed_body.splitlines():
Fangrui Song3823fc42018-02-02 16:41:07 +000091 print(' ' + l, file=sys.stderr)
Fangrui Songee4e2e72018-01-30 00:40:05 +000092 for prefix in prefixes:
93 if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body:
94 if prefix == prefixes[-1]:
Fangrui Song3823fc42018-02-02 16:41:07 +000095 print('WARNING: Found conflicting asm under the '
96 'same prefix: %r!' % (prefix,), file=sys.stderr)
Fangrui Songee4e2e72018-01-30 00:40:05 +000097 else:
98 func_dict[prefix][func] = None
99 continue
100
101 func_dict[prefix][func] = scrubbed_body
Fangrui Song4f0f4262018-02-10 05:01:33 +0000102
103##### Generator of LLVM IR CHECK lines
104
105SCRUB_IR_COMMENT_RE = re.compile(r'\s*;.*')
106
107# Match things that look at identifiers, but only if they are followed by
108# spaces, commas, paren, or end of the string
109IR_VALUE_RE = re.compile(r'(\s+)%([\w\.]+?)([,\s\(\)]|\Z)')
110
111# Create a FileCheck variable name based on an IR name.
112def get_value_name(var):
113 if var.isdigit():
114 var = 'TMP' + var
115 var = var.replace('.', '_')
116 return var.upper()
117
118
119# Create a FileCheck variable from regex.
120def get_value_definition(var):
121 return '[[' + get_value_name(var) + ':%.*]]'
122
123
124# Use a FileCheck variable.
125def get_value_use(var):
126 return '[[' + get_value_name(var) + ']]'
127
128# Replace IR value defs and uses with FileCheck variables.
129def genericize_check_lines(lines):
130 # This gets called for each match that occurs in
131 # a line. We transform variables we haven't seen
132 # into defs, and variables we have seen into uses.
133 def transform_line_vars(match):
134 var = match.group(2)
135 if var in vars_seen:
136 rv = get_value_use(var)
137 else:
138 vars_seen.add(var)
139 rv = get_value_definition(var)
140 # re.sub replaces the entire regex match
141 # with whatever you return, so we have
142 # to make sure to hand it back everything
143 # including the commas and spaces.
144 return match.group(1) + rv + match.group(3)
145
146 vars_seen = set()
147 lines_with_def = []
148
149 for i, line in enumerate(lines):
150 # An IR variable named '%.' matches the FileCheck regex string.
151 line = line.replace('%.', '%dot')
152 # Ignore any comments, since the check lines will too.
153 scrubbed_line = SCRUB_IR_COMMENT_RE.sub(r'', line)
154 lines[i] = IR_VALUE_RE.sub(transform_line_vars, scrubbed_line)
155 return lines
156
157
158def add_ir_checks(output_lines, prefix_list, func_dict, func_name, opt_basename):
159 # Label format is based on IR string.
160 check_label_format = "; %s-LABEL: @%s("
161
162 printed_prefixes = []
163 for checkprefixes, _ in prefix_list:
164 for checkprefix in checkprefixes:
165 if checkprefix in printed_prefixes:
166 break
167 if not func_dict[checkprefix][func_name]:
168 continue
169 # Add some space between different check prefixes, but not after the last
170 # check line (before the test code).
171 #if len(printed_prefixes) != 0:
172 # output_lines.append(';')
173 printed_prefixes.append(checkprefix)
174 output_lines.append(check_label_format % (checkprefix, func_name))
175 func_body = func_dict[checkprefix][func_name].splitlines()
176
177 # For IR output, change all defs to FileCheck variables, so we're immune
178 # to variable naming fashions.
179 func_body = genericize_check_lines(func_body)
180
181 # This could be selectively enabled with an optional invocation argument.
182 # Disabled for now: better to check everything. Be safe rather than sorry.
183
184 # Handle the first line of the function body as a special case because
185 # it's often just noise (a useless asm comment or entry label).
186 #if func_body[0].startswith("#") or func_body[0].startswith("entry:"):
187 # is_blank_line = True
188 #else:
189 # output_lines.append('; %s: %s' % (checkprefix, func_body[0]))
190 # is_blank_line = False
191
192 is_blank_line = False
193
194 for func_line in func_body:
195 if func_line.strip() == '':
196 is_blank_line = True
197 continue
198 # Do not waste time checking IR comments.
199 func_line = SCRUB_IR_COMMENT_RE.sub(r'', func_line)
200
201 # Skip blank lines instead of checking them.
202 if is_blank_line == True:
203 output_lines.append('; %s: %s' % (checkprefix, func_line))
204 else:
205 output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line))
206 is_blank_line = False
207
208 # Add space between different check prefixes and also before the first
209 # line of code in the test function.
210 output_lines.append(';')
211 break
212 return output_lines