[llvm] added a script to update llvm-mc test file (PR #107246)

Brox Chen via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 12:45:20 PDT 2024


https://github.com/broxigarchen updated https://github.com/llvm/llvm-project/pull/107246

>From b9275ebe2371ef9fc19bd36e9f44ff581f40fd25 Mon Sep 17 00:00:00 2001
From: guochen2 <guochen2 at amd.com>
Date: Wed, 4 Sep 2024 09:39:47 -0400
Subject: [PATCH 1/3] added a script to update llvm-mc test file

---
 .../update_mc_test_checks/Inputs/amdgpu_asm.s |   3 +
 .../Inputs/amdgpu_asm.s.expected              |   5 +
 .../Inputs/amdgpu_dasm.txt                    |   5 +
 .../Inputs/amdgpu_dasm.txt.expected           |   8 +
 .../update_mc_test_checks/amdgpu-basic.test   |   7 +
 llvm/utils/UpdateTestChecks/common.py         |   2 +-
 llvm/utils/update_mc_test_check.py            | 330 ++++++++++++++++++
 7 files changed, 359 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s
 create mode 100644 llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected
 create mode 100644 llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt
 create mode 100644 llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected
 create mode 100644 llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
 create mode 100755 llvm/utils/update_mc_test_check.py

diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s
new file mode 100644
index 00000000000000..b21935e1d1a3ab
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s
@@ -0,0 +1,3 @@
+// RUN: llvm-mc -triple=amdgcn -show-encoding %s 2>&1 | FileCheck --check-prefixes=CHECK %s
+
+v_bfrev_b32 v5, v1
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected
new file mode 100644
index 00000000000000..d29e1fc121e852
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected
@@ -0,0 +1,5 @@
+; NOTE: Assertions have been autogenerated by utils/update_mc_test_check.py UTC_ARGS: --version 5
+// RUN: llvm-mc -triple=amdgcn -show-encoding %s 2>&1 | FileCheck --check-prefixes=CHECK %s
+
+// CHECK: v_bfrev_b32_e32 v5, v1                  ; encoding: [0x01,0x71,0x0a,0x7e]
+v_bfrev_b32 v5, v1
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt
new file mode 100644
index 00000000000000..9f5fba6e50df25
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt
@@ -0,0 +1,5 @@
+# RUN: llvm-mc -triple=amdgcn -mcpu=gfx1100 -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK %s
+
+0x00,0x00,0x00,0x7e
+
+0xfd,0xb8,0x0a,0x7f
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected
new file mode 100644
index 00000000000000..896d5beb12d575
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected
@@ -0,0 +1,8 @@
+; NOTE: Assertions have been autogenerated by utils/update_mc_test_check.py UTC_ARGS: --version 5
+# RUN: llvm-mc -triple=amdgcn -mcpu=gfx1100 -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK %s
+
+# CHECK: v_nop                                   ; encoding: [0x00,0x00,0x00,0x7e]
+0x00,0x00,0x00,0x7e
+
+# COM: CHECK: warning: invalid instruction encoding
+0xfd,0xb8,0x0a,0x7f
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
new file mode 100644
index 00000000000000..a74e0ae4e76f95
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
@@ -0,0 +1,7 @@
+# REQUIRES: amdgpu-registered-target
+## Check that basic asm/dasm process is correct
+
+# RUN: cp -f %S/Inputs/amdgpu_asm.s %t.s && %update_mc_test_checks %t.s
+# RUN: diff -u %S/Inputs/amdgpu_asm.s.expected %t.s
+# RUN: cp -f %S/Inputs/amdgpu_dasm.txt %t.txt && %update_mc_test_checks %t.txt
+# RUN: diff -u %S/Inputs/amdgpu_dasm.txt.expected %t.txt
diff --git a/llvm/utils/UpdateTestChecks/common.py b/llvm/utils/UpdateTestChecks/common.py
index 9b9be69ee38448..b861bd010e2b25 100644
--- a/llvm/utils/UpdateTestChecks/common.py
+++ b/llvm/utils/UpdateTestChecks/common.py
@@ -573,7 +573,7 @@ def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False):
 
 IR_FUNCTION_RE = re.compile(r'^\s*define\s+(?:internal\s+)?[^@]*@"?([\w.$-]+)"?\s*\(')
 TRIPLE_IR_RE = re.compile(r'^\s*target\s+triple\s*=\s*"([^"]+)"$')
