update_llc_test_checks.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env python
  2. """A test case update script.
  3. This script is a utility to update LLVM 'llc' based test cases with new
  4. FileCheck patterns. It can either update all of the tests in the file or
  5. a single test function.
  6. """
  7. from __future__ import print_function
  8. import argparse
  9. import glob
  10. import os # Used to advertise this file's name ("autogenerated_note").
  11. import string
  12. import subprocess
  13. import sys
  14. import re
  15. from UpdateTestChecks import asm, common
  16. ADVERT = '; NOTE: Assertions have been autogenerated by '
  17. def main():
  18. parser = argparse.ArgumentParser(description=__doc__)
  19. parser.add_argument('-v', '--verbose', action='store_true',
  20. help='Show verbose output')
  21. parser.add_argument('--llc-binary', default='llc',
  22. help='The "llc" binary to use to generate the test case')
  23. parser.add_argument(
  24. '--function', help='The function in the test file to update')
  25. parser.add_argument(
  26. '--extra_scrub', action='store_true',
  27. help='Always use additional regex to further reduce diffs between various subtargets')
  28. parser.add_argument(
  29. '--x86_scrub_rip', action='store_true', default=True,
  30. help='Use more regex for x86 matching to reduce diffs between various subtargets')
  31. parser.add_argument(
  32. '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip')
  33. parser.add_argument('-u', '--update-only', action='store_true',
  34. help='Only update test if it was already autogened')
  35. parser.add_argument('tests', nargs='+')
  36. args = parser.parse_args()
  37. script_name = os.path.basename(__file__)
  38. autogenerated_note = (ADVERT + 'utils/' + script_name)
  39. test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
  40. for test in test_paths:
  41. if args.verbose:
  42. print('Scanning for RUN lines in test file: %s' % (test,), file=sys.stderr)
  43. with open(test) as f:
  44. input_lines = [l.rstrip() for l in f]
  45. first_line = input_lines[0] if input_lines else ""
  46. if 'autogenerated' in first_line and script_name not in first_line:
  47. common.warn("Skipping test which wasn't autogenerated by " + script_name, test)
  48. continue
  49. if args.update_only:
  50. if not first_line or 'autogenerated' not in first_line:
  51. common.warn("Skipping test which isn't autogenerated: " + test)
  52. continue
  53. triple_in_ir = None
  54. for l in input_lines:
  55. m = common.TRIPLE_IR_RE.match(l)
  56. if m:
  57. triple_in_ir = m.groups()[0]
  58. break
  59. raw_lines = [m.group(1)
  60. for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
  61. run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
  62. for l in raw_lines[1:]:
  63. if run_lines[-1].endswith("\\"):
  64. run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
  65. else:
  66. run_lines.append(l)
  67. if args.verbose:
  68. print('Found %d RUN lines:' % (len(run_lines),), file=sys.stderr)
  69. for l in run_lines:
  70. print(' RUN: ' + l, file=sys.stderr)
  71. run_list = []
  72. for l in run_lines:
  73. if '|' not in l:
  74. common.warn('Skipping unparseable RUN line: ' + l)
  75. continue
  76. commands = [cmd.strip() for cmd in l.split('|', 1)]
  77. llc_cmd = commands[0]
  78. triple_in_cmd = None
  79. m = common.TRIPLE_ARG_RE.search(llc_cmd)
  80. if m:
  81. triple_in_cmd = m.groups()[0]
  82. march_in_cmd = None
  83. m = common.MARCH_ARG_RE.search(llc_cmd)
  84. if m:
  85. march_in_cmd = m.groups()[0]
  86. filecheck_cmd = ''
  87. if len(commands) > 1:
  88. filecheck_cmd = commands[1]
  89. common.verify_filecheck_prefixes(filecheck_cmd)
  90. if not llc_cmd.startswith('llc '):
  91. common.warn('Skipping non-llc RUN line: ' + l)
  92. continue
  93. if not filecheck_cmd.startswith('FileCheck '):
  94. common.warn('Skipping non-FileChecked RUN line: ' + l)
  95. continue
  96. llc_cmd_args = llc_cmd[len('llc'):].strip()
  97. llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
  98. check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
  99. for item in m.group(1).split(',')]
  100. if not check_prefixes:
  101. check_prefixes = ['CHECK']
  102. # FIXME: We should use multiple check prefixes to common check lines. For
  103. # now, we just ignore all but the last.
  104. run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd, march_in_cmd))
  105. func_dict = {}
  106. for p in run_list:
  107. prefixes = p[0]
  108. for prefix in prefixes:
  109. func_dict.update({prefix: dict()})
  110. for prefixes, llc_args, triple_in_cmd, march_in_cmd in run_list:
  111. if args.verbose:
  112. print('Extracted LLC cmd: llc ' + llc_args, file=sys.stderr)
  113. print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr)
  114. raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
  115. triple = triple_in_cmd or triple_in_ir
  116. if not triple:
  117. triple = asm.get_triple_from_march(march_in_cmd)
  118. asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
  119. triple, prefixes, func_dict)
  120. is_in_function = False
  121. is_in_function_start = False
  122. func_name = None
  123. prefix_set = set([prefix for p in run_list for prefix in p[0]])
  124. if args.verbose:
  125. print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr)
  126. output_lines = []
  127. output_lines.append(autogenerated_note)
  128. for input_line in input_lines:
  129. if is_in_function_start:
  130. if input_line == '':
  131. continue
  132. if input_line.lstrip().startswith(';'):
  133. m = common.CHECK_RE.match(input_line)
  134. if not m or m.group(1) not in prefix_set:
  135. output_lines.append(input_line)
  136. continue
  137. # Print out the various check lines here.
  138. asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
  139. is_in_function_start = False
  140. if is_in_function:
  141. if common.should_add_line_to_output(input_line, prefix_set):
  142. # This input line of the function body will go as-is into the output.
  143. output_lines.append(input_line)
  144. else:
  145. continue
  146. if input_line.strip() == '}':
  147. is_in_function = False
  148. continue
  149. # Discard any previous script advertising.
  150. if input_line.startswith(ADVERT):
  151. continue
  152. # If it's outside a function, it just gets copied to the output.
  153. output_lines.append(input_line)
  154. m = common.IR_FUNCTION_RE.match(input_line)
  155. if not m:
  156. continue
  157. func_name = m.group(1)
  158. if args.function is not None and func_name != args.function:
  159. # When filtering on a specific function, skip all others.
  160. continue
  161. is_in_function = is_in_function_start = True
  162. if args.verbose:
  163. print('Writing %d lines to %s...' % (len(output_lines), test), file=sys.stderr)
  164. with open(test, 'wb') as f:
  165. f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
  166. if __name__ == '__main__':
  167. main()