Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/tools/codegenstat.py
2723 views
1
#!/usr/bin/python3
2
# This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
3
4
# Given the output of --compile=codegenverbose in stdin, this script outputs statistics about bytecode/IR
5
6
import sys
7
import re
8
from collections import defaultdict
9
10
count_bc = defaultdict(int)
11
count_ir = defaultdict(int)
12
count_asm = defaultdict(int)
13
count_irasm = defaultdict(int)
14
15
# GETTABLEKS R10 R1 K18 ['s']
16
# L1: DIV R14 R13 R3
17
re_bc = re.compile(r'^(?:L\d+: )?([A-Z_]+) ')
18
# # CHECK_SLOT_MATCH %178, K3, bb_fallback_37
19
# # %175 = LOAD_TAG R15
20
re_ir = re.compile(r'^# (?:%\d+ = )?([A-Z_]+) ')
21
# cmp w14,#5
22
re_asm = re.compile(r'^ ([a-z.]+) ')
23
24
current_ir = None
25
26
for line in sys.stdin.buffer.readlines():
27
line = line.decode('utf-8', errors='ignore').rstrip()
28
29
if m := re_asm.match(line):
30
count_asm[m[1]] += 1
31
if current_ir:
32
count_irasm[current_ir] += 1
33
elif m := re_ir.match(line):
34
count_ir[m[1]] += 1
35
current_ir = m[1]
36
elif m := re_bc.match(line):
37
count_bc[m[1]] += 1
38
39
def display(name, counts, limit=None, extra=None):
40
items = sorted(counts.items(), key=lambda p: p[1], reverse=True)
41
total = 0
42
for k,v in items:
43
total += v
44
shown = 0
45
print(name)
46
for i, (k,v) in enumerate(items):
47
if i == limit:
48
if shown < total:
49
print(f' {"Others":25}: {total-shown} ({(total-shown)/total*100:.1f}%)')
50
break
51
print(f' {k:25}: {v} ({v/total*100:.1f}%){"; "+extra(k) if extra else ""}')
52
shown += v
53
print()
54
55
display("Bytecode", count_bc, limit=20)
56
display("IR", count_ir, limit=20)
57
display("Assembly", count_asm, limit=10)
58
display("IR->Assembly", count_irasm, limit=30, extra=lambda op: f'{count_irasm[op] / count_ir[op]:.1f} insn/op')
59
60