blob: b95dd69c23801a261c795e24ec73514f041b2134 [file] [log] [blame]
Fangrui Song3823fc42018-02-02 16:41:07 +00001from __future__ import print_function
Fangrui Songee4e2e72018-01-30 00:40:05 +00002import re
3import subprocess
Fangrui Song3823fc42018-02-02 16:41:07 +00004import sys
Fangrui Songee4e2e72018-01-30 00:40:05 +00005
6RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$')
7CHECK_PREFIX_RE = re.compile('--?check-prefix(?:es)?=(\S+)')
8CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
9
10IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
11TRIPLE_IR_RE = re.compile(r'^target\s+triple\s*=\s*"([^"]+)"$')
12TRIPLE_ARG_RE = re.compile(r'-mtriple=([^ ]+)')
13
14SCRUB_LEADING_WHITESPACE_RE = re.compile(r'^(\s+)')
15SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
16SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
17SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
18SCRUB_LOOP_COMMENT_RE = re.compile(
19 r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
20
21def should_add_line_to_output(input_line, prefix_set):
22 # Skip any blank comment lines in the IR.
23 if input_line.strip() == ';':
24 return False
25 # Skip any blank lines in the IR.
26 #if input_line.strip() == '':
27 # return False
28 # And skip any CHECK lines. We're building our own.
29 m = CHECK_RE.match(input_line)
30 if m and m.group(1) in prefix_set:
31 return False
32
33 return True
34
35# Invoke the tool that is being tested.
36def invoke_tool(exe, cmd_args, ir):
37 with open(ir) as ir_file:
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
45# Build up a dictionary of all the function bodies.
46def build_function_body_dictionary(function_re, scrubber, scrubber_args, raw_tool_output, prefixes, func_dict, verbose):
47 for m in function_re.finditer(raw_tool_output):
48 if not m:
49 continue
50 func = m.group('func')
51 scrubbed_body = scrubber(m.group('body'), *scrubber_args)
52 if func.startswith('stress'):
53 # We only use the last line of the function body for stress tests.
54 scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
55 if verbose:
Fangrui Song3823fc42018-02-02 16:41:07 +000056 print('Processing function: ' + func, file=sys.stderr)
Fangrui Songee4e2e72018-01-30 00:40:05 +000057 for l in scrubbed_body.splitlines():
Fangrui Song3823fc42018-02-02 16:41:07 +000058 print(' ' + l, file=sys.stderr)
Fangrui Songee4e2e72018-01-30 00:40:05 +000059 for prefix in prefixes:
60 if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body:
61 if prefix == prefixes[-1]:
Fangrui Song3823fc42018-02-02 16:41:07 +000062 print('WARNING: Found conflicting asm under the '
63 'same prefix: %r!' % (prefix,), file=sys.stderr)
Fangrui Songee4e2e72018-01-30 00:40:05 +000064 else:
65 func_dict[prefix][func] = None
66 continue
67
68 func_dict[prefix][func] = scrubbed_body