@@ -3,7 +3,6 @@ use std::{collections::BTreeMap, io::BufWriter};
33
44use acir:: circuit:: brillig:: BrilligFunctionId ;
55use acir:: circuit:: OpcodeLocation ;
6- use acir:: AcirField ;
76use color_eyre:: eyre:: { self } ;
87use fm:: codespan_files:: Files ;
98use fxhash:: FxHashMap as HashMap ;
@@ -13,18 +12,66 @@ use noirc_errors::reporter::line_and_column_from_span;
1312use noirc_errors:: Location ;
1413use noirc_evaluator:: brillig:: ProcedureId ;
1514
16- use crate :: opcode_formatter:: AcirOrBrilligOpcode ;
15+ pub ( crate ) trait Sample {
16+ fn count ( & self ) -> usize ;
1717
18- use super :: opcode_formatter:: format_opcode;
18+ fn brillig_function_id ( & self ) -> Option < BrilligFunctionId > ;
19+
20+ fn call_stack ( & self ) -> & [ OpcodeLocation ] ;
21+
22+ fn opcode ( self ) -> Option < String > ;
23+ }
1924
2025#[ derive( Debug ) ]
21- pub ( crate ) struct Sample < F : AcirField > {
22- pub ( crate ) opcode : Option < AcirOrBrilligOpcode < F > > ,
26+ pub ( crate ) struct CompilationSample {
27+ pub ( crate ) opcode : Option < String > ,
2328 pub ( crate ) call_stack : Vec < OpcodeLocation > ,
2429 pub ( crate ) count : usize ,
2530 pub ( crate ) brillig_function_id : Option < BrilligFunctionId > ,
2631}
2732
33+ impl Sample for CompilationSample {
34+ fn count ( & self ) -> usize {
35+ self . count
36+ }
37+
38+ fn brillig_function_id ( & self ) -> Option < BrilligFunctionId > {
39+ self . brillig_function_id
40+ }
41+
42+ fn call_stack ( & self ) -> & [ OpcodeLocation ] {
43+ & self . call_stack
44+ }
45+
46+ fn opcode ( self ) -> Option < String > {
47+ self . opcode
48+ }
49+ }
50+
51+ pub ( crate ) struct BrilligExecutionSample {
52+ pub ( crate ) opcode : Option < String > ,
53+ pub ( crate ) call_stack : Vec < OpcodeLocation > ,
54+ pub ( crate ) brillig_function_id : Option < BrilligFunctionId > ,
55+ }
56+
57+ impl Sample for BrilligExecutionSample {
58+ fn count ( & self ) -> usize {
59+ 1
60+ }
61+
62+ fn brillig_function_id ( & self ) -> Option < BrilligFunctionId > {
63+ self . brillig_function_id
64+ }
65+
66+ fn call_stack ( & self ) -> & [ OpcodeLocation ] {
67+ & self . call_stack
68+ }
69+
70+ fn opcode ( self ) -> Option < String > {
71+ self . opcode
72+ }
73+ }
74+
2875#[ derive( Debug , Default ) ]
2976pub ( crate ) struct FoldedStackItem {
3077 pub ( crate ) total_samples : usize ,
@@ -33,9 +80,9 @@ pub(crate) struct FoldedStackItem {
3380
3481pub ( crate ) trait FlamegraphGenerator {
3582 #[ allow( clippy:: too_many_arguments) ]
36- fn generate_flamegraph < ' files , F : AcirField > (
83+ fn generate_flamegraph < ' files , S : Sample > (
3784 & self ,
38- samples : Vec < Sample < F > > ,
85+ samples : Vec < S > ,
3986 debug_symbols : & DebugInfo ,
4087 files : & ' files impl Files < ' files , FileId = fm:: FileId > ,
4188 artifact_name : & str ,
@@ -49,9 +96,9 @@ pub(crate) struct InfernoFlamegraphGenerator {
4996}
5097
5198impl FlamegraphGenerator for InfernoFlamegraphGenerator {
52- fn generate_flamegraph < ' files , F : AcirField > (
99+ fn generate_flamegraph < ' files , S : Sample > (
53100 & self ,
54- samples : Vec < Sample < F > > ,
101+ samples : Vec < S > ,
55102 debug_symbols : & DebugInfo ,
56103 files : & ' files impl Files < ' files , FileId = fm:: FileId > ,
57104 artifact_name : & str ,
@@ -82,8 +129,8 @@ impl FlamegraphGenerator for InfernoFlamegraphGenerator {
82129 }
83130}
84131
85- fn generate_folded_sorted_lines < ' files , F : AcirField > (
86- samples : Vec < Sample < F > > ,
132+ fn generate_folded_sorted_lines < ' files , S : Sample > (
133+ samples : Vec < S > ,
87134 debug_symbols : & DebugInfo ,
88135 files : & ' files impl Files < ' files , FileId = fm:: FileId > ,
89136) -> Vec < String > {
@@ -92,15 +139,15 @@ fn generate_folded_sorted_lines<'files, F: AcirField>(
92139
93140 let mut resolution_cache: HashMap < OpcodeLocation , Vec < String > > = HashMap :: default ( ) ;
94141 for sample in samples {
95- let mut location_names = Vec :: with_capacity ( sample. call_stack . len ( ) ) ;
96- for opcode_location in sample. call_stack {
142+ let mut location_names = Vec :: with_capacity ( sample. call_stack ( ) . len ( ) ) ;
143+ for opcode_location in sample. call_stack ( ) {
97144 let callsite_labels = resolution_cache
98- . entry ( opcode_location)
145+ . entry ( * opcode_location)
99146 . or_insert_with ( || {
100147 find_callsite_labels (
101148 debug_symbols,
102- & opcode_location,
103- sample. brillig_function_id ,
149+ opcode_location,
150+ sample. brillig_function_id ( ) ,
104151 files,
105152 )
106153 } )
@@ -109,11 +156,14 @@ fn generate_folded_sorted_lines<'files, F: AcirField>(
109156 location_names. extend ( callsite_labels) ;
110157 }
111158
112- if let Some ( opcode) = & sample. opcode {
113- location_names. push ( format_opcode ( opcode) ) ;
159+ // We move `sample` by calling `sample.opcode()` so we want to fetch the sample count here.
160+ let count = sample. count ( ) ;
161+
162+ if let Some ( opcode) = sample. opcode ( ) {
163+ location_names. push ( opcode) ;
114164 }
115165
116- add_locations_to_folded_stack_items ( & mut folded_stack_items, location_names, sample . count ) ;
166+ add_locations_to_folded_stack_items ( & mut folded_stack_items, location_names, count) ;
117167 }
118168
119169 to_folded_sorted_lines ( & folded_stack_items, Default :: default ( ) )
@@ -251,7 +301,7 @@ mod tests {
251301 use noirc_errors:: { debug_info:: DebugInfo , Location , Span } ;
252302 use std:: { collections:: BTreeMap , path:: Path } ;
253303
254- use crate :: { flamegraph:: Sample , opcode_formatter:: AcirOrBrilligOpcode } ;
304+ use crate :: { flamegraph:: CompilationSample , opcode_formatter:: format_acir_opcode } ;
255305
256306 use super :: generate_folded_sorted_lines;
257307
@@ -338,25 +388,25 @@ mod tests {
338388 BTreeMap :: default ( ) ,
339389 ) ;
340390
341- let samples: Vec < Sample < FieldElement > > = vec ! [
342- Sample {
343- opcode: Some ( AcirOrBrilligOpcode :: Acir ( AcirOpcode :: AssertZero (
391+ let samples: Vec < CompilationSample > = vec ! [
392+ CompilationSample {
393+ opcode: Some ( format_acir_opcode ( & AcirOpcode :: AssertZero :: < FieldElement > (
344394 Expression :: default ( ) ,
345395 ) ) ) ,
346396 call_stack: vec![ OpcodeLocation :: Acir ( 0 ) ] ,
347397 count: 10 ,
348398 brillig_function_id: None ,
349399 } ,
350- Sample {
351- opcode: Some ( AcirOrBrilligOpcode :: Acir ( AcirOpcode :: AssertZero (
400+ CompilationSample {
401+ opcode: Some ( format_acir_opcode ( & AcirOpcode :: AssertZero :: < FieldElement > (
352402 Expression :: default ( ) ,
353403 ) ) ) ,
354404 call_stack: vec![ OpcodeLocation :: Acir ( 1 ) ] ,
355405 count: 20 ,
356406 brillig_function_id: None ,
357407 } ,
358- Sample {
359- opcode: Some ( AcirOrBrilligOpcode :: Acir ( AcirOpcode :: MemoryInit {
408+ CompilationSample {
409+ opcode: Some ( format_acir_opcode ( & AcirOpcode :: MemoryInit :: < FieldElement > {
360410 block_id: BlockId ( 0 ) ,
361411 init: vec![ ] ,
362412 block_type: acir:: circuit:: opcodes:: BlockType :: Memory ,
0 commit comments