-TRIPLE_ARG_RE = re.compile(r"-mtriple[= ]([^ ]+)")
+TRIPLE_ARG_RE = re.compile(r"-m?triple[= ]([^ ]+)")
 MARCH_ARG_RE = re.compile(r"-march[= ]([^ ]+)")
 DEBUG_ONLY_ARG_RE = re.compile(r"-debug-only[= ]([^ ]+)")
 
diff --git a/llvm/utils/update_mc_test_check.py b/llvm/utils/update_mc_test_check.py
new file mode 100755
index 00000000000000..ccaee25b3fa6ad
--- /dev/null
+++ b/llvm/utils/update_mc_test_check.py
@@ -0,0 +1,330 @@
+#!/usr/bin/env python3
+"""
+A test update script.  This script is a utility to update LLVM 'llvm-mc' based test cases with new FileCheck patterns.
+"""
+
+from __future__ import print_function
+
+import argparse
+import os  # Used to advertise this file's name ("autogenerated_note").
+
+from UpdateTestChecks import common
+
+import subprocess
+import re
+
+mc_LIKE_TOOLS = [
+    "llvm-mc",
+]
+
+ERROR_RE = re.compile(r"(warning|error): .*")
+ERROR_CHECK_RE = re.compile(r"# COM: .*")
+OUTPUT_SKIPPED_RE = re.compile(r"(.text)")
+COMMENT = {
+        "asm" : "//",
+        "dasm" : "#"
+        }
+
+
+def invoke_tool(exe, cmd_args, testline, verbose=False):
+    if isinstance(cmd_args, list):
+        args = [applySubstitutions(a, substitutions) for a in cmd_args]
+    else:
+        args = cmd_args
+
+    cmd = "echo \"" + testline + "\" | " + exe + " " + args
+    if verbose:
+        print("Command: ", cmd)
+    out = subprocess.check_output(cmd, shell=True)
+    # Fix line endings to unix CR style.
+    return out.decode().replace("\r\n", "\n")
+
+
+# create tests line-by-line, here we just filter out the check lines and comments
+# and treat all others as tests
+def isTestLine(input_line, mc_mode):
+    # Skip comment lines
+    if input_line.strip(' \t\r').startswith(COMMENT[mc_mode]):
+        return False
+    elif input_line.strip(' \t\r') == '':
+        return False
+    # skip any CHECK lines.
+    elif common.CHECK_RE.match(input_line):
+        return False
+    return True
+
+def hasErr(err):
+    if err is None or len(err) == 0:
+        return False
+    if ERROR_RE.search(err):
+        return True
+    return False
+
+def getErrString(err):
+    if err is None or len(err) == 0:
+        return ""
+
+    lines = err.split('\n')
+    # take the first match
+    for line in lines:
+        s = ERROR_RE.search(line)
+        if s:
+            return s.group(0)
+    return ""
+
+def getOutputString(out):
+    if out is None or len(out) == 0:
+        return ""
+    lines = out.split('\n')
+    output = ""
+
+    for line in lines:
+        if OUTPUT_SKIPPED_RE.search(line):
+            continue
+        if line.strip('\t ') == '':
+            continue
+        output += line.lstrip('\t ')
+    return output
+
+def should_add_line_to_output(input_line, prefix_set, mc_mode):
+    # special check line
+    if mc_mode == 'dasm' and ERROR_CHECK_RE.search(input_line):
+        return False
+    else:
+        return common.should_add_line_to_output(input_line, prefix_set, comment_marker=COMMENT[mc_mode])
+
+
+def getStdCheckLine(prefix, output, mc_mode):
+    lines = output.split('\n')
+    output = ""
+    for line in lines:
+        output += COMMENT[mc_mode] + ' ' + prefix + ": " + line + '\n'
+    return output
+
+def getErrCheckLine(prefix, output, mc_mode):
+    if mc_mode == 'asm':
+        return COMMENT[mc_mode] + ' ' + prefix + ": " + output + '\n'
+    elif mc_mode == 'dasm':
+        return COMMENT[mc_mode] + ' COM: ' + prefix + ": " + output + '\n'
+
+def main():
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument(
+        "--mc-binary",
+        default=None,
+        help='The "mc" binary to use to generate the test case',
+    )
+    parser.add_argument(
+        "--tool",
+        default=None,
+        help="Treat the given tool name as an mc-like tool for which check lines should be generated",
+    )
+    parser.add_argument(
+        "--default-march",
+        default=None,
+        help="Set a default -march for when neither triple nor arch are found in a RUN line",
+    )
+    parser.add_argument("tests", nargs="+")
+    initial_args = common.parse_commandline_args(parser)
+
+    script_name = os.path.basename(__file__)
+
+    for ti in common.itertests(
+        initial_args.tests, parser, script_name="utils/" + script_name
+    ):
+        if ti.path.endswith('.s'):
+            mc_mode = "asm"
+        elif ti.path.endswith('.txt'):
+            mc_mode = "dasm"
+        else:
+            common.warn("Expected .s and .txt, Skipping file : ", ti.path)
+            continue
+
+        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
+
+        run_list = []
+        for l in ti.run_lines:
+            if "|" not in l:
+                common.warn("Skipping unparsable RUN line: " + l)
+                continue
+
+            commands = [cmd.strip() for cmd in l.split("|")]
+            assert len(commands) >= 2
+            mc_cmd = " | ".join(commands[:-1])
+            filecheck_cmd = commands[-1]
+            mc_tool = mc_cmd.split(" ")[0]
+
+            triple_in_cmd = None
+            m = common.TRIPLE_ARG_RE.search(mc_cmd)
+            if m:
+                triple_in_cmd = m.groups()[0]
+
+            march_in_cmd = ti.args.default_march
+            m = common.MARCH_ARG_RE.search(mc_cmd)
+            if m:
+                march_in_cmd = m.groups()[0]
+
+            common.verify_filecheck_prefixes(filecheck_cmd)
+
+            mc_like_tools = mc_LIKE_TOOLS[:]
+            if ti.args.tool:
+                mc_like_tools.append(ti.args.tool)
+            if mc_tool not in mc_like_tools:
+                common.warn("Skipping non-mc RUN line: " + l)
+                continue
+
+            if not filecheck_cmd.startswith("FileCheck "):
+                common.warn("Skipping non-FileChecked RUN line: " + l)
+                continue
+
+            mc_cmd_args = mc_cmd[len(mc_tool) :].strip()
+            mc_cmd_args = mc_cmd_args.replace("< %s", "").replace("%s", "").strip()
+            check_prefixes = common.get_check_prefixes(filecheck_cmd)
+
+            run_list.append(
+                (
+                    check_prefixes,
+                    mc_tool,
+                    mc_cmd_args,
+                    triple_in_cmd,
+                    march_in_cmd,
+                )
+            )
+        
+
+        # find all test line from input
+        testlines = [l for l in ti.input_lines if isTestLine(l, mc_mode)]
+        run_list_size = len(run_list)
+        testnum = len(testlines)
+
+        raw_output = []
+        raw_prefixes = []
+        for (
+            prefixes,
+            mc_tool,
+            mc_args,
+            triple_in_cmd,
+            march_in_cmd,
+        ) in run_list:
+            common.debug("Extracted mc cmd:", mc_tool, mc_args)
+            common.debug("Extracted FileCheck prefixes:", str(prefixes))
+            common.debug("Extracted triple :", str(triple_in_cmd))
+            common.debug("Extracted march:", str(march_in_cmd))
+
+            triple = triple_in_cmd or triple_in_ir
+            if not triple:
+                triple = common.get_triple_from_march(march_in_cmd)
+
+            raw_output.append([])
+            for line in testlines:
+                # get output for each testline
+                out = invoke_tool(
+                    ti.args.mc_binary or mc_tool,
+                    mc_args,
+                    line,
+                    verbose=ti.args.verbose,
+                )
+                raw_output[-1].append(out)
+
+            common.debug("Collect raw tool lines:", str(len(raw_output[-1])))
+            
+            raw_prefixes.append(prefixes)
+
+        output_lines = []
+        generated_prefixes = []
+        used_prefixes = set()
+        prefix_set = set([prefix for p in run_list for prefix in p[0]])
+        common.debug("Rewriting FileCheck prefixes:", str(prefix_set))
+
+        for test_id in range(testnum):
+            input_line = testlines[test_id]
+
+            # a {prefix : output, [runid] } dict
+            # insert output to a prefix-key dict, and do a max sorting
+            # to select the most-used prefix which share the same output string
+            p_dict = {}
+            for run_id in range(run_list_size):
+                out = raw_output[run_id][test_id]
+
+                if hasErr(out):
+                    o = getErrString(out)
+                else:
+                    o = getOutputString(out)
+                
+                prefixes = raw_prefixes[run_id]
+
+                for p in prefixes:
+                    if p not in p_dict:
+                        p_dict[p] = o, [run_id]
+                    else:
+                        if p_dict[p] == (None, []):
+                            continue
+
+                        prev_o, run_ids = p_dict[p]
+                        if o == prev_o:
+                            run_ids.append(run_id)
+                            p_dict[p] = o, run_ids
+                        else:
+                            # conflict, discard
+                            p_dict[p] = None, []
+
+            p_dict_sorted = dict(sorted(p_dict.items(), key=lambda item: -len(item[1][1])))
+
+            # prefix is selected and generated with most shared output lines
+            # each run_id can only be used once
+            gen_prefix = ""
+            used_runid = set()
+            for prefix, tup in p_dict_sorted.items():
+                o, run_ids = tup
+
+                if len(run_ids) == 0:
+                    continue
+
+                skip = False
+                for i in run_ids:
+                    if i in used_runid:
+                        skip = True
+                    else:
+                        used_runid.add(i)
+                if not skip:
+                    used_prefixes.add(prefix)
+
+                    if hasErr(o):
+                        gen_prefix += getErrCheckLine(prefix, o, mc_mode)
+                    else:
+                        gen_prefix += getStdCheckLine(prefix, o, mc_mode)
+
+            generated_prefixes.append(gen_prefix.rstrip('\n'))
+
+        # write output
+        prefix_id = 0
+        for input_info in ti.iterlines(output_lines):
+            input_line = input_info.line
+            if isTestLine(input_line, mc_mode):
+                output_lines.append(generated_prefixes[prefix_id])
+                output_lines.append(input_line)
+                prefix_id += 1
+
+            elif should_add_line_to_output(input_line, prefix_set, mc_mode):
+                output_lines.append(input_line)
+
+            elif input_line in ti.run_lines or input_line == "":
+                output_lines.append(input_line)
+
+        if ti.args.gen_unused_prefix_body:
+            output_lines.extend(
+                ti.get_checks_for_unused_prefixes(run_list, used_prefixes)
+            )
+
+        common.debug("Writing %d lines to %s..." % (len(output_lines), ti.path))
+        with open(ti.path, "wb") as f:
+            f.writelines(["{}\n".format(l).encode("utf-8") for l in output_lines])
+
+
+if __name__ == "__main__":
+    main()

