Nicolai Hähnle 597ac471cc
update_test_checks: match IR basic block labels (#88979)
Labels are matched using a regexp of the form '^(pattern):', which
requires the addition of a "suffix" concept to NamelessValue.

Aside from that, the key challenge is that block labels are values, and
we typically capture values including the prefix '%'. However, when
labels appear at the start of a basic block, the prefix '%' is not
included, so we must capture block label values *without* the prefix
'%'.

We don't know ahead of time whether an IR value is a label or not. In
most cases, they are prefixed by the word "label" (their type), but this
isn't the case in phi nodes. We solve this issue by leveraging the
two-phase nature of variable generalization: the first pass finds all
occurences of a variable and determines whether the '%' prefix can be
included or not. The second pass does the actual substitution.

This change also unifies the generalization path for assembly with that
for IR and analysis, in the hope that any future changes avoid diverging
those cases future.

I also considered the alternative of trying to detect the phi node case
using more regular expression special cases but ultimately decided
against that because it seemed more fragile, and perhaps the approach of
keeping a tentative prefix that may later be discarded could also be
eventually applied to some metadata and attribute cases.

Note that an early version of this change was reviewed as
https://reviews.llvm.org/D142452, before version numbers were
introduced. This is a substantially updated version of that change.
2024-05-19 01:39:47 +02:00

80 lines
1.9 KiB
Python

import re
from . import common
import sys
if sys.version_info[0] > 2:
class string:
expandtabs = str.expandtabs
else:
import string
# Support of isel debug checks
# RegEx: this is where the magic happens.
##### iSel parser
# TODO: add function prefix
ISEL_FUNCTION_DEFAULT_RE = re.compile(
r"Selected[\s]*selection[\s]*DAG:[\s]*%bb.0[\s]*\'(?P<func>.*?):[^\']*\'*\n"
r"(?P<body>.*?)\n"
r"Total[\s]*amount[\s]*of[\s]*phi[\s]*nodes[\s]*to[\s]*update:[\s]*[0-9]+",
flags=(re.M | re.S),
)
def scrub_isel_default(isel, args):
# Scrub runs of whitespace out of the iSel debug output, but leave the leading
# whitespace in place.
isel = common.SCRUB_WHITESPACE_RE.sub(r" ", isel)
# Expand the tabs used for indentation.
isel = string.expandtabs(isel, 2)
# Strip trailing whitespace.
isel = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r"", isel)
return isel
def get_run_handler(triple):
target_handlers = {}
handler = None
best_prefix = ""
for prefix, s in target_handlers.items():
if triple.startswith(prefix) and len(prefix) > len(best_prefix):
handler = s
best_prefix = prefix
if handler is None:
common.debug("Using default handler.")
handler = (scrub_isel_default, ISEL_FUNCTION_DEFAULT_RE)
return handler
##### Generator of iSel CHECK lines
def add_checks(
output_lines,
comment_marker,
prefix_list,
func_dict,
func_name,
ginfo: common.GeneralizerInfo,
global_vars_seen_dict,
is_filtered,
):
# Label format is based on iSel string.
check_label_format = "{} %s-LABEL: %s%s%s%s".format(comment_marker)
return common.add_checks(
output_lines,
comment_marker,
prefix_list,
func_dict,
func_name,
check_label_format,
ginfo,
global_vars_seen_dict,
is_filtered=is_filtered,
)