From 2adaa3b564f87b759f3b2baade92e8463f1d6628 Mon Sep 17 00:00:00 2001 From: "Henrik G. Olsson" Date: Wed, 21 Feb 2024 18:10:25 -0800 Subject: [PATCH 1/3] [UTC] Don't leave dangling CHECK-SAME when removing CHECK lines When removing only lines that are global value CHECK lines, a related CHECK-SAME line could be left dangling without a previous line to belong to. Resolves #78517 --- .../Inputs/global_remove_same.ll | 15 ++++++++++++ .../Inputs/global_remove_same.ll.expected | 13 +++++++++++ .../global_remove_same.test | 4 ++++ llvm/utils/UpdateTestChecks/common.py | 23 ++++++++++++++----- llvm/utils/update_test_checks.py | 15 +++++++++--- 5 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll create mode 100644 llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll.expected create mode 100644 llvm/test/tools/UpdateTestChecks/update_test_checks/global_remove_same.test diff --git a/llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll b/llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll new file mode 100644 index 0000000000000..d3d13ae2622e6 --- /dev/null +++ b/llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll @@ -0,0 +1,15 @@ +; RUN: opt -S < %s | FileCheck %s + +define i32 @foo() { +; CHECK-LABEL: @foo( +; CHECK-NEXT: [[RESULT:%.*]] = call i32 @bar(i32 0, i32 1) +; CHECK-NEXT: ret i32 [[RESULT]] +; + %result = call i32 @bar(i32 0, i32 1) + ret i32 %result +} + +declare i32 @bar(i32, i32) +; CHECK-LABEL: @bar( +; CHECK-SAME: i32 +; CHECK-SAME: i32 diff --git a/llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll.expected b/llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll.expected new file mode 100644 index 0000000000000..e76efaedd172c --- /dev/null +++ b/llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/global_remove_same.ll.expected @@ -0,0 +1,13 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S < %s | FileCheck %s + +define i32 @foo() { +; CHECK-LABEL: @foo( +; CHECK-NEXT: [[RESULT:%.*]] = call i32 @bar(i32 0, i32 1) +; CHECK-NEXT: ret i32 [[RESULT]] +; + %result = call i32 @bar(i32 0, i32 1) + ret i32 %result +} + +declare i32 @bar(i32, i32) diff --git a/llvm/test/tools/UpdateTestChecks/update_test_checks/global_remove_same.test b/llvm/test/tools/UpdateTestChecks/update_test_checks/global_remove_same.test new file mode 100644 index 0000000000000..5d447babddea4 --- /dev/null +++ b/llvm/test/tools/UpdateTestChecks/update_test_checks/global_remove_same.test @@ -0,0 +1,4 @@ +## Basic test checking global checks split over multiple lines are removed together +# RUN: cp -f %S/Inputs/global_remove_same.ll %t.ll && %update_test_checks %t.ll +# RUN: diff -u %t.ll %S/Inputs/global_remove_same.ll.expected + diff --git a/llvm/utils/UpdateTestChecks/common.py b/llvm/utils/UpdateTestChecks/common.py index 4a02a92f824e6..831dd704160cd 100644 --- a/llvm/utils/UpdateTestChecks/common.py +++ b/llvm/utils/UpdateTestChecks/common.py @@ -387,27 +387,37 @@ def itertests( ) +# Returns a tuple of two bools, where the first value indicates whether the line should be added to the output, and the second indicates whether the line is a CHECK line def should_add_line_to_output( - input_line, prefix_set, skip_global_checks=False, comment_marker=";" + input_line, + prefix_set, + skip_global_checks=False, + skip_same_checks=False, + comment_marker=";", ): # Skip any blank comment lines in the IR. if not skip_global_checks and input_line.strip() == comment_marker: - return False + return False, False # Skip a special double comment line we use as a separator. if input_line.strip() == comment_marker + SEPARATOR: - return False + return False, False # Skip any blank lines in the IR. # if input_line.strip() == '': # return False # And skip any CHECK lines. We're building our own. m = CHECK_RE.match(input_line) if m and m.group(1) in prefix_set: + if skip_same_checks and CHECK_SAME_RE.match(input_line): + # The previous CHECK line was removed, so don't leave this dangling + return False, True if skip_global_checks: + # Skip checks only if they are of global value definitions global_ir_value_re = re.compile(r"(\[\[|@)", flags=(re.M)) - return not global_ir_value_re.search(input_line) - return False + is_global = global_ir_value_re.search(input_line) + return not is_global, True + return False, True - return True + return True, False # Perform lit-like substitutions @@ -483,6 +493,7 @@ def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False): CHECK_RE = re.compile( r"^\s*(?://|[;#])\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL|-SAME|-EMPTY)?:" ) +CHECK_SAME_RE = re.compile(r"^\s*(?://|[;#])\s*([^:]+?)(?:-SAME)?:") UTC_ARGS_KEY = "UTC_ARGS:" UTC_ARGS_CMD = re.compile(r".*" + UTC_ARGS_KEY + r"\s*(?P.*)\s*$") diff --git a/llvm/utils/update_test_checks.py b/llvm/utils/update_test_checks.py index 06c247c8010a9..451f76389a356 100755 --- a/llvm/utils/update_test_checks.py +++ b/llvm/utils/update_test_checks.py @@ -235,6 +235,7 @@ def main(): ) else: # "Normal" mode. + skip_same_check_line = False for input_line_info in ti.iterlines(output_lines): input_line = input_line_info.line args = input_line_info.args @@ -281,18 +282,26 @@ def main(): ) has_checked_pre_function_globals = True - if common.should_add_line_to_output( - input_line, prefix_set, not is_in_function - ): + should_add, is_check_line = common.should_add_line_to_output( + input_line, + prefix_set, + not is_in_function, + skip_same_check_line, + ) + if should_add: # This input line of the function body will go as-is into the output. # Except make leading whitespace uniform: 2 spaces. input_line = common.SCRUB_LEADING_WHITESPACE_RE.sub( r" ", input_line ) output_lines.append(input_line) + skip_same_check_line = False if input_line.strip() == "}": is_in_function = False continue + else: + # If we are removing a check line, and the next line is CHECK-SAME, it MUST also be removed + skip_same_check_line = is_check_line if is_in_function: continue From ee8e6a22f840f4267a138cd3ce4c39c92c9435e9 Mon Sep 17 00:00:00 2001 From: "Henrik G. Olsson" Date: Thu, 22 Feb 2024 13:48:18 -0800 Subject: [PATCH 2/3] [UTC] Fix broken tests and simplify should_add_line_to_output --- llvm/utils/UpdateTestChecks/common.py | 13 ++++++------- llvm/utils/update_test_checks.py | 7 +++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/llvm/utils/UpdateTestChecks/common.py b/llvm/utils/UpdateTestChecks/common.py index 831dd704160cd..5b4ac7159631c 100644 --- a/llvm/utils/UpdateTestChecks/common.py +++ b/llvm/utils/UpdateTestChecks/common.py @@ -387,7 +387,6 @@ def itertests( ) -# Returns a tuple of two bools, where the first value indicates whether the line should be added to the output, and the second indicates whether the line is a CHECK line def should_add_line_to_output( input_line, prefix_set, @@ -397,10 +396,10 @@ def should_add_line_to_output( ): # Skip any blank comment lines in the IR. if not skip_global_checks and input_line.strip() == comment_marker: - return False, False + return False # Skip a special double comment line we use as a separator. if input_line.strip() == comment_marker + SEPARATOR: - return False, False + return False # Skip any blank lines in the IR. # if input_line.strip() == '': # return False @@ -409,15 +408,15 @@ def should_add_line_to_output( if m and m.group(1) in prefix_set: if skip_same_checks and CHECK_SAME_RE.match(input_line): # The previous CHECK line was removed, so don't leave this dangling - return False, True + return False if skip_global_checks: # Skip checks only if they are of global value definitions global_ir_value_re = re.compile(r"(\[\[|@)", flags=(re.M)) is_global = global_ir_value_re.search(input_line) - return not is_global, True - return False, True + return not is_global + return False - return True, False + return True # Perform lit-like substitutions diff --git a/llvm/utils/update_test_checks.py b/llvm/utils/update_test_checks.py index 451f76389a356..ca479f99476f9 100755 --- a/llvm/utils/update_test_checks.py +++ b/llvm/utils/update_test_checks.py @@ -282,13 +282,12 @@ def main(): ) has_checked_pre_function_globals = True - should_add, is_check_line = common.should_add_line_to_output( + if common.should_add_line_to_output( input_line, prefix_set, not is_in_function, skip_same_check_line, - ) - if should_add: + ): # This input line of the function body will go as-is into the output. # Except make leading whitespace uniform: 2 spaces. input_line = common.SCRUB_LEADING_WHITESPACE_RE.sub( @@ -301,7 +300,7 @@ def main(): continue else: # If we are removing a check line, and the next line is CHECK-SAME, it MUST also be removed - skip_same_check_line = is_check_line + skip_same_check_line = True if is_in_function: continue From 86845e980db8da47b25f28ba13db7573e025de15 Mon Sep 17 00:00:00 2001 From: "Henrik G. Olsson" Date: Fri, 23 Feb 2024 10:47:14 -0800 Subject: [PATCH 3/3] [UTC] Use named parameters for should_add_line_to_output (NFC) --- llvm/utils/UpdateTestChecks/common.py | 1 + llvm/utils/update_test_checks.py | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/llvm/utils/UpdateTestChecks/common.py b/llvm/utils/UpdateTestChecks/common.py index 5b4ac7159631c..53777523ec2a5 100644 --- a/llvm/utils/UpdateTestChecks/common.py +++ b/llvm/utils/UpdateTestChecks/common.py @@ -390,6 +390,7 @@ def itertests( def should_add_line_to_output( input_line, prefix_set, + *, skip_global_checks=False, skip_same_checks=False, comment_marker=";", diff --git a/llvm/utils/update_test_checks.py b/llvm/utils/update_test_checks.py index ca479f99476f9..b5077d7935137 100755 --- a/llvm/utils/update_test_checks.py +++ b/llvm/utils/update_test_checks.py @@ -235,7 +235,7 @@ def main(): ) else: # "Normal" mode. - skip_same_check_line = False + dropped_previous_line = False for input_line_info in ti.iterlines(output_lines): input_line = input_line_info.line args = input_line_info.args @@ -285,8 +285,8 @@ def main(): if common.should_add_line_to_output( input_line, prefix_set, - not is_in_function, - skip_same_check_line, + skip_global_checks=not is_in_function, + skip_same_checks=dropped_previous_line, ): # This input line of the function body will go as-is into the output. # Except make leading whitespace uniform: 2 spaces. @@ -294,13 +294,13 @@ def main(): r" ", input_line ) output_lines.append(input_line) - skip_same_check_line = False + dropped_previous_line = False if input_line.strip() == "}": is_in_function = False continue else: # If we are removing a check line, and the next line is CHECK-SAME, it MUST also be removed - skip_same_check_line = True + dropped_previous_line = True if is_in_function: continue