xref: /openbsd-src/gnu/llvm/llvm/utils/chunk-print-before-all.py (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
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
13
14# This function gets the pass name from the following line:
15# *** IR Dump Before/After PASS_NAME... ***
16def get_pass_name(line, prefix):
17    short_line = line[line.find(prefix) + len(prefix) + 1:]
18    return re.split(' |<', short_line)[0]
19
20def print_chunk(lines, prefix, pass_name):
21    global chunk_id
22    fname = str(chunk_id) + "-" + prefix + "-" + pass_name + ".ll"
23    chunk_id = chunk_id + 1
24    print("writing chunk " + fname + " (" + str(len(lines)) + " lines)")
25    with open(fname, "w") as f:
26        f.writelines(lines)
27
28is_dump = False
29cur = []
30for line in sys.stdin:
31    if line.startswith("*** IR Dump Before "):
32        if len(cur) != 0:
33            print_chunk(cur, "before", pass_name)
34            cur = []
35        cur.append("; " + line)
36        pass_name = get_pass_name(line, "Before")
37    elif line.startswith("*** IR Dump After "):
38        if len(cur) != 0:
39            print_chunk(cur, "after", pass_name)
40            cur = []
41        cur.append("; " + line)
42        pass_name = get_pass_name(line, "After")
43    elif line.startswith("Stack dump:"):
44        print_chunk(cur, "crash", pass_name)
45        cur = []
46        cur.append(line)
47        is_dump = True
48    else:
49        cur.append(line)
50
51if is_dump:
52    print("writing crashinfo.txt (" + str(len(cur)) + " lines)")
53    with open("crashinfo.txt", "w") as f:
54        f.writelines(cur)
55else:
56    print_chunk(cur, "last", pass_name)
57