Skip to content

Commit f021c9b

Browse files
committed
[utils] update_llc_test_checks --downstream-target-handler-path option
This patch adds a new option to update_llce_test_checks that points to a downstream python module implementing handler functions for a downstream compiler.
1 parent df28c81 commit f021c9b

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; RUN: llc < %s -mtriple=i686-- | FileCheck %s
2+
define void @foo() {
3+
ret void
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; RUN: llc < %s -mtriple=i686-- | FileCheck %s
2+
define void @foo() {
3+
; CHECK-LABEL: foo:
4+
; CHECK: Test
5+
; CHECK-NEXT: Downstream
6+
; CHECK-NEXT: Target
7+
ret void
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This is an example of a downstream module, which in this case is for i686.
2+
# The goal is to test the updater's downstream-target-module-path option.
3+
#
4+
# This target module implements a simple scrubber that returns a fixed string,
5+
# a regex that matches the fixed strings generated by the scrubber.
6+
#
7+
# This module needs to define:
8+
# 2. The `downstream_scrub(asm, arg)` scrubber function
9+
# 3. The `downstream_asm_fn_re` regex for matching func, body, etc.
10+
11+
import re
12+
import sys
13+
import os
14+
from pathlib import Path
15+
common_dir = Path(__file__).parent / "../../../../../utils/"
16+
sys.path.append(common_dir)
17+
from UpdateTestChecks import common
18+
19+
# The asm scrubber returns the scrubbed asm.
20+
# In this test we return a fixed string.
21+
def scrub(asm, args):
22+
return ("Test\n"
23+
"Downstream\n"
24+
"Target\n")
25+
26+
# The regular expression for matching func, body, etc.
27+
regex = re.compile(
28+
r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*(@"?(?P=func)"?| -- Begin function (?P=func))\n(?:\s*\.?Lfunc_begin[^:\n]*:\n)?'
29+
r"(?:\.L(?P=func)\$local:\n)?" # drop .L<func>$local:
30+
r"(?:\s*\.type\s+\.L(?P=func)\$local,@function\n)?" # drop .type .L<func>$local
31+
r"(?:[ \t]*(?:\.cfi_startproc|\.cfi_personality|\.cfi_lsda|\.seh_proc|\.seh_handler)\b[^\n]*\n)*" # drop optional cfi
32+
r"(?P<body>^##?[ \t]+[^:]+:.*?)\s*"
33+
r"^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section|#+ -- End function)",
34+
flags=(re.M | re.S),
35+
)
36+
37+
def get_run_handler(triple):
38+
return (scrub, regex)
39+
40+
def add_checks(
41+
output_lines,
42+
comment_marker,
43+
prefix_list,
44+
func_dict,
45+
func_name,
46+
ginfo: common.GeneralizerInfo,
47+
global_vars_seen_dict,
48+
is_filtered,
49+
):
50+
# Label format is based on ASM string.
51+
check_label_format = "{} %s-LABEL: %s%s%s%s".format(comment_marker)
52+
return common.add_checks(
53+
output_lines,
54+
comment_marker,
55+
prefix_list,
56+
func_dict,
57+
func_name,
58+
check_label_format,
59+
ginfo,
60+
global_vars_seen_dict,
61+
is_filtered=is_filtered,
62+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# REQUIRES: x86-registered-target
2+
## Tests that the downstream module support works
3+
4+
# RUN: cp -f %S/Inputs/downstream_target.ll %t.ll && %update_llc_test_checks %t.ll --downstream-target-handler-path=%S/Inputs/downstream_target.py
5+
# RUN: grep -v '^; NOTE:' %t.ll > %t.ll.expected
6+
# RUN: diff -b -u %S/Inputs/downstream_target.ll.expected %t.ll.expected
7+

llvm/utils/update_llc_test_checks.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import argparse
1313
import os # Used to advertise this file's name ("autogenerated_note").
14+
import sys
15+
import importlib.util
1416

1517
from UpdateTestChecks import common
1618

@@ -20,6 +22,18 @@
2022
"llc",
2123
]
2224

25+
# Helper that imports module from `module_path` and returns it.
26+
def import_module(module_path):
27+
if not os.path.exists(module_path):
28+
common.error("Path does not exist", module_path)
29+
sys.exit(1)
30+
module_name_with_ext = os.path.basename(module_path)
31+
module_name = os.path.splitext(module_name_with_ext)[0]
32+
spec = importlib.util.spec_from_file_location(module_name, module_path)
33+
module = importlib.util.module_from_spec(spec)
34+
sys.modules[module_name] = module
35+
spec.loader.exec_module(module)
36+
return module
2337

2438
def main():
2539
parser = argparse.ArgumentParser(description=__doc__)
@@ -67,6 +81,12 @@ def main():
6781
help="Set a default -march for when neither triple nor arch are found in a RUN line",
6882
)
6983
parser.add_argument("tests", nargs="+")
84+
parser.add_argument(
85+
"--downstream-target-handler-path",
86+
default=None,
87+
dest="downstream_target_handler_path",
88+
help="The path of a python module that implements the scrubber and the regex generator and adds it to the targets handlers map for a downstream target",
89+
)
7090
initial_args = common.parse_commandline_args(parser)
7191

7292
script_name = os.path.basename(__file__)
@@ -107,10 +127,15 @@ def main():
107127
march_in_cmd = m.groups()[0]
108128

109129
m = common.DEBUG_ONLY_ARG_RE.search(llc_cmd)
110-
if m and m.groups()[0] == "isel":
111-
from UpdateTestChecks import isel as output_type
130+
if initial_args.downstream_target_handler_path:
131+
common.warn("Downstream handlers provided by '%s'"
132+
% initial_args.downstream_target_handler_path)
133+
output_type = import_module(initial_args.downstream_target_handler_path)
112134
else:
113-
from UpdateTestChecks import asm as output_type
135+
if m and m.groups()[0] == "isel":
136+
from UpdateTestChecks import isel as output_type
137+
else:
138+
from UpdateTestChecks import asm as output_type
114139

115140
common.verify_filecheck_prefixes(filecheck_cmd)
116141

0 commit comments

Comments
 (0)