@@ -3,7 +3,6 @@ use std::{collections::BTreeMap, io::BufWriter};
3
3
4
4
use acir:: circuit:: brillig:: BrilligFunctionId ;
5
5
use acir:: circuit:: OpcodeLocation ;
6
- use acir:: AcirField ;
7
6
use color_eyre:: eyre:: { self } ;
8
7
use fm:: codespan_files:: Files ;
9
8
use fxhash:: FxHashMap as HashMap ;
@@ -13,18 +12,66 @@ use noirc_errors::reporter::line_and_column_from_span;
13
12
use noirc_errors:: Location ;
14
13
use noirc_evaluator:: brillig:: ProcedureId ;
15
14
16
- use crate :: opcode_formatter:: AcirOrBrilligOpcode ;
15
+ pub ( crate ) trait Sample {
16
+ fn count ( & self ) -> usize ;
17
17
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
+ }
19
24
20
25
#[ 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 > ,
23
28
pub ( crate ) call_stack : Vec < OpcodeLocation > ,
24
29
pub ( crate ) count : usize ,
25
30
pub ( crate ) brillig_function_id : Option < BrilligFunctionId > ,
26
31
}
27
32
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
+
28
75
#[ derive( Debug , Default ) ]
29
76
pub ( crate ) struct FoldedStackItem {
30
77
pub ( crate ) total_samples : usize ,
@@ -33,9 +80,9 @@ pub(crate) struct FoldedStackItem {
33
80
34
81
pub ( crate ) trait FlamegraphGenerator {
35
82
#[ allow( clippy:: too_many_arguments) ]
36
- fn generate_flamegraph < ' files , F : AcirField > (
83
+ fn generate_flamegraph < ' files , S : Sample > (
37
84
& self ,
38
- samples : Vec < Sample < F > > ,
85
+ samples : Vec < S > ,
39
86
debug_symbols : & DebugInfo ,
40
87
files : & ' files impl Files < ' files , FileId = fm:: FileId > ,
41
88
artifact_name : & str ,
@@ -49,9 +96,9 @@ pub(crate) struct InfernoFlamegraphGenerator {
49
96
}
50
97
51
98
impl FlamegraphGenerator for InfernoFlamegraphGenerator {
52
- fn generate_flamegraph < ' files , F : AcirField > (
99
+ fn generate_flamegraph < ' files , S : Sample > (
53
100
& self ,
54
- samples : Vec < Sample < F > > ,
101
+ samples : Vec < S > ,
55
102
debug_symbols : & DebugInfo ,
56
103
files : & ' files impl Files < ' files , FileId = fm:: FileId > ,
57
104
artifact_name : & str ,
@@ -82,8 +129,8 @@ impl FlamegraphGenerator for InfernoFlamegraphGenerator {
82
129
}
83
130
}
84
131
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 > ,
87
134
debug_symbols : & DebugInfo ,
88
135
files : & ' files impl Files < ' files , FileId = fm:: FileId > ,
89
136
) -> Vec < String > {
@@ -92,15 +139,15 @@ fn generate_folded_sorted_lines<'files, F: AcirField>(
92
139
93
140
let mut resolution_cache: HashMap < OpcodeLocation , Vec < String > > = HashMap :: default ( ) ;
94
141
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 ( ) {
97
144
let callsite_labels = resolution_cache
98
- . entry ( opcode_location)
145
+ . entry ( * opcode_location)
99
146
. or_insert_with ( || {
100
147
find_callsite_labels (
101
148
debug_symbols,
102
- & opcode_location,
103
- sample. brillig_function_id ,
149
+ opcode_location,
150
+ sample. brillig_function_id ( ) ,
104
151
files,
105
152
)
106
153
} )
@@ -109,11 +156,14 @@ fn generate_folded_sorted_lines<'files, F: AcirField>(
109
156
location_names. extend ( callsite_labels) ;
110
157
}
111
158
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) ;
114
164
}
115
165
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) ;
117
167
}
118
168
119
169
to_folded_sorted_lines ( & folded_stack_items, Default :: default ( ) )
@@ -251,7 +301,7 @@ mod tests {
251
301
use noirc_errors:: { debug_info:: DebugInfo , Location , Span } ;
252
302
use std:: { collections:: BTreeMap , path:: Path } ;
253
303
254
- use crate :: { flamegraph:: Sample , opcode_formatter:: AcirOrBrilligOpcode } ;
304
+ use crate :: { flamegraph:: CompilationSample , opcode_formatter:: format_acir_opcode } ;
255
305
256
306
use super :: generate_folded_sorted_lines;
257
307
@@ -338,25 +388,25 @@ mod tests {
338
388
BTreeMap :: default ( ) ,
339
389
) ;
340
390
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 > (
344
394
Expression :: default ( ) ,
345
395
) ) ) ,
346
396
call_stack: vec![ OpcodeLocation :: Acir ( 0 ) ] ,
347
397
count: 10 ,
348
398
brillig_function_id: None ,
349
399
} ,
350
- Sample {
351
- opcode: Some ( AcirOrBrilligOpcode :: Acir ( AcirOpcode :: AssertZero (
400
+ CompilationSample {
401
+ opcode: Some ( format_acir_opcode ( & AcirOpcode :: AssertZero :: < FieldElement > (
352
402
Expression :: default ( ) ,
353
403
) ) ) ,
354
404
call_stack: vec![ OpcodeLocation :: Acir ( 1 ) ] ,
355
405
count: 20 ,
356
406
brillig_function_id: None ,
357
407
} ,
358
- Sample {
359
- opcode: Some ( AcirOrBrilligOpcode :: Acir ( AcirOpcode :: MemoryInit {
408
+ CompilationSample {
409
+ opcode: Some ( format_acir_opcode ( & AcirOpcode :: MemoryInit :: < FieldElement > {
360
410
block_id: BlockId ( 0 ) ,
361
411
init: vec![ ] ,
362
412
block_type: acir:: circuit:: opcodes:: BlockType :: Memory ,
0 commit comments