diff options
author | Alex Richardson <alexrichardson@google.com> | 2022-05-12 10:22:28 +0000 |
---|---|---|
committer | Alex Richardson <alexrichardson@google.com> | 2022-05-14 14:40:50 +0000 |
commit | 996873cdcbe0df80ce8cda9f708ac28d6bffbe78 (patch) | |
tree | 460256a3ea463609622c6e85aabaf8990245fb37 /llvm/utils/UpdateTestChecks/common.py | |
parent | 37a68497f1176cab3b2100c3bcb861e063cfaead (diff) | |
download | llvm-996873cdcbe0df80ce8cda9f708ac28d6bffbe78.zip llvm-996873cdcbe0df80ce8cda9f708ac28d6bffbe78.tar.gz llvm-996873cdcbe0df80ce8cda9f708ac28d6bffbe78.tar.bz2 |
[UpdateTestChecks] Use a counter for unpredictable FileCheck variables
Variable captures such as `<MCInst #` can change based on unrelated changes
to the LLVM backends, to avoid the generated test cases being different
use an incrementing counter for variable names instead of using the
actual value from the output file.
This change may also be beneficial for some nameless IR variables
(especially when combined with filtering of output), but for now I've
restricted this change to the obvious candidates (--asm-show-inst output).
Reviewed By: MaskRay
Differential Revision: https://reviews.llvm.org/D125405
Diffstat (limited to 'llvm/utils/UpdateTestChecks/common.py')
-rw-r--r-- | llvm/utils/UpdateTestChecks/common.py | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/llvm/utils/UpdateTestChecks/common.py b/llvm/utils/UpdateTestChecks/common.py index c6553b1..90dd326 100644 --- a/llvm/utils/UpdateTestChecks/common.py +++ b/llvm/utils/UpdateTestChecks/common.py @@ -597,7 +597,8 @@ SCRUB_IR_COMMENT_RE = re.compile(r'\s*;.*') class NamelessValue: def __init__(self, check_prefix, check_key, ir_prefix, global_ir_prefix, global_ir_prefix_regexp, - ir_regexp, global_ir_rhs_regexp, is_before_functions, is_number=False): + ir_regexp, global_ir_rhs_regexp, is_before_functions, *, + is_number=False, replace_number_with_counter=False): self.check_prefix = check_prefix self.check_key = check_key self.ir_prefix = ir_prefix @@ -607,6 +608,10 @@ class NamelessValue: self.global_ir_rhs_regexp = global_ir_rhs_regexp self.is_before_functions = is_before_functions self.is_number = is_number + # Some variable numbers (e.g. MCINST1234) will change based on unrelated + # modifications to LLVM, replace those with an incrementing counter. + self.replace_number_with_counter = replace_number_with_counter + self.variable_mapping = {} # Return true if this kind of IR value is "local", basically if it matches '%{{.*}}'. def is_local_def_ir_value_match(self, match): @@ -632,10 +637,33 @@ class NamelessValue: return self.ir_regexp return self.global_ir_prefix_regexp + # Create a FileCheck variable name based on an IR name. + def get_value_name(self, var: str, check_prefix: str): + var = var.replace('!', '') + if self.replace_number_with_counter: + assert var.isdigit(), var + replacement = self.variable_mapping.get(var, None) + if replacement is None: + # Replace variable with an incrementing counter + replacement = str(len(self.variable_mapping) + 1) + self.variable_mapping[var] = replacement + var = replacement + # This is a nameless value, prepend check_prefix. + if var.isdigit(): + var = check_prefix + var + else: + # This is a named value that clashes with the check_prefix, prepend with + # _prefix_filecheck_ir_name, if it has been defined. + if may_clash_with_default_check_prefix_name(check_prefix, var) and _prefix_filecheck_ir_name: + var = _prefix_filecheck_ir_name + var + var = var.replace('.', '_') + var = var.replace('-', '_') + return var.upper() + # Create a FileCheck variable from regex. def get_value_definition(self, var, match): # for backwards compatibility we check locals with '.*' - varname = get_value_name(var, self.check_prefix) + varname = self.get_value_name(var, self.check_prefix) prefix = self.get_ir_prefix_from_ir_value_match(match)[0] if self.is_number: regex = '' # always capture a number in the default format @@ -653,9 +681,9 @@ class NamelessValue: var_prefix = self.check_prefix capture_start = '[[#' if self.is_number else '[[' if self.is_local_def_ir_value_match(match): - return capture_start + get_value_name(var, var_prefix) + ']]' + return capture_start + self.get_value_name(var, var_prefix) + ']]' prefix = self.get_ir_prefix_from_ir_value_match(match)[0] - return prefix + capture_start + get_value_name(var, var_prefix) + ']]' + return prefix + capture_start + self.get_value_name(var, var_prefix) + ']]' # Description of the different "unnamed" values we match in the IR, e.g., # (local) ssa values, (debug) metadata, etc. @@ -675,8 +703,10 @@ ir_nameless_values = [ ] asm_nameless_values = [ - NamelessValue(r'MCINST', 'Inst#', None, '<MCInst #', r'\d+', None, r'.+', False, True), - NamelessValue(r'MCREG', 'Reg:', None, '<MCOperand Reg:', r'\d+', None, r'.+', False, True), + NamelessValue(r'MCINST', 'Inst#', None, '<MCInst #', r'\d+', None, r'.+', + False, is_number=True, replace_number_with_counter=True), + NamelessValue(r'MCREG', 'Reg:', None, '<MCOperand Reg:', r'\d+', None, r'.+', + False, is_number=True, replace_number_with_counter=True), ] def createOrRegexp(old, new): @@ -745,21 +775,6 @@ def get_nameless_value_from_match(match, nameless_values) -> NamelessValue: def may_clash_with_default_check_prefix_name(check_prefix, var): return check_prefix and re.match(r'^' + check_prefix + r'[0-9]+?$', var, re.IGNORECASE) -# Create a FileCheck variable name based on an IR name. -def get_value_name(var, check_prefix): - var = var.replace('!', '') - # This is a nameless value, prepend check_prefix. - if var.isdigit(): - var = check_prefix + var - else: - # This is a named value that clashes with the check_prefix, prepend with _prefix_filecheck_ir_name, - # if it has been defined. - if may_clash_with_default_check_prefix_name(check_prefix, var) and _prefix_filecheck_ir_name: - var = _prefix_filecheck_ir_name + var - var = var.replace('.', '_') - var = var.replace('-', '_') - return var.upper() - def generalize_check_lines_common(lines, is_analyze, vars_seen, global_vars_seen, nameless_values, nameless_value_regex, is_asm): |