@@ -337,10 +337,11 @@ enum BuildStrat {
337
337
Custom ( u64 , usize , usize ) ,
338
338
}
339
339
340
- /// An enum for the extraction strategies used to synthesize LUT networks.
341
- /// Only [ExtractStrat::Exact] uses ILP.
340
+ /// An enum for the optimization strategies used to synthesize LUT/cell networks.
342
341
#[ derive( Debug , Clone ) ]
343
- enum ExtractStrat {
342
+ enum OptStrat {
343
+ /// Extract the circuit by the syntax of the expression
344
+ AstSize ,
344
345
/// Extract the cirucit using exact cell areas.
345
346
Area ,
346
347
/// Extract maximum circuit depth (RAM bomb).
@@ -353,8 +354,15 @@ enum ExtractStrat {
353
354
CellCountRegWeighted ( usize , u64 ) ,
354
355
/// Disassemble into set of logic gates.
355
356
Disassemble ( HashSet < String > ) ,
356
- #[ cfg( feature = "exactness" ) ]
357
- /// Extract LUTs exactly using ILP with timeout in seconds.
357
+ }
358
+
359
+ /// An enum for the extraction strategies used to synthesize LUT/cell networks.
360
+ #[ derive( Debug , Clone ) ]
361
+ enum ExtractStrat {
362
+ /// Use greedy extraction algorithm.
363
+ Greedy ,
364
+ #[ allow( dead_code) ]
365
+ /// Use exact ILP extraction with timeout in seconds.
358
366
Exact ( u64 ) ,
359
367
}
360
368
@@ -366,7 +374,7 @@ pub const GATE_WHITELIST: [&str; 9] = [
366
374
"MUX" , "AND" , "OR" , "XOR" , "NOT" , "INV" , "REG" , "NAND" , "NOR" ,
367
375
] ;
368
376
369
- impl ExtractStrat {
377
+ impl OptStrat {
370
378
/// Create an extraction strategy from a comma-separated list of gates.
371
379
/// For example, `list` can be `"MUX,AND,NOT"`.
372
380
pub fn from_gate_set ( list : & str ) -> Result < Self , String > {
@@ -384,7 +392,7 @@ impl ExtractStrat {
384
392
) ) ;
385
393
}
386
394
}
387
- Ok ( ExtractStrat :: Disassemble ( gates) )
395
+ Ok ( OptStrat :: Disassemble ( gates) )
388
396
}
389
397
}
390
398
@@ -481,6 +489,9 @@ where
481
489
/// The rewrite rules used to simplify the expression.
482
490
rules : Vec < Rewrite < L , A > > ,
483
491
492
+ /// The optimization strategy to use.
493
+ opt_strat : OptStrat ,
494
+
484
495
/// The extraction strategy to use.
485
496
extract_strat : ExtractStrat ,
486
497
@@ -522,7 +533,8 @@ impl<L: Language, A: Analysis<L>> std::default::Default for SynthRequest<L, A> {
522
533
Self {
523
534
expr : RecExpr :: default ( ) ,
524
535
rules : Vec :: new ( ) ,
525
- extract_strat : ExtractStrat :: CellCount ( 6 ) ,
536
+ opt_strat : OptStrat :: CellCount ( 6 ) ,
537
+ extract_strat : ExtractStrat :: Greedy ,
526
538
build_strat : BuildStrat :: Custom ( 10 , 20_000 , 16 ) ,
527
539
no_canonicalize : false ,
528
540
assert_sat : false ,
@@ -543,6 +555,7 @@ impl<L: Language, A: Analysis<L> + std::clone::Clone> std::clone::Clone for Synt
543
555
Self {
544
556
expr : self . expr . clone ( ) ,
545
557
rules : self . rules . clone ( ) ,
558
+ opt_strat : self . opt_strat . clone ( ) ,
546
559
extract_strat : self . extract_strat . clone ( ) ,
547
560
build_strat : self . build_strat . clone ( ) ,
548
561
no_canonicalize : self . no_canonicalize ,
@@ -567,28 +580,40 @@ where
567
580
/// Request greedy extraction of cells/LUTs with at most `k` inputs.
568
581
pub fn with_k ( self , k : usize ) -> Self {
569
582
Self {
570
- extract_strat : ExtractStrat :: CellCount ( k) ,
583
+ opt_strat : OptStrat :: CellCount ( k) ,
584
+ extract_strat : ExtractStrat :: Greedy ,
571
585
..self
572
586
}
573
587
}
574
588
575
589
/// Request greedy extraction of cells/LUTs with at most `k` inputs and registers with weight `w`.
576
590
pub fn with_klut_regw ( self , k : usize , w : u64 ) -> Self {
577
591
Self {
578
- extract_strat : ExtractStrat :: CellCountRegWeighted ( k, w) ,
592
+ opt_strat : OptStrat :: CellCountRegWeighted ( k, w) ,
593
+ extract_strat : ExtractStrat :: Greedy ,
579
594
..self
580
595
}
581
596
}
582
597
583
598
/// Request greedy extraction using exact cell areas.
584
599
pub fn with_area ( self ) -> Self {
585
600
Self {
586
- extract_strat : ExtractStrat :: Area ,
601
+ opt_strat : OptStrat :: Area ,
602
+ extract_strat : ExtractStrat :: Greedy ,
603
+ ..self
604
+ }
605
+ }
606
+
607
+ /// Request greedy extraction by syntax complexity.
608
+ pub fn with_ast_size ( self ) -> Self {
609
+ Self {
610
+ opt_strat : OptStrat :: AstSize ,
611
+ extract_strat : ExtractStrat :: Greedy ,
587
612
..self
588
613
}
589
614
}
590
615
591
- /// Request exact LUT extraction using ILP with `timeout` in seconds.
616
+ /// Request exact extraction using ILP with `timeout` in seconds.
592
617
#[ cfg( feature = "exactness" ) ]
593
618
pub fn with_exactness ( self , timeout : u64 ) -> Self {
594
619
Self {
@@ -600,31 +625,35 @@ where
600
625
/// Extract based on minimum circuit depth.
601
626
pub fn with_min_depth ( self ) -> Self {
602
627
Self {
603
- extract_strat : ExtractStrat :: MinDepth ,
628
+ opt_strat : OptStrat :: MinDepth ,
629
+ extract_strat : ExtractStrat :: Greedy ,
604
630
..self
605
631
}
606
632
}
607
633
608
- /// Extract based on maximum circuit depth.
634
+ /// Extract based on maximum circuit depth. *Does not work with cycles in e-graph.*
609
635
pub fn with_max_depth ( self ) -> Self {
610
636
Self {
611
- extract_strat : ExtractStrat :: MaxDepth ,
637
+ opt_strat : OptStrat :: MaxDepth ,
638
+ extract_strat : ExtractStrat :: Greedy ,
612
639
..self
613
640
}
614
641
}
615
642
616
643
/// Extract by disassembling into basic logic gates. The exact list can be found at [GATE_WHITELIST].
617
644
pub fn with_disassembler ( self ) -> Self {
618
645
Self {
619
- extract_strat : ExtractStrat :: from_gate_set ( GATE_WHITELIST_STR ) . unwrap ( ) ,
646
+ opt_strat : OptStrat :: from_gate_set ( GATE_WHITELIST_STR ) . unwrap ( ) ,
647
+ extract_strat : ExtractStrat :: Greedy ,
620
648
..self
621
649
}
622
650
}
623
651
624
652
/// Extract by disassembling into logic gates in the `list`. Elements in the list must be matched against elements in [GATE_WHITELIST].
625
653
pub fn with_disassembly_into ( self , list : & str ) -> Result < Self , String > {
626
654
Ok ( Self {
627
- extract_strat : ExtractStrat :: from_gate_set ( list) ?,
655
+ opt_strat : OptStrat :: from_gate_set ( list) ?,
656
+ extract_strat : ExtractStrat :: Greedy ,
628
657
..self
629
658
} )
630
659
}
@@ -1006,24 +1035,45 @@ where
1006
1035
where
1007
1036
R : Report < L > ,
1008
1037
{
1009
- match self . extract_strat . to_owned ( ) {
1010
- ExtractStrat :: Area => self . greedy_extract_with ( L :: exact_area_cost_fn ( ) ) ,
1011
- ExtractStrat :: MinDepth => self . greedy_extract_with ( L :: depth_cost_fn ( ) ) ,
1012
- ExtractStrat :: MaxDepth => {
1038
+ match ( self . opt_strat . to_owned ( ) , self . extract_strat . to_owned ( ) ) {
1039
+ ( OptStrat :: AstSize , ExtractStrat :: Greedy ) => self . greedy_extract_with ( egg:: AstSize ) ,
1040
+ ( OptStrat :: Area , ExtractStrat :: Greedy ) => {
1041
+ self . greedy_extract_with ( L :: exact_area_cost_fn ( ) )
1042
+ }
1043
+ ( OptStrat :: MinDepth , ExtractStrat :: Greedy ) => {
1044
+ self . greedy_extract_with ( L :: depth_cost_fn ( ) )
1045
+ }
1046
+ ( OptStrat :: MaxDepth , ExtractStrat :: Greedy ) => {
1013
1047
eprintln ! ( "WARNING: Maximizing cost on e-graphs with cycles will crash." ) ;
1014
1048
self . greedy_extract_with ( NegativeCostFn :: new ( L :: depth_cost_fn ( ) ) )
1015
1049
}
1016
- ExtractStrat :: CellCount ( k) => self . greedy_extract_with ( L :: cell_cost_fn ( k) ) ,
1017
- ExtractStrat :: CellCountRegWeighted ( k, w) => {
1050
+ ( OptStrat :: CellCount ( k) , ExtractStrat :: Greedy ) => {
1051
+ self . greedy_extract_with ( L :: cell_cost_fn ( k) )
1052
+ }
1053
+ ( OptStrat :: CellCountRegWeighted ( k, w) , ExtractStrat :: Greedy ) => {
1018
1054
self . greedy_extract_with ( L :: cell_cost_with_reg_weight_fn ( k, w) )
1019
1055
}
1020
- ExtractStrat :: Disassemble ( set) => self . greedy_extract_with ( L :: filter_cost_fn ( set) ) ,
1056
+ ( OptStrat :: Disassemble ( set) , ExtractStrat :: Greedy ) => {
1057
+ self . greedy_extract_with ( L :: filter_cost_fn ( set) )
1058
+ }
1059
+ #[ cfg( feature = "exactness" ) ]
1060
+ ( OptStrat :: CellCountRegWeighted ( 6 , 1 ) , ExtractStrat :: Exact ( t) ) => {
1061
+ self . extract_with ( |egraph, root| {
1062
+ eprintln ! ( "INFO: ILP ON" ) ;
1063
+ let mut e = egg:: LpExtractor :: new ( egraph, egg:: AstSize ) ;
1064
+ L :: canonicalize_expr ( e. timeout ( t as f64 ) . solve ( root) )
1065
+ } )
1066
+ }
1021
1067
#[ cfg( feature = "exactness" ) ]
1022
- ExtractStrat :: Exact ( t) => self . extract_with ( |egraph, root| {
1068
+ ( OptStrat :: AstSize , ExtractStrat :: Exact ( t) ) => self . extract_with ( |egraph, root| {
1023
1069
eprintln ! ( "INFO: ILP ON" ) ;
1024
1070
let mut e = egg:: LpExtractor :: new ( egraph, egg:: AstSize ) ;
1025
1071
L :: canonicalize_expr ( e. timeout ( t as f64 ) . solve ( root) )
1026
1072
} ) ,
1073
+ _ => Err ( format ! (
1074
+ "{:?} optimization strategy is incomptabile with {:?} extraction." ,
1075
+ self . opt_strat, self . extract_strat
1076
+ ) ) ,
1027
1077
}
1028
1078
}
1029
1079
}
0 commit comments