Skip to content

Commit 916c4da

Browse files
authored
Handle annotations in update_lit_checks.py (#8320)
Update the auto-update script for lit/filecheck output to handle annotations appearing on top-level module items such as functions. When parsing test ouput to find named items, search backward to find the start of the annotations attached to each item. When printing the updated file, collect annotations to be printed after the check lines for an item have been printed.
1 parent fa3352b commit 916c4da

File tree

5 files changed

+101
-66
lines changed

5 files changed

+101
-66
lines changed

scripts/update_lit_checks.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
FUZZ_EXEC_FUNC = re.compile(r'^\[fuzz-exec\] calling (?P<name>\S*)$')
5959

60+
ANNOTATION_RE = re.compile(r'^\s*\(\@.*')
61+
6062

6163
def indentKindName(match):
6264
# Return the indent, kind, and name from an ITEM_RE match
@@ -127,6 +129,31 @@ def find_end(module, start):
127129
return end
128130

129131

132+
def find_annotations(module, start):
133+
# Search backward to find the start of the line containing the first of the
134+
# annotations preceding `start`, if any.
135+
depth = 0
136+
annotation = start
137+
for i in range(start - 1, -1, -1):
138+
if module[i] == ')':
139+
depth += 1
140+
elif module[i] == '(':
141+
depth -= 1
142+
if depth == 0:
143+
if module[i + 1] == '@':
144+
# Found the start of a new annotation.
145+
annotation = i
146+
else:
147+
# Found something that isn't an annotation.
148+
break
149+
# Look for the start of the line containin the first annoation.
150+
for i in range(annotation - 1, -1, -1):
151+
if module[i] == '\n':
152+
return i + 1
153+
# The annotation should not have been on the first line of the module
154+
assert False
155+
156+
130157
def split_modules(text):
131158
# Return a list of strings; one for each module
132159
module_starts = [match.start() for match in MODULE_RE.finditer(text)]
@@ -150,8 +177,9 @@ def parse_output_modules(text):
150177
items = []
151178
for match in ITEM_RE.finditer(module):
152179
_, kind, name = indentKindName(match)
180+
start = find_annotations(module, match.start())
153181
end = find_end(module, match.end(1))
154-
lines = module[match.start():end].split('\n')
182+
lines = module[start:end].split('\n')
155183
items.append(((kind, name), lines))
156184
modules.append(items)
157185
return modules
@@ -277,7 +305,12 @@ def pad(line):
277305
# Remove extra newlines at the end of modules
278306
input_modules = [m[:-1] for m in input_modules[:-1]] + [input_modules[-1]]
279307

308+
# Collect annotation lines as we see them so we can put them after any
309+
# checks we generate.
310+
annotation_lines = []
311+
280312
for module_idx in range(len(input_modules)):
313+
assert len(annotation_lines) == 0
281314
output = command_output[module_idx] \
282315
if module_idx < len(command_output) else {}
283316

@@ -286,8 +319,16 @@ def pad(line):
286319
if check_line_re.match(line):
287320
continue
288321

322+
# Collect annotations to emit later once we know what they should
323+
# attach to.
324+
if ANNOTATION_RE.match(line):
325+
annotation_lines.append(line)
326+
continue
327+
289328
match = ITEM_RE.match(line)
290329
if not match:
330+
output_lines.extend(annotation_lines)
331+
annotation_lines = []
291332
output_lines.append(line)
292333
continue
293334

@@ -313,6 +354,8 @@ def pad(line):
313354
emit_checks(indent, prefix, lines)
314355
if name and (kind, name) == kind_name:
315356
break
357+
output_lines.extend(annotation_lines)
358+
annotation_lines = []
316359
output_lines.append(line)
317360

318361
# Output any remaining checks for each prefix

test/lit/inline-hints-func.wast

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
12
;; RUN: wasm-opt -all %s -S -o - | filecheck %s
23
;; RUN: wasm-opt -all --roundtrip %s -S -o - | filecheck %s
34

45
(module
6+
;; CHECK: (@metadata.code.inline "\12")
7+
;; CHECK-NEXT: (func $func-annotation (type $0)
8+
;; CHECK-NEXT: (drop
9+
;; CHECK-NEXT: (i32.const 0)
10+
;; CHECK-NEXT: )
11+
;; CHECK-NEXT: )
512
(@metadata.code.inline "\12")
613
(func $func-annotation
714
;; The annotation here is on the function.
@@ -11,13 +18,4 @@
1118
)
1219
)
1320

14-
;; CHECK: (module
15-
;; CHECK-NEXT: (type $0 (func))
16-
;; CHECK-NEXT: (@metadata.code.inline "\12")
17-
;; CHECK-NEXT: (func $func-annotation
18-
;; CHECK-NEXT: (drop
19-
;; CHECK-NEXT: (i32.const 0)
20-
;; CHECK-NEXT: )
21-
;; CHECK-NEXT: )
22-
;; CHECK-NEXT: )
2321

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
12
;; RUN: wasm-opt -all --strip-toolchain-annotations %s -S -o - | filecheck %s
23

34
(module
5+
;; CHECK: (@metadata.code.inline "\00")
6+
;; CHECK-NEXT: (func $test-func-a (type $0)
7+
;; CHECK-NEXT: )
48
(@metadata.code.inline "\00")
59
(func $test-func-a
610
;; This VM annotation is kept.
711
)
812

13+
;; CHECK: (func $test-func-b (type $0)
14+
;; CHECK-NEXT: )
915
(@binaryen.removable.if.unused)
1016
(func $test-func-b
1117
;; Toolchain one is removed.
1218
)
1319

20+
;; CHECK: (@metadata.code.inline "\00")
21+
;; CHECK-NEXT: (func $test-func-c (type $0)
22+
;; CHECK-NEXT: )
1423
(@metadata.code.inline "\00")
1524
(@binaryen.removable.if.unused)
1625
(func $test-func-c
1726
;; Toolchain one is removed, VM one is kept.
1827
)
1928

29+
;; CHECK: (@metadata.code.inline "\00")
30+
;; CHECK-NEXT: (func $test-func-d (type $0)
31+
;; CHECK-NEXT: )
2032
(@binaryen.removable.if.unused)
2133
(@metadata.code.inline "\00")
2234
(func $test-func-d
2335
;; Reverse order of above.
2436
)
2537
)
2638

27-
;; CHECK: (module
28-
;; CHECK-NEXT: (type $0 (func))
29-
;; CHECK-NEXT: (@metadata.code.inline "\00")
30-
;; CHECK-NEXT: (func $test-func-a (type $0)
31-
;; CHECK-NEXT: )
32-
;; CHECK-NEXT: (func $test-func-b (type $0)
33-
;; CHECK-NEXT: )
34-
;; CHECK-NEXT: (@metadata.code.inline "\00")
35-
;; CHECK-NEXT: (func $test-func-c (type $0)
36-
;; CHECK-NEXT: )
37-
;; CHECK-NEXT: (@metadata.code.inline "\00")
38-
;; CHECK-NEXT: (func $test-func-d (type $0)
39-
;; CHECK-NEXT: )
40-
;; CHECK-NEXT: )
4139

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
12
;; RUN: wasm-opt -all --vacuum %s -S -o - | filecheck %s
23

34
;; Test the function-level annotation of removable.if.unused.
45
(module
6+
;; CHECK: (@binaryen.removable.if.unused)
7+
;; CHECK-NEXT: (func $calls-marked (type $0) (param $x i32) (result i32)
8+
;; CHECK-NEXT: (local.set $x
9+
;; CHECK-NEXT: (call $calls-marked
10+
;; CHECK-NEXT: (i32.const 1)
11+
;; CHECK-NEXT: )
12+
;; CHECK-NEXT: )
13+
;; CHECK-NEXT: (i32.const 2)
14+
;; CHECK-NEXT: )
515
(@binaryen.removable.if.unused)
616
(func $calls-marked (param $x i32) (result i32)
717
;; The function is marked as removable if unused, and this is dropped, so optimize.
@@ -19,6 +29,19 @@
1929
(i32.const 2)
2030
)
2131

32+
;; CHECK: (func $calls-unmarked (type $0) (param $x i32) (result i32)
33+
;; CHECK-NEXT: (drop
34+
;; CHECK-NEXT: (call $calls-unmarked
35+
;; CHECK-NEXT: (i32.const 0)
36+
;; CHECK-NEXT: )
37+
;; CHECK-NEXT: )
38+
;; CHECK-NEXT: (local.set $x
39+
;; CHECK-NEXT: (call $calls-unmarked
40+
;; CHECK-NEXT: (i32.const 1)
41+
;; CHECK-NEXT: )
42+
;; CHECK-NEXT: )
43+
;; CHECK-NEXT: (i32.const 2)
44+
;; CHECK-NEXT: )
2245
(func $calls-unmarked (param $x i32) (result i32)
2346
;; As above, but unmarked with the hint. We change nothing here.
2447
(drop
@@ -34,6 +57,14 @@
3457
(i32.const 2)
3558
)
3659

60+
;; CHECK: (func $calls-other (type $0) (param $x i32) (result i32)
61+
;; CHECK-NEXT: (drop
62+
;; CHECK-NEXT: (call $calls-unmarked
63+
;; CHECK-NEXT: (i32.const 1)
64+
;; CHECK-NEXT: )
65+
;; CHECK-NEXT: )
66+
;; CHECK-NEXT: (i32.const 2)
67+
;; CHECK-NEXT: )
3768
(func $calls-other (param $x i32) (result i32)
3869
;; As above, but calling another function, to check we look for annotations in
3970
;; the right place. Both calls are dropped, and only the one to the marked
@@ -52,37 +83,4 @@
5283
)
5384
)
5485

55-
;; CHECK: (module
56-
;; CHECK-NEXT: (type $0 (func (param i32) (result i32)))
57-
;; CHECK-NEXT: (@binaryen.removable.if.unused)
58-
;; CHECK-NEXT: (func $calls-marked (type $0) (param $x i32) (result i32)
59-
;; CHECK-NEXT: (local.set $x
60-
;; CHECK-NEXT: (call $calls-marked
61-
;; CHECK-NEXT: (i32.const 1)
62-
;; CHECK-NEXT: )
63-
;; CHECK-NEXT: )
64-
;; CHECK-NEXT: (i32.const 2)
65-
;; CHECK-NEXT: )
66-
;; CHECK-NEXT: (func $calls-unmarked (type $0) (param $x i32) (result i32)
67-
;; CHECK-NEXT: (drop
68-
;; CHECK-NEXT: (call $calls-unmarked
69-
;; CHECK-NEXT: (i32.const 0)
70-
;; CHECK-NEXT: )
71-
;; CHECK-NEXT: )
72-
;; CHECK-NEXT: (local.set $x
73-
;; CHECK-NEXT: (call $calls-unmarked
74-
;; CHECK-NEXT: (i32.const 1)
75-
;; CHECK-NEXT: )
76-
;; CHECK-NEXT: )
77-
;; CHECK-NEXT: (i32.const 2)
78-
;; CHECK-NEXT: )
79-
;; CHECK-NEXT: (func $calls-other (type $0) (param $x i32) (result i32)
80-
;; CHECK-NEXT: (drop
81-
;; CHECK-NEXT: (call $calls-unmarked
82-
;; CHECK-NEXT: (i32.const 1)
83-
;; CHECK-NEXT: )
84-
;; CHECK-NEXT: )
85-
;; CHECK-NEXT: (i32.const 2)
86-
;; CHECK-NEXT: )
87-
;; CHECK-NEXT: )
8886

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.
12
;; RUN: wasm-opt -all %s -S -o - | filecheck %s
23
;; RUN: wasm-opt -all --roundtrip %s -S -o - | filecheck %s
34

45
(module
6+
;; CHECK: (@binaryen.removable.if.unused)
7+
;; CHECK-NEXT: (func $func-annotation (type $0)
8+
;; CHECK-NEXT: (drop
9+
;; CHECK-NEXT: (i32.const 0)
10+
;; CHECK-NEXT: )
11+
;; CHECK-NEXT: )
512
(@binaryen.removable.if.unused)
613
(func $func-annotation
714
;; The annotation here is on the function.
@@ -11,13 +18,4 @@
1118
)
1219
)
1320

14-
;; CHECK: (module
15-
;; CHECK-NEXT: (type $0 (func))
16-
;; CHECK-NEXT: (@binaryen.removable.if.unused)
17-
;; CHECK-NEXT: (func $func-annotation
18-
;; CHECK-NEXT: (drop
19-
;; CHECK-NEXT: (i32.const 0)
20-
;; CHECK-NEXT: )
21-
;; CHECK-NEXT: )
22-
;; CHECK-NEXT: )
2321

0 commit comments

Comments
 (0)