forked from davemenendez/alive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-loops.py
executable file
·82 lines (64 loc) · 1.98 KB
/
find-loops.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
#!/usr/bin/env python
import loops
import argparse, logging, itertools, sys
def search(opts, n, on_error=None):
'Generate candidate n-cycles'
if n == 1:
for i1,o in itertools.izip(itertools.count(), opts):
yield (o,i1,(o,))
return
for (o1,i1,oz) in search(opts, n-1, on_error):
for i2 in range(0, len(opts)):
o2 = opts[i2]
os = oz + (o2,)
for c in loops.all_bin_compositions(o1, o2, on_error):
yield (c,i2, os)
status = '\rTested: {} SatChecks: {} Loops: {}'
def count_src(o):
return sum(1 for v in o.src.itervalues() if isinstance(v, loops.Instr))
def main():
logging.basicConfig(filename='find-loops.log', filemode='w')
parser = argparse.ArgumentParser()
parser.add_argument('length', type=int,
help='Length of cycles to search for')
parser.add_argument('file', type=argparse.FileType('r'),
help='optimization suite to analyze')
args = parser.parse_args()
if args.length < 1:
sys.stderr.write('cycle length must be positive\n')
exit(1)
sys.stderr.write('Reading ' + args.file.name + '\n')
opts = loops.parse_transforms(args.file.read())
sys.stderr.write('{} optimizations'.format(len(opts)))
count = 0
sat_checks = 0
cycles = 0
errors = [0]
def count_error(e,o1,o2):
errors[0] += 1
for o,_,os in search(opts, args.length):
o_src = count_src(o)
for oo in loops.all_bin_compositions(o,o,count_error):
sys.stderr.write(status.format(count, sat_checks, cycles))
count += 1
oo_src = count_src(oo)
if o_src < oo_src:
continue
sat_checks += 1
if not loops.satisfiable(oo):
continue
cycles += 1
print '\n-----\nLoop: ', o.name
for opt in os:
opt.dump()
print
o.dump()
sys.stderr.write(status.format(count, sat_checks, cycles))
sys.stderr.write('\n')
print
print 'final count', count
print 'loops', cycles
print 'sat_checks', sat_checks
print 'errors', errors[0]
if __name__ == '__main__':
main()