xref: /llvm-project/llvm/utils/schedcover.py (revision 94ed47f2e98ca62155a56a486e81215ce38da45d)
1#!/usr/bin/env python
2
3# This creates a CSV file from the output of the debug output of subtarget:
4#   llvm-tblgen --gen-subtarget --debug-only=subtarget-emitter
5# With thanks to Dave Estes for mentioning the idea at 2014 LLVM Developers' Meeting
6
7import os
8import sys
9import re
10import operator
11
12table = {}
13models = set()
14filt = None
15
16
17def add(instr, model, resource=None):
18    global table, models
19
20    entry = table.setdefault(instr, dict())
21    entry[model] = resource
22    models.add(model)
23
24
25def filter_model(m):
26    global filt
27    if m and filt:
28        return filt.search(m) is not None
29    else:
30        return True
31
32
33def display():
34    global table, models
35
36    # remove default and itinerary so we can control their sort order to make
37    # them first
38    models.discard("default")
39    models.discard("itinerary")
40
41    ordered_table = sorted(table.items(), key=operator.itemgetter(0))
42    ordered_models = ["itinerary", "default"]
43    ordered_models.extend(sorted(models))
44    ordered_models = [m for m in ordered_models if filter_model(m)]
45
46    # print header
47    sys.stdout.write("instruction")
48    for model in ordered_models:
49        sys.stdout.write(", {}".format(model))
50    sys.stdout.write(os.linesep)
51
52    for (instr, mapping) in ordered_table:
53        sys.stdout.write(instr)
54        for model in ordered_models:
55            if model in mapping and mapping[model] is not None:
56                sys.stdout.write(", {}".format(mapping[model]))
57            else:
58                sys.stdout.write(", ")
59        sys.stdout.write(os.linesep)
60
61
62def machineModelCover(path):
63    # The interesting bits
64    re_sched_default = re.compile("SchedRW machine model for ([^ ]*) (.*)\n")
65    re_sched_no_default = re.compile("No machine model for ([^ ]*)\n")
66    re_sched_spec = re.compile("InstRW on ([^ ]*) for ([^ ]*) (.*)\n")
67    re_sched_no_spec = re.compile("No machine model for ([^ ]*) on processor (.*)\n")
68    re_sched_itin = re.compile("Itinerary for ([^ ]*): ([^ ]*)\n")
69
70    # scan the file
71    with open(path, "r") as f:
72        for line in f.readlines():
73            match = re_sched_default.match(line)
74            if match:
75                add(match.group(1), "default", match.group(2))
76            match = re_sched_no_default.match(line)
77            if match:
78                add(match.group(1), "default")
79            match = re_sched_spec.match(line)
80            if match:
81                add(match.group(2), match.group(1), match.group(3))
82            match = re_sched_no_spec.match(line)
83            if match:
84                add(match.group(1), match.group(2))
85            match = re_sched_itin.match(line)
86            if match:
87                add(match.group(1), "itinerary", match.group(2))
88
89    display()
90
91
92if len(sys.argv) > 2:
93    filt = re.compile(sys.argv[2], re.IGNORECASE)
94machineModelCover(sys.argv[1])
95