1
1
use std:: collections:: hash_map:: RandomState ;
2
2
use std:: collections:: HashSet ;
3
3
4
+ use hugr_core:: ops:: handle:: NodeHandle ;
5
+ use hugr_core:: ops:: Const ;
6
+ use hugr_core:: std_extensions:: arithmetic:: { int_ops, int_types} ;
4
7
use itertools:: Itertools ;
5
8
use lazy_static:: lazy_static;
6
9
use rstest:: rstest;
7
10
8
11
use hugr_core:: builder:: {
9
12
endo_sig, inout_sig, Container , DFGBuilder , Dataflow , DataflowHugr , DataflowSubContainer ,
10
- SubContainer ,
13
+ HugrBuilder , ModuleBuilder , SubContainer ,
11
14
} ;
12
15
use hugr_core:: extension:: prelude:: {
13
16
bool_t, const_ok, error_type, string_type, sum_with_error, ConstError , ConstString , MakeTuple ,
@@ -25,7 +28,7 @@ use hugr_core::std_extensions::arithmetic::{
25
28
int_types:: { ConstInt , INT_TYPES } ,
26
29
} ;
27
30
use hugr_core:: std_extensions:: logic:: LogicOp ;
28
- use hugr_core:: types:: { Signature , SumType , Type , TypeRow , TypeRowRV } ;
31
+ use hugr_core:: types:: { Signature , SumType , Type , TypeBound , TypeRow , TypeRowRV } ;
29
32
use hugr_core:: { type_row, Hugr , HugrView , IncomingPort , Node } ;
30
33
31
34
use crate :: dataflow:: { partial_from_const, DFContext , PartialValue } ;
@@ -1580,3 +1583,50 @@ fn test_cfg(
1580
1583
assert_eq ! ( output_src, nested) ;
1581
1584
}
1582
1585
}
1586
+
1587
+ #[ test]
1588
+ fn test_module ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
1589
+ let mut mb = ModuleBuilder :: new ( ) ;
1590
+ // Define a top-level constant, (only) the second of which can be removed
1591
+ let c7 = mb. add_constant ( Value :: from ( ConstInt :: new_u ( 5 , 7 ) ?) ) ;
1592
+ let c17 = mb. add_constant ( Value :: from ( ConstInt :: new_u ( 5 , 17 ) ?) ) ;
1593
+ let ad1 = mb. add_alias_declare ( "unused" , TypeBound :: Any ) ?;
1594
+ let ad2 = mb. add_alias_def ( "unused2" , INT_TYPES [ 3 ] . clone ( ) ) ?;
1595
+ let mut main = mb. define_function (
1596
+ "main" ,
1597
+ Signature :: new ( type_row ! [ ] , vec ! [ INT_TYPES [ 5 ] . clone( ) ; 2 ] )
1598
+ . with_extension_delta ( int_types:: EXTENSION_ID )
1599
+ . with_extension_delta ( int_ops:: EXTENSION_ID ) ,
1600
+ ) ?;
1601
+ let lc7 = main. load_const ( & c7) ;
1602
+ let lc17 = main. load_const ( & c17) ;
1603
+ let [ add] = main
1604
+ . add_dataflow_op ( IntOpDef :: iadd. with_log_width ( 5 ) , [ lc7, lc17] ) ?
1605
+ . outputs_arr ( ) ;
1606
+ let main = main. finish_with_outputs ( [ lc7, add] ) ?;
1607
+ let mut hugr = mb. finish_hugr ( ) ?;
1608
+ constant_fold_pass ( & mut hugr) ;
1609
+ assert ! ( hugr. get_optype( hugr. root( ) ) . is_module( ) ) ;
1610
+ assert_eq ! (
1611
+ hugr. children( hugr. root( ) ) . collect_vec( ) ,
1612
+ [ c7. node( ) , ad1. node( ) , ad2. node( ) , main. node( ) ]
1613
+ ) ;
1614
+ let tags = hugr
1615
+ . children ( main. node ( ) )
1616
+ . map ( |n| hugr. get_optype ( n) . tag ( ) )
1617
+ . collect_vec ( ) ;
1618
+ for ( tag, expected_count) in [
1619
+ ( OpTag :: Input , 1 ) ,
1620
+ ( OpTag :: Output , 1 ) ,
1621
+ ( OpTag :: Const , 1 ) ,
1622
+ ( OpTag :: LoadConst , 2 ) ,
1623
+ ] {
1624
+ assert_eq ! ( tags. iter( ) . filter( |t| * * t == tag) . count( ) , expected_count) ;
1625
+ }
1626
+ assert_eq ! (
1627
+ hugr. children( main. node( ) )
1628
+ . find_map( |n| hugr. get_optype( n) . as_const( ) ) ,
1629
+ Some ( & Const :: new( ConstInt :: new_u( 5 , 24 ) . unwrap( ) . into( ) ) )
1630
+ ) ;
1631
+ Ok ( ( ) )
1632
+ }
0 commit comments