2019-11-26 10:40:52 -08:00
|
|
|
#!/usr/bin/env python3
|
2018-04-06 12:36:27 +00:00
|
|
|
|
|
|
|
"""A script to generate FileCheck statements for 'opt' analysis tests.
|
|
|
|
|
|
|
|
This script is a utility to update LLVM opt analysis test cases with new
|
|
|
|
FileCheck patterns. It can either update all of the tests in the file or
|
|
|
|
a single test function.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
$ update_analyze_test_checks.py --opt=../bin/opt test/foo.ll
|
|
|
|
|
|
|
|
Workflow:
|
|
|
|
1. Make a compiler patch that requires updating some number of FileCheck lines
|
|
|
|
in regression test files.
|
|
|
|
2. Save the patch and revert it from your local work area.
|
|
|
|
3. Update the RUN-lines in the affected regression tests to look canonical.
|
2022-02-10 14:24:04 -08:00
|
|
|
Example: "; RUN: opt < %s -passes='print<cost-model>' -disable-output 2>&1 | FileCheck %s"
|
2018-04-06 12:36:27 +00:00
|
|
|
4. Refresh the FileCheck lines for either the entire file or select functions by
|
|
|
|
running this script.
|
|
|
|
5. Commit the fresh baseline of checks.
|
|
|
|
6. Apply your patch from step 1 and rebuild your local binaries.
|
|
|
|
7. Re-run this script on affected regression tests.
|
|
|
|
8. Check the diffs to ensure the script has done something reasonable.
|
|
|
|
9. Submit a patch including the regression test diffs for review.
|
|
|
|
|
|
|
|
A common pattern is to have the script insert complete checking of every
|
|
|
|
instruction. Then, edit it down to only check the relevant instructions.
|
|
|
|
The script is designed to make adding checks to a test case fast, it is *not*
|
|
|
|
designed to be authoratitive about what constitutes a good test!
|
|
|
|
"""
|
|
|
|
|
2019-01-03 14:11:33 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
import argparse
|
|
|
|
import os # Used to advertise this file's name ("autogenerated_note").
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
|
|
|
|
from UpdateTestChecks import common
|
|
|
|
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
def main():
|
|
|
|
from argparse import RawTextHelpFormatter
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=__doc__, formatter_class=RawTextHelpFormatter
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--opt-binary",
|
|
|
|
default="opt",
|
|
|
|
help="The opt binary used to generate the test case",
|
|
|
|
)
|
|
|
|
parser.add_argument("--function", help="The function in the test file to update")
|
|
|
|
parser.add_argument("tests", nargs="+")
|
2022-03-07 16:54:57 +03:00
|
|
|
initial_args = common.parse_commandline_args(parser)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2019-08-07 14:44:50 +00:00
|
|
|
script_name = os.path.basename(__file__)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-03-07 16:54:57 +03:00
|
|
|
opt_basename = os.path.basename(initial_args.opt_binary)
|
2018-04-06 12:36:27 +00:00
|
|
|
if opt_basename != "opt":
|
2019-08-07 14:44:50 +00:00
|
|
|
common.error("Unexpected opt name: " + opt_basename)
|
2018-04-06 12:36:27 +00:00
|
|
|
sys.exit(1)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-03-07 16:54:57 +03:00
|
|
|
for ti in common.itertests(
|
|
|
|
initial_args.tests, parser, script_name="utils/" + script_name
|
|
|
|
):
|
|
|
|
triple_in_ir = None
|
|
|
|
for l in ti.input_lines:
|
|
|
|
m = common.TRIPLE_IR_RE.match(l)
|
|
|
|
if m:
|
|
|
|
triple_in_ir = m.groups()[0]
|
|
|
|
break
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
prefix_list = []
|
2022-03-07 16:54:57 +03:00
|
|
|
for l in ti.run_lines:
|
2019-08-07 14:44:50 +00:00
|
|
|
if "|" not in l:
|
2022-11-29 23:08:47 +01:00
|
|
|
common.warn("Skipping unparsable RUN line: " + l)
|
2019-08-07 14:44:50 +00:00
|
|
|
continue
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
(tool_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split("|", 1)])
|
[UpdateTestChecks] Emit warning when invalid value for -check-prefix(es) option
Summary:
The script is silent for the following issue:
FileCheck %s -check-prefix=CHECK,POPCOUNT
FileCheck will catch it later, but I think we can warn here too.
Now it warns:
./update_llc_test_checks.py file.ll
WARNING: Supplied prefix 'CHECK,POPCOUNT' is invalid. Prefix must contain only alphanumeric characters, hyphens and underscores. Did you mean --check-prefixes=CHECK,POPCOUNT?
Reviewers: lebedev.ri, spatel, RKSimon, craig.topper, nikic, gbedwell
Reviewed By: RKSimon
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D64589
llvm-svn: 367244
2019-07-29 17:41:00 +00:00
|
|
|
common.verify_filecheck_prefixes(filecheck_cmd)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
if not tool_cmd.startswith(opt_basename + " "):
|
2019-08-07 14:44:50 +00:00
|
|
|
common.warn("WSkipping non-%s RUN line: %s" % (opt_basename, l))
|
2018-04-06 12:36:27 +00:00
|
|
|
continue
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
if not filecheck_cmd.startswith("FileCheck "):
|
2019-08-07 14:44:50 +00:00
|
|
|
common.warn("Skipping non-FileChecked RUN line: " + l)
|
2018-04-06 12:36:27 +00:00
|
|
|
continue
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
tool_cmd_args = tool_cmd[len(opt_basename) :].strip()
|
|
|
|
tool_cmd_args = tool_cmd_args.replace("< %s", "").replace("%s", "").strip()
|
2023-02-04 09:35:32 +08:00
|
|
|
check_prefixes = common.get_check_prefixes(filecheck_cmd)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
# FIXME: We should use multiple check prefixes to common check lines. For
|
|
|
|
# now, we just ignore all but the last.
|
|
|
|
prefix_list.append((check_prefixes, tool_cmd_args))
|
2023-05-15 11:02:42 +02:00
|
|
|
|
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
|
|
|
ginfo = common.make_analyze_generalizer(version=1)
|
2020-12-16 10:20:12 -08:00
|
|
|
builder = common.FunctionTestBuilder(
|
|
|
|
run_list=prefix_list,
|
2020-12-29 11:56:59 +09:00
|
|
|
flags=type(
|
|
|
|
"",
|
|
|
|
(object,),
|
|
|
|
{
|
2022-03-07 16:54:57 +03:00
|
|
|
"verbose": ti.args.verbose,
|
|
|
|
"filters": ti.args.filters,
|
2020-12-29 11:56:59 +09:00
|
|
|
"function_signature": False,
|
2021-03-22 11:03:55 -07:00
|
|
|
"check_attributes": False,
|
2021-05-05 11:46:02 -07:00
|
|
|
"replace_value_regex": [],
|
2023-05-15 11:02:42 +02:00
|
|
|
},
|
2021-05-05 11:46:02 -07:00
|
|
|
),
|
2021-06-20 13:14:33 +03:00
|
|
|
scrubber_args=[],
|
2022-03-07 16:54:57 +03:00
|
|
|
path=ti.path,
|
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
|
|
|
ginfo=ginfo,
|
2019-12-02 10:50:23 +00:00
|
|
|
)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2019-12-02 10:50:23 +00:00
|
|
|
for prefixes, opt_args in prefix_list:
|
|
|
|
common.debug("Extracted opt cmd:", opt_basename, opt_args, file=sys.stderr)
|
2022-03-07 16:54:57 +03:00
|
|
|
common.debug(
|
2022-04-15 15:37:29 +03:00
|
|
|
"Extracted FileCheck prefixes:", str(prefixes), file=sys.stderr
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
|
|
|
|
2022-04-15 15:37:29 +03:00
|
|
|
raw_tool_outputs = common.invoke_tool(ti.args.opt_binary, opt_args, ti.path)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-04-15 15:37:29 +03:00
|
|
|
if re.search(r"Printing analysis ", raw_tool_outputs) is not None:
|
|
|
|
# Split analysis outputs by "Printing analysis " declarations.
|
|
|
|
for raw_tool_output in re.split(
|
|
|
|
r"Printing analysis ", raw_tool_outputs
|
|
|
|
):
|
|
|
|
builder.process_run_line(
|
2018-04-06 12:36:27 +00:00
|
|
|
common.ANALYZE_FUNCTION_RE,
|
2019-12-02 10:50:23 +00:00
|
|
|
common.scrub_body,
|
2022-04-15 15:37:29 +03:00
|
|
|
raw_tool_output,
|
2019-12-02 10:50:23 +00:00
|
|
|
prefixes,
|
|
|
|
)
|
2024-07-03 10:50:52 +01:00
|
|
|
elif (
|
|
|
|
re.search(r"(LV|LDist): Checking a loop in ", raw_tool_outputs)
|
|
|
|
is not None
|
|
|
|
):
|
2018-04-06 12:36:27 +00:00
|
|
|
for raw_tool_output in re.split(
|
2024-07-03 10:50:52 +01:00
|
|
|
r"(LV|LDist): Checking a loop in ", raw_tool_outputs
|
2023-05-15 11:02:42 +02:00
|
|
|
):
|
2018-04-06 12:36:27 +00:00
|
|
|
builder.process_run_line(
|
2024-07-03 10:50:52 +01:00
|
|
|
common.LOOP_PASS_DEBUG_RE,
|
2022-04-15 15:37:29 +03:00
|
|
|
common.scrub_body,
|
|
|
|
raw_tool_output,
|
2018-04-06 12:36:27 +00:00
|
|
|
prefixes,
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
|
|
|
else:
|
2018-04-06 12:36:27 +00:00
|
|
|
common.warn("Don't know how to deal with this output")
|
|
|
|
continue
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-11-28 10:02:14 -08:00
|
|
|
builder.processed_prefixes(prefixes)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-11-28 10:02:14 -08:00
|
|
|
func_dict = builder.finish_and_get_func_dict()
|
|
|
|
is_in_function = False
|
2018-04-06 12:36:27 +00:00
|
|
|
is_in_function_start = False
|
|
|
|
prefix_set = set([prefix for prefixes, _ in prefix_list for prefix in prefixes])
|
2019-12-02 10:50:23 +00:00
|
|
|
common.debug("Rewriting FileCheck prefixes:", str(prefix_set), file=sys.stderr)
|
2018-04-06 12:36:27 +00:00
|
|
|
output_lines = []
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-11-28 10:02:14 -08:00
|
|
|
generated_prefixes = []
|
2022-03-07 16:54:57 +03:00
|
|
|
for input_info in ti.iterlines(output_lines):
|
|
|
|
input_line = input_info.line
|
|
|
|
args = input_info.args
|
2018-04-06 12:36:27 +00:00
|
|
|
if is_in_function_start:
|
|
|
|
if input_line == "":
|
2023-05-15 11:02:42 +02:00
|
|
|
continue
|
2018-04-06 12:36:27 +00:00
|
|
|
if input_line.lstrip().startswith(";"):
|
2020-03-24 10:59:08 +00:00
|
|
|
m = common.CHECK_RE.match(input_line)
|
2018-04-06 12:36:27 +00:00
|
|
|
if not m or m.group(1) not in prefix_set:
|
|
|
|
output_lines.append(input_line)
|
2023-05-15 11:02:42 +02:00
|
|
|
continue
|
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
# Print out the various check lines here.
|
2022-11-28 10:02:14 -08:00
|
|
|
generated_prefixes.extend(
|
|
|
|
common.add_analyze_checks(
|
|
|
|
output_lines,
|
2023-05-15 11:02:42 +02:00
|
|
|
";",
|
2022-11-28 10:02:14 -08:00
|
|
|
prefix_list,
|
|
|
|
func_dict,
|
|
|
|
func_name,
|
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
|
|
|
ginfo,
|
2022-11-28 10:02:14 -08:00
|
|
|
is_filtered=builder.is_filtered(),
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
|
|
|
)
|
2018-04-06 12:36:27 +00:00
|
|
|
is_in_function_start = False
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
if is_in_function:
|
|
|
|
if common.should_add_line_to_output(input_line, prefix_set):
|
|
|
|
# This input line of the function body will go as-is into the output.
|
|
|
|
output_lines.append(input_line)
|
2023-05-15 11:02:42 +02:00
|
|
|
else:
|
|
|
|
continue
|
2018-04-06 12:36:27 +00:00
|
|
|
if input_line.strip() == "}":
|
|
|
|
is_in_function = False
|
2023-05-15 11:02:42 +02:00
|
|
|
continue
|
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
# If it's outside a function, it just gets copied to the output.
|
|
|
|
output_lines.append(input_line)
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2020-03-24 10:59:08 +00:00
|
|
|
m = common.IR_FUNCTION_RE.match(input_line)
|
2018-04-06 12:36:27 +00:00
|
|
|
if not m:
|
2023-05-15 11:02:42 +02:00
|
|
|
continue
|
2018-04-06 12:36:27 +00:00
|
|
|
func_name = m.group(1)
|
2022-03-07 16:54:57 +03:00
|
|
|
if ti.args.function is not None and func_name != ti.args.function:
|
2018-04-06 12:36:27 +00:00
|
|
|
# When filtering on a specific function, skip all others.
|
2023-05-15 11:02:42 +02:00
|
|
|
continue
|
2018-04-06 12:36:27 +00:00
|
|
|
is_in_function = is_in_function_start = True
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-11-28 10:02:14 -08:00
|
|
|
if ti.args.gen_unused_prefix_body:
|
|
|
|
output_lines.extend(
|
|
|
|
ti.get_checks_for_unused_prefixes(prefix_list, generated_prefixes)
|
2023-05-15 11:02:42 +02:00
|
|
|
)
|
|
|
|
|
2022-03-07 16:54:57 +03:00
|
|
|
common.debug("Writing %d lines to %s..." % (len(output_lines), ti.path))
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2022-03-07 16:54:57 +03:00
|
|
|
with open(ti.path, "wb") as f:
|
2019-01-30 16:15:59 +00:00
|
|
|
f.writelines(["{}\n".format(l).encode("utf-8") for l in output_lines])
|
2023-05-15 11:02:42 +02:00
|
|
|
|
2018-04-06 12:36:27 +00:00
|
|
|
|
2022-03-07 16:54:57 +03:00
|
|
|
if __name__ == "__main__":
|
2018-04-06 12:36:27 +00:00
|
|
|
main()
|