Skip to content

Commit 82cf54f

Browse files
authored
[UTC] CHECK-EMPTY instead of skipping blank lines (llvm#165718)
Previously, any blank lines in IR were ignored by UTC, leading to more fragile `CHECK`s being generated. This change lets UTC, 1) emit `CHECK-EMPTY` to check blank lines, and 2) generate more `CHECK-NEXT`s, landing the discussion llvm#165419 (comment). Moreover, this change also aligns the behavior of IR check-gen to ASM check-gen, which has been emitting `CHECK-EMPTY` since llvm@a8a89c7.
1 parent c07440b commit 82cf54f

File tree

5 files changed

+133
-15
lines changed

5 files changed

+133
-15
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; RUN: opt < %s -S | FileCheck %s
2+
3+
; Test whether UTC checks empty lines instead of skipping them.
4+
define i32 @test(i32 %x) {
5+
entry:
6+
br label %block1
7+
8+
block1:
9+
%cmp = icmp eq i32 %x, 0
10+
br i1 %cmp, label %block2, label %exit1
11+
12+
block2:
13+
br i1 %cmp, label %block3, label %exit2
14+
15+
block3:
16+
br i1 %cmp, label %exit3, label %exit4
17+
18+
exit1:
19+
ret i32 0
20+
21+
exit2:
22+
ret i32 %x
23+
24+
exit3:
25+
ret i32 %x
26+
27+
exit4:
28+
ret i32 %x
29+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 7
2+
; RUN: opt < %s -S | FileCheck %s
3+
4+
; Test whether UTC checks empty lines instead of skipping them.
5+
define i32 @test(i32 %x) {
6+
; CHECK-LABEL: define i32 @test(
7+
; CHECK-SAME: i32 [[X:%.*]]) {
8+
; CHECK-NEXT: [[ENTRY:.*:]]
9+
; CHECK-NEXT: br label %[[BLOCK1:.*]]
10+
; CHECK-EMPTY:
11+
; CHECK-NEXT: [[BLOCK1]]:
12+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X]], 0
13+
; CHECK-NEXT: br i1 [[CMP]], label %[[BLOCK2:.*]], label %[[EXIT1:.*]]
14+
; CHECK-EMPTY:
15+
; CHECK-NEXT: [[BLOCK2]]:
16+
; CHECK-NEXT: br i1 [[CMP]], label %[[BLOCK3:.*]], label %[[EXIT2:.*]]
17+
; CHECK-EMPTY:
18+
; CHECK-NEXT: [[BLOCK3]]:
19+
; CHECK-NEXT: br i1 [[CMP]], label %[[EXIT3:.*]], label %[[EXIT4:.*]]
20+
; CHECK-EMPTY:
21+
; CHECK-NEXT: [[EXIT1]]:
22+
; CHECK-NEXT: ret i32 0
23+
; CHECK-EMPTY:
24+
; CHECK-NEXT: [[EXIT2]]:
25+
; CHECK-NEXT: ret i32 [[X]]
26+
; CHECK-EMPTY:
27+
; CHECK-NEXT: [[EXIT3]]:
28+
; CHECK-NEXT: ret i32 [[X]]
29+
; CHECK-EMPTY:
30+
; CHECK-NEXT: [[EXIT4]]:
31+
; CHECK-NEXT: ret i32 [[X]]
32+
;
33+
entry:
34+
br label %block1
35+
36+
block1:
37+
%cmp = icmp eq i32 %x, 0
38+
br i1 %cmp, label %block2, label %exit1
39+
40+
block2:
41+
br i1 %cmp, label %block3, label %exit2
42+
43+
block3:
44+
br i1 %cmp, label %exit3, label %exit4
45+
46+
exit1:
47+
ret i32 0
48+
49+
exit2:
50+
ret i32 %x
51+
52+
exit3:
53+
ret i32 %x
54+
55+
exit4:
56+
ret i32 %x
57+
}

llvm/test/tools/UpdateTestChecks/update_test_checks/Inputs/switch_case.ll.expected

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ define i8 @testi8(i8 %x) {
1212
; CHECK-NEXT: i8 2, label %[[CASE3:.*]]
1313
; CHECK-NEXT: i8 3, label %[[CASE3]]
1414
; CHECK-NEXT: ]
15-
; CHECK: [[DEFAULT]]:
15+
; CHECK-EMPTY:
16+
; CHECK-NEXT: [[DEFAULT]]:
1617
; CHECK-NEXT: ret i8 0
17-
; CHECK: [[CASE1]]:
18+
; CHECK-EMPTY:
19+
; CHECK-NEXT: [[CASE1]]:
1820
; CHECK-NEXT: ret i8 1
19-
; CHECK: [[CASE2]]:
21+
; CHECK-EMPTY:
22+
; CHECK-NEXT: [[CASE2]]:
2023
; CHECK-NEXT: ret i8 2
21-
; CHECK: [[CASE3]]:
24+
; CHECK-EMPTY:
25+
; CHECK-NEXT: [[CASE3]]:
2226
; CHECK-NEXT: ret i8 3
2327
;
2428
switch i8 %x, label %default [
@@ -46,13 +50,17 @@ define i32 @testi32(i32 %x) {
4650
; CHECK-NEXT: i32 2, label %[[CASE3:.*]]
4751
; CHECK-NEXT: i32 3, label %[[CASE3]]
4852
; CHECK-NEXT: ]
49-
; CHECK: [[DEFAULT]]:
53+
; CHECK-EMPTY:
54+
; CHECK-NEXT: [[DEFAULT]]:
5055
; CHECK-NEXT: ret i32 0
51-
; CHECK: [[CASE1]]:
56+
; CHECK-EMPTY:
57+
; CHECK-NEXT: [[CASE1]]:
5258
; CHECK-NEXT: ret i32 1
53-
; CHECK: [[CASE2]]:
59+
; CHECK-EMPTY:
60+
; CHECK-NEXT: [[CASE2]]:
5461
; CHECK-NEXT: ret i32 2
55-
; CHECK: [[CASE3]]:
62+
; CHECK-EMPTY:
63+
; CHECK-NEXT: [[CASE3]]:
5664
; CHECK-NEXT: ret i32 3
5765
;
5866
switch i32 %x, label %default [
@@ -80,13 +88,17 @@ define i128 @testi128(i128 %x) {
8088
; CHECK-NEXT: i128 2, label %[[CASE3:.*]]
8189
; CHECK-NEXT: i128 3, label %[[CASE3]]
8290
; CHECK-NEXT: ]
83-
; CHECK: [[DEFAULT]]:
91+
; CHECK-EMPTY:
92+
; CHECK-NEXT: [[DEFAULT]]:
8493
; CHECK-NEXT: ret i128 0
85-
; CHECK: [[CASE1]]:
94+
; CHECK-EMPTY:
95+
; CHECK-NEXT: [[CASE1]]:
8696
; CHECK-NEXT: ret i128 1
87-
; CHECK: [[CASE2]]:
97+
; CHECK-EMPTY:
98+
; CHECK-NEXT: [[CASE2]]:
8899
; CHECK-NEXT: ret i128 2
89-
; CHECK: [[CASE3]]:
100+
; CHECK-EMPTY:
101+
; CHECK-NEXT: [[CASE3]]:
90102
; CHECK-NEXT: ret i128 3
91103
;
92104
switch i128 %x, label %default [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## test whether the UTC generates CHECK-EMPTY for blank lines
2+
# RUN: cp -f %S/Inputs/check_empty.ll %t.ll && %update_test_checks %t.ll --version 7
3+
# RUN: diff -u %t.ll %S/Inputs/check_empty.ll.expected

llvm/utils/UpdateTestChecks/common.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'none' and 'all'. 'smart' is the default.
3030
5: Basic block labels are matched by FileCheck expressions
3131
6: The semantics of TBAA checks has been incorporated in the check lines.
32-
7: Indent switch-cases correctly.
32+
7: Indent switch-cases correctly; CHECK-EMPTY instead of skipping blank lines.
3333
"""
3434
DEFAULT_VERSION = 6
3535

@@ -2280,6 +2280,14 @@ def add_checks(
22802280
# For IR output, change all defs to FileCheck variables, so we're immune
22812281
# to variable naming fashions.
22822282
else:
2283+
if ginfo.get_version() >= 7:
2284+
# Record the indices of blank lines in the function body preemptively.
2285+
blank_line_indices = {
2286+
i for i, line in enumerate(func_body) if line.strip() == ""
2287+
}
2288+
else:
2289+
blank_line_indices = set()
2290+
22832291
func_body = generalize_check_lines(
22842292
func_body,
22852293
ginfo,
@@ -2305,9 +2313,18 @@ def add_checks(
23052313

23062314
is_blank_line = False
23072315

2308-
for func_line in func_body:
2316+
for idx, func_line in enumerate(func_body):
23092317
if func_line.strip() == "":
2310-
is_blank_line = True
2318+
# We should distinguish if the line is a 'fake' blank line generated by
2319+
# generalize_check_lines removing comments.
2320+
# Fortunately, generalize_check_lines does not change the index of each line,
2321+
# we can record the indices of blank lines preemptively.
2322+
if idx in blank_line_indices:
2323+
output_lines.append(
2324+
"{} {}-EMPTY:".format(comment_marker, checkprefix)
2325+
)
2326+
else:
2327+
is_blank_line = True
23112328
continue
23122329
if not check_inst_comments:
23132330
# Do not waste time checking IR comments unless necessary.

0 commit comments

Comments
 (0)