Skip to content

Commit 09eaa33

Browse files
hnrklssnmylai-mtk
authored andcommitted
[UTC] Don't leave dangling CHECK-SAME when removing CHECK lines (llvm#82569)
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 llvm#78517
1 parent ffa970b commit 09eaa33

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: opt -S < %s | FileCheck %s
2+
3+
define i32 @foo() {
4+
; CHECK-LABEL: @foo(
5+
; CHECK-NEXT: [[RESULT:%.*]] = call i32 @bar(i32 0, i32 1)
6+
; CHECK-NEXT: ret i32 [[RESULT]]
7+
;
8+
%result = call i32 @bar(i32 0, i32 1)
9+
ret i32 %result
10+
}
11+
12+
declare i32 @bar(i32, i32)
13+
; CHECK-LABEL: @bar(
14+
; CHECK-SAME: i32
15+
; CHECK-SAME: i32
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S < %s | FileCheck %s
3+
4+
define i32 @foo() {
5+
; CHECK-LABEL: @foo(
6+
; CHECK-NEXT: [[RESULT:%.*]] = call i32 @bar(i32 0, i32 1)
7+
; CHECK-NEXT: ret i32 [[RESULT]]
8+
;
9+
%result = call i32 @bar(i32 0, i32 1)
10+
ret i32 %result
11+
}
12+
13+
declare i32 @bar(i32, i32)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Basic test checking global checks split over multiple lines are removed together
2+
# RUN: cp -f %S/Inputs/global_remove_same.ll %t.ll && %update_test_checks %t.ll
3+
# RUN: diff -u %t.ll %S/Inputs/global_remove_same.ll.expected
4+

llvm/utils/UpdateTestChecks/common.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,12 @@ def itertests(
388388

389389

390390
def should_add_line_to_output(
391-
input_line, prefix_set, skip_global_checks=False, comment_marker=";"
391+
input_line,
392+
prefix_set,
393+
*,
394+
skip_global_checks=False,
395+
skip_same_checks=False,
396+
comment_marker=";",
392397
):
393398
# Skip any blank comment lines in the IR.
394399
if not skip_global_checks and input_line.strip() == comment_marker:
@@ -402,9 +407,14 @@ def should_add_line_to_output(
402407
# And skip any CHECK lines. We're building our own.
403408
m = CHECK_RE.match(input_line)
404409
if m and m.group(1) in prefix_set:
410+
if skip_same_checks and CHECK_SAME_RE.match(input_line):
411+
# The previous CHECK line was removed, so don't leave this dangling
412+
return False
405413
if skip_global_checks:
414+
# Skip checks only if they are of global value definitions
406415
global_ir_value_re = re.compile(r"(\[\[|@)", flags=(re.M))
407-
return not global_ir_value_re.search(input_line)
416+
is_global = global_ir_value_re.search(input_line)
417+
return not is_global
408418
return False
409419

410420
return True
@@ -483,6 +493,7 @@ def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False):
483493
CHECK_RE = re.compile(
484494
r"^\s*(?://|[;#])\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL|-SAME|-EMPTY)?:"
485495
)
496+
CHECK_SAME_RE = re.compile(r"^\s*(?://|[;#])\s*([^:]+?)(?:-SAME)?:")
486497

487498
UTC_ARGS_KEY = "UTC_ARGS:"
488499
UTC_ARGS_CMD = re.compile(r".*" + UTC_ARGS_KEY + r"\s*(?P<cmd>.*)\s*$")

llvm/utils/update_test_checks.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def main():
235235
)
236236
else:
237237
# "Normal" mode.
238+
dropped_previous_line = False
238239
for input_line_info in ti.iterlines(output_lines):
239240
input_line = input_line_info.line
240241
args = input_line_info.args
@@ -282,17 +283,24 @@ def main():
282283
has_checked_pre_function_globals = True
283284

284285
if common.should_add_line_to_output(
285-
input_line, prefix_set, not is_in_function
286+
input_line,
287+
prefix_set,
288+
skip_global_checks=not is_in_function,
289+
skip_same_checks=dropped_previous_line,
286290
):
287291
# This input line of the function body will go as-is into the output.
288292
# Except make leading whitespace uniform: 2 spaces.
289293
input_line = common.SCRUB_LEADING_WHITESPACE_RE.sub(
290294
r" ", input_line
291295
)
292296
output_lines.append(input_line)
297+
dropped_previous_line = False
293298
if input_line.strip() == "}":
294299
is_in_function = False
295300
continue
301+
else:
302+
# If we are removing a check line, and the next line is CHECK-SAME, it MUST also be removed
303+
dropped_previous_line = True
296304

297305
if is_in_function:
298306
continue

0 commit comments

Comments
 (0)