>From 7df9ecbdf07c8f0e421939dd9a82bc29f083d489 Mon Sep 17 00:00:00 2001
From: guochen2 <guochen2 at amd.com>
Date: Wed, 4 Sep 2024 10:31:03 -0400
Subject: [PATCH 2/3] formatter

---
 .../Inputs/amdgpu_multirun_dasm.txt           |  6 ++
 .../Inputs/amdgpu_multirun_dasm.txt.expected  | 10 ++++
 .../update_mc_test_checks/amdgpu-basic.test   |  2 +
 llvm/utils/update_mc_test_check.py            | 58 ++++++++++---------
 4 files changed, 50 insertions(+), 26 deletions(-)
 create mode 100644 llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt
 create mode 100644 llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected

diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt
new file mode 100644
index 00000000000000..0f680d0d5fab9f
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt
@@ -0,0 +1,6 @@
+# RUN: llvm-mc -triple=amdgcn -mcpu=tonga -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK,CHECKA %s
+# RUN: llvm-mc -triple=amdgcn -mcpu=gfx1100 -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK,CHECKB %s
+
+0x00,0x00,0x00,0x7e
+
+0x01,0x71,0x0a,0x7e
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected
new file mode 100644
index 00000000000000..57d1d3f0139820
--- /dev/null
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected
@@ -0,0 +1,10 @@
+; NOTE: Assertions have been autogenerated by utils/update_mc_test_check.py UTC_ARGS: --version 5
+# RUN: llvm-mc -triple=amdgcn -mcpu=tonga -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK,CHECKA %s
+# RUN: llvm-mc -triple=amdgcn -mcpu=gfx1100 -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK,CHECKB %s
+
+# CHECK: v_nop                                   ; encoding: [0x00,0x00,0x00,0x7e]
+0x00,0x00,0x00,0x7e
+
+# CHECKA: v_movrelsd_b32_e32 v5, v1               ; encoding: [0x01,0x71,0x0a,0x7e]
+# CHECKB: v_bfrev_b32_e32 v5, v1                  ; encoding: [0x01,0x71,0x0a,0x7e]
+0x01,0x71,0x0a,0x7e
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
index a74e0ae4e76f95..933ccf39bd559f 100644
--- a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
@@ -5,3 +5,5 @@
 # RUN: diff -u %S/Inputs/amdgpu_asm.s.expected %t.s
 # RUN: cp -f %S/Inputs/amdgpu_dasm.txt %t.txt && %update_mc_test_checks %t.txt
 # RUN: diff -u %S/Inputs/amdgpu_dasm.txt.expected %t.txt
