-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathconstant_folding.rs
1631 lines (1464 loc) · 60.3 KB
/
constant_folding.rs
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
//! The goal of the constant folding optimization pass is to propagate any constants forwards into
//! later [`Instruction`]s to maximize the impact of [compile-time simplifications][Instruction::simplify()].
//!
//! The pass works as follows:
//! - Re-insert each instruction in order to apply the instruction simplification performed
//! by the [`DataFlowGraph`] automatically as new instructions are pushed.
//! - Check whether any input values have been constrained to be equal to a value of a simpler form
//! by a [constrain instruction][Instruction::Constrain]. If so, replace the input value with the simpler form.
//! - Check whether the instruction [can_be_deduplicated][Instruction::can_be_deduplicated()]
//! by duplicate instruction earlier in the same block.
//!
//! These operations are done in parallel so that they can each benefit from each other
//! without the need for multiple passes.
//!
//! Other passes perform a certain amount of constant folding automatically as they insert instructions
//! into the [`DataFlowGraph`] but this pass can become needed if [`DataFlowGraph::set_value`] or
//! [`DataFlowGraph::set_value_from_id`] are used on a value which enables instructions dependent on the value to
//! now be simplified.
//!
//! This is the only pass which removes duplicated pure [`Instruction`]s however and so is needed when
//! different blocks are merged, i.e. after the [`flatten_cfg`][super::flatten_cfg] pass.
use std::collections::{BTreeMap, HashSet, VecDeque};
use acvm::{
acir::AcirField,
brillig_vm::{MemoryValue, VMStatus, VM},
FieldElement,
};
use bn254_blackbox_solver::Bn254BlackBoxSolver;
use im::Vector;
use iter_extended::vecmap;
use crate::{
brillig::{
brillig_gen::gen_brillig_for,
brillig_ir::{artifact::BrilligParameter, brillig_variable::get_bit_size_from_ssa_type},
Brillig,
},
ssa::{
ir::{
basic_block::BasicBlockId,
dfg::{DataFlowGraph, InsertInstructionResult},
dom::DominatorTree,
function::{Function, FunctionId, RuntimeType},
instruction::{Instruction, InstructionId},
types::{NumericType, Type},
value::{Value, ValueId},
},
ssa_gen::Ssa,
},
};
use fxhash::FxHashMap as HashMap;
impl Ssa {
/// Performs constant folding on each instruction.
///
/// It will not look at constraints to inform simplifications
/// based on the stated equivalence of two instructions.
///
/// See [`constant_folding`][self] module for more information.
#[tracing::instrument(level = "trace", skip(self))]
pub(crate) fn fold_constants(mut self) -> Ssa {
for function in self.functions.values_mut() {
function.constant_fold(false, None);
}
self
}
/// Performs constant folding on each instruction.
///
/// Also uses constraint information to inform more optimizations.
///
/// See [`constant_folding`][self] module for more information.
#[tracing::instrument(level = "trace", skip(self))]
pub(crate) fn fold_constants_using_constraints(mut self) -> Ssa {
for function in self.functions.values_mut() {
function.constant_fold(true, None);
}
self
}
/// Performs constant folding on each instruction while also replacing calls to brillig functions
/// with all constant arguments by trying to evaluate those calls.
#[tracing::instrument(level = "trace", skip(self, brillig))]
pub(crate) fn fold_constants_with_brillig(mut self, brillig: &Brillig) -> Ssa {
// Collect all brillig functions so that later we can find them when processing a call instruction
let mut brillig_functions: BTreeMap<FunctionId, Function> = BTreeMap::new();
for (func_id, func) in &self.functions {
if let RuntimeType::Brillig(..) = func.runtime() {
let cloned_function = Function::clone_with_id(*func_id, func);
brillig_functions.insert(*func_id, cloned_function);
};
}
let brillig_info = Some(BrilligInfo { brillig, brillig_functions: &brillig_functions });
for function in self.functions.values_mut() {
function.constant_fold(false, brillig_info);
}
// It could happen that we inlined all calls to a given brillig function.
// In that case it's unused so we can remove it. This is what we check next.
self.remove_unused_brillig_functions(brillig_functions)
}
fn remove_unused_brillig_functions(
mut self,
mut brillig_functions: BTreeMap<FunctionId, Function>,
) -> Ssa {
// Remove from the above map functions that are called
for function in self.functions.values() {
for block_id in function.reachable_blocks() {
for instruction_id in function.dfg[block_id].instructions() {
let instruction = &function.dfg[*instruction_id];
let Instruction::Call { func: func_id, arguments: _ } = instruction else {
continue;
};
let func_value = &function.dfg[*func_id];
let Value::Function(func_id) = func_value else { continue };
brillig_functions.remove(func_id);
}
}
}
// The ones that remain are never called: let's remove them.
for (func_id, func) in &brillig_functions {
// We never want to remove the main function (it could be `unconstrained` or it
// could have been turned into brillig if `--force-brillig` was given).
// We also don't want to remove entry points.
let runtime = func.runtime();
if self.main_id == *func_id
|| (runtime.is_entry_point() && matches!(runtime, RuntimeType::Acir(_)))
{
continue;
}
self.functions.remove(func_id);
}
self
}
}
impl Function {
/// The structure of this pass is simple:
/// Go through each block and re-insert all instructions.
pub(crate) fn constant_fold(
&mut self,
use_constraint_info: bool,
brillig_info: Option<BrilligInfo>,
) {
let mut context = Context::new(use_constraint_info, brillig_info);
let mut dom = DominatorTree::with_function(self);
context.block_queue.push_back(self.entry_block());
while let Some(block) = context.block_queue.pop_front() {
if context.visited_blocks.contains(&block) {
continue;
}
context.visited_blocks.insert(block);
context.fold_constants_in_block(self, &mut dom, block);
}
}
}
struct Context<'a> {
use_constraint_info: bool,
brillig_info: Option<BrilligInfo<'a>>,
/// Maps pre-folded ValueIds to the new ValueIds obtained by re-inserting the instruction.
visited_blocks: HashSet<BasicBlockId>,
block_queue: VecDeque<BasicBlockId>,
/// Contains sets of values which are constrained to be equivalent to each other.
///
/// The mapping's structure is `side_effects_enabled_var => (constrained_value => simplified_value)`.
///
/// We partition the maps of constrained values according to the side-effects flag at the point
/// at which the values are constrained. This prevents constraints which are only sometimes enforced
/// being used to modify the rest of the program.
constraint_simplification_mappings: ConstraintSimplificationCache,
// Cache of instructions without any side-effects along with their outputs.
cached_instruction_results: InstructionResultCache,
}
#[derive(Copy, Clone)]
pub(crate) struct BrilligInfo<'a> {
brillig: &'a Brillig,
brillig_functions: &'a BTreeMap<FunctionId, Function>,
}
/// Records a simplified equivalents of an [`Instruction`] in the blocks
/// where the constraint that advised the simplification has been encountered.
///
/// For more information see [`ConstraintSimplificationCache`].
#[derive(Default)]
struct SimplificationCache {
/// Simplified expressions where we found them.
///
/// It will always have at least one value because `add` is called
/// after the default is constructed.
simplifications: HashMap<BasicBlockId, ValueId>,
}
impl SimplificationCache {
/// Called with a newly encountered simplification.
fn add(&mut self, dfg: &DataFlowGraph, simple: ValueId, block: BasicBlockId) {
self.simplifications
.entry(block)
.and_modify(|existing| {
// `SimplificationCache` may already hold a simplification in this block
// so we check whether `simple` is a better simplification than the current one.
if let Some((_, simpler)) = simplify(dfg, *existing, simple) {
*existing = simpler;
};
})
.or_insert(simple);
}
/// Try to find a simplification in a visible block.
fn get(&self, block: BasicBlockId, dom: &DominatorTree) -> Option<ValueId> {
// Deterministically walk up the dominator chain until we encounter a block that contains a simplification.
dom.find_map_dominator(block, |b| self.simplifications.get(&b).cloned())
}
}
/// HashMap from `(side_effects_enabled_var, Instruction)` to a simplified expression that it can
/// be replaced with based on constraints that testify to their equivalence, stored together
/// with the set of blocks at which this constraint has been observed.
///
/// Only blocks dominated by one in the cache should have access to this information, otherwise
/// we create a sort of time paradox where we replace an instruction with a constant we believe
/// it _should_ equal to, without ever actually producing and asserting the value.
type ConstraintSimplificationCache = HashMap<ValueId, HashMap<ValueId, SimplificationCache>>;
/// HashMap from `(Instruction, side_effects_enabled_var)` to the results of the instruction.
/// Stored as a two-level map to avoid cloning Instructions during the `.get` call.
///
/// The `side_effects_enabled_var` is optional because we only use them when `Instruction::requires_acir_gen_predicate`
/// is true _and_ the constraint information is also taken into account.
///
/// In addition to each result, the original BasicBlockId is stored as well. This allows us
/// to deduplicate instructions across blocks as long as the new block dominates the original.
type InstructionResultCache = HashMap<Instruction, HashMap<Option<ValueId>, ResultCache>>;
/// Records the results of all duplicate [`Instruction`]s along with the blocks in which they sit.
///
/// For more information see [`InstructionResultCache`].
#[derive(Default)]
struct ResultCache {
result: Option<(BasicBlockId, Vec<ValueId>)>,
}
impl<'brillig> Context<'brillig> {
fn new(use_constraint_info: bool, brillig_info: Option<BrilligInfo<'brillig>>) -> Self {
Self {
use_constraint_info,
brillig_info,
visited_blocks: Default::default(),
block_queue: Default::default(),
constraint_simplification_mappings: Default::default(),
cached_instruction_results: Default::default(),
}
}
fn fold_constants_in_block(
&mut self,
function: &mut Function,
dom: &mut DominatorTree,
block: BasicBlockId,
) {
let instructions = function.dfg[block].take_instructions();
// Default side effect condition variable with an enabled state.
let mut side_effects_enabled_var =
function.dfg.make_constant(FieldElement::one(), NumericType::bool());
for instruction_id in instructions {
self.fold_constants_into_instruction(
function,
dom,
block,
instruction_id,
&mut side_effects_enabled_var,
);
}
self.block_queue.extend(function.dfg[block].successors());
}
fn fold_constants_into_instruction(
&mut self,
function: &mut Function,
dom: &mut DominatorTree,
mut block: BasicBlockId,
id: InstructionId,
side_effects_enabled_var: &mut ValueId,
) {
let constraint_simplification_mapping = self.get_constraint_map(*side_effects_enabled_var);
let dfg = &mut function.dfg;
let instruction =
Self::resolve_instruction(id, block, dfg, dom, constraint_simplification_mapping);
let old_results = dfg.instruction_results(id).to_vec();
// If a copy of this instruction exists earlier in the block, then reuse the previous results.
if let Some(cache_result) =
self.get_cached(dfg, dom, &instruction, *side_effects_enabled_var, block)
{
match cache_result {
CacheResult::Cached(cached) => {
// We track whether we may mutate MakeArray instructions before we deduplicate
// them but we still need to issue an extra inc_rc in case they're mutated afterward.
if matches!(instruction, Instruction::MakeArray { .. }) {
let value = *cached.last().unwrap();
let inc_rc = Instruction::IncrementRc { value };
let call_stack = dfg.get_call_stack(id);
dfg.insert_instruction_and_results(inc_rc, block, None, call_stack);
}
Self::replace_result_ids(dfg, &old_results, cached);
return;
}
CacheResult::NeedToHoistToCommonBlock(dominator) => {
// Just change the block to insert in the common dominator instead.
// This will only move the current instance of the instruction right now.
// When constant folding is run a second time later on, it'll catch
// that the previous instance can be deduplicated to this instance.
block = dominator;
}
}
};
// First try to inline a call to a brillig function with all constant arguments.
let new_results = Self::try_inline_brillig_call_with_all_constants(
&instruction,
&old_results,
block,
dfg,
self.brillig_info,
)
// Otherwise, try inserting the instruction again to apply any optimizations using the newly resolved inputs.
.unwrap_or_else(|| {
Self::push_instruction(id, instruction.clone(), &old_results, block, dfg)
});
Self::replace_result_ids(dfg, &old_results, &new_results);
self.cache_instruction(
instruction.clone(),
new_results,
function,
*side_effects_enabled_var,
block,
);
// If we just inserted an `Instruction::EnableSideEffectsIf`, we need to update `side_effects_enabled_var`
// so that we use the correct set of constrained values in future.
if let Instruction::EnableSideEffectsIf { condition } = instruction {
*side_effects_enabled_var = condition;
};
}
/// Fetches an [`Instruction`] by its [`InstructionId`] and fully resolves its inputs.
fn resolve_instruction(
instruction_id: InstructionId,
block: BasicBlockId,
dfg: &DataFlowGraph,
dom: &mut DominatorTree,
constraint_simplification_mapping: &HashMap<ValueId, SimplificationCache>,
) -> Instruction {
let instruction = dfg[instruction_id].clone();
// Alternate between resolving `value_id` in the `dfg` and checking to see if the resolved value
// has been constrained to be equal to some simpler value in the current block.
//
// This allows us to reach a stable final `ValueId` for each instruction input as we add more
// constraints to the cache.
fn resolve_cache(
block: BasicBlockId,
dfg: &DataFlowGraph,
dom: &mut DominatorTree,
cache: &HashMap<ValueId, SimplificationCache>,
value_id: ValueId,
) -> ValueId {
let resolved_id = dfg.resolve(value_id);
match cache.get(&resolved_id) {
Some(simplification_cache) => {
if let Some(simplified) = simplification_cache.get(block, dom) {
resolve_cache(block, dfg, dom, cache, simplified)
} else {
resolved_id
}
}
None => resolved_id,
}
}
// Resolve any inputs to ensure that we're comparing like-for-like instructions.
instruction.map_values(|value_id| {
resolve_cache(block, dfg, dom, constraint_simplification_mapping, value_id)
})
}
/// Pushes a new [`Instruction`] into the [`DataFlowGraph`] which applies any optimizations
/// based on newly resolved values for its inputs.
///
/// This may result in the [`Instruction`] being optimized away or replaced with a constant value.
fn push_instruction(
id: InstructionId,
instruction: Instruction,
old_results: &[ValueId],
block: BasicBlockId,
dfg: &mut DataFlowGraph,
) -> Vec<ValueId> {
let ctrl_typevars = instruction
.requires_ctrl_typevars()
.then(|| vecmap(old_results, |result| dfg.type_of_value(*result)));
let call_stack = dfg.get_call_stack(id);
let new_results =
match dfg.insert_instruction_and_results(instruction, block, ctrl_typevars, call_stack)
{
InsertInstructionResult::SimplifiedTo(new_result) => vec![new_result],
InsertInstructionResult::SimplifiedToMultiple(new_results) => new_results,
InsertInstructionResult::Results(_, new_results) => new_results.to_vec(),
InsertInstructionResult::InstructionRemoved => vec![],
};
// Optimizations while inserting the instruction should not change the number of results.
assert_eq!(old_results.len(), new_results.len());
new_results
}
fn cache_instruction(
&mut self,
instruction: Instruction,
instruction_results: Vec<ValueId>,
function: &Function,
side_effects_enabled_var: ValueId,
block: BasicBlockId,
) {
if self.use_constraint_info {
// If the instruction was a constraint, then create a link between the two `ValueId`s
// to map from the more complex to the simpler value.
if let Instruction::Constrain(lhs, rhs, _) = instruction {
// These `ValueId`s should be fully resolved now.
if let Some((complex, simple)) = simplify(&function.dfg, lhs, rhs) {
self.get_constraint_map(side_effects_enabled_var)
.entry(complex)
.or_default()
.add(&function.dfg, simple, block);
}
}
}
// If we have an array get whose value is from an array set on the same array at the same index,
// we can simplify that array get to the value of the previous array set.
//
// For example:
// v3 = array_set v0, index v1, value v2
// v4 = array_get v3, index v1 -> Field
//
// We know that `v4` can be simplified to `v2`.
// Thus, even if the index is dynamic (meaning the array get would have side effects),
// we can simplify the operation when we take into account the predicate.
if let Instruction::ArraySet { index, value, .. } = &instruction {
let use_predicate =
self.use_constraint_info && instruction.requires_acir_gen_predicate(&function.dfg);
let predicate = use_predicate.then_some(side_effects_enabled_var);
let array_get = Instruction::ArrayGet { array: instruction_results[0], index: *index };
self.cached_instruction_results
.entry(array_get)
.or_default()
.entry(predicate)
.or_default()
.cache(block, vec![*value]);
}
self.remove_possibly_mutated_cached_make_arrays(&instruction, function);
// If the instruction doesn't have side-effects and if it won't interact with enable_side_effects during acir_gen,
// we cache the results so we can reuse them if the same instruction appears again later in the block.
// Others have side effects representing failure, which are implicit in the ACIR code and can also be deduplicated.
let can_be_deduplicated =
instruction.can_be_deduplicated(function, self.use_constraint_info);
// We also allow deduplicating MakeArray instructions that we have tracked which haven't
// been mutated.
if can_be_deduplicated || matches!(instruction, Instruction::MakeArray { .. }) {
let use_predicate =
self.use_constraint_info && instruction.requires_acir_gen_predicate(&function.dfg);
let predicate = use_predicate.then_some(side_effects_enabled_var);
self.cached_instruction_results
.entry(instruction)
.or_default()
.entry(predicate)
.or_default()
.cache(block, instruction_results);
}
}
/// Get the simplification mapping from complex to simpler instructions,
/// which all depend on the same side effect condition variable.
fn get_constraint_map(
&mut self,
side_effects_enabled_var: ValueId,
) -> &mut HashMap<ValueId, SimplificationCache> {
self.constraint_simplification_mappings.entry(side_effects_enabled_var).or_default()
}
/// Replaces a set of [`ValueId`]s inside the [`DataFlowGraph`] with another.
fn replace_result_ids(
dfg: &mut DataFlowGraph,
old_results: &[ValueId],
new_results: &[ValueId],
) {
for (old_result, new_result) in old_results.iter().zip(new_results) {
dfg.set_value_from_id(*old_result, *new_result);
}
}
/// Get a cached result if it can be used in this context.
fn get_cached(
&self,
dfg: &DataFlowGraph,
dom: &mut DominatorTree,
instruction: &Instruction,
side_effects_enabled_var: ValueId,
block: BasicBlockId,
) -> Option<CacheResult> {
let results_for_instruction = self.cached_instruction_results.get(instruction)?;
let predicate = self.use_constraint_info && instruction.requires_acir_gen_predicate(dfg);
let predicate = predicate.then_some(side_effects_enabled_var);
results_for_instruction.get(&predicate)?.get(block, dom, instruction.has_side_effects(dfg))
}
/// Checks if the given instruction is a call to a brillig function with all constant arguments.
/// If so, we can try to evaluate that function and replace the results with the evaluation results.
fn try_inline_brillig_call_with_all_constants(
instruction: &Instruction,
old_results: &[ValueId],
block: BasicBlockId,
dfg: &mut DataFlowGraph,
brillig_info: Option<BrilligInfo>,
) -> Option<Vec<ValueId>> {
let evaluation_result = Self::evaluate_const_brillig_call(
instruction,
brillig_info?.brillig,
brillig_info?.brillig_functions,
dfg,
);
match evaluation_result {
EvaluationResult::NotABrilligCall | EvaluationResult::CannotEvaluate(_) => None,
EvaluationResult::Evaluated(memory_values) => {
let mut memory_index = 0;
let new_results = vecmap(old_results, |old_result| {
let typ = dfg.type_of_value(*old_result);
Self::new_value_for_type_and_memory_values(
typ,
block,
&memory_values,
&mut memory_index,
dfg,
)
});
Some(new_results)
}
}
}
/// Tries to evaluate an instruction if it's a call that points to a brillig function,
/// and all its arguments are constant.
/// We do this by directly executing the function with a brillig VM.
fn evaluate_const_brillig_call(
instruction: &Instruction,
brillig: &Brillig,
brillig_functions: &BTreeMap<FunctionId, Function>,
dfg: &mut DataFlowGraph,
) -> EvaluationResult {
let Instruction::Call { func: func_id, arguments } = instruction else {
return EvaluationResult::NotABrilligCall;
};
let func_value = &dfg[*func_id];
let Value::Function(func_id) = func_value else {
return EvaluationResult::NotABrilligCall;
};
let Some(func) = brillig_functions.get(func_id) else {
return EvaluationResult::NotABrilligCall;
};
if !arguments.iter().all(|argument| dfg.is_constant(*argument)) {
return EvaluationResult::CannotEvaluate(*func_id);
}
let mut brillig_arguments = Vec::new();
for argument in arguments {
let typ = dfg.type_of_value(*argument);
let Some(parameter) = type_to_brillig_parameter(&typ) else {
return EvaluationResult::CannotEvaluate(*func_id);
};
brillig_arguments.push(parameter);
}
// Check that return value types are supported by brillig
for return_id in func.returns().iter() {
let typ = func.dfg.type_of_value(*return_id);
if type_to_brillig_parameter(&typ).is_none() {
return EvaluationResult::CannotEvaluate(*func_id);
}
}
let Ok(generated_brillig) = gen_brillig_for(func, brillig_arguments, brillig) else {
return EvaluationResult::CannotEvaluate(*func_id);
};
let mut calldata = Vec::new();
for argument in arguments {
value_id_to_calldata(*argument, dfg, &mut calldata);
}
let bytecode = &generated_brillig.byte_code;
let foreign_call_results = Vec::new();
let black_box_solver = Bn254BlackBoxSolver;
let profiling_active = false;
let mut vm =
VM::new(calldata, bytecode, foreign_call_results, &black_box_solver, profiling_active);
let vm_status: VMStatus<_> = vm.process_opcodes();
let VMStatus::Finished { return_data_offset, return_data_size } = vm_status else {
return EvaluationResult::CannotEvaluate(*func_id);
};
let memory =
vm.get_memory()[return_data_offset..(return_data_offset + return_data_size)].to_vec();
EvaluationResult::Evaluated(memory)
}
/// Creates a new value inside this function by reading it from `memory_values` starting at
/// `memory_index` depending on the given Type: if it's an array multiple values will be read
/// and a new `make_array` instruction will be created.
fn new_value_for_type_and_memory_values(
typ: Type,
block_id: BasicBlockId,
memory_values: &[MemoryValue<FieldElement>],
memory_index: &mut usize,
dfg: &mut DataFlowGraph,
) -> ValueId {
match typ {
Type::Numeric(typ) => {
let memory = memory_values[*memory_index];
*memory_index += 1;
let field_value = match memory {
MemoryValue::Field(field_value) => field_value,
MemoryValue::Integer(u128_value, _) => u128_value.into(),
};
dfg.make_constant(field_value, typ)
}
Type::Array(types, length) => {
let mut new_array_values = Vector::new();
for _ in 0..length {
for typ in types.iter() {
let new_value = Self::new_value_for_type_and_memory_values(
typ.clone(),
block_id,
memory_values,
memory_index,
dfg,
);
new_array_values.push_back(new_value);
}
}
let instruction = Instruction::MakeArray {
elements: new_array_values,
typ: Type::Array(types, length),
};
let instruction_id = dfg.make_instruction(instruction, None);
dfg[block_id].instructions_mut().push(instruction_id);
*dfg.instruction_results(instruction_id).first().unwrap()
}
Type::Reference(_) => {
panic!("Unexpected reference type in brillig function result")
}
Type::Slice(_) => {
panic!("Unexpected slice type in brillig function result")
}
Type::Function => {
panic!("Unexpected function type in brillig function result")
}
}
}
fn remove_possibly_mutated_cached_make_arrays(
&mut self,
instruction: &Instruction,
function: &Function,
) {
use Instruction::{ArraySet, Store};
// Should we consider calls to slice_push_back and similar to be mutating operations as well?
if let Store { value: array, .. } | ArraySet { array, .. } = instruction {
let instruction = match &function.dfg[*array] {
Value::Instruction { instruction, .. } => &function.dfg[*instruction],
_ => return,
};
if matches!(instruction, Instruction::MakeArray { .. }) {
self.cached_instruction_results.remove(instruction);
}
}
}
}
impl ResultCache {
/// Records that an `Instruction` in block `block` produced the result values `results`.
fn cache(&mut self, block: BasicBlockId, results: Vec<ValueId>) {
if self.result.is_none() {
self.result = Some((block, results));
}
}
/// Returns a set of [`ValueId`]s produced from a copy of this [`Instruction`] which sits
/// within a block which dominates `block`.
///
/// We require that the cached instruction's block dominates `block` in order to avoid
/// cycles causing issues (e.g. two instructions being replaced with the results of each other
/// such that neither instruction exists anymore.)
fn get(
&self,
block: BasicBlockId,
dom: &mut DominatorTree,
has_side_effects: bool,
) -> Option<CacheResult> {
self.result.as_ref().and_then(|(origin_block, results)| {
if dom.dominates(*origin_block, block) {
Some(CacheResult::Cached(results))
} else if !has_side_effects {
// Insert a copy of this instruction in the common dominator
let dominator = dom.common_dominator(*origin_block, block);
Some(CacheResult::NeedToHoistToCommonBlock(dominator))
} else {
None
}
})
}
}
enum CacheResult<'a> {
Cached(&'a [ValueId]),
NeedToHoistToCommonBlock(BasicBlockId),
}
/// Result of trying to evaluate an instruction (any instruction) in this pass.
enum EvaluationResult {
/// Nothing was done because the instruction wasn't a call to a brillig function,
/// or some arguments to it were not constants.
NotABrilligCall,
/// The instruction was a call to a brillig function, but we couldn't evaluate it.
/// This can occur in the situation where the brillig function reaches a "trap" or a foreign call opcode.
CannotEvaluate(FunctionId),
/// The instruction was a call to a brillig function and we were able to evaluate it,
/// returning evaluation memory values.
Evaluated(Vec<MemoryValue<FieldElement>>),
}
/// Similar to FunctionContext::ssa_type_to_parameter but never panics and disallows reference types.
pub(crate) fn type_to_brillig_parameter(typ: &Type) -> Option<BrilligParameter> {
match typ {
Type::Numeric(_) => Some(BrilligParameter::SingleAddr(get_bit_size_from_ssa_type(typ))),
Type::Array(item_type, size) => {
let mut parameters = Vec::with_capacity(item_type.len());
for item_typ in item_type.iter() {
parameters.push(type_to_brillig_parameter(item_typ)?);
}
Some(BrilligParameter::Array(parameters, *size as usize))
}
_ => None,
}
}
fn value_id_to_calldata(value_id: ValueId, dfg: &DataFlowGraph, calldata: &mut Vec<FieldElement>) {
if let Some(value) = dfg.get_numeric_constant(value_id) {
calldata.push(value);
return;
}
if let Some((values, _type)) = dfg.get_array_constant(value_id) {
for value in values {
value_id_to_calldata(value, dfg, calldata);
}
return;
}
panic!("Expected ValueId to be numeric constant or array constant");
}
/// Check if one expression is simpler than the other.
/// Returns `Some((complex, simple))` if a simplification was found, otherwise `None`.
/// Expects the `ValueId`s to be fully resolved.
fn simplify(dfg: &DataFlowGraph, lhs: ValueId, rhs: ValueId) -> Option<(ValueId, ValueId)> {
match (&dfg[lhs], &dfg[rhs]) {
// Ignore trivial constraints
(Value::NumericConstant { .. }, Value::NumericConstant { .. }) => None,
// Prefer replacing with constants where possible.
(Value::NumericConstant { .. }, _) => Some((rhs, lhs)),
(_, Value::NumericConstant { .. }) => Some((lhs, rhs)),
// Otherwise prefer block parameters over instruction results.
// This is as block parameters are more likely to be a single witness rather than a full expression.
(Value::Param { .. }, Value::Instruction { .. }) => Some((rhs, lhs)),
(Value::Instruction { .. }, Value::Param { .. }) => Some((lhs, rhs)),
(_, _) => None,
}
}
#[cfg(test)]
mod test {
use std::sync::Arc;
use crate::ssa::{
function_builder::FunctionBuilder,
ir::{
map::Id,
types::{NumericType, Type},
},
opt::assert_normalized_ssa_equals,
Ssa,
};
#[test]
fn simple_constant_fold() {
// After constructing this IR, we set the value of v0 to 2.
// The expected return afterwards should be 9.
let src = "
acir(inline) fn main f0 {
b0(v0: Field):
v1 = add v0, Field 1
v2 = mul v1, Field 3
return v2
}
";
let mut ssa = Ssa::from_str(src).unwrap();
let main = ssa.main_mut();
let instructions = main.dfg[main.entry_block()].instructions();
assert_eq!(instructions.len(), 2); // The final return is not counted
let v0 = main.parameters()[0];
let two = main.dfg.make_constant(2_u128.into(), NumericType::NativeField);
main.dfg.set_value_from_id(v0, two);
let expected = "
acir(inline) fn main f0 {
b0(v0: Field):
return Field 9
}
";
let ssa = ssa.fold_constants();
assert_normalized_ssa_equals(ssa, expected);
}
#[test]
fn redundant_truncation() {
// After constructing this IR, we set the value of v1 to 2^8.
// The expected return afterwards should be v2.
let src = "
acir(inline) fn main f0 {
b0(v0: u16, v1: u16):
v2 = div v0, v1
v3 = truncate v2 to 8 bits, max_bit_size: 16
return v3
}
";
let mut ssa = Ssa::from_str(src).unwrap();
let main = ssa.main_mut();
let instructions = main.dfg[main.entry_block()].instructions();
assert_eq!(instructions.len(), 2); // The final return is not counted
let v1 = main.parameters()[1];
// Note that this constant guarantees that `v0/constant < 2^8`. We then do not need to truncate the result.
let constant = 2_u128.pow(8);
let constant = main.dfg.make_constant(constant.into(), NumericType::unsigned(16));
main.dfg.set_value_from_id(v1, constant);
let expected = "
acir(inline) fn main f0 {
b0(v0: u16, v1: u16):
v3 = div v0, u16 256
return v3
}
";
let ssa = ssa.fold_constants();
assert_normalized_ssa_equals(ssa, expected);
}
#[test]
fn non_redundant_truncation() {
// After constructing this IR, we set the value of v1 to 2^8 - 1.
// This should not result in the truncation being removed.
let src = "
acir(inline) fn main f0 {
b0(v0: u16, v1: u16):
v2 = div v0, v1
v3 = truncate v2 to 8 bits, max_bit_size: 16
return v3
}
";
let mut ssa = Ssa::from_str(src).unwrap();
let main = ssa.main_mut();
let instructions = main.dfg[main.entry_block()].instructions();
assert_eq!(instructions.len(), 2); // The final return is not counted
let v1 = main.parameters()[1];
// Note that this constant does not guarantee that `v0/constant < 2^8`. We must then truncate the result.
let constant = 2_u128.pow(8) - 1;
let constant = main.dfg.make_constant(constant.into(), NumericType::unsigned(16));
main.dfg.set_value_from_id(v1, constant);
let expected = "
acir(inline) fn main f0 {
b0(v0: u16, v1: u16):
v3 = div v0, u16 255
v4 = truncate v3 to 8 bits, max_bit_size: 16
return v4
}
";
let ssa = ssa.fold_constants();
assert_normalized_ssa_equals(ssa, expected);
}
#[test]
fn arrays_elements_are_updated() {
// After constructing this IR, we run constant folding with no expected benefit, but to
// ensure that all new values ids are correctly propagated.
let src = "
acir(inline) fn main f0 {
b0(v0: Field):
v2 = add v0, Field 1
v3 = make_array [v2] : [Field; 1]
return v3
}
";
let ssa = Ssa::from_str(src).unwrap();
let ssa = ssa.fold_constants();
assert_normalized_ssa_equals(ssa, src);
}
#[test]
fn instruction_deduplication() {
// After constructing this IR, we run constant folding which should replace the second cast
// with a reference to the results to the first. This then allows us to optimize away
// the constrain instruction as both inputs are known to be equal.
//
// The first cast instruction is retained and will be removed in the dead instruction elimination pass.
let src = "
acir(inline) fn main f0 {
b0(v0: u16):
v1 = cast v0 as u32
v2 = cast v0 as u32
constrain v1 == v2
return
}
";
let expected = "
acir(inline) fn main f0 {
b0(v0: u16):
v1 = cast v0 as u32
return
}
";
let ssa = Ssa::from_str(src).unwrap();
let ssa = ssa.fold_constants();
assert_normalized_ssa_equals(ssa, expected);
}
#[test]
fn constant_index_array_access_deduplication() {
// After constructing this IR, we run constant folding which should replace the second constant-index array get
// with a reference to the results to the first. This then allows us to optimize away