1from __future__ import print_function 2 3import argparse 4import copy 5import glob 6import itertools 7import os 8import re 9import subprocess 10import sys 11import shlex 12 13from typing import List 14 15##### Common utilities for update_*test_checks.py 16 17 18_verbose = False 19_prefix_filecheck_ir_name = '' 20 21class Regex(object): 22 """Wrap a compiled regular expression object to allow deep copy of a regexp. 23 This is required for the deep copy done in do_scrub. 24 25 """ 26 def __init__(self, regex): 27 self.regex = regex 28 29 def __deepcopy__(self, memo): 30 result = copy.copy(self) 31 result.regex = self.regex 32 return result 33 34 def search(self, line): 35 return self.regex.search(line) 36 37 def sub(self, repl, line): 38 return self.regex.sub(repl, line) 39 40 def pattern(self): 41 return self.regex.pattern 42 43 def flags(self): 44 return self.regex.flags 45 46class Filter(Regex): 47 """Augment a Regex object with a flag indicating whether a match should be 48 added (!is_filter_out) or removed (is_filter_out) from the generated checks. 49 50 """ 51 def __init__(self, regex, is_filter_out): 52 super(Filter, self).__init__(regex) 53 self.is_filter_out = is_filter_out 54 55 def __deepcopy__(self, memo): 56 result = copy.deepcopy(super(Filter, self), memo) 57 result.is_filter_out = copy.deepcopy(self.is_filter_out, memo) 58 return result 59 60def parse_commandline_args(parser): 61 class RegexAction(argparse.Action): 62 """Add a regular expression option value to a list of regular expressions. 63 This compiles the expression, wraps it in a Regex and adds it to the option 64 value list.""" 65 def __init__(self, option_strings, dest, nargs=None, **kwargs): 66 if nargs is not None: 67 raise ValueError('nargs not allowed') 68 super(RegexAction, self).__init__(option_strings, dest, **kwargs) 69 70 def do_call(self, namespace, values, flags): 71 value_list = getattr(namespace, self.dest) 72 if value_list is None: 73 value_list = [] 74 75 try: 76 value_list.append(Regex(re.compile(values, flags))) 77 except re.error as error: 78 raise ValueError('{}: Invalid regular expression \'{}\' ({})'.format( 79 option_string, error.pattern, error.msg)) 80 81 setattr(namespace, self.dest, value_list) 82 83 def __call__(self, parser, namespace, values, option_string=None): 84 self.do_call(namespace, values, 0) 85 86 class FilterAction(RegexAction): 87 """Add a filter to a list of filter option values.""" 88 def __init__(self, option_strings, dest, nargs=None, **kwargs): 89 super(FilterAction, self).__init__(option_strings, dest, nargs, **kwargs) 90 91 def __call__(self, parser, namespace, values, option_string=None): 92 super(FilterAction, self).__call__(parser, namespace, values, option_string) 93 94 value_list = getattr(namespace, self.dest) 95 96 is_filter_out = ( option_string == '--filter-out' ) 97 98 value_list[-1] = Filter(value_list[-1].regex, is_filter_out) 99 100 setattr(namespace, self.dest, value_list) 101 102 filter_group = parser.add_argument_group( 103 'filtering', 104 """Filters are applied to each output line according to the order given. The 105 first matching filter terminates filter processing for that current line.""") 106 107 filter_group.add_argument('--filter', action=FilterAction, dest='filters', 108 metavar='REGEX', 109 help='Only include lines matching REGEX (may be specified multiple times)') 110 filter_group.add_argument('--filter-out', action=FilterAction, dest='filters', 111 metavar='REGEX', 112 help='Exclude lines matching REGEX') 113 114 parser.add_argument('--include-generated-funcs', action='store_true', 115 help='Output checks for functions not in source') 116 parser.add_argument('-v', '--verbose', action='store_true', 117 help='Show verbose output') 118 parser.add_argument('-u', '--update-only', action='store_true', 119 help='Only update test if it was already autogened') 120 parser.add_argument('--force-update', action='store_true', 121 help='Update test even if it was autogened by a different script') 122 parser.add_argument('--enable', action='store_true', dest='enabled', default=True, 123 help='Activate CHECK line generation from this point forward') 124 parser.add_argument('--disable', action='store_false', dest='enabled', 125 help='Deactivate CHECK line generation from this point forward') 126 parser.add_argument('--replace-value-regex', nargs='+', default=[], 127 help='List of regular expressions to replace matching value names') 128 parser.add_argument('--prefix-filecheck-ir-name', default='', 129 help='Add a prefix to FileCheck IR value names to avoid conflicts with scripted names') 130 parser.add_argument('--global-value-regex', nargs='+', default=[], 131 help='List of regular expressions that a global value declaration must match to generate a check (has no effect if checking globals is not enabled)') 132 parser.add_argument('--global-hex-value-regex', nargs='+', default=[], 133 help='List of regular expressions such that, for matching global value declarations, literal integer values should be encoded in hex in the associated FileCheck directives') 134 # FIXME: in 3.9, we can use argparse.BooleanOptionalAction. At that point, 135 # we need to rename the flag to just -generate-body-for-unused-prefixes. 136 parser.add_argument('--no-generate-body-for-unused-prefixes', 137 action='store_false', 138 dest='gen_unused_prefix_body', 139 default=True, 140 help='Generate a function body that always matches for unused prefixes. This is useful when unused prefixes are desired, and it avoids needing to annotate each FileCheck as allowing them.') 141 args = parser.parse_args() 142 global _verbose, _global_value_regex, _global_hex_value_regex 143 _verbose = args.verbose 144 _global_value_regex = args.global_value_regex 145 _global_hex_value_regex = args.global_hex_value_regex 146 return args 147 148 149class InputLineInfo(object): 150 def __init__(self, line, line_number, args, argv): 151 self.line = line 152 self.line_number = line_number 153 self.args = args 154 self.argv = argv 155 156 157class TestInfo(object): 158 def __init__(self, test, parser, script_name, input_lines, args, argv, 159 comment_prefix, argparse_callback): 160 self.parser = parser 161 self.argparse_callback = argparse_callback 162 self.path = test 163 self.args = args 164 if args.prefix_filecheck_ir_name: 165 global _prefix_filecheck_ir_name 166 _prefix_filecheck_ir_name = args.prefix_filecheck_ir_name 167 self.argv = argv 168 self.input_lines = input_lines 169 self.run_lines = find_run_lines(test, self.input_lines) 170 self.comment_prefix = comment_prefix 171 if self.comment_prefix is None: 172 if self.path.endswith('.mir'): 173 self.comment_prefix = '#' 174 else: 175 self.comment_prefix = ';' 176 self.autogenerated_note_prefix = self.comment_prefix + ' ' + UTC_ADVERT 177 self.test_autogenerated_note = self.autogenerated_note_prefix + script_name 178 self.test_autogenerated_note += get_autogennote_suffix(parser, self.args) 179 self.test_unused_note = self.comment_prefix + self.comment_prefix + ' ' + UNUSED_NOTE 180 181 def ro_iterlines(self): 182 for line_num, input_line in enumerate(self.input_lines): 183 args, argv = check_for_command(input_line, self.parser, 184 self.args, self.argv, self.argparse_callback) 185 yield InputLineInfo(input_line, line_num, args, argv) 186 187 def iterlines(self, output_lines): 188 output_lines.append(self.test_autogenerated_note) 189 for line_info in self.ro_iterlines(): 190 input_line = line_info.line 191 # Discard any previous script advertising. 192 if input_line.startswith(self.autogenerated_note_prefix): 193 continue 194 self.args = line_info.args 195 self.argv = line_info.argv 196 if not self.args.enabled: 197 output_lines.append(input_line) 198 continue 199 yield line_info 200 201 def get_checks_for_unused_prefixes(self, run_list, used_prefixes: List[str]) -> List[str]: 202 unused_prefixes = set( 203 [prefix for sublist in run_list for prefix in sublist[0]]).difference(set(used_prefixes)) 204 205 ret = [] 206 if not unused_prefixes: 207 return ret 208 ret.append(self.test_unused_note) 209 for unused in sorted(unused_prefixes): 210 ret.append('{comment} {prefix}: {match_everything}'.format( 211 comment=self.comment_prefix, 212 prefix=unused, 213 match_everything=r"""{{.*}}""" 214 )) 215 return ret 216 217def itertests(test_patterns, parser, script_name, comment_prefix=None, argparse_callback=None): 218 for pattern in test_patterns: 219 # On Windows we must expand the patterns ourselves. 220 tests_list = glob.glob(pattern) 221 if not tests_list: 222 warn("Test file pattern '%s' was not found. Ignoring it." % (pattern,)) 223 continue 224 for test in tests_list: 225 with open(test) as f: 226 input_lines = [l.rstrip() for l in f] 227 args = parser.parse_args() 228 if argparse_callback is not None: 229 argparse_callback(args) 230 argv = sys.argv[:] 231 first_line = input_lines[0] if input_lines else "" 232 if UTC_ADVERT in first_line: 233 if script_name not in first_line and not args.force_update: 234 warn("Skipping test which wasn't autogenerated by " + script_name, test) 235 continue 236 args, argv = check_for_command(first_line, parser, args, argv, argparse_callback) 237 elif args.update_only: 238 assert UTC_ADVERT not in first_line 239 warn("Skipping test which isn't autogenerated: " + test) 240 continue 241 final_input_lines = [] 242 for l in input_lines: 243 if UNUSED_NOTE in l: 244 break 245 final_input_lines.append(l) 246 yield TestInfo(test, parser, script_name, final_input_lines, args, argv, 247 comment_prefix, argparse_callback) 248 249 250def should_add_line_to_output(input_line, prefix_set, skip_global_checks = False, comment_marker = ';'): 251 # Skip any blank comment lines in the IR. 252 if not skip_global_checks and input_line.strip() == comment_marker: 253 return False 254 # Skip a special double comment line we use as a separator. 255 if input_line.strip() == comment_marker + SEPARATOR: 256 return False 257 # Skip any blank lines in the IR. 258 #if input_line.strip() == '': 259 # return False 260 # And skip any CHECK lines. We're building our own. 261 m = CHECK_RE.match(input_line) 262 if m and m.group(1) in prefix_set: 263 if skip_global_checks: 264 global_ir_value_re = re.compile(r'\[\[', flags=(re.M)) 265 return not global_ir_value_re.search(input_line) 266 return False 267 268 return True 269 270# Perform lit-like substitutions 271def getSubstitutions(sourcepath): 272 sourcedir = os.path.dirname(sourcepath) 273 return [('%s', sourcepath), 274 ('%S', sourcedir), 275 ('%p', sourcedir), 276 ('%{pathsep}', os.pathsep)] 277 278def applySubstitutions(s, substitutions): 279 for a,b in substitutions: 280 s = s.replace(a, b) 281 return s 282 283# Invoke the tool that is being tested. 284def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False): 285 with open(ir) as ir_file: 286 substitutions = getSubstitutions(ir) 287 288 # TODO Remove the str form which is used by update_test_checks.py and 289 # update_llc_test_checks.py 290 # The safer list form is used by update_cc_test_checks.py 291 if preprocess_cmd: 292 # Allow pre-processing the IR file (e.g. using sed): 293 assert isinstance(preprocess_cmd, str) # TODO: use a list instead of using shell 294 preprocess_cmd = applySubstitutions(preprocess_cmd, substitutions).strip() 295 if verbose: 296 print('Pre-processing input file: ', ir, " with command '", 297 preprocess_cmd, "'", sep="", file=sys.stderr) 298 # Python 2.7 doesn't have subprocess.DEVNULL: 299 with open(os.devnull, 'w') as devnull: 300 pp = subprocess.Popen(preprocess_cmd, shell=True, stdin=devnull, 301 stdout=subprocess.PIPE) 302 ir_file = pp.stdout 303 304 if isinstance(cmd_args, list): 305 args = [applySubstitutions(a, substitutions) for a in cmd_args] 306 stdout = subprocess.check_output([exe] + args, stdin=ir_file) 307 else: 308 stdout = subprocess.check_output(exe + ' ' + applySubstitutions(cmd_args, substitutions), 309 shell=True, stdin=ir_file) 310 if sys.version_info[0] > 2: 311 # FYI, if you crashed here with a decode error, your run line probably 312 # results in bitcode or other binary format being written to the pipe. 313 # For an opt test, you probably want to add -S or -disable-output. 314 stdout = stdout.decode() 315 # Fix line endings to unix CR style. 316 return stdout.replace('\r\n', '\n') 317 318##### LLVM IR parser 319RUN_LINE_RE = re.compile(r'^\s*(?://|[;#])\s*RUN:\s*(.*)$') 320CHECK_PREFIX_RE = re.compile(r'--?check-prefix(?:es)?[= ](\S+)') 321PREFIX_RE = re.compile('^[a-zA-Z0-9_-]+$') 322CHECK_RE = re.compile(r'^\s*(?://|[;#])\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL|-SAME|-EMPTY)?:') 323 324UTC_ARGS_KEY = 'UTC_ARGS:' 325UTC_ARGS_CMD = re.compile(r'.*' + UTC_ARGS_KEY + '\s*(?P<cmd>.*)\s*$') 326UTC_ADVERT = 'NOTE: Assertions have been autogenerated by ' 327UNUSED_NOTE = 'NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:' 328 329OPT_FUNCTION_RE = re.compile( 330 r'^(\s*;\s*Function\sAttrs:\s(?P<attrs>[\w\s]+?))?\s*define\s+(?:internal\s+)?[^@]*@(?P<func>[\w.$-]+?)\s*' 331 r'(?P<args_and_sig>\((\)|(.*?[\w.-]+?)\))[^{]*\{)\n(?P<body>.*?)^\}$', 332 flags=(re.M | re.S)) 333 334ANALYZE_FUNCTION_RE = re.compile( 335 r'^\s*\'(?P<analysis>[\w\s-]+?)\'\s+for\s+function\s+\'(?P<func>[\w.$-]+?)\':' 336 r'\s*\n(?P<body>.*)$', 337 flags=(re.X | re.S)) 338 339LV_DEBUG_RE = re.compile( 340 r'^\s*\'(?P<func>[\w.$-]+?)\'[^\n]*' 341 r'\s*\n(?P<body>.*)$', 342 flags=(re.X | re.S)) 343 344IR_FUNCTION_RE = re.compile(r'^\s*define\s+(?:internal\s+)?[^@]*@"?([\w.$-]+)"?\s*\(') 345TRIPLE_IR_RE = re.compile(r'^\s*target\s+triple\s*=\s*"([^"]+)"$') 346TRIPLE_ARG_RE = re.compile(r'-mtriple[= ]([^ ]+)') 347MARCH_ARG_RE = re.compile(r'-march[= ]([^ ]+)') 348DEBUG_ONLY_ARG_RE = re.compile(r'-debug-only[= ]([^ ]+)') 349 350SCRUB_LEADING_WHITESPACE_RE = re.compile(r'^(\s+)') 351SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M) 352SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M) 353SCRUB_TRAILING_WHITESPACE_TEST_RE = SCRUB_TRAILING_WHITESPACE_RE 354SCRUB_TRAILING_WHITESPACE_AND_ATTRIBUTES_RE = re.compile(r'([ \t]|(#[0-9]+))+$', flags=re.M) 355SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n') 356SCRUB_LOOP_COMMENT_RE = re.compile( 357 r'# =>This Inner Loop Header:.*|# in Loop:.*', flags=re.M) 358SCRUB_TAILING_COMMENT_TOKEN_RE = re.compile(r'(?<=\S)+[ \t]*#$', flags=re.M) 359 360SEPARATOR = '.' 361 362def error(msg, test_file=None): 363 if test_file: 364 msg = '{}: {}'.format(msg, test_file) 365 print('ERROR: {}'.format(msg), file=sys.stderr) 366 367def warn(msg, test_file=None): 368 if test_file: 369 msg = '{}: {}'.format(msg, test_file) 370 print('WARNING: {}'.format(msg), file=sys.stderr) 371 372def debug(*args, **kwargs): 373 # Python2 does not allow def debug(*args, file=sys.stderr, **kwargs): 374 if 'file' not in kwargs: 375 kwargs['file'] = sys.stderr 376 if _verbose: 377 print(*args, **kwargs) 378 379def find_run_lines(test, lines): 380 debug('Scanning for RUN lines in test file:', test) 381 raw_lines = [m.group(1) 382 for m in [RUN_LINE_RE.match(l) for l in lines] if m] 383 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else [] 384 for l in raw_lines[1:]: 385 if run_lines[-1].endswith('\\'): 386 run_lines[-1] = run_lines[-1].rstrip('\\') + ' ' + l 387 else: 388 run_lines.append(l) 389 debug('Found {} RUN lines in {}:'.format(len(run_lines), test)) 390 for l in run_lines: 391 debug(' RUN: {}'.format(l)) 392 return run_lines 393 394def get_triple_from_march(march): 395 triples = { 396 'amdgcn': 'amdgcn', 397 'r600': 'r600', 398 'mips': 'mips', 399 'sparc': 'sparc', 400 'hexagon': 'hexagon', 401 've': 've', 402 } 403 for prefix, triple in triples.items(): 404 if march.startswith(prefix): 405 return triple 406 print("Cannot find a triple. Assume 'x86'", file=sys.stderr) 407 return 'x86' 408 409def apply_filters(line, filters): 410 has_filter = False 411 for f in filters: 412 if not f.is_filter_out: 413 has_filter = True 414 if f.search(line): 415 return False if f.is_filter_out else True 416 # If we only used filter-out, keep the line, otherwise discard it since no 417 # filter matched. 418 return False if has_filter else True 419 420def do_filter(body, filters): 421 return body if not filters else '\n'.join(filter( 422 lambda line: apply_filters(line, filters), body.splitlines())) 423 424def scrub_body(body): 425 # Scrub runs of whitespace out of the assembly, but leave the leading 426 # whitespace in place. 427 body = SCRUB_WHITESPACE_RE.sub(r' ', body) 428 # Expand the tabs used for indentation. 429 body = str.expandtabs(body, 2) 430 # Strip trailing whitespace. 431 body = SCRUB_TRAILING_WHITESPACE_TEST_RE.sub(r'', body) 432 return body 433 434def do_scrub(body, scrubber, scrubber_args, extra): 435 if scrubber_args: 436 local_args = copy.deepcopy(scrubber_args) 437 local_args[0].extra_scrub = extra 438 return scrubber(body, *local_args) 439 return scrubber(body, *scrubber_args) 440 441# Build up a dictionary of all the function bodies. 442class function_body(object): 443 def __init__(self, string, extra, args_and_sig, attrs, func_name_separator): 444 self.scrub = string 445 self.extrascrub = extra 446 self.args_and_sig = args_and_sig 447 self.attrs = attrs 448 self.func_name_separator = func_name_separator 449 def is_same_except_arg_names(self, extrascrub, args_and_sig, attrs, is_backend): 450 arg_names = set() 451 def drop_arg_names(match): 452 arg_names.add(match.group(variable_group_in_ir_value_match)) 453 if match.group(attribute_group_in_ir_value_match): 454 attr = match.group(attribute_group_in_ir_value_match) 455 else: 456 attr = '' 457 return match.group(1) + attr + match.group(match.lastindex) 458 def repl_arg_names(match): 459 if match.group(variable_group_in_ir_value_match) is not None and match.group(variable_group_in_ir_value_match) in arg_names: 460 return match.group(1) + match.group(match.lastindex) 461 return match.group(1) + match.group(2) + match.group(match.lastindex) 462 if self.attrs != attrs: 463 return False 464 ans0 = IR_VALUE_RE.sub(drop_arg_names, self.args_and_sig) 465 ans1 = IR_VALUE_RE.sub(drop_arg_names, args_and_sig) 466 if ans0 != ans1: 467 return False 468 if is_backend: 469 # Check without replacements, the replacements are not applied to the 470 # body for backend checks. 471 return self.extrascrub == extrascrub 472 473 es0 = IR_VALUE_RE.sub(repl_arg_names, self.extrascrub) 474 es1 = IR_VALUE_RE.sub(repl_arg_names, extrascrub) 475 es0 = SCRUB_IR_COMMENT_RE.sub(r'', es0) 476 es1 = SCRUB_IR_COMMENT_RE.sub(r'', es1) 477 return es0 == es1 478 479 def __str__(self): 480 return self.scrub 481 482class FunctionTestBuilder: 483 def __init__(self, run_list, flags, scrubber_args, path): 484 self._verbose = flags.verbose 485 self._record_args = flags.function_signature 486 self._check_attributes = flags.check_attributes 487 # Strip double-quotes if input was read by UTC_ARGS 488 self._filters = list(map(lambda f: Filter(re.compile(f.pattern().strip('"'), 489 f.flags()), 490 f.is_filter_out), 491 flags.filters)) if flags.filters else [] 492 self._scrubber_args = scrubber_args 493 self._path = path 494 # Strip double-quotes if input was read by UTC_ARGS 495 self._replace_value_regex = list(map(lambda x: x.strip('"'), flags.replace_value_regex)) 496 self._func_dict = {} 497 self._func_order = {} 498 self._global_var_dict = {} 499 for tuple in run_list: 500 for prefix in tuple[0]: 501 self._func_dict.update({prefix:dict()}) 502 self._func_order.update({prefix: []}) 503 self._global_var_dict.update({prefix:dict()}) 504 505 def finish_and_get_func_dict(self): 506 for prefix in self.get_failed_prefixes(): 507 warn('Prefix %s had conflicting output from different RUN lines for all functions in test %s' % (prefix,self._path,)) 508 return self._func_dict 509 510 def func_order(self): 511 return self._func_order 512 513 def global_var_dict(self): 514 return self._global_var_dict 515 516 def is_filtered(self): 517 return bool(self._filters) 518 519 def process_run_line(self, function_re, scrubber, raw_tool_output, prefixes, is_backend): 520 build_global_values_dictionary(self._global_var_dict, raw_tool_output, prefixes) 521 for m in function_re.finditer(raw_tool_output): 522 if not m: 523 continue 524 func = m.group('func') 525 body = m.group('body') 526 # func_name_separator is the string that is placed right after function name at the 527 # beginning of assembly function definition. In most assemblies, that is just a 528 # colon: `foo:`. But, for example, in nvptx it is a brace: `foo(`. If is_backend is 529 # False, just assume that separator is an empty string. 530 if is_backend: 531 # Use ':' as default separator. 532 func_name_separator = m.group('func_name_separator') if 'func_name_separator' in m.groupdict() else ':' 533 else: 534 func_name_separator = '' 535 attrs = m.group('attrs') if self._check_attributes else '' 536 # Determine if we print arguments, the opening brace, or nothing after the 537 # function name 538 if self._record_args and 'args_and_sig' in m.groupdict(): 539 args_and_sig = scrub_body(m.group('args_and_sig').strip()) 540 elif 'args_and_sig' in m.groupdict(): 541 args_and_sig = '(' 542 else: 543 args_and_sig = '' 544 filtered_body = do_filter(body, self._filters) 545 scrubbed_body = do_scrub(filtered_body, scrubber, self._scrubber_args, 546 extra=False) 547 scrubbed_extra = do_scrub(filtered_body, scrubber, self._scrubber_args, 548 extra=True) 549 if 'analysis' in m.groupdict(): 550 analysis = m.group('analysis') 551 if analysis.lower() != 'cost model analysis': 552 warn('Unsupported analysis mode: %r!' % (analysis,)) 553 if func.startswith('stress'): 554 # We only use the last line of the function body for stress tests. 555 scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:]) 556 if self._verbose: 557 print('Processing function: ' + func, file=sys.stderr) 558 for l in scrubbed_body.splitlines(): 559 print(' ' + l, file=sys.stderr) 560 for prefix in prefixes: 561 # Replace function names matching the regex. 562 for regex in self._replace_value_regex: 563 # Pattern that matches capture groups in the regex in leftmost order. 564 group_regex = re.compile(r'\(.*?\)') 565 # Replace function name with regex. 566 match = re.match(regex, func) 567 if match: 568 func_repl = regex 569 # Replace any capture groups with their matched strings. 570 for g in match.groups(): 571 func_repl = group_regex.sub(re.escape(g), func_repl, count=1) 572 func = re.sub(func_repl, '{{' + func_repl + '}}', func) 573 574 # Replace all calls to regex matching functions. 575 matches = re.finditer(regex, scrubbed_body) 576 for match in matches: 577 func_repl = regex 578 # Replace any capture groups with their matched strings. 579 for g in match.groups(): 580 func_repl = group_regex.sub(re.escape(g), func_repl, count=1) 581 # Substitute function call names that match the regex with the same 582 # capture groups set. 583 scrubbed_body = re.sub(func_repl, '{{' + func_repl + '}}', 584 scrubbed_body) 585 586 if func in self._func_dict[prefix]: 587 if (self._func_dict[prefix][func] is None or 588 str(self._func_dict[prefix][func]) != scrubbed_body or 589 self._func_dict[prefix][func].args_and_sig != args_and_sig or 590 self._func_dict[prefix][func].attrs != attrs): 591 if (self._func_dict[prefix][func] is not None and 592 self._func_dict[prefix][func].is_same_except_arg_names( 593 scrubbed_extra, 594 args_and_sig, 595 attrs, 596 is_backend)): 597 self._func_dict[prefix][func].scrub = scrubbed_extra 598 self._func_dict[prefix][func].args_and_sig = args_and_sig 599 continue 600 else: 601 # This means a previous RUN line produced a body for this function 602 # that is different from the one produced by this current RUN line, 603 # so the body can't be common accross RUN lines. We use None to 604 # indicate that. 605 self._func_dict[prefix][func] = None 606 continue 607 608 self._func_dict[prefix][func] = function_body( 609 scrubbed_body, scrubbed_extra, args_and_sig, attrs, func_name_separator) 610 self._func_order[prefix].append(func) 611 612 def get_failed_prefixes(self): 613 # This returns the list of those prefixes that failed to match any function, 614 # because there were conflicting bodies produced by different RUN lines, in 615 # all instances of the prefix. 616 for prefix in self._func_dict: 617 if (self._func_dict[prefix] and 618 (not [fct for fct in self._func_dict[prefix] 619 if self._func_dict[prefix][fct] is not None])): 620 yield prefix 621 622 623##### Generator of LLVM IR CHECK lines 624 625SCRUB_IR_COMMENT_RE = re.compile(r'\s*;.*') 626 627# TODO: We should also derive check lines for global, debug, loop declarations, etc.. 628 629class NamelessValue: 630 def __init__(self, check_prefix, check_key, ir_prefix, global_ir_prefix, global_ir_prefix_regexp, 631 ir_regexp, global_ir_rhs_regexp, is_before_functions, *, 632 is_number=False, replace_number_with_counter=False): 633 self.check_prefix = check_prefix 634 self.check_key = check_key 635 self.ir_prefix = ir_prefix 636 self.global_ir_prefix = global_ir_prefix 637 self.global_ir_prefix_regexp = global_ir_prefix_regexp 638 self.ir_regexp = ir_regexp 639 self.global_ir_rhs_regexp = global_ir_rhs_regexp 640 self.is_before_functions = is_before_functions 641 self.is_number = is_number 642 # Some variable numbers (e.g. MCINST1234) will change based on unrelated 643 # modifications to LLVM, replace those with an incrementing counter. 644 self.replace_number_with_counter = replace_number_with_counter 645 self.variable_mapping = {} 646 647 # Return true if this kind of IR value is "local", basically if it matches '%{{.*}}'. 648 def is_local_def_ir_value_match(self, match): 649 return self.ir_prefix == '%' 650 651 # Return true if this kind of IR value is "global", basically if it matches '#{{.*}}'. 652 def is_global_scope_ir_value_match(self, match): 653 return self.global_ir_prefix is not None 654 655 # Return the IR prefix and check prefix we use for this kind or IR value, 656 # e.g., (%, TMP) for locals. 657 def get_ir_prefix_from_ir_value_match(self, match): 658 if self.ir_prefix and match.group(0).strip().startswith(self.ir_prefix): 659 return self.ir_prefix, self.check_prefix 660 return self.global_ir_prefix, self.check_prefix 661 662 # Return the IR regexp we use for this kind or IR value, e.g., [\w.-]+? for locals 663 def get_ir_regex_from_ir_value_re_match(self, match): 664 # for backwards compatibility we check locals with '.*' 665 if self.is_local_def_ir_value_match(match): 666 return '.*' 667 if self.ir_prefix and match.group(0).strip().startswith(self.ir_prefix): 668 return self.ir_regexp 669 return self.global_ir_prefix_regexp 670 671 # Create a FileCheck variable name based on an IR name. 672 def get_value_name(self, var: str, check_prefix: str): 673 var = var.replace('!', '') 674 if self.replace_number_with_counter: 675 assert var.isdigit(), var 676 replacement = self.variable_mapping.get(var, None) 677 if replacement is None: 678 # Replace variable with an incrementing counter 679 replacement = str(len(self.variable_mapping) + 1) 680 self.variable_mapping[var] = replacement 681 var = replacement 682 # This is a nameless value, prepend check_prefix. 683 if var.isdigit(): 684 var = check_prefix + var 685 else: 686 # This is a named value that clashes with the check_prefix, prepend with 687 # _prefix_filecheck_ir_name, if it has been defined. 688 if may_clash_with_default_check_prefix_name(check_prefix, var) and _prefix_filecheck_ir_name: 689 var = _prefix_filecheck_ir_name + var 690 var = var.replace('.', '_') 691 var = var.replace('-', '_') 692 return var.upper() 693 694 # Create a FileCheck variable from regex. 695 def get_value_definition(self, var, match): 696 # for backwards compatibility we check locals with '.*' 697 varname = self.get_value_name(var, self.check_prefix) 698 prefix = self.get_ir_prefix_from_ir_value_match(match)[0] 699 if self.is_number: 700 regex = '' # always capture a number in the default format 701 capture_start = '[[#' 702 else: 703 regex = self.get_ir_regex_from_ir_value_re_match(match) 704 capture_start = '[[' 705 if self.is_local_def_ir_value_match(match): 706 return capture_start + varname + ':' + prefix + regex + ']]' 707 return prefix + capture_start + varname + ':' + regex + ']]' 708 709 # Use a FileCheck variable. 710 def get_value_use(self, var, match, var_prefix=None): 711 if var_prefix is None: 712 var_prefix = self.check_prefix 713 capture_start = '[[#' if self.is_number else '[[' 714 if self.is_local_def_ir_value_match(match): 715 return capture_start + self.get_value_name(var, var_prefix) + ']]' 716 prefix = self.get_ir_prefix_from_ir_value_match(match)[0] 717 return prefix + capture_start + self.get_value_name(var, var_prefix) + ']]' 718 719# Description of the different "unnamed" values we match in the IR, e.g., 720# (local) ssa values, (debug) metadata, etc. 721ir_nameless_values = [ 722 NamelessValue(r'TMP' , '%' , r'%' , None , None , r'[\w$.-]+?' , None , False) , 723 NamelessValue(r'ATTR' , '#' , r'#' , None , None , r'[0-9]+' , None , False) , 724 NamelessValue(r'ATTR' , '#' , None , r'attributes #' , r'[0-9]+' , None , r'{[^}]*}' , False) , 725 NamelessValue(r'GLOB' , '@' , r'@' , None , None , r'[0-9]+' , None , False) , 726 NamelessValue(r'GLOB' , '@' , None , r'@' , r'[a-zA-Z0-9_$"\\.-]+' , None , r'.+' , True) , 727 NamelessValue(r'DBG' , '!' , r'!dbg ' , None , None , r'![0-9]+' , None , False) , 728 NamelessValue(r'PROF' , '!' , r'!prof ' , None , None , r'![0-9]+' , None , False) , 729 NamelessValue(r'TBAA' , '!' , r'!tbaa ' , None , None , r'![0-9]+' , None , False) , 730 NamelessValue(r'RNG' , '!' , r'!range ' , None , None , r'![0-9]+' , None , False) , 731 NamelessValue(r'LOOP' , '!' , r'!llvm.loop ' , None , None , r'![0-9]+' , None , False) , 732 NamelessValue(r'META' , '!' , r'metadata ' , None , None , r'![0-9]+' , None , False) , 733 NamelessValue(r'META' , '!' , None , r'' , r'![0-9]+' , None , r'(?:distinct |)!.*' , False) , 734] 735 736asm_nameless_values = [ 737 NamelessValue(r'MCINST', 'Inst#', None, '<MCInst #', r'\d+', None, r'.+', 738 False, is_number=True, replace_number_with_counter=True), 739 NamelessValue(r'MCREG', 'Reg:', None, '<MCOperand Reg:', r'\d+', None, r'.+', 740 False, is_number=True, replace_number_with_counter=True), 741] 742 743def createOrRegexp(old, new): 744 if not old: 745 return new 746 if not new: 747 return old 748 return old + '|' + new 749 750def createPrefixMatch(prefix_str, prefix_re): 751 if prefix_str is None or prefix_re is None: 752 return '' 753 return '(?:' + prefix_str + '(' + prefix_re + '))' 754 755# Build the regexp that matches an "IR value". This can be a local variable, 756# argument, global, or metadata, anything that is "named". It is important that 757# the PREFIX and SUFFIX below only contain a single group, if that changes 758# other locations will need adjustment as well. 759IR_VALUE_REGEXP_PREFIX = r'(\s*)' 760IR_VALUE_REGEXP_STRING = r'' 761for nameless_value in ir_nameless_values: 762 lcl_match = createPrefixMatch(nameless_value.ir_prefix, nameless_value.ir_regexp) 763 glb_match = createPrefixMatch(nameless_value.global_ir_prefix, nameless_value.global_ir_prefix_regexp) 764 assert((lcl_match or glb_match) and not (lcl_match and glb_match)) 765 if lcl_match: 766 IR_VALUE_REGEXP_STRING = createOrRegexp(IR_VALUE_REGEXP_STRING, lcl_match) 767 elif glb_match: 768 IR_VALUE_REGEXP_STRING = createOrRegexp(IR_VALUE_REGEXP_STRING, '^' + glb_match) 769IR_VALUE_REGEXP_SUFFIX = r'([,\s\(\)]|\Z)' 770IR_VALUE_RE = re.compile(IR_VALUE_REGEXP_PREFIX + r'(' + IR_VALUE_REGEXP_STRING + r')' + IR_VALUE_REGEXP_SUFFIX) 771 772# Build the regexp that matches an "ASM value" (currently only for --asm-show-inst comments). 773ASM_VALUE_REGEXP_STRING = '' 774for nameless_value in asm_nameless_values: 775 glb_match = createPrefixMatch(nameless_value.global_ir_prefix, nameless_value.global_ir_prefix_regexp) 776 assert not nameless_value.ir_prefix and not nameless_value.ir_regexp 777 ASM_VALUE_REGEXP_STRING = createOrRegexp(ASM_VALUE_REGEXP_STRING, glb_match) 778ASM_VALUE_REGEXP_SUFFIX = r'([>\s]|\Z)' 779ASM_VALUE_RE = re.compile(r'((?:#|//)\s*)' + '(' + ASM_VALUE_REGEXP_STRING + ')' + ASM_VALUE_REGEXP_SUFFIX) 780 781# The entire match is group 0, the prefix has one group (=1), the entire 782# IR_VALUE_REGEXP_STRING is one group (=2), and then the nameless values start. 783first_nameless_group_in_ir_value_match = 3 784 785# constants for the group id of special matches 786variable_group_in_ir_value_match = 3 787attribute_group_in_ir_value_match = 4 788 789# Check a match for IR_VALUE_RE and inspect it to determine if it was a local 790# value, %..., global @..., debug number !dbg !..., etc. See the PREFIXES above. 791def get_idx_from_ir_value_match(match): 792 for i in range(first_nameless_group_in_ir_value_match, match.lastindex): 793 if match.group(i) is not None: 794 return i - first_nameless_group_in_ir_value_match 795 error("Unable to identify the kind of IR value from the match!") 796 return 0 797 798# See get_idx_from_ir_value_match 799def get_name_from_ir_value_match(match): 800 return match.group(get_idx_from_ir_value_match(match) + first_nameless_group_in_ir_value_match) 801 802def get_nameless_value_from_match(match, nameless_values) -> NamelessValue: 803 return nameless_values[get_idx_from_ir_value_match(match)] 804 805# Return true if var clashes with the scripted FileCheck check_prefix. 806def may_clash_with_default_check_prefix_name(check_prefix, var): 807 return check_prefix and re.match(r'^' + check_prefix + r'[0-9]+?$', var, re.IGNORECASE) 808 809def generalize_check_lines_common(lines, is_analyze, vars_seen, 810 global_vars_seen, nameless_values, 811 nameless_value_regex, is_asm): 812 # This gets called for each match that occurs in 813 # a line. We transform variables we haven't seen 814 # into defs, and variables we have seen into uses. 815 def transform_line_vars(match): 816 var = get_name_from_ir_value_match(match) 817 nameless_value = get_nameless_value_from_match(match, nameless_values) 818 if may_clash_with_default_check_prefix_name(nameless_value.check_prefix, var): 819 warn("Change IR value name '%s' or use --prefix-filecheck-ir-name to prevent possible conflict" 820 " with scripted FileCheck name." % (var,)) 821 key = (var, nameless_value.check_key) 822 is_local_def = nameless_value.is_local_def_ir_value_match(match) 823 if is_local_def and key in vars_seen: 824 rv = nameless_value.get_value_use(var, match) 825 elif not is_local_def and key in global_vars_seen: 826 # We could have seen a different prefix for the global variables first, 827 # ensure we use that one instead of the prefix for the current match. 828 rv = nameless_value.get_value_use(var, match, global_vars_seen[key]) 829 else: 830 if is_local_def: 831 vars_seen.add(key) 832 else: 833 global_vars_seen[key] = nameless_value.check_prefix 834 rv = nameless_value.get_value_definition(var, match) 835 # re.sub replaces the entire regex match 836 # with whatever you return, so we have 837 # to make sure to hand it back everything 838 # including the commas and spaces. 839 return match.group(1) + rv + match.group(match.lastindex) 840 841 lines_with_def = [] 842 843 for i, line in enumerate(lines): 844 if not is_asm: 845 # An IR variable named '%.' matches the FileCheck regex string. 846 line = line.replace('%.', '%dot') 847 for regex in _global_hex_value_regex: 848 if re.match('^@' + regex + ' = ', line): 849 line = re.sub(r'\bi([0-9]+) ([0-9]+)', 850 lambda m : 'i' + m.group(1) + ' [[#' + hex(int(m.group(2))) + ']]', 851 line) 852 break 853 # Ignore any comments, since the check lines will too. 854 scrubbed_line = SCRUB_IR_COMMENT_RE.sub(r'', line) 855 lines[i] = scrubbed_line 856 if is_asm or not is_analyze: 857 # It can happen that two matches are back-to-back and for some reason sub 858 # will not replace both of them. For now we work around this by 859 # substituting until there is no more match. 860 changed = True 861 while changed: 862 (lines[i], changed) = nameless_value_regex.subn(transform_line_vars, 863 lines[i], count=1) 864 return lines 865 866# Replace IR value defs and uses with FileCheck variables. 867def generalize_check_lines(lines, is_analyze, vars_seen, global_vars_seen): 868 return generalize_check_lines_common(lines, is_analyze, vars_seen, 869 global_vars_seen, ir_nameless_values, 870 IR_VALUE_RE, False) 871 872def generalize_asm_check_lines(lines, vars_seen, global_vars_seen): 873 return generalize_check_lines_common(lines, False, vars_seen, 874 global_vars_seen, asm_nameless_values, 875 ASM_VALUE_RE, True) 876 877def add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, check_label_format, is_backend, is_analyze, global_vars_seen_dict, is_filtered): 878 # prefix_exclusions are prefixes we cannot use to print the function because it doesn't exist in run lines that use these prefixes as well. 879 prefix_exclusions = set() 880 printed_prefixes = [] 881 for p in prefix_list: 882 checkprefixes = p[0] 883 # If not all checkprefixes of this run line produced the function we cannot check for it as it does not 884 # exist for this run line. A subset of the check prefixes might know about the function but only because 885 # other run lines created it. 886 if any(map(lambda checkprefix: func_name not in func_dict[checkprefix], checkprefixes)): 887 prefix_exclusions |= set(checkprefixes) 888 continue 889 890 # prefix_exclusions is constructed, we can now emit the output 891 for p in prefix_list: 892 global_vars_seen = {} 893 checkprefixes = p[0] 894 for checkprefix in checkprefixes: 895 if checkprefix in global_vars_seen_dict: 896 global_vars_seen.update(global_vars_seen_dict[checkprefix]) 897 else: 898 global_vars_seen_dict[checkprefix] = {} 899 if checkprefix in printed_prefixes: 900 break 901 902 # Check if the prefix is excluded. 903 if checkprefix in prefix_exclusions: 904 continue 905 906 # If we do not have output for this prefix we skip it. 907 if not func_dict[checkprefix][func_name]: 908 continue 909 910 # Add some space between different check prefixes, but not after the last 911 # check line (before the test code). 912 if is_backend: 913 if len(printed_prefixes) != 0: 914 output_lines.append(comment_marker) 915 916 if checkprefix not in global_vars_seen_dict: 917 global_vars_seen_dict[checkprefix] = {} 918 919 global_vars_seen_before = [key for key in global_vars_seen.keys()] 920 921 vars_seen = set() 922 printed_prefixes.append(checkprefix) 923 attrs = str(func_dict[checkprefix][func_name].attrs) 924 attrs = '' if attrs == 'None' else attrs 925 if attrs: 926 output_lines.append('%s %s: Function Attrs: %s' % (comment_marker, checkprefix, attrs)) 927 args_and_sig = str(func_dict[checkprefix][func_name].args_and_sig) 928 if args_and_sig: 929 args_and_sig = generalize_check_lines([args_and_sig], is_analyze, vars_seen, global_vars_seen)[0] 930 func_name_separator = func_dict[checkprefix][func_name].func_name_separator 931 if '[[' in args_and_sig: 932 output_lines.append(check_label_format % (checkprefix, func_name, '', func_name_separator)) 933 output_lines.append('%s %s-SAME: %s' % (comment_marker, checkprefix, args_and_sig)) 934 else: 935 output_lines.append(check_label_format % (checkprefix, func_name, args_and_sig, func_name_separator)) 936 func_body = str(func_dict[checkprefix][func_name]).splitlines() 937 if not func_body: 938 # We have filtered everything. 939 continue 940 941 # For ASM output, just emit the check lines. 942 if is_backend: 943 body_start = 1 944 if is_filtered: 945 # For filtered output we don't add "-NEXT" so don't add extra spaces 946 # before the first line. 947 body_start = 0 948 else: 949 output_lines.append('%s %s: %s' % (comment_marker, checkprefix, func_body[0])) 950 func_lines = generalize_asm_check_lines(func_body[body_start:], 951 vars_seen, global_vars_seen) 952 for func_line in func_lines: 953 if func_line.strip() == '': 954 output_lines.append('%s %s-EMPTY:' % (comment_marker, checkprefix)) 955 else: 956 check_suffix = '-NEXT' if not is_filtered else '' 957 output_lines.append('%s %s%s: %s' % (comment_marker, checkprefix, 958 check_suffix, func_line)) 959 # Remember new global variables we have not seen before 960 for key in global_vars_seen: 961 if key not in global_vars_seen_before: 962 global_vars_seen_dict[checkprefix][key] = global_vars_seen[key] 963 break 964 965 # For IR output, change all defs to FileCheck variables, so we're immune 966 # to variable naming fashions. 967 func_body = generalize_check_lines(func_body, is_analyze, vars_seen, global_vars_seen) 968 969 # This could be selectively enabled with an optional invocation argument. 970 # Disabled for now: better to check everything. Be safe rather than sorry. 971 972 # Handle the first line of the function body as a special case because 973 # it's often just noise (a useless asm comment or entry label). 974 #if func_body[0].startswith("#") or func_body[0].startswith("entry:"): 975 # is_blank_line = True 976 #else: 977 # output_lines.append('%s %s: %s' % (comment_marker, checkprefix, func_body[0])) 978 # is_blank_line = False 979 980 is_blank_line = False 981 982 for func_line in func_body: 983 if func_line.strip() == '': 984 is_blank_line = True 985 continue 986 # Do not waste time checking IR comments. 987 func_line = SCRUB_IR_COMMENT_RE.sub(r'', func_line) 988 989 # Skip blank lines instead of checking them. 990 if is_blank_line: 991 output_lines.append('{} {}: {}'.format( 992 comment_marker, checkprefix, func_line)) 993 else: 994 check_suffix = '-NEXT' if not is_filtered else '' 995 output_lines.append('{} {}{}: {}'.format( 996 comment_marker, checkprefix, check_suffix, func_line)) 997 is_blank_line = False 998 999 # Add space between different check prefixes and also before the first 1000 # line of code in the test function. 1001 output_lines.append(comment_marker) 1002 1003 # Remember new global variables we have not seen before 1004 for key in global_vars_seen: 1005 if key not in global_vars_seen_before: 1006 global_vars_seen_dict[checkprefix][key] = global_vars_seen[key] 1007 break 1008 return printed_prefixes 1009 1010def add_ir_checks(output_lines, comment_marker, prefix_list, func_dict, 1011 func_name, preserve_names, function_sig, 1012 global_vars_seen_dict, is_filtered): 1013 # Label format is based on IR string. 1014 function_def_regex = 'define {{[^@]+}}' if function_sig else '' 1015 check_label_format = '{} %s-LABEL: {}@%s%s%s'.format(comment_marker, function_def_regex) 1016 return add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, 1017 check_label_format, False, preserve_names, global_vars_seen_dict, 1018 is_filtered) 1019 1020def add_analyze_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, is_filtered): 1021 check_label_format = '{} %s-LABEL: \'%s%s%s\''.format(comment_marker) 1022 global_vars_seen_dict = {} 1023 return add_checks(output_lines, comment_marker, prefix_list, func_dict, func_name, 1024 check_label_format, False, True, global_vars_seen_dict, 1025 is_filtered) 1026 1027def build_global_values_dictionary(glob_val_dict, raw_tool_output, prefixes): 1028 for nameless_value in itertools.chain(ir_nameless_values, asm_nameless_values): 1029 if nameless_value.global_ir_prefix is None: 1030 continue 1031 1032 lhs_re_str = nameless_value.global_ir_prefix + nameless_value.global_ir_prefix_regexp 1033 rhs_re_str = nameless_value.global_ir_rhs_regexp 1034 1035 global_ir_value_re_str = r'^' + lhs_re_str + r'\s=\s' + rhs_re_str + r'$' 1036 global_ir_value_re = re.compile(global_ir_value_re_str, flags=(re.M)) 1037 lines = [] 1038 for m in global_ir_value_re.finditer(raw_tool_output): 1039 lines.append(m.group(0)) 1040 1041 for prefix in prefixes: 1042 if glob_val_dict[prefix] is None: 1043 continue 1044 if nameless_value.check_prefix in glob_val_dict[prefix]: 1045 if lines == glob_val_dict[prefix][nameless_value.check_prefix]: 1046 continue 1047 if prefix == prefixes[-1]: 1048 warn('Found conflicting asm under the same prefix: %r!' % (prefix,)) 1049 else: 1050 glob_val_dict[prefix][nameless_value.check_prefix] = None 1051 continue 1052 glob_val_dict[prefix][nameless_value.check_prefix] = lines 1053 1054def add_global_checks(glob_val_dict, comment_marker, prefix_list, output_lines, global_vars_seen_dict, is_analyze, is_before_functions): 1055 printed_prefixes = set() 1056 for nameless_value in ir_nameless_values: 1057 if nameless_value.global_ir_prefix is None: 1058 continue 1059 if nameless_value.is_before_functions != is_before_functions: 1060 continue 1061 for p in prefix_list: 1062 global_vars_seen = {} 1063 checkprefixes = p[0] 1064 if checkprefixes is None: 1065 continue 1066 for checkprefix in checkprefixes: 1067 if checkprefix in global_vars_seen_dict: 1068 global_vars_seen.update(global_vars_seen_dict[checkprefix]) 1069 else: 1070 global_vars_seen_dict[checkprefix] = {} 1071 if (checkprefix, nameless_value.check_prefix) in printed_prefixes: 1072 break 1073 if not glob_val_dict[checkprefix]: 1074 continue 1075 if nameless_value.check_prefix not in glob_val_dict[checkprefix]: 1076 continue 1077 if not glob_val_dict[checkprefix][nameless_value.check_prefix]: 1078 continue 1079 1080 check_lines = [] 1081 global_vars_seen_before = [key for key in global_vars_seen.keys()] 1082 for line in glob_val_dict[checkprefix][nameless_value.check_prefix]: 1083 if _global_value_regex: 1084 matched = False 1085 for regex in _global_value_regex: 1086 if re.match('^@' + regex + ' = ', line): 1087 matched = True 1088 break 1089 if not matched: 1090 continue 1091 tmp = generalize_check_lines([line], is_analyze, set(), global_vars_seen) 1092 check_line = '%s %s: %s' % (comment_marker, checkprefix, tmp[0]) 1093 check_lines.append(check_line) 1094 if not check_lines: 1095 continue 1096 1097 output_lines.append(comment_marker + SEPARATOR) 1098 for check_line in check_lines: 1099 output_lines.append(check_line) 1100 1101 printed_prefixes.add((checkprefix, nameless_value.check_prefix)) 1102 1103 # Remembe new global variables we have not seen before 1104 for key in global_vars_seen: 1105 if key not in global_vars_seen_before: 1106 global_vars_seen_dict[checkprefix][key] = global_vars_seen[key] 1107 break 1108 1109 if printed_prefixes: 1110 output_lines.append(comment_marker + SEPARATOR) 1111 1112 1113def check_prefix(prefix): 1114 if not PREFIX_RE.match(prefix): 1115 hint = "" 1116 if ',' in prefix: 1117 hint = " Did you mean '--check-prefixes=" + prefix + "'?" 1118 warn(("Supplied prefix '%s' is invalid. Prefix must contain only alphanumeric characters, hyphens and underscores." + hint) % 1119 (prefix)) 1120 1121 1122def verify_filecheck_prefixes(fc_cmd): 1123 fc_cmd_parts = fc_cmd.split() 1124 for part in fc_cmd_parts: 1125 if "check-prefix=" in part: 1126 prefix = part.split('=', 1)[1] 1127 check_prefix(prefix) 1128 elif "check-prefixes=" in part: 1129 prefixes = part.split('=', 1)[1].split(',') 1130 for prefix in prefixes: 1131 check_prefix(prefix) 1132 if prefixes.count(prefix) > 1: 1133 warn("Supplied prefix '%s' is not unique in the prefix list." % (prefix,)) 1134 1135 1136def get_autogennote_suffix(parser, args): 1137 autogenerated_note_args = '' 1138 for action in parser._actions: 1139 if not hasattr(args, action.dest): 1140 continue # Ignore options such as --help that aren't included in args 1141 # Ignore parameters such as paths to the binary or the list of tests 1142 if action.dest in ('tests', 'update_only', 'opt_binary', 'llc_binary', 1143 'clang', 'opt', 'llvm_bin', 'verbose'): 1144 continue 1145 value = getattr(args, action.dest) 1146 if action.const is not None: # action stores a constant (usually True/False) 1147 # Skip actions with different constant values (this happens with boolean 1148 # --foo/--no-foo options) 1149 if value != action.const: 1150 continue 1151 if parser.get_default(action.dest) == value: 1152 continue # Don't add default values 1153 if action.dest == 'filters': 1154 # Create a separate option for each filter element. The value is a list 1155 # of Filter objects. 1156 for elem in value: 1157 opt_name = 'filter-out' if elem.is_filter_out else 'filter' 1158 opt_value = elem.pattern() 1159 new_arg = '--%s "%s" ' % (opt_name, opt_value.strip('"')) 1160 if new_arg not in autogenerated_note_args: 1161 autogenerated_note_args += new_arg 1162 else: 1163 autogenerated_note_args += action.option_strings[0] + ' ' 1164 if action.const is None: # action takes a parameter 1165 if action.nargs == '+': 1166 value = ' '.join(map(lambda v: '"' + v.strip('"') + '"', value)) 1167 autogenerated_note_args += '%s ' % value 1168 if autogenerated_note_args: 1169 autogenerated_note_args = ' %s %s' % (UTC_ARGS_KEY, autogenerated_note_args[:-1]) 1170 return autogenerated_note_args 1171 1172 1173def check_for_command(line, parser, args, argv, argparse_callback): 1174 cmd_m = UTC_ARGS_CMD.match(line) 1175 if cmd_m: 1176 for option in shlex.split(cmd_m.group('cmd').strip()): 1177 if option: 1178 argv.append(option) 1179 args = parser.parse_args(filter(lambda arg: arg not in args.tests, argv)) 1180 if argparse_callback is not None: 1181 argparse_callback(args) 1182 return args, argv 1183 1184def find_arg_in_test(test_info, get_arg_to_check, arg_string, is_global): 1185 result = get_arg_to_check(test_info.args) 1186 if not result and is_global: 1187 # See if this has been specified via UTC_ARGS. This is a "global" option 1188 # that affects the entire generation of test checks. If it exists anywhere 1189 # in the test, apply it to everything. 1190 saw_line = False 1191 for line_info in test_info.ro_iterlines(): 1192 line = line_info.line 1193 if not line.startswith(';') and line.strip() != '': 1194 saw_line = True 1195 result = get_arg_to_check(line_info.args) 1196 if result: 1197 if warn and saw_line: 1198 # We saw the option after already reading some test input lines. 1199 # Warn about it. 1200 print('WARNING: Found {} in line following test start: '.format(arg_string) 1201 + line, file=sys.stderr) 1202 print('WARNING: Consider moving {} to top of file'.format(arg_string), 1203 file=sys.stderr) 1204 break 1205 return result 1206 1207def dump_input_lines(output_lines, test_info, prefix_set, comment_string): 1208 for input_line_info in test_info.iterlines(output_lines): 1209 line = input_line_info.line 1210 args = input_line_info.args 1211 if line.strip() == comment_string: 1212 continue 1213 if line.strip() == comment_string + SEPARATOR: 1214 continue 1215 if line.lstrip().startswith(comment_string): 1216 m = CHECK_RE.match(line) 1217 if m and m.group(1) in prefix_set: 1218 continue 1219 output_lines.append(line.rstrip('\n')) 1220 1221def add_checks_at_end(output_lines, prefix_list, func_order, 1222 comment_string, check_generator): 1223 added = set() 1224 generated_prefixes = [] 1225 for prefix in prefix_list: 1226 prefixes = prefix[0] 1227 tool_args = prefix[1] 1228 for prefix in prefixes: 1229 for func in func_order[prefix]: 1230 if added: 1231 output_lines.append(comment_string) 1232 added.add(func) 1233 1234 # The add_*_checks routines expect a run list whose items are 1235 # tuples that have a list of prefixes as their first element and 1236 # tool command args string as their second element. They output 1237 # checks for each prefix in the list of prefixes. By doing so, it 1238 # implicitly assumes that for each function every run line will 1239 # generate something for that function. That is not the case for 1240 # generated functions as some run lines might not generate them 1241 # (e.g. -fopenmp vs. no -fopenmp). 1242 # 1243 # Therefore, pass just the prefix we're interested in. This has 1244 # the effect of generating all of the checks for functions of a 1245 # single prefix before moving on to the next prefix. So checks 1246 # are ordered by prefix instead of by function as in "normal" 1247 # mode. 1248 generated_prefixes.extend(check_generator(output_lines, 1249 [([prefix], tool_args)], 1250 func)) 1251 return generated_prefixes 1252