+# RUN: cp -f %S/Inputs/amdgpu_multi_dasm.txt %t.s && %update_mc_test_checks %t.txt
+# RUN: diff -u %S/Inputs/amdgpu_multi_dasm.txt.expected %t.txt
diff --git a/llvm/utils/update_mc_test_check.py b/llvm/utils/update_mc_test_check.py
index ccaee25b3fa6ad..1b82cd8c102c8e 100755
--- a/llvm/utils/update_mc_test_check.py
+++ b/llvm/utils/update_mc_test_check.py
@@ -20,10 +20,7 @@
 ERROR_RE = re.compile(r"(warning|error): .*")
 ERROR_CHECK_RE = re.compile(r"# COM: .*")
 OUTPUT_SKIPPED_RE = re.compile(r"(.text)")
-COMMENT = {
-        "asm" : "//",
-        "dasm" : "#"
-        }
+COMMENT = {"asm": "//", "dasm": "#"}
 
 
 def invoke_tool(exe, cmd_args, testline, verbose=False):
@@ -32,7 +29,7 @@ def invoke_tool(exe, cmd_args, testline, verbose=False):
     else:
         args = cmd_args
 
-    cmd = "echo \"" + testline + "\" | " + exe + " " + args
+    cmd = 'echo "' + testline + '" | ' + exe + " " + args
     if verbose:
         print("Command: ", cmd)
     out = subprocess.check_output(cmd, shell=True)
