-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathgenerate_lower_listing.py
169 lines (129 loc) · 5.04 KB
/
generate_lower_listing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
Generate documentation for all registered implementation for lowering
using reStructured text.
"""
from subprocess import check_output
import os.path
try:
from StringIO import StringIO # py2
except ImportError:
from io import StringIO
from collections import defaultdict
import inspect
from functools import partial
import numba
from numba.core.registry import cpu_target
def git_hash():
out = check_output(['git', 'log', "--pretty=format:'%H'", '-n', '1'])
return out.decode('ascii').strip("'\"")
def get_func_name(fn):
return getattr(fn, '__qualname__', fn.__name__)
def gather_function_info(backend):
fninfos = defaultdict(list)
basepath = os.path.dirname(os.path.dirname(numba.__file__))
for fn, osel in backend._defns.items():
for sig, impl in osel.versions:
info = {}
fninfos[fn].append(info)
info['fn'] = fn
info['sig'] = sig
code, firstlineno = inspect.getsourcelines(impl)
path = inspect.getsourcefile(impl)
info['impl'] = {
'name': get_func_name(impl),
'filename': os.path.relpath(path, start=basepath),
'lines': (firstlineno, firstlineno + len(code) - 1),
'docstring': impl.__doc__
}
return fninfos
def bind_file_to_print(fobj):
return partial(print, file=fobj)
def format_signature(sig):
def fmt(c):
try:
return c.__name__
except AttributeError:
return repr(c).strip('\'"')
out = tuple(map(fmt, sig))
return '`({0})`'.format(', '.join(out))
github_url = ('https://github.com/numba/numba/blob/'
'{commit}/{path}#L{firstline}-L{lastline}')
description = """
This lists all lowering definition registered to the CPU target.
Each subsection corresponds to a Python function that is supported by numba
nopython mode. These functions have one or more lower implementation with
different signatures. The compiler chooses the most specific implementation
from all overloads.
"""
def format_function_infos(fninfos):
buf = StringIO()
try:
print = bind_file_to_print(buf)
title_line = "Lowering Listing"
print(title_line)
print('=' * len(title_line))
print(description)
commit = git_hash()
def format_fname(fn):
try:
fname = "{0}.{1}".format(fn.__module__, get_func_name(fn))
except AttributeError:
fname = repr(fn)
return fn, fname
for fn, fname in sorted(map(format_fname, fninfos), key=lambda x: x[1]):
impinfos = fninfos[fn]
header_line = "``{0}``".format(fname)
print(header_line)
print('-' * len(header_line))
print()
formatted_sigs = map(
lambda x: format_signature(x['sig']), impinfos)
sorted_impinfos = sorted(zip(formatted_sigs, impinfos),
key=lambda x: x[0])
col_signatures = ['Signature']
col_urls = ['Definition']
for fmtsig, info in sorted_impinfos:
impl = info['impl']
filename = impl['filename']
lines = impl['lines']
fname = impl['name']
source = '{0} lines {1}-{2}'.format(filename, *lines)
link = github_url.format(commit=commit, path=filename,
firstline=lines[0], lastline=lines[1])
url = '``{0}`` `{1} <{2}>`_'.format(fname, source, link)
col_signatures.append(fmtsig)
col_urls.append(url)
# table formatting
max_width_col_sig = max(map(len, col_signatures))
max_width_col_url = max(map(len, col_urls))
padding = 2
width_col_sig = padding * 2 + max_width_col_sig
width_col_url = padding * 2 + max_width_col_url
line_format = "{{0:^{0}}} {{1:^{1}}}".format(width_col_sig,
width_col_url)
print(line_format.format('=' * width_col_sig, '=' * width_col_url))
print(line_format.format(col_signatures[0], col_urls[0]))
print(line_format.format('=' * width_col_sig, '=' * width_col_url))
for sig, url in zip(col_signatures[1:], col_urls[1:]):
print(line_format.format(sig, url))
print(line_format.format('=' * width_col_sig, '=' * width_col_url))
print()
return buf.getvalue()
finally:
buf.close()
# Main routine for this module:
def gen_lower_listing(path=None):
"""
Generate lowering listing to ``path`` or (if None) to stdout.
"""
cpu_backend = cpu_target.target_context
cpu_backend.refresh()
fninfos = gather_function_info(cpu_backend)
out = format_function_infos(fninfos)
if path is None:
print(out)
else:
with open(path, 'w') as fobj:
print(out, file=fobj)
if __name__ == '__main__':
gen_lower_listing()