common.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from __future__ import print_function
  2. import re
  3. import subprocess
  4. import sys
  5. RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$')
  6. CHECK_PREFIX_RE = re.compile('--?check-prefix(?:es)?=(\S+)')
  7. CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
  8. IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
  9. TRIPLE_IR_RE = re.compile(r'^target\s+triple\s*=\s*"([^"]+)"$')
  10. TRIPLE_ARG_RE = re.compile(r'-mtriple=([^ ]+)')
  11. SCRUB_LEADING_WHITESPACE_RE = re.compile(r'^(\s+)')
  12. SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
  13. SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
  14. SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
  15. SCRUB_LOOP_COMMENT_RE = re.compile(
  16. r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M)
  17. def should_add_line_to_output(input_line, prefix_set):
  18. # Skip any blank comment lines in the IR.
  19. if input_line.strip() == ';':
  20. return False
  21. # Skip any blank lines in the IR.
  22. #if input_line.strip() == '':
  23. # return False
  24. # And skip any CHECK lines. We're building our own.
  25. m = CHECK_RE.match(input_line)
  26. if m and m.group(1) in prefix_set:
  27. return False
  28. return True
  29. # Invoke the tool that is being tested.
  30. def invoke_tool(exe, cmd_args, ir):
  31. with open(ir) as ir_file:
  32. stdout = subprocess.check_output(exe + ' ' + cmd_args,
  33. shell=True, stdin=ir_file)
  34. if sys.version_info[0] > 2:
  35. stdout = stdout.decode()
  36. # Fix line endings to unix CR style.
  37. return stdout.replace('\r\n', '\n')
  38. # Build up a dictionary of all the function bodies.
  39. def build_function_body_dictionary(function_re, scrubber, scrubber_args, raw_tool_output, prefixes, func_dict, verbose):
  40. for m in function_re.finditer(raw_tool_output):
  41. if not m:
  42. continue
  43. func = m.group('func')
  44. scrubbed_body = scrubber(m.group('body'), *scrubber_args)
  45. if func.startswith('stress'):
  46. # We only use the last line of the function body for stress tests.
  47. scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
  48. if verbose:
  49. print('Processing function: ' + func, file=sys.stderr)
  50. for l in scrubbed_body.splitlines():
  51. print(' ' + l, file=sys.stderr)
  52. for prefix in prefixes:
  53. if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body:
  54. if prefix == prefixes[-1]:
  55. print('WARNING: Found conflicting asm under the '
  56. 'same prefix: %r!' % (prefix,), file=sys.stderr)
  57. else:
  58. func_dict[prefix][func] = None
  59. continue
  60. func_dict[prefix][func] = scrubbed_body