@@ -44,15 +41,16 @@ def invoke_tool(exe, cmd_args, testline, verbose=False):
 # and treat all others as tests
 def isTestLine(input_line, mc_mode):
     # Skip comment lines
-    if input_line.strip(' \t\r').startswith(COMMENT[mc_mode]):
+    if input_line.strip(" \t\r").startswith(COMMENT[mc_mode]):
         return False
-    elif input_line.strip(' \t\r') == '':
+    elif input_line.strip(" \t\r") == "":
         return False
     # skip any CHECK lines.
     elif common.CHECK_RE.match(input_line):
         return False
     return True
 
+
 def hasErr(err):
     if err is None or len(err) == 0:
         return False
@@ -60,11 +58,12 @@ def hasErr(err):
         return True
     return False
 
+
 def getErrString(err):
     if err is None or len(err) == 0:
         return ""
 
-    lines = err.split('\n')
+    lines = err.split("\n")
     # take the first match
     for line in lines:
         s = ERROR_RE.search(line)
@@ -72,40 +71,46 @@ def getErrString(err):
             return s.group(0)
     return ""
 
+
 def getOutputString(out):
     if out is None or len(out) == 0:
         return ""
-    lines = out.split('\n')
+    lines = out.split("\n")
     output = ""
 
     for line in lines:
         if OUTPUT_SKIPPED_RE.search(line):
             continue
-        if line.strip('\t ') == '':
+        if line.strip("\t ") == "":
             continue
-        output += line.lstrip('\t ')
+        output += line.lstrip("\t ")
     return output
 
+
 def should_add_line_to_output(input_line, prefix_set, mc_mode):
     # special check line
-    if mc_mode == 'dasm' and ERROR_CHECK_RE.search(input_line):
+    if mc_mode == "dasm" and ERROR_CHECK_RE.search(input_line):
         return False
     else:
-        return common.should_add_line_to_output(input_line, prefix_set, comment_marker=COMMENT[mc_mode])
+        return common.should_add_line_to_output(
+            input_line, prefix_set, comment_marker=COMMENT[mc_mode]
+        )
 
 
 def getStdCheckLine(prefix, output, mc_mode):
-    lines = output.split('\n')
+    lines = output.split("\n")
     output = ""
     for line in lines:
-        output += COMMENT[mc_mode] + ' ' + prefix + ": " + line + '\n'
+        output += COMMENT[mc_mode] + " " + prefix + ": " + line + "\n"
     return output
 
+
 def getErrCheckLine(prefix, output, mc_mode):
-    if mc_mode == 'asm':
-        return COMMENT[mc_mode] + ' ' + prefix + ": " + output + '\n'
-    elif mc_mode == 'dasm':
-        return COMMENT[mc_mode] + ' COM: ' + prefix + ": " + output + '\n'
+    if mc_mode == "asm":
+        return COMMENT[mc_mode] + " " + prefix + ": " + output + "\n"
+    elif mc_mode == "dasm":
+        return COMMENT[mc_mode] + " COM: " + prefix + ": " + output + "\n"
+
 
 def main():
     parser = argparse.ArgumentParser(description=__doc__)
@@ -132,9 +137,9 @@ def main():
     for ti in common.itertests(
         initial_args.tests, parser, script_name="utils/" + script_name
     ):
-        if ti.path.endswith('.s'):
+        if ti.path.endswith(".s"):
             mc_mode = "asm"
-        elif ti.path.endswith('.txt'):
+        elif ti.path.endswith(".txt"):
             mc_mode = "dasm"
         else:
             common.warn("Expected .s and .txt, Skipping file : ", ti.path)
@@ -195,7 +200,6 @@ def main():
                     march_in_cmd,
                 )
             )
-        
 
         # find all test line from input
         testlines = [l for l in ti.input_lines if isTestLine(l, mc_mode)]
@@ -232,7 +236,7 @@ def main():
                 raw_output[-1].append(out)
 
             common.debug("Collect raw tool lines:", str(len(raw_output[-1])))
-            
+
             raw_prefixes.append(prefixes)
 
         output_lines = []
@@ -255,7 +259,7 @@ def main():
                     o = getErrString(out)
                 else:
                     o = getOutputString(out)
-                
+
                 prefixes = raw_prefixes[run_id]
 
                 for p in prefixes:
@@ -273,7 +277,9 @@ def main():
                             # conflict, discard
                             p_dict[p] = None, []
 
-            p_dict_sorted = dict(sorted(p_dict.items(), key=lambda item: -len(item[1][1])))
+            p_dict_sorted = dict(
+                sorted(p_dict.items(), key=lambda item: -len(item[1][1]))
+            )
 
             # prefix is selected and generated with most shared output lines
             # each run_id can only be used once
@@ -299,7 +305,7 @@ def main():
                     else:
                         gen_prefix += getStdCheckLine(prefix, o, mc_mode)
 
-            generated_prefixes.append(gen_prefix.rstrip('\n'))
+            generated_prefixes.append(gen_prefix.rstrip("\n"))
 
         # write output
         prefix_id = 0

>From 425b4c2f6bcd0d0f01061c3a8cf6f7247eef586a Mon Sep 17 00:00:00 2001
From: guochen2 <guochen2 at amd.com>
Date: Wed, 4 Sep 2024 15:45:00 -0400
Subject: [PATCH 3/3] update name of scripts, correct test files

---
 llvm/test/tools/UpdateTestChecks/lit.local.cfg                | 4 ++++
 .../update_mc_test_checks/Inputs/amdgpu_asm.s.expected        | 2 +-
 .../update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected     | 2 +-
 .../Inputs/amdgpu_multirun_dasm.txt.expected                  | 2 +-
 .../UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test  | 4 ++--
 .../{update_mc_test_check.py => update_mc_test_checks.py}     | 4 ++--
 6 files changed, 11 insertions(+), 7 deletions(-)
 rename llvm/utils/{update_mc_test_check.py => update_mc_test_checks.py} (99%)

diff --git a/llvm/test/tools/UpdateTestChecks/lit.local.cfg b/llvm/test/tools/UpdateTestChecks/lit.local.cfg
index 2e695490b005e2..a954eb7ba1740c 100644
--- a/llvm/test/tools/UpdateTestChecks/lit.local.cfg
+++ b/llvm/test/tools/UpdateTestChecks/lit.local.cfg
@@ -52,3 +52,7 @@ if os.path.isfile(llvm_mca_path):
 split_file_path = os.path.join(config.llvm_tools_dir, "split-file")
 if os.path.isfile(split_file_path):
     add_update_script_substition("%update_test_body")
+
+llvm_mc_path = os.path.join(config.llvm_tools_dir, "llvm-mc")
+if os.path.isfile(llvm_mc_path):
+    add_update_script_substition("%update_mc_test_checks")
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected
index d29e1fc121e852..1d3b4c98cb8ccd 100644
--- a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_asm.s.expected
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_mc_test_check.py UTC_ARGS: --version 5
+; NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py
 // RUN: llvm-mc -triple=amdgcn -show-encoding %s 2>&1 | FileCheck --check-prefixes=CHECK %s
 
 // CHECK: v_bfrev_b32_e32 v5, v1                  ; encoding: [0x01,0x71,0x0a,0x7e]
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected
index 896d5beb12d575..897b042a3c13ee 100644
--- a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_dasm.txt.expected
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_mc_test_check.py UTC_ARGS: --version 5
+; NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py
 # RUN: llvm-mc -triple=amdgcn -mcpu=gfx1100 -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK %s
 
 # CHECK: v_nop                                   ; encoding: [0x00,0x00,0x00,0x7e]
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected
index 57d1d3f0139820..08048979ad3114 100644
--- a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/Inputs/amdgpu_multirun_dasm.txt.expected
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_mc_test_check.py UTC_ARGS: --version 5
+; NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py
 # RUN: llvm-mc -triple=amdgcn -mcpu=tonga -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK,CHECKA %s
 # RUN: llvm-mc -triple=amdgcn -mcpu=gfx1100 -disassemble -show-encoding %s 2>&1 | FileCheck -check-prefixes=CHECK,CHECKB %s
 
diff --git a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
index 933ccf39bd559f..e7676e9da00b80 100644
--- a/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
+++ b/llvm/test/tools/UpdateTestChecks/update_mc_test_checks/amdgpu-basic.test
@@ -5,5 +5,5 @@
 # RUN: diff -u %S/Inputs/amdgpu_asm.s.expected %t.s
 # RUN: cp -f %S/Inputs/amdgpu_dasm.txt %t.txt && %update_mc_test_checks %t.txt
 # RUN: diff -u %S/Inputs/amdgpu_dasm.txt.expected %t.txt
-# RUN: cp -f %S/Inputs/amdgpu_multi_dasm.txt %t.s && %update_mc_test_checks %t.txt
-# RUN: diff -u %S/Inputs/amdgpu_multi_dasm.txt.expected %t.txt
+# RUN: cp -f %S/Inputs/amdgpu_multirun_dasm.txt %t.txt && %update_mc_test_checks %t.txt
+# RUN: diff -u %S/Inputs/amdgpu_multirun_dasm.txt.expected %t.txt
diff --git a/llvm/utils/update_mc_test_check.py b/llvm/utils/update_mc_test_checks.py
similarity index 99%
rename from llvm/utils/update_mc_test_check.py
rename to llvm/utils/update_mc_test_checks.py
index 1b82cd8c102c8e..625446cd89bf2d 100755
--- a/llvm/utils/update_mc_test_check.py
+++ b/llvm/utils/update_mc_test_checks.py
@@ -115,7 +115,7 @@ def getErrCheckLine(prefix, output, mc_mode):
 def main():
     parser = argparse.ArgumentParser(description=__doc__)
     parser.add_argument(
-        "--mc-binary",
+        "--llvm-mc-binary",
         default=None,
         help='The "mc" binary to use to generate the test case',
     )
@@ -228,7 +228,7 @@ def main():
             for line in testlines:
                 # get output for each testline
                 out = invoke_tool(
-                    ti.args.mc_binary or mc_tool,
+                    ti.args.llvm_mc_binary or mc_tool,
                     mc_args,
                     line,
                     verbose=ti.args.verbose,



More information about the llvm-commits mailing list