-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabc_common.py
2007 lines (1881 loc) · 66.6 KB
/
abc_common.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from pyabc import *
import pyabc_split
import par
import redirect
import sys
import os
import time
import math
global G_C,G_T,latches_before_abs,latches_before_pba,n_pos_before,x_factor
"""
The functions that are currently available from module _abc are:
int n_ands();
int n_pis();
int n_pos();
int n_latches();
int n_bmc_frames();
int prob_status(); 1 = unsat, 0 = sat, -1 = unsolved
int run_command(char* cmd);
bool has_comb_model();
bool has_seq_model();
bool is_true_cex();
bool is_valid_cex();
return 1 if the number of PIs in the current network and in the current counter-example are equal
int n_cex_pis();
return the number of PIs in the current counter-example
int n_cex_regs();
return the number of flops in the current counter-example
int cex_po();
returns the zero-based output PO number that is SAT by cex
int cex_frame();
return the zero-based frame number where the outputs is SAT
The last four APIs return -1, if the counter-example is not defined.
"""
#global variables
stackno_gabs = stackno_gore = stackno_greg= 0
STATUS_UNKNOWN = -1
STATUS_SAT = 0
STATUS_UNSAT = 1
RESULT = ('SAT' , 'SAT', 'UNSAT', 'UNDECIDED', 'UNDECIDED,', 'UNDCIDED' )
Sat_reg = 0
Sat_true = 1
Unsat = 2
Undecided_reduction = 3
Undecided_no_reduction = 4
Error = 5
Restart = 6
xfi = x_factor = 1 #set this to higher for larger problems or if you want to try harder during abstraction
max_bmc = -1
last_time = 0
# Function definitions:
# simple functions: ________________________________________________________________________
# set_globals, abc, q, x, has_any_model, is_sat, is_unsat, push, pop
# ALIASES
def p():
return prove()
def ps():
print_circuit_stats()
def n_real_inputs():
"""This gives the number of 'real' inputs. This is determined by trimming away inputs that
have no connection to the logic. This is done by the ABC alias 'trm', which changes the current
circuit. In some applications we do not want to change the circuit, but just to know how may inputs
would go away if we did this. So the current circuit is saved and then restored afterwards."""
abc('w %s_savetempreal.aig; logic; trim; st'%f_name)
n = n_pis()
abc('r %s_savetempreal.aig'%f_name)
return n
def long(t):
if t<20:
t = t
else:
t = 20+(t-20)/3
return max(10,t)
def rif():
"""Not used"""
global f_name
print 'Type in the name of the aig file to be read in'
s = raw_input()
if s[-5:] == '.blif':
f_name = s[:-5]
else:
f_name = s
s = s+'.blif'
run_command(s)
x('st;constr -i')
print_circuit_stats()
a = n_ands()
f = max(1,30000/a)
f = min (f,16)
x('scorr -c -F %d'%f)
x('fold')
print_circuit_stats()
x('w %s_c.aig'%f_name)
def abc(cmd):
abc_redirect_all(cmd)
def abc_redirect( cmd, dst = redirect.null_file, src = sys.stdout ):
"""This is our main way of calling an ABC function. Redirect, means that we suppress any output from ABC"""
with redirect.redirect( dst, src ):
return run_command( cmd )
def abc_redirect_all( cmd ):
"""This is our main way of calling an ABC function. Redirect, means that we suppress any output from ABC, including error printouts"""
with redirect.redirect( redirect.null_file, sys.stdout ):
with redirect.redirect( redirect.null_file, sys.stderr ):
return run_command( cmd )
def set_globals():
"""This sets global parameters that are used to limit the resources used by all the operations
bmc, interpolation BDDs, abstract etc. There is a global factor 'x_factor' that can
control all of the various resource limiting parameters"""
global G_C,G_T,x_factor
nl=n_latches()
na=n_ands()
np = n_pis()
#G_C = min(500000,(3*na+500*(nl+np)))
G_C = x_factor * min(100000,(3*na+500*(nl+np)))
#G_T = min(250,G_C/2000)
G_T = x_factor * min(75,G_C/2000)
G_T = max(1,G_T)
#print('Global values: BMC conflicts = %d, Max time = %d sec.'%(G_C,G_T))
def a():
"""this puts the system into direct abc input mode"""
print "Entering ABC direct-input mode. Type q to quit ABC-mode"
n = 0
while True:
print ' abc %d> '%n,
n = n+1
s = raw_input()
if s == "q":
break
run_command(s)
def set_fname(name):
""" Another way to set an f_name, but currently this is not used"""
global f_name
s = name
if s[-4:] == '.aig':
f_name = s[:-4]
else:
f_name = s
s = s+'.aig'
#read in file
run_command(s)
#print_circuit_stats()
def read_file_quiet(fname=None):
"""This is the main program used for reading in a new circuit. The global file name is stored (f_name)
Sometimes we want to know the initial starting name. The file name can have the .aig extension left off
and it will assume that the .aig extension is implied. This should not be used for .blif files.
Any time we want to process a new circuit, we should use this since otherwise we would not have the
correct f_name."""
global max_bmc, f_name, d_name, initial_f_name, x_factor, init_initial_f_name
x_factor = 1
max_bmc = -1
if fname is None:
print 'Type in the name of the aig file to be read in'
s = raw_input()
else:
s = fname
if s[-4:] == '.aig':
f_name = s[:-4]
else:
f_name = s
s = s+'.aig'
run_command(s)
initial_f_name = f_name
init_initial_f_name = f_name
def read_file():
read_file_quiet()
print_circuit_stats()
def rf():
read_file()
def write_file(s):
"""this is the main method for writing the current circuit to an AIG file on disk.
It manages the name of the file, by giving an extension (s). The file name 'f_name'
keeps increasing as more extensions are written. A typical sequence is
name, name_smp, name_smp_abs, name_smp_abs_spec, name_smp_abs_spec_final"""
global f_name
"""Writes out the current file as an aig file using f_name appended with argument"""
f_name = '%s_%s'%(f_name,s)
ss = '%s.aig'%(f_name)
print 'WRITING %s: '%ss,
print_circuit_stats()
abc('w '+ss)
def wf():
"""Not used"""
print 'input type of file to be written'
s = raw_input()
write_file(s)
def bmc_depth():
""" Finds the number of BMC frames that the latest operation has used. The operation could be BMC, reachability
interpolation, abstract, speculate. max_bmc is continually increased. It reflects the maximum depth of any version of the circuit
including g ones, for which it is known that there is not cex out to that depth."""
global max_bmc
b = n_bmc_frames()
max_bmc = max(b,max_bmc)
return max_bmc
def set_max_bmc(b):
""" Keeps increasing max_bmc which is the maximum number of time frames for
which the current circuit is known to be UNSAT for"""
global max_bmc
max_bmc = max(b,max_bmc)
def print_circuit_stats():
"""Stardard way of outputting statistice about the current circuit"""
global max_bmc
i = n_pis()
o = n_pos()
l = n_latches()
a = n_ands()
b = max(max_bmc,bmc_depth())
c = cex_frame()
if b>= 0:
if c>=0:
print 'PIs = %d, POs = %d, FF = %d, ANDs = %d, max depth = %d, CEX depth = %d'%(i,o,l,a,b,c)
else:
print 'PIs = %d, POs = %d, FF = %d, ANDs = %d, max depth = %d'%(i,o,l,a,b)
else:
if c>=0:
print 'PIs = %d, POs = %d, FF = %d, ANDs = %d, CEX depth = %d'%(i,o,l,a,c)
else:
print 'PIs = %d, POs = %d, FF = %d, ANDs = %d'%(i,o,l,a)
def q():
exit()
def x(s):
"""execute an ABC command"""
print "RUNNING: ", s
run_command(s)
def has_any_model():
""" check if a satisfying assignment has been found"""
res = has_comb_model() or has_seq_model()
return res
def is_unsat():
if prob_status() == 1:
return True
else:
return False
def is_sat():
if prob_status() == 0:
return True
else:
return False
def wc(file):
"""writes <file> so that costraints are preserved explicitly"""
abc('&get;&w %s'%file)
def rc(file):
"""reads <file> so that if constraints are explicit, it will preserve them"""
abc('&r %s;&put'%file)
def fast_int(n):
"""This is used to eliminate easy-to-prove outputs. Arg n is conflict limit to be used
in the interpolation routine. Typically n = 1 or 10"""
n_po = n_pos()
abc('int -k -C %d'%n)
print 'Reduced POs from %d to %d'%(n_po,n_pos())
def refine_with_cex():
"""Refines the greg (which contains the original problem with the set of FF's that have been abstracted).
This generates a new abstraction (gabs) and modifies the greg file to reflect which regs are in the
current abstraction"""
global f_name
print 'CEX in frame %d for output %d'%(cex_frame(),cex_po())
abc('&r %s_greg.aig; &abs_refine; &w %s_greg.aig'%(f_name,f_name))
return
def generate_abs(n):
"""generates an abstracted model (gabs) from the greg file. The gabs file is automatically
generated in the & space by &abs_derive. We store it away using the f_name of the problem
being solved at the moment. The f_name keeps changing with an extension given by the latest
operation done - e.g. smp, abs, spec, final, group. """
global f_name
#we have a cex and we use this generate a new gabs file
abc('&r %s_greg.aig; &abs_derive; &put; w %s_gabs.aig'%(f_name,f_name)) # do we still need the gabs file
if n == 1:
print 'New abstraction: ',
print_circuit_stats()
return
#more complex functions: ________________________________________________________
#, abstract, pba, speculate, final_verify, dprove3
def simplify():
"""Our standard simplification of logic routine. What it does depende on the problem size.
For large problems, we use the &methods which use a simple circuit based SAT solver. Also problem
size dictates the level of k-step induction done in 'scorr' The stongest simplification is done if
n_ands < 20000. Then it used the clause based solver and k-step induction where |k| depends
on the problem size """
# set_globals()
## print 'simplify initial ',
## ps()
#abc('w t.aig')
n=n_ands()
abc('scl')
if n > 40000:
abc('&get;&scl;&put')
n = n_ands()
if n < 100000:
abc("&dc2;&put;dr;&get;&lcorr;&dc2;&put;dr;&get;&scorr;&fraig;&dc2;&put;dr")
run_command('ps')
print '.',
n = n_ands()
if n<60000:
abc("&get;&scorr -F 2;&put;dc2rs")
print '.',
#ps()
else:
abc("dc2rs")
print '.',
#ps()
n = n_ands()
n = n_ands()
if n <= 40000:
print '.',
#ps()
if n > 30000:
abc("dc2rs")
print '.',
## else:
## abc("scorr -F 2;dc2rs")
## print '.',
## ps()
n = max(1,n_ands())
#ps()
if n < 30000:
abc('scl;rw;dr;lcorr;rw;dr')
m = int(min( 60000/n, 16))
#print 'm = %d'%m
if m >= 1:
j = 1
while j <= m:
set_size()
#print 'j - %d'%j
#abc('scl;dr;dc2;scorr -C 5000 -F %d'%j)
if j<8:
abc('dc2')
else:
abc('dc2rs')
abc('scorr -C 5000 -F %d'%j)
if check_size():
break
j = 2*j
#ps()
continue
print '.',
def iterate_simulation(latches_before):
"""Subroutine of 'abstract' which does the refinement of the abstracted model,
using counterexamples found by simulation. Simulation is controlled by the amount
of memory it might use. At first wide but shallow simulation is done, bollowed by
successively more narrow but deeper simulation"""
global x_factor, f_name
## print 'RUNNING simulation iteratively'
f = 5
w = 255
for k in range(9):
f = min(f *2, 3500)
w = max(((w+1)/2)-1,1)
print '.',
abc('sim -m -F %d -W %d'%(f,w))
if not is_sat():
continue
while True:
refine_with_cex()
if is_sat():
print 'cex failed to refine abstraction '
return Sat_true
generate_abs(0)
latches_after = n_latches()
print 'Latches increased to %d'%latches_after
if latches_after >= .99*latches_before:
abc('r %s_savetempabs.aig'%f_name)
print "No reduction!."
return Undecided_no_reduction
abc('sim -m -F %d -W %d'%(f,w))
if not is_sat():
break
def simulate():
"""Simulation is controlled by the amount
of memory it might use. At first wide but shallow simulation is done, bollowed by
successively more narrow but deeper simulation"""
global x_factor, f_name
## print 'RUNNING simulation iteratively'
f = 5
w = 255
for k in range(9):
f = min(f *2, 3500)
w = max(((w+1)/2)-1,1)
print '.',
abc('sim -m -F %d -W %d'%(f,w))
if is_sat():
print 'cex found in frame %d'%cex_frame()
return 'SAT'
return 'UNDECIDED'
def iterate_abstraction_refinement(latches_before,NBF):
"""Subroutine of 'abstract' which does the refinement of the abstracted model,
using counterexamples found by BMC or BDD reachability"""
global x_factor, f_name
if NBF == -1:
F = 2000
else:
F = 2*NBF
print '\nIterating BMC/PDR'
always_reach = 0
cexf = 0
reach_failed = 0
while True:
#print 'Generating problem abstraction'
generate_abs(1)
set_globals()
latches_after = n_latches()
if latches_after >= .98*latches_before:
## print 'No reduction'
## abc('r &s_savetemp.aig'%f_name)
break
ri = n_real_inputs() # No. of inputs after trm
nlri = n_latches()+ri
#reach_allowed = ((nlri<150) or (((cexf>250))&(nlri<300)))
reach_allowed = ((nlri<75) or (((cexf>250))&(nlri<300)))
pdr_allowed = True
bmc = False
t = max(1,G_T)
if not F == -1:
F = int(1.5*max_bmc)
if (((reach_allowed or pdr_allowed ) and not reach_failed)):
#cmd = 'reach -B 200000 -F 3500 -T %f'%t
#cmd = 'reachx -e %d -t %d'%(int(long(t)),max(10,int(t)))
#cmd = 'reachx -t %d'%max(10,int(t))
cmd = '&get;,pdr -vt=%f'%t
else:
#cmd = 'bmc3 -C %d -T %f -F %d'%(G_C,t,F)
bmc = True
cmd = '&get;,bmc -vt=%f'%(t)
#cmd = '&get;,pdr -vt=%f'%(t)
print '\n***RUNNING %s'%cmd
#run_command(cmd)
last_bmc = max_bmc
abc(cmd)
if prob_status() == 1:
#print 'Depth reached %d frames, '%n_bmc_frames()
print 'UNSAT'
return Unsat
cexf = cex_frame()
#print 'cex depth = %d'%cexf
#set_max_bmc(cexf -1)
if ((not is_sat()) ):
reach_failed = 1 # if ever reach failed, then we should not try it again on more refined models
if is_sat():
#print 'CEX in frame %d for output %d'%(cex_frame(),cex_po())
#set_max_bmc(cexf-1)
refine_with_cex() # if cex can't refine, status is set to Sat_true
if is_sat():
print 'cex did not refine. Implies true_sat'
return Sat_true
else:
print "No CEX found in %d frames"%n_bmc_frames()
set_max_bmc(n_bmc_frames())
if bmc:
break
elif max_bmc> 1.2*last_bmc: # if pdr increased significantly over abs, the assume OK
break
else:
continue
latches_after = n_latches()
if latches_after >= .98*latches_before:
abc('r %s_savetempabs.aig'%f_name)
print "No reduction!"
return Undecided_no_reduction
else:
print 'Latches reduced from %d to %d'%(latches_before, n_latches())
return Undecided_reduction
def abstract():
""" abstracts using N Een's method 3 - cex/proof based abstraction. The result is further refined using
simulation, BMC or BDD reachability"""
global G_C, G_T, latches_before_abs, x_factor
set_globals()
latches_before_abs = n_latches()
abc('w %s_savetempabs.aig'%f_name)
print 'Start: ',
print_circuit_stats()
c = 1.5*G_C
#t = max(1,1.25*G_T)
t = 2*max(1,1.25*G_T)
s = min(max(3,c/30000),10) # stability between 3 and 10
time = max(1,.01*G_T)
abc('&get;,bmc -vt=%f'%time)
print 'BMC went %d frames'%n_bmc_frames()
set_max_bmc(bmc_depth())
f = max(2*max_bmc,20)
b = min(max(10,max_bmc),200)
cmd = '&get;,abs -bob=%d -stable=%d -timeout=%f -vt=%f -depth=%d'%(b,s,t,t,f)
print ' Running %s'%cmd
run_command(cmd)
if is_sat():
print 'Found true counterexample in frame %d'%cex_frame()
return Sat_true
NBF = bmc_depth()
print 'Abstraction good to %d frames'%n_bmc_frames()
set_max_bmc(NBF)
abc('&w %s_greg.aig; &abs_derive; &put; w %s_gabs.aig'%(f_name,f_name))
## print 'First abstraction: ',
## print_circuit_stats()
latches_after = n_latches()
#if latches_before_abs == latches_after:
if latches_after >= .98*latches_before_abs:
abc('r %s_savetempabs.aig'%f_name)
print "No reduction!"
return Undecided_no_reduction
# refinement loop
if (n_ands() + n_latches() + n_pis()) < 15000:
print '\n***Running simulation iteratively'
for i in range(5):
result = iterate_simulation(latches_before_abs)
if result == Restart:
return result
if result == Sat_true:
return result
result = iterate_abstraction_refinement(latches_before_abs, NBF)
#if the result is 'Restart' we return and the calling routine increase
#x_factor to try one more time.
return result
def absv(n,v):
"""This is a version of 'abstract' which can control the methods used in Een's abstraction code (method = n)
as well as whether we want to view the output of this (v = 1)"""
global G_C, G_T, latches_before_abs, x_factor
#input_x_factor()
#x_factor = 3
set_globals()
latches_before_abs = n_latches()
print 'Start: ',
print_circuit_stats()
c = 1.5*G_C
t = max(1,1.25*G_T)
s = min(max(3,c/30000),10) # stability between 3 and 10
if max_bmc == -1:
time = max(1,.01*G_T)
#abc('bmc3 -C %d -T %f -F 165'%(.01*G_C, time))
abc('&get;,bmc -vt=%f'%time)
set_max_bmc(bmc_depth())
f = min(250,1.5*max_bmc)
f = max(20, f)
f = 10*f
b = x_factor*20
if not n == 0:
b = 10
b = max(b,max_bmc+2)
b = b*2**(x_factor-1)
b = 2*b
print 'Neen abstraction params: Method #%d, %d conflicts, %d stable, %f sec.'%(n,c/3,s,t)
if v == 1:
run_command('&get; &abs_newstart -v -B %f -A %d -C %d -S %d -V %f'%(b,n,c/3,s,t))
else:
abc('&get; &abs_newstart -v -B %f -A %d -C %d -S %d -T %f'%(b,n,c/3,s,t))
set_max_bmc(n_bmc_frames())
print 'Abstraction good to %d'%n_bmc_frames()
abc('&w %s_greg.aig; &abs_derive; &put; w %s_gabs.aig'%(f_name,f_name))
print 'Final abstraction: ',
print_circuit_stats()
latches_after = n_latches()
if latches_after >= .98*latches_before_abs:
print "No reduction!"
return Undecided_no_reduction
return Undecided_reduction
def spec():
"""Main speculative reduction routine. Finds candidate sequential equivalences and refines them by simulation, BMC, or reachability
using any cex found. """
input_x_factor()
global G_C,G_T,n_pos_before, x_factor, n_latches_before
set_globals()
n_pos_before = n_pos()
n_latches_before = n_latches()
set_globals()
t = max(1,.5*G_T)
r = max(1,int(t))
print '\n***Running &equiv2 with C = %d, T = %f sec., F = %d -S 1 -R %d'%(G_C,t,200,r)
abc("&get; &equiv2 -C %d -F 200 -T %f -S 1 -R %d; &semi -F 50; &speci -F 20 -C 1000;&srm; r gsrm.aig; w %s_gsrm.aig; &w %s_gore.aig"%((G_C),t,r,f_name,f_name))
print 'Initial speculation: ',
print_circuit_stats()
print 'Speculation good to %d frames'%n_bmc_frames()
return
def speculate():
"""Main speculative reduction routine. Finds candidate sequential equivalences and refines them by simulation, BMC, or reachability
using any cex found. """
global G_C,G_T,n_pos_before, x_factor, n_latches_before
set_globals()
n_pos_before = n_pos()
def refine_with_cex():
"""Refines the gore file to reflect equivalences that go away because of cex"""
global f_name
print 'CEX in frame %d for output %d'%(cex_frame(),cex_po())
abc('&r %s_gore.aig; &resim -m; &w %s_gore.aig'%(f_name,f_name))
#abc('&r %s_gore.aig; &equiv2 -vx ; &w %s_gore.aig'%(f_name,f_name))
return
def generate_srm(n):
"""generates a speculated reduced model (srm) from the gore file"""
global f_name
pos = n_pos()
ab = n_ands()
abc('&r %s_gore.aig; &srm ; r gsrm.aig; w %s_gsrm.aig'%(f_name,f_name)) #do we still need to write the gsrm file
if n == 0:
if ((pos == n_pos()) and (ab == n_ands())):
print 'Failed to refine'
return 'failed'
if n == 1:
print 'Spec. Red. Miter: ',
print_circuit_stats()
return 'OK'
def run_simulation(n):
f = 5
w = (256/n)-1
for k in range(9):
f = min(f * 2, 3500)
w = max(((w+1)/2)-1,1)
print '.',
#generate_srm(0)
abc('sim -m -F %d -W %d'%(f,w))
if not is_sat():
continue
if cex_po() < n_pos_before:
print 'Sim found true cex: Output = %d, Frame = %d'%(cex_po(),cex_frame())
return Sat_true
refine_with_cex()
if n_pos_before == n_pos():
return Undecided_no_reduction
while True:
result = generate_srm(0)
if result == 'failed':
return Sat_true
abc('sim -m -F %d -W %d'%(f,w))
if not is_sat():
break
if cex_po() < n_pos_before:
print 'Sim found true cex: Output = %d, Frame = %d'%(cex_po(),cex_frame())
return Sat_true
refine_with_cex()
if n_pos_before == n_pos():
return Undecided_no_reduction
return Undecided_no_reduction
n_pos_before = n_pos()
n_latches_before = n_latches()
set_globals()
t = max(1,.5*G_T)
r = max(1,int(t))
abc('write spec_temp.aig')
print '\n***Running &equiv2 with C = %d, T = %f sec., F = %d -S 1 -R %d'%(G_C,t,200,r)
## abc("&get; &equiv2 -C %d -F 200 -T %f -S 1 -R %d; &semi -F 50; &speci -F 20 -C 1000;&srm; r gsrm.aig; w %s_gsrm.aig; &w %s_gore.aig"%((G_C),t,r,f_name,f_name))
abc("&get; &equiv2 -C %d -F 200 -T %f -S 1 -R %d; &semi -W 63 -S 5 -C 500 -F 500; &speci -F 200 -C 5000;&srm; r gsrm.aig; w %s_gsrm.aig; &w %s_gore.aig"%((G_C),t,r,f_name,f_name))
print 'Initial speculation: ',
print_circuit_stats()
#print 'Speculation good to %d frames'%n_bmc_frames()
#simplify()
if n_pos_before == n_pos():
print 'No new outputs. Quitting speculate'
return Undecided_no_reduction # return result is unknown
if is_sat():
#print '\nWARNING: if an abstraction was done, need to refine it further\n'
return Sat_true
if n_latches() > .98*n_latches_before:
pre_simp()
if n_latches() > .98*n_latches_before:
print 'Quitting speculation - not enough gain'
abc('r spec_temp.aig')
return Undecided_no_reduction # not worth it
k = n_ands() + n_latches() + n_pis()
n = 0
if k < 15000:
n = 1
elif k < 30000:
n = 2
elif k < 60000:
n = 4
elif k < 120000:
n = 8
if n > 0: # simulation can run out of memory for too large designs, so be careful
print '\n***RUNNING simulation iteratively'
for i in range(5):
result = run_simulation(n)
if result == Sat_true:
return result
simp_sw = 1
int_sw = 1
reach_sw = 0
cexf = 0
reach_failed = 0
init = 1
run_command('write temptemp.aig')
print '\nIterating BMC or BDD reachability'
while True: # now try real hard to get the last cex.
set_globals()
if not init:
set_size()
result = generate_srm(1)
if check_size() == True:
print 'Failed to refine'
return Error
if result == 'failed':
return Sat_true
if simp_sw == 1:
na = n_ands()
simplify()
if n_ands() > .7*na: #if not significant reduction, stop simplification
simp_sw = 0
if n_latches() == 0:
return check_sat()
init = 0 # make it so that next time it is not the first time through
time = max(1,G_T/(5*n_pos()))
if int_sw ==1:
npo = n_pos()
if n_pos() > .5*npo: # if not sufficient reduction, turn this off
int_sw = 0
if is_sat(): #if fast interpolation found a cex
cexf = cex_frame()
set_max_bmc(cexf - 1)
if cex_po() < n_pos_before:
print 'Int found true cex: Output = %d, Frame = %d'%(cex_po(),cex_frame())
return Sat_true
refine_with_cex()
if ((n_pos_before == n_pos()) or (n_latches_before == n_latches())):
abc('r temp_spec.aig')
return Undecided_no_reduction
if is_sat():
print '1. cex failed to refine abstraction'
return Sat_true
continue
else:
if n_latches() == 0:
return check_sat()
ri = n_real_inputs() #seeing how many inputs would trm get rid of
nlri = n_latches() + ri
reach_allowed = ((nlri<75) or (((cexf>250)) and (nlri<300)))
pdr_allowed = True
bmc = False
if (((reach_allowed or pdr_allowed ) and not reach_failed)):
t = max(1,1.2*G_T)
f = max(3500, 2*max_bmc)
#cmd = 'reachx -t %d'%max(10,int(t))
cmd ='&get;,pdr -vt=%f'%t
else:
t = max(1,1.5*G_T)
if max_bmc == -1:
f = 200
else:
f = max_bmc
f = int(1.5*f)
#cmd = 'bmc3 -C %d -T %f -F %f'%(1.5*G_C,1.2*t,f)
bmc = True
cmd = '&get;,bmc -vt=%f'%(1.2*t)
print '\n***Running %s'%cmd
last_bmc = max_bmc
abc(cmd)
#run_command(cmd)
if is_sat():
cexf = cex_frame()
#set_max_bmc(cexf - 1)
#This is a temporary fix since reachx always reports cex_ps = 0
if ((cex_po() < n_pos_before) and (cmd[:4] == '&get')):
print 'BMC/PDR found true cex: Output = %d, Frame = %d'%(cex_po(),cex_frame())
return Sat_true
#End of temporary fix
refine_with_cex()#change the number of equivalences
if n_pos_before == n_pos():
return Undecided_no_reduction
continue
else:
set_max_bmc(n_bmc_frames())
if prob_status() == 1:
#print 'Convergence reached in %d frames'%n_bmc_frames()
return Unsat
print 'No cex found in %d frames'%n_bmc_frames()
if bmc:
break
elif max_bmc > 1.2*last_bmc:
break
else:
reach_failed = 1
init = 1
continue
if n_pos_before == n_pos():
return Undecided_no_reduction
else:
return Undecided_reduction
def set_size():
"""Stores the problem size of the current design. Size is defined as (PIs, POs, ANDS, FF, max_bmc)"""
global npi, npo, nands, nff, nmd
npi = n_pis()
npo = n_pos()
nands = n_ands()
nff = n_latches()
nmd = max_bmc
def check_size():
"""Assumes the problem size has been set by set_size before some operation. This checks if the size was changed
Size is defined as (PIs, POs, ANDS, FF, max_bmc)
Returns TRUE is size is the same"""
global npi, npo, nands, nff, nmd
result = ((npi == n_pis()) and (npo == n_pos()) and (nands == n_ands()) and (nff == n_latches()) and (nmd == max_bmc))
## if result == 1:
## print 'Size unchanged'
return result
def inferior_size():
"""Assumes the problem size has been set by set_size beore some operation.
This checks if the new size is inferior (larger) to the old one
Size is defined as (PIs, POs, ANDS, FF)"""
global npi, npo, nands, nff
result = ((npi < n_pis()) or (npo < n_pos()) or (nands < n_ands()) )
return result
def quick_verify(n):
"""Low resource version of final_verify n = 1 means to do an initial simplification first"""
abc('trm')
if n == 1:
simplify()
if n_latches == 0:
return check_sat()
abc('trm')
if is_sat():
return Sat_true
print 'After trimming: ',
print_circuit_stats()
#try_rpm()
set_globals()
t = max(1,.4*G_T)
print ' Running PDR for %d sec '%(t)
abc('&get;,pdr -vt=%f'%(t*.8))
status = get_status()
if not status == Unsat:
print 'PDR went to %d frames, '%n_bmc_frames(),
print RESULT[status]
return status #temporary
if status <= Unsat:
return status
N = bmc_depth()
c = max(G_C/10, 1000)
t = max(1,.4*G_T)
print ' RUNNING interpolation with %d conflicts, max %d sec and 100 frames'%(c,t)
#abc('int -v -F 100 -C %d -T %f'%(c,t))
abc(',imc -vt=%f '%t)
status = get_status()
if status <= Unsat:
print 'Interpolation went to %d frames, '%n_bmc_frames(),
print RESULT[status]
return status
L = n_latches()
I = n_real_inputs()
if ( ((I+L<200)&(N>100)) or (I+L<125) or L < 51 ): #heuristic that if bmc went deep, then reachability might also
t = max(1,.4*G_T)
cmd = 'reachx -t %d'%max(10,int(t))
print ' Running %s'%cmd
abc(cmd)
status = get_status()
if status <= Unsat:
print 'Reachability went to %d frames, '%n_bmc_frames()
print RESULT[status]
return status
print 'BDD reachability aborted'
simplify() #why is this here
if n_latches() == 0:
print 'Simplified to 0 FF'
return check_sat()
set_max_bmc(bmc_depth()) # doesn't do anything
print 'No success, max_depth = %d'%max_bmc
return Undecided_reduction
def get_status():
"""this simply translates the problem status encoding done by ABC (-1,0,1)=(undecided,SAT,UNSAT) into the status code used by our python code."""
status = prob_status() #interrogates ABC for the current status of the problem.
# 0 = SAT
if status == 1:
status = Unsat
if status == -1: #undecided
status = Undecided_no_reduction
return status
def try_rpm():
"""rpm is a cheap way of doing reparameterization and is an abstraction method, so may introduce false cex's.
It finds a minimum cut between the PIs and the main sequential logic and replaces this cut by free inputs.
A quick BMC is then done, and if no cex is found, we assume the abstraction is valid. Otherwise we revert back
to the original problem before rpm was tried."""
global x_factor
if n_ands() > 30000:
return
set_globals()
pis_before = n_pis()
abc('w %s_savetemp.aig'%f_name)
abc('rpm')
result = 0
if n_pis() < .5*pis_before:
bmc_before = bmc_depth()
#print 'running quick bmc to see if rpm is OK'
t = max(1,.1*G_T)
#abc('bmc3 -C %d, -T %f'%(.1*G_C, t))
abc('&get;,bmc -vt=%f'%t)
if is_sat(): #rpm made it sat by bmc test, so undo rpm
abc('r %s_savetemp.aig'%f_name)
else:
abc('trm')
print 'WARNING: rpm reduced PIs to %d. May make SAT.'%n_pis()
result = 1
else:
abc('r %s_savetemp.aig'%f_name)
return result
def final_verify():
"""This is the final method for verifying anything is nothing else has worked. It first tries BDD reachability
if the problem is small enough. If this aborts or if the problem is too large, then interpolation is called."""
global x_factor
set_globals()
## simplify()
## if n_latches() == 0:
## return check_sat()
## abc('trm')
#rpm_result = try_rpm()
set_globals()
N = bmc_depth()
L = n_latches()
I = n_real_inputs()
#try_induction(G_C)
c = max(G_C/5, 1000)
t = max(1,G_T)
print '\n***Running PDR for %d sec'%(t)
abc('&get;,pdr -vt=%f'%(t*.8))
status = get_status()
if status <= Unsat:
print 'PDR went to %d frames, '%n_bmc_frames(),
print RESULT[status]
return status
if ( ((I+L<250)&(N>100)) or (I+L<200) or (L<51) ): #heuristic that if bmc went deep, then reachability might also
t = max(1,1.5*G_T)
#cmd = 'reach -v -B 1000000 -F 10000 -T %f'%t
#cmd = 'reachx -e %d'%int(long(t))
#cmd = 'reachx -e %d -t %d'%(int(long(t)),max(10,int(t)))
cmd = 'reachx -t %d'%max(10,int(t))
print '\n***Running %s'%cmd
abc(cmd)
status = get_status()
if status <= Unsat:
print 'Reachability went to %d frames, '%n_bmc_frames(),
print RESULT[status]
return status
print 'BDD reachability aborted'
return status #temporary
#f = max(100, bmc_depth())
print '\n***RUNNING interpolation with %d conflicts, %d sec, max 100 frames'%(c,t)
#abc('int -v -F 100 -C %d -T %f'%(c,t))
abc(',imc -vt=%f '%t)
status = get_status()