@@ -59,10 +59,20 @@ impl Ssa {
59
59
60
60
for ( func_id, entries) in calls {
61
61
let function = self . functions [ & func_id] . clone ( ) ;
62
+ let function_num_instructions = function. num_instructions ( ) ;
62
63
for ( constants, _) in entries {
63
- let new_function_id = self . add_fn ( |func_id| {
64
- inline_constants_into_function ( & function, & constants, func_id)
65
- } ) ;
64
+ let Some ( new_function_id) = self . maybe_add_fn ( |func_id| {
65
+ let new_function =
66
+ inline_constants_into_function ( & function, & constants, func_id) ;
67
+ // No point in using the new function if it's not more optimal
68
+ if new_function. num_instructions ( ) < function_num_instructions {
69
+ Some ( new_function)
70
+ } else {
71
+ None
72
+ }
73
+ } ) else {
74
+ continue ;
75
+ } ;
66
76
let entry = new_functions. entry ( func_id) . or_default ( ) ;
67
77
entry. entry ( constants) . insert_entry ( new_function_id) ;
68
78
}
@@ -287,8 +297,8 @@ mod tests {
287
297
}
288
298
brillig(inline) fn foo f1 {
289
299
b0(v0: Field, v1: Field):
290
- v2 = add v0, v1
291
- return v2
300
+ v3 = add v0, Field 1
301
+ return v3
292
302
}
293
303
" ;
294
304
let ssa = Ssa :: from_str ( src) . unwrap ( ) ;
@@ -303,13 +313,12 @@ mod tests {
303
313
}
304
314
brillig(inline) fn foo f1 {
305
315
b0(v0: Field, v1: Field):
306
- v2 = add v0, v1
307
- return v2
316
+ v3 = add v0, Field 1
317
+ return v3
308
318
}
309
319
brillig(inline) fn foo f2 {
310
320
b0(v0: Field):
311
- v2 = add Field 1, v0
312
- return v2
321
+ return Field 2
313
322
}
314
323
" ;
315
324
let ssa = ssa. inline_constants_into_brillig_functions ( ) ;
@@ -363,6 +372,27 @@ mod tests {
363
372
assert_normalized_ssa_equals ( ssa, expected) ;
364
373
}
365
374
375
+ #[ test]
376
+ fn does_not_inline_if_inlined_function_does_not_have_less_instructions ( ) {
377
+ let src = "
378
+ acir(inline) fn main f0 {
379
+ b0(v0: Field):
380
+ v3 = call f1(Field 1, v0) -> Field
381
+ v4 = call f1(Field 1, v0) -> Field
382
+ v5 = add v3, v4
383
+ return v5
384
+ }
385
+ brillig(inline) fn foo f1 {
386
+ b0(v0: Field, v1: Field):
387
+ v2 = add v0, v1
388
+ return v2
389
+ }
390
+ " ;
391
+ let ssa = Ssa :: from_str ( src) . unwrap ( ) ;
392
+ let ssa = ssa. inline_constants_into_brillig_functions ( ) ;
393
+ assert_normalized_ssa_equals ( ssa, src) ;
394
+ }
395
+
366
396
#[ test]
367
397
fn does_not_inline_brillig_call_into_brillig_function ( ) {
368
398
let src = "
0 commit comments