@@ -21,6 +21,9 @@ use extension::{
21
21
qsystem:: { LowerTk2Error , LowerTket2ToQSystemPass , QSystemOp } ,
22
22
} ;
23
23
24
+ #[ cfg( feature = "llvm" ) ]
25
+ use hugr:: llvm:: utils:: inline_constant_functions;
26
+
24
27
#[ cfg( feature = "cli" ) ]
25
28
pub mod cli;
26
29
pub mod extension;
@@ -66,6 +69,9 @@ pub enum QSystemPassError<N = Node> {
66
69
ConstantFoldError ( ConstFoldError ) ,
67
70
/// An error from the component [LinearizeArrayPass] pass.
68
71
LinearizeArrayError ( ReplaceTypesError ) ,
72
+ #[ cfg( feature = "llvm" ) ]
73
+ /// An error from the component [inline_constant_functions()] pass.
74
+ InlineConstantFunctionsError ( anyhow:: Error ) ,
69
75
/// An error when running [RemoveDeadFuncsPass] after the monomorphisation
70
76
/// pass.
71
77
///
@@ -112,6 +118,12 @@ impl QSystemPass {
112
118
self . replace_bools ( ) . run ( hugr) ?;
113
119
}
114
120
self . linearize_arrays ( ) . run ( hugr) ?;
121
+ #[ cfg( feature = "llvm" ) ]
122
+ {
123
+ // TODO: Remove "llvm" feature gate once `inline_constant_functions` is moved to
124
+ // `hugr-passes`. See https://github.com/CQCL/hugr/issues/2419
125
+ inline_constant_functions ( hugr) ?;
126
+ }
115
127
if self . constant_fold {
116
128
self . constant_fold ( ) . run ( hugr) ?;
117
129
}
@@ -326,4 +338,49 @@ mod test {
326
338
assert ! ( get_pos( call_node) < get_pos( n) ) ;
327
339
}
328
340
}
341
+
342
+ #[ cfg( feature = "llvm" ) ]
343
+ #[ test]
344
+ fn const_function ( ) {
345
+ use hugr:: builder:: DataflowHugr ;
346
+ use hugr:: builder:: { DFGBuilder , ModuleBuilder } ;
347
+ use hugr:: ops:: { CallIndirect , Value } ;
348
+
349
+ let qb_sig: Signature = Signature :: new_endo ( qb_t ( ) ) ;
350
+ let mut hugr = {
351
+ let mut builder = ModuleBuilder :: new ( ) ;
352
+ let val = Value :: function ( {
353
+ let builder = DFGBuilder :: new ( Signature :: new_endo ( qb_t ( ) ) ) . unwrap ( ) ;
354
+ let [ r] = builder. input_wires_arr ( ) ;
355
+ builder. finish_hugr_with_outputs ( [ r] ) . unwrap ( )
356
+ } )
357
+ . unwrap ( ) ;
358
+ let const_node = builder. add_constant ( val) ;
359
+ {
360
+ let mut builder = builder. define_function ( "main" , qb_sig. clone ( ) ) . unwrap ( ) ;
361
+ let [ i] = builder. input_wires_arr ( ) ;
362
+ let fun = builder. load_const ( & const_node) ;
363
+ let [ r] = builder
364
+ . add_dataflow_op (
365
+ CallIndirect {
366
+ signature : qb_sig. clone ( ) ,
367
+ } ,
368
+ [ fun, i] ,
369
+ )
370
+ . unwrap ( )
371
+ . outputs_arr ( ) ;
372
+ builder. finish_with_outputs ( [ r] ) . unwrap ( ) ;
373
+ } ;
374
+ builder. finish_hugr ( ) . unwrap ( )
375
+ } ;
376
+
377
+ QSystemPass :: default ( ) . run ( & mut hugr) . unwrap ( ) ;
378
+
379
+ // QSystemPass should have removed the const function
380
+ for n in hugr. descendants ( hugr. module_root ( ) ) {
381
+ if hugr. get_optype ( n) . as_const ( ) . is_some ( ) {
382
+ panic ! ( "Const function is still there!" ) ;
383
+ }
384
+ }
385
+ }
329
386
}
0 commit comments