xref: /llvm-project/llvm/utils/chunk-print-before-all.py (revision 0361c415e17f45b480be5befdb12175e8899357d)
1#!/usr/bin/env python
2
3# Given a -print-before-all and/or -print-after-all -print-module-scope log from
4# an opt invocation, chunk it into a series of individual IR files, one for each
5# pass invocation. If the log ends with an obvious stack trace, try to split off
6# a separate "crashinfo.txt" file leaving only the valid input IR in the last
7# chunk. Files are written to current working directory.
8
9import sys
10import re
11
12chunk_id = 0
13pass_regex = re.compile(r"\(([\w-]+)\)")
14
15# This function gets the pass name from the following line:
16# *** IR Dump Before/After PASS_NAME... ***
17def get_pass_name(line, prefix):
18    # avoid accidentally parsing function name in e.g.
19    # "*** IR Dump Before InlinerPass on (foo) ***"
20    line = line.split(" on ")[0]
21    non_pretty_pass_name = pass_regex.search(line)
22    # machine function pass names can contain spaces,
23    # but have a CLI friendly name also, e.g.:
24    # "*** IR Dump Before Stack Frame Layout Analysis (stack-frame-layout) ***"
25    if non_pretty_pass_name:
26        return non_pretty_pass_name.group(1)
27    short_line = line[line.find(prefix) + len(prefix) + 1 :]
28    return re.split(" |<", short_line)[0]
29
30
31def print_chunk(lines, prefix, pass_name):
32    global chunk_id
33    fname = str(chunk_id).zfill(4) + "-" + prefix + "-" + pass_name + ".ll"
34    chunk_id = chunk_id + 1
35    print("writing chunk " + fname + " (" + str(len(lines)) + " lines)")
36    with open(fname, "w") as f:
37        f.writelines(lines)
38
39
40is_dump = False
41cur = []
42for line in sys.stdin:
43    if "*** IR Dump Before " in line:
44        if len(cur) != 0:
45            print_chunk(cur, "before", pass_name)
46            cur = []
47        cur.append("; " + line)
48        pass_name = get_pass_name(line, "Before")
49    elif "*** IR Dump After " in line:
50        if len(cur) != 0:
51            print_chunk(cur, "after", pass_name)
52            cur = []
53        cur.append("; " + line)
54        pass_name = get_pass_name(line, "After")
55    elif line.startswith("Stack dump:"):
56        print_chunk(cur, "crash", pass_name)
57        cur = []
58        cur.append(line)
59        is_dump = True
60    else:
61        cur.append(line)
62
63if is_dump:
64    print("writing crashinfo.txt (" + str(len(cur)) + " lines)")
65    with open("crashinfo.txt", "w") as f:
66        f.writelines(cur)
67else:
68    print_chunk(cur, "last", pass_name)
69