blob: d8c3b57207ca9dffd85078e1de39d8f2887f7cee [file] [log] [blame]
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +00001llvm-mca - LLVM Machine Code Analyzer
2=====================================
3
4SYNOPSIS
5--------
6
7:program:`llvm-mca` [*options*] [input]
8
9DESCRIPTION
10-----------
11
12:program:`llvm-mca` is a performance analysis tool that uses information
13available in LLVM (e.g. scheduling models) to statically measure the performance
14of machine code in a specific CPU.
15
16Performance is measured in terms of throughput as well as processor resource
17consumption. The tool currently works for processors with an out-of-order
18backend, for which there is a scheduling model available in LLVM.
19
20The main goal of this tool is not just to predict the performance of the code
21when run on the target, but also help with diagnosing potential performance
22issues.
23
Matt Davisb4588e52018-08-03 15:56:07 +000024Given an assembly code sequence, :program:`llvm-mca` estimates the Instructions
25Per Cycle (IPC), as well as hardware resource pressure. The analysis and
26reporting style were inspired by the IACA tool from Intel.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000027
Matt Davisb4588e52018-08-03 15:56:07 +000028For example, you can compile code with clang, output assembly, and pipe it
29directly into :program:`llvm-mca` for analysis:
Sanjay Patelc86033a2018-04-10 17:49:45 +000030
31.. code-block:: bash
32
Sanjay Patel40ad9262018-04-10 18:10:14 +000033 $ clang foo.c -O2 -target x86_64-unknown-unknown -S -o - | llvm-mca -mcpu=btver2
Andrea Di Biagioc6590122018-04-09 16:39:52 +000034
Andrea Di Biagiod8d940a2018-05-17 16:48:53 +000035Or for Intel syntax:
36
Simon Pilgrim93d45bc2018-05-17 16:58:42 +000037.. code-block:: bash
Andrea Di Biagiod8d940a2018-05-17 16:48:53 +000038
39 $ clang foo.c -O2 -target x86_64-unknown-unknown -mllvm -x86-asm-syntax=intel -S -o - | llvm-mca -mcpu=btver2
40
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000041OPTIONS
42-------
43
44If ``input`` is "``-``" or omitted, :program:`llvm-mca` reads from standard
45input. Otherwise, it will read from the specified filename.
46
47If the :option:`-o` option is omitted, then :program:`llvm-mca` will send its output
48to standard output if the input is from standard input. If the :option:`-o`
49option specifies "``-``", then the output will also be sent to standard output.
50
51
52.. option:: -help
53
54 Print a summary of command line options.
55
56.. option:: -mtriple=<target triple>
57
58 Specify a target triple string.
59
60.. option:: -march=<arch>
61
62 Specify the architecture for which to analyze the code. It defaults to the
63 host default target.
64
65.. option:: -mcpu=<cpuname>
66
Andrea Di Biagio93c49d52018-04-25 10:18:25 +000067 Specify the processor for which to analyze the code. By default, the cpu name
68 is autodetected from the host.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000069
70.. option:: -output-asm-variant=<variant id>
71
72 Specify the output assembly variant for the report generated by the tool.
73 On x86, possible values are [0, 1]. A value of 0 (vic. 1) for this flag enables
74 the AT&T (vic. Intel) assembly format for the code printed out by the tool in
75 the analysis report.
76
77.. option:: -dispatch=<width>
78
79 Specify a different dispatch width for the processor. The dispatch width
Andrea Di Biagioefc3f392018-04-05 16:42:32 +000080 defaults to field 'IssueWidth' in the processor scheduling model. If width is
81 zero, then the default dispatch width is used.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000082
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000083.. option:: -register-file-size=<size>
84
Andrea Di Biagioefc3f392018-04-05 16:42:32 +000085 Specify the size of the register file. When specified, this flag limits how
Matt Davise8c70bc2018-07-31 18:59:46 +000086 many physical registers are available for register renaming purposes. A value
87 of zero for this flag means "unlimited number of physical registers".
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000088
89.. option:: -iterations=<number of iterations>
90
91 Specify the number of iterations to run. If this flag is set to 0, then the
Andrea Di Biagio074cef32018-04-10 12:50:03 +000092 tool sets the number of iterations to a default value (i.e. 100).
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +000093
94.. option:: -noalias=<bool>
95
96 If set, the tool assumes that loads and stores don't alias. This is the
97 default behavior.
98
99.. option:: -lqueue=<load queue size>
100
101 Specify the size of the load queue in the load/store unit emulated by the tool.
102 By default, the tool assumes an unbound number of entries in the load queue.
103 A value of zero for this flag is ignored, and the default load queue size is
Matt Davisa448670b2018-07-17 16:11:54 +0000104 used instead.
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000105
106.. option:: -squeue=<store queue size>
107
108 Specify the size of the store queue in the load/store unit emulated by the
109 tool. By default, the tool assumes an unbound number of entries in the store
110 queue. A value of zero for this flag is ignored, and the default store queue
111 size is used instead.
112
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000113.. option:: -timeline
114
115 Enable the timeline view.
116
117.. option:: -timeline-max-iterations=<iterations>
118
119 Limit the number of iterations to print in the timeline view. By default, the
120 timeline view prints information for up to 10 iterations.
121
122.. option:: -timeline-max-cycles=<cycles>
123
124 Limit the number of cycles in the timeline view. By default, the number of
125 cycles is set to 80.
126
Andrea Di Biagio1feccc22018-03-26 13:21:48 +0000127.. option:: -resource-pressure
128
129 Enable the resource pressure view. This is enabled by default.
130
Andrea Di Biagio8dabf4f2018-04-03 16:46:23 +0000131.. option:: -register-file-stats
132
133 Enable register file usage statistics.
134
Andrea Di Biagio821f6502018-04-10 14:55:14 +0000135.. option:: -dispatch-stats
136
137 Enable extra dispatch statistics. This view collects and analyzes instruction
138 dispatch events, as well as static/dynamic dispatch stall events. This view
139 is disabled by default.
140
Andrea Di Biagio1cc29c02018-04-11 11:37:46 +0000141.. option:: -scheduler-stats
142
143 Enable extra scheduler statistics. This view collects and analyzes instruction
144 issue events. This view is disabled by default.
145
Andrea Di Biagiof41ad5c2018-04-11 12:12:53 +0000146.. option:: -retire-stats
147
148 Enable extra retire control unit statistics. This view is disabled by default.
149
Andrea Di Biagioff9c1092018-03-26 13:44:54 +0000150.. option:: -instruction-info
151
152 Enable the instruction info view. This is enabled by default.
153
Andrea Di Biagio650b5fc2018-05-17 12:27:03 +0000154.. option:: -all-stats
155
156 Print all hardware statistics. This enables extra statistics related to the
157 dispatch logic, the hardware schedulers, the register file(s), and the retire
158 control unit. This option is disabled by default.
159
160.. option:: -all-views
161
162 Enable all the view.
163
Andrea Di Biagiod1569292018-03-26 12:04:53 +0000164.. option:: -instruction-tables
165
166 Prints resource pressure information based on the static information
167 available from the processor model. This differs from the resource pressure
168 view because it doesn't require that the code is simulated. It instead prints
169 the theoretical uniform distribution of resource pressure for every
170 instruction in sequence.
171
Andrea Di Biagiobe3281a2019-03-04 11:52:34 +0000172.. option:: -bottleneck-analysis
173
174 Print information about bottlenecks that affect the throughput. This analysis
175 can be expensive, and it is disabled by default. Bottlenecks are highlighted
176 in the summary view.
177
Matt Davisa448670b2018-07-17 16:11:54 +0000178
Andrea Di Biagio3a6b0922018-03-08 13:05:02 +0000179EXIT STATUS
180-----------
181
182:program:`llvm-mca` returns 0 on success. Otherwise, an error message is printed
183to standard error, and the tool returns 1.
184
Matt Davisb4588e52018-08-03 15:56:07 +0000185USING MARKERS TO ANALYZE SPECIFIC CODE BLOCKS
186---------------------------------------------
187:program:`llvm-mca` allows for the optional usage of special code comments to
188mark regions of the assembly code to be analyzed. A comment starting with
189substring ``LLVM-MCA-BEGIN`` marks the beginning of a code region. A comment
190starting with substring ``LLVM-MCA-END`` marks the end of a code region. For
191example:
192
193.. code-block:: none
194
Andrea Di Biagio4e625542019-05-09 15:18:09 +0000195 # LLVM-MCA-BEGIN
Matt Davisb4588e52018-08-03 15:56:07 +0000196 ...
197 # LLVM-MCA-END
198
Andrea Di Biagio4e625542019-05-09 15:18:09 +0000199If no user-defined region is specified, then :program:`llvm-mca` assumes a
200default region which contains every instruction in the input file. Every region
201is analyzed in isolation, and the final performance report is the union of all
202the reports generated for every code region.
203
204Code regions can have names. For example:
205
206.. code-block:: none
207
208 # LLVM-MCA-BEGIN A simple example
209 add %eax, %eax
210 # LLVM-MCA-END
211
212The code from the example above defines a region named "A simple example" with a
213single instruction in it. Note how the region name doesn't have to be repeated
214in the ``LLVM-MCA-END`` directive. In the absence of overlapping regions,
215an anonymous ``LLVM-MCA-END`` directive always ends the currently active user
216defined region.
217
218Example of nesting regions:
219
220.. code-block:: none
221
222 # LLVM-MCA-BEGIN foo
223 add %eax, %edx
224 # LLVM-MCA-BEGIN bar
225 sub %eax, %edx
226 # LLVM-MCA-END bar
227 # LLVM-MCA-END foo
228
229Example of overlapping regions:
230
231.. code-block:: none
232
233 # LLVM-MCA-BEGIN foo
234 add %eax, %edx
235 # LLVM-MCA-BEGIN bar
236 sub %eax, %edx
237 # LLVM-MCA-END foo
238 add %eax, %edx
239 # LLVM-MCA-END bar
240
241Note that multiple anonymous regions cannot overlap. Also, overlapping regions
242cannot have the same name.
Matt Davisb4588e52018-08-03 15:56:07 +0000243
Matt Davis41bf4442019-06-10 20:38:56 +0000244There is no support for marking regions from high-level source code, like C or
245C++. As a workaround, inline assembly directives may be used:
Matt Davisb4588e52018-08-03 15:56:07 +0000246
247.. code-block:: c++
248
249 int foo(int a, int b) {
250 __asm volatile("# LLVM-MCA-BEGIN foo");
251 a += 42;
252 __asm volatile("# LLVM-MCA-END");
253 a *= b;
254 return a;
255 }
256
Matt Davis41bf4442019-06-10 20:38:56 +0000257However, this interferes with optimizations like loop vectorization and may have
258an impact on the code generated. This is because the ``__asm`` statements are
259seen as real code having important side effects, which limits how the code
260around them can be transformed. If users want to make use of inline assembly
261to emit markers, then the recommendation is to always verify that the output
262assembly is equivalent to the assembly generated in the absence of markers.
263The `Clang options to emit optimization reports <https://clang.llvm.org/docs/UsersManual.html#options-to-emit-optimization-reports>`_
264can also help in detecting missed optimizations.
265
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000266HOW LLVM-MCA WORKS
267------------------
Matt Davisbc093ea2018-07-19 20:33:59 +0000268
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000269:program:`llvm-mca` takes assembly code as input. The assembly code is parsed
270into a sequence of MCInst with the help of the existing LLVM target assembly
271parsers. The parsed sequence of MCInst is then analyzed by a ``Pipeline`` module
272to generate a performance report.
Matt Davisbc093ea2018-07-19 20:33:59 +0000273
274The Pipeline module simulates the execution of the machine code sequence in a
275loop of iterations (default is 100). During this process, the pipeline collects
276a number of execution related statistics. At the end of this process, the
277pipeline generates and prints a report from the collected statistics.
278
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000279Here is an example of a performance report generated by the tool for a
280dot-product of two packed float vectors of four elements. The analysis is
281conducted for target x86, cpu btver2. The following result can be produced via
282the following command using the example located at
Matt Davisbc093ea2018-07-19 20:33:59 +0000283``test/tools/llvm-mca/X86/BtVer2/dot-product.s``:
284
285.. code-block:: bash
286
287 $ llvm-mca -mtriple=x86_64-unknown-unknown -mcpu=btver2 -iterations=300 dot-product.s
288
289.. code-block:: none
290
291 Iterations: 300
292 Instructions: 900
293 Total Cycles: 610
Andrea Di Biagioa2eee472018-08-29 17:56:39 +0000294 Total uOps: 900
295
Matt Davisbc093ea2018-07-19 20:33:59 +0000296 Dispatch Width: 2
Andrea Di Biagioa2eee472018-08-29 17:56:39 +0000297 uOps Per Cycle: 1.48
Matt Davisbc093ea2018-07-19 20:33:59 +0000298 IPC: 1.48
299 Block RThroughput: 2.0
300
301
302 Instruction Info:
303 [1]: #uOps
304 [2]: Latency
305 [3]: RThroughput
306 [4]: MayLoad
307 [5]: MayStore
308 [6]: HasSideEffects (U)
309
310 [1] [2] [3] [4] [5] [6] Instructions:
311 1 2 1.00 vmulps %xmm0, %xmm1, %xmm2
312 1 3 1.00 vhaddps %xmm2, %xmm2, %xmm3
313 1 3 1.00 vhaddps %xmm3, %xmm3, %xmm4
314
315
316 Resources:
317 [0] - JALU0
318 [1] - JALU1
319 [2] - JDiv
320 [3] - JFPA
321 [4] - JFPM
322 [5] - JFPU0
323 [6] - JFPU1
324 [7] - JLAGU
325 [8] - JMul
326 [9] - JSAGU
327 [10] - JSTC
328 [11] - JVALU0
329 [12] - JVALU1
330 [13] - JVIMUL
331
332
333 Resource pressure per iteration:
334 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
335 - - - 2.00 1.00 2.00 1.00 - - - - - - -
336
337 Resource pressure by instruction:
338 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] Instructions:
339 - - - - 1.00 - 1.00 - - - - - - - vmulps %xmm0, %xmm1, %xmm2
340 - - - 1.00 - 1.00 - - - - - - - - vhaddps %xmm2, %xmm2, %xmm3
341 - - - 1.00 - 1.00 - - - - - - - - vhaddps %xmm3, %xmm3, %xmm4
342
343According to this report, the dot-product kernel has been executed 300 times,
Andrea Di Biagioa2eee472018-08-29 17:56:39 +0000344for a total of 900 simulated instructions. The total number of simulated micro
345opcodes (uOps) is also 900.
Matt Davisbc093ea2018-07-19 20:33:59 +0000346
347The report is structured in three main sections. The first section collects a
348few performance numbers; the goal of this section is to give a very quick
Andrea Di Biagioa2eee472018-08-29 17:56:39 +0000349overview of the performance throughput. Important performance indicators are
350**IPC**, **uOps Per Cycle**, and **Block RThroughput** (Block Reciprocal
Andrea Di Biagio1dac6ba2018-07-31 18:19:15 +0000351Throughput).
352
353IPC is computed dividing the total number of simulated instructions by the total
Andrea Di Biagioa2eee472018-08-29 17:56:39 +0000354number of cycles. In the absence of loop-carried data dependencies, the
Andrea Di Biagio1dac6ba2018-07-31 18:19:15 +0000355observed IPC tends to a theoretical maximum which can be computed by dividing
356the number of instructions of a single iteration by the *Block RThroughput*.
357
Andrea Di Biagioa2eee472018-08-29 17:56:39 +0000358Field 'uOps Per Cycle' is computed dividing the total number of simulated micro
359opcodes by the total number of cycles. A delta between Dispatch Width and this
360field is an indicator of a performance issue. In the absence of loop-carried
361data dependencies, the observed 'uOps Per Cycle' should tend to a theoretical
362maximum throughput which can be computed by dividing the number of uOps of a
363single iteration by the *Block RThroughput*.
Andrea Di Biagio1dac6ba2018-07-31 18:19:15 +0000364
Andrea Di Biagioa2eee472018-08-29 17:56:39 +0000365Field *uOps Per Cycle* is bounded from above by the dispatch width. That is
366because the dispatch width limits the maximum size of a dispatch group. Both IPC
367and 'uOps Per Cycle' are limited by the amount of hardware parallelism. The
368availability of hardware resources affects the resource pressure distribution,
369and it limits the number of instructions that can be executed in parallel every
370cycle. A delta between Dispatch Width and the theoretical maximum uOps per
371Cycle (computed by dividing the number of uOps of a single iteration by the
372*Block RTrhoughput*) is an indicator of a performance bottleneck caused by the
373lack of hardware resources.
374In general, the lower the Block RThroughput, the better.
375
376In this example, ``uOps per iteration/Block RThroughput`` is 1.50. Since there
377are no loop-carried dependencies, the observed *uOps Per Cycle* is expected to
378approach 1.50 when the number of iterations tends to infinity. The delta between
379the Dispatch Width (2.00), and the theoretical maximum uOp throughput (1.50) is
380an indicator of a performance bottleneck caused by the lack of hardware
381resources, and the *Resource pressure view* can help to identify the problematic
382resource usage.
Matt Davisbc093ea2018-07-19 20:33:59 +0000383
384The second section of the report shows the latency and reciprocal
385throughput of every instruction in the sequence. That section also reports
386extra information related to the number of micro opcodes, and opcode properties
387(i.e., 'MayLoad', 'MayStore', and 'HasSideEffects').
388
389The third section is the *Resource pressure view*. This view reports
390the average number of resource cycles consumed every iteration by instructions
391for every processor resource unit available on the target. Information is
392structured in two tables. The first table reports the number of resource cycles
393spent on average every iteration. The second table correlates the resource
394cycles to the machine instruction in the sequence. For example, every iteration
395of the instruction vmulps always executes on resource unit [6]
396(JFPU1 - floating point pipeline #1), consuming an average of 1 resource cycle
Matt Davisf2603c02018-07-21 18:32:47 +0000397per iteration. Note that on AMD Jaguar, vector floating-point multiply can
398only be issued to pipeline JFPU1, while horizontal floating-point additions can
399only be issued to pipeline JFPU0.
Matt Davisbc093ea2018-07-19 20:33:59 +0000400
401The resource pressure view helps with identifying bottlenecks caused by high
402usage of specific hardware resources. Situations with resource pressure mainly
403concentrated on a few resources should, in general, be avoided. Ideally,
404pressure should be uniformly distributed between multiple resources.
405
406Timeline View
407^^^^^^^^^^^^^
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000408The timeline view produces a detailed report of each instruction's state
Matt Davisbc093ea2018-07-19 20:33:59 +0000409transitions through an instruction pipeline. This view is enabled by the
410command line option ``-timeline``. As instructions transition through the
411various stages of the pipeline, their states are depicted in the view report.
412These states are represented by the following characters:
413
414* D : Instruction dispatched.
415* e : Instruction executing.
416* E : Instruction executed.
417* R : Instruction retired.
418* = : Instruction already dispatched, waiting to be executed.
419* \- : Instruction executed, waiting to be retired.
420
421Below is the timeline view for a subset of the dot-product example located in
422``test/tools/llvm-mca/X86/BtVer2/dot-product.s`` and processed by
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000423:program:`llvm-mca` using the following command:
Matt Davisbc093ea2018-07-19 20:33:59 +0000424
425.. code-block:: bash
426
427 $ llvm-mca -mtriple=x86_64-unknown-unknown -mcpu=btver2 -iterations=3 -timeline dot-product.s
428
429.. code-block:: none
430
431 Timeline view:
432 012345
433 Index 0123456789
434
435 [0,0] DeeER. . . vmulps %xmm0, %xmm1, %xmm2
436 [0,1] D==eeeER . . vhaddps %xmm2, %xmm2, %xmm3
437 [0,2] .D====eeeER . vhaddps %xmm3, %xmm3, %xmm4
438 [1,0] .DeeE-----R . vmulps %xmm0, %xmm1, %xmm2
439 [1,1] . D=eeeE---R . vhaddps %xmm2, %xmm2, %xmm3
440 [1,2] . D====eeeER . vhaddps %xmm3, %xmm3, %xmm4
441 [2,0] . DeeE-----R . vmulps %xmm0, %xmm1, %xmm2
442 [2,1] . D====eeeER . vhaddps %xmm2, %xmm2, %xmm3
443 [2,2] . D======eeeER vhaddps %xmm3, %xmm3, %xmm4
444
445
446 Average Wait times (based on the timeline view):
447 [0]: Executions
448 [1]: Average time spent waiting in a scheduler's queue
449 [2]: Average time spent waiting in a scheduler's queue while ready
450 [3]: Average time elapsed from WB until retire stage
451
452 [0] [1] [2] [3]
453 0. 3 1.0 1.0 3.3 vmulps %xmm0, %xmm1, %xmm2
454 1. 3 3.3 0.7 1.0 vhaddps %xmm2, %xmm2, %xmm3
455 2. 3 5.7 0.0 0.0 vhaddps %xmm3, %xmm3, %xmm4
456
457The timeline view is interesting because it shows instruction state changes
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000458during execution. It also gives an idea of how the tool processes instructions
Matt Davisbc093ea2018-07-19 20:33:59 +0000459executed on the target, and how their timing information might be calculated.
460
461The timeline view is structured in two tables. The first table shows
462instructions changing state over time (measured in cycles); the second table
463(named *Average Wait times*) reports useful timing statistics, which should
464help diagnose performance bottlenecks caused by long data dependencies and
465sub-optimal usage of hardware resources.
466
467An instruction in the timeline view is identified by a pair of indices, where
468the first index identifies an iteration, and the second index is the
469instruction index (i.e., where it appears in the code sequence). Since this
470example was generated using 3 iterations: ``-iterations=3``, the iteration
471indices range from 0-2 inclusively.
472
473Excluding the first and last column, the remaining columns are in cycles.
474Cycles are numbered sequentially starting from 0.
475
476From the example output above, we know the following:
477
478* Instruction [1,0] was dispatched at cycle 1.
479* Instruction [1,0] started executing at cycle 2.
480* Instruction [1,0] reached the write back stage at cycle 4.
481* Instruction [1,0] was retired at cycle 10.
482
483Instruction [1,0] (i.e., vmulps from iteration #1) does not have to wait in the
484scheduler's queue for the operands to become available. By the time vmulps is
485dispatched, operands are already available, and pipeline JFPU1 is ready to
486serve another instruction. So the instruction can be immediately issued on the
487JFPU1 pipeline. That is demonstrated by the fact that the instruction only
488spent 1cy in the scheduler's queue.
489
490There is a gap of 5 cycles between the write-back stage and the retire event.
491That is because instructions must retire in program order, so [1,0] has to wait
492for [0,2] to be retired first (i.e., it has to wait until cycle 10).
493
494In the example, all instructions are in a RAW (Read After Write) dependency
495chain. Register %xmm2 written by vmulps is immediately used by the first
496vhaddps, and register %xmm3 written by the first vhaddps is used by the second
497vhaddps. Long data dependencies negatively impact the ILP (Instruction Level
498Parallelism).
499
500In the dot-product example, there are anti-dependencies introduced by
501instructions from different iterations. However, those dependencies can be
502removed at register renaming stage (at the cost of allocating register aliases,
Matt Davise8c70bc2018-07-31 18:59:46 +0000503and therefore consuming physical registers).
Matt Davisbc093ea2018-07-19 20:33:59 +0000504
505Table *Average Wait times* helps diagnose performance issues that are caused by
506the presence of long latency instructions and potentially long data dependencies
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000507which may limit the ILP. Note that :program:`llvm-mca`, by default, assumes at
508least 1cy between the dispatch event and the issue event.
Matt Davisbc093ea2018-07-19 20:33:59 +0000509
510When the performance is limited by data dependencies and/or long latency
511instructions, the number of cycles spent while in the *ready* state is expected
512to be very small when compared with the total number of cycles spent in the
513scheduler's queue. The difference between the two counters is a good indicator
514of how large of an impact data dependencies had on the execution of the
515instructions. When performance is mostly limited by the lack of hardware
516resources, the delta between the two counters is small. However, the number of
517cycles spent in the queue tends to be larger (i.e., more than 1-3cy),
518especially when compared to other low latency instructions.
Matt Davisf2603c02018-07-21 18:32:47 +0000519
520Extra Statistics to Further Diagnose Performance Issues
521^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
522The ``-all-stats`` command line option enables extra statistics and performance
523counters for the dispatch logic, the reorder buffer, the retire control unit,
524and the register file.
525
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000526Below is an example of ``-all-stats`` output generated by :program:`llvm-mca`
Andrea Di Biagiob89b96c2018-08-27 14:52:52 +0000527for 300 iterations of the dot-product example discussed in the previous
528sections.
Matt Davisf2603c02018-07-21 18:32:47 +0000529
530.. code-block:: none
531
532 Dynamic Dispatch Stall Cycles:
533 RAT - Register unavailable: 0
534 RCU - Retire tokens unavailable: 0
Andrea Di Biagio8b647dc2018-08-30 10:50:20 +0000535 SCHEDQ - Scheduler full: 272 (44.6%)
Matt Davisf2603c02018-07-21 18:32:47 +0000536 LQ - Load queue full: 0
537 SQ - Store queue full: 0
538 GROUP - Static restrictions on the dispatch group: 0
539
540
Andrea Di Biagio8b647dc2018-08-30 10:50:20 +0000541 Dispatch Logic - number of cycles where we saw N micro opcodes dispatched:
Matt Davisf2603c02018-07-21 18:32:47 +0000542 [# dispatched], [# cycles]
543 0, 24 (3.9%)
544 1, 272 (44.6%)
545 2, 314 (51.5%)
546
547
Andrea Di Biagiof6a60f12019-04-08 16:05:54 +0000548 Schedulers - number of cycles where we saw N micro opcodes issued:
Matt Davisf2603c02018-07-21 18:32:47 +0000549 [# issued], [# cycles]
550 0, 7 (1.1%)
551 1, 306 (50.2%)
552 2, 297 (48.7%)
553
Matt Davisf2603c02018-07-21 18:32:47 +0000554 Scheduler's queue usage:
Andrea Di Biagiob89b96c2018-08-27 14:52:52 +0000555 [1] Resource name.
556 [2] Average number of used buffer entries.
557 [3] Maximum number of used buffer entries.
558 [4] Total number of buffer entries.
559
560 [1] [2] [3] [4]
561 JALU01 0 0 20
562 JFPU01 17 18 18
563 JLSAGU 0 0 12
Matt Davisf2603c02018-07-21 18:32:47 +0000564
565
566 Retire Control Unit - number of cycles where we saw N instructions retired:
567 [# retired], [# cycles]
568 0, 109 (17.9%)
569 1, 102 (16.7%)
570 2, 399 (65.4%)
571
Andrea Di Biagio07a82552018-11-23 12:12:57 +0000572 Total ROB Entries: 64
573 Max Used ROB Entries: 35 ( 54.7% )
574 Average Used ROB Entries per cy: 32 ( 50.0% )
575
Matt Davisf2603c02018-07-21 18:32:47 +0000576
577 Register File statistics:
578 Total number of mappings created: 900
579 Max number of mappings used: 35
580
581 * Register File #1 -- JFpuPRF:
582 Number of physical registers: 72
583 Total number of mappings created: 900
584 Max number of mappings used: 35
585
586 * Register File #2 -- JIntegerPRF:
587 Number of physical registers: 64
588 Total number of mappings created: 0
589 Max number of mappings used: 0
590
591If we look at the *Dynamic Dispatch Stall Cycles* table, we see the counter for
592SCHEDQ reports 272 cycles. This counter is incremented every time the dispatch
Andrea Di Biagio8b647dc2018-08-30 10:50:20 +0000593logic is unable to dispatch a full group because the scheduler's queue is full.
Matt Davisf2603c02018-07-21 18:32:47 +0000594
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000595Looking at the *Dispatch Logic* table, we see that the pipeline was only able to
Andrea Di Biagio8b647dc2018-08-30 10:50:20 +0000596dispatch two micro opcodes 51.5% of the time. The dispatch group was limited to
597one micro opcode 44.6% of the cycles, which corresponds to 272 cycles. The
Matt Davisf2603c02018-07-21 18:32:47 +0000598dispatch statistics are displayed by either using the command option
599``-all-stats`` or ``-dispatch-stats``.
600
601The next table, *Schedulers*, presents a histogram displaying a count,
Andrea Di Biagiof6a60f12019-04-08 16:05:54 +0000602representing the number of micro opcodes issued on some number of cycles. In
603this case, of the 610 simulated cycles, single opcodes were issued 306 times
604(50.2%) and there were 7 cycles where no opcodes were issued.
Matt Davisf2603c02018-07-21 18:32:47 +0000605
Andrea Di Biagiob89b96c2018-08-27 14:52:52 +0000606The *Scheduler's queue usage* table shows that the average and maximum number of
607buffer entries (i.e., scheduler queue entries) used at runtime. Resource JFPU01
Matt Davisf2603c02018-07-21 18:32:47 +0000608reached its maximum (18 of 18 queue entries). Note that AMD Jaguar implements
609three schedulers:
610
611* JALU01 - A scheduler for ALU instructions.
612* JFPU01 - A scheduler floating point operations.
613* JLSAGU - A scheduler for address generation.
614
615The dot-product is a kernel of three floating point instructions (a vector
616multiply followed by two horizontal adds). That explains why only the floating
617point scheduler appears to be used.
618
619A full scheduler queue is either caused by data dependency chains or by a
620sub-optimal usage of hardware resources. Sometimes, resource pressure can be
621mitigated by rewriting the kernel using different instructions that consume
622different scheduler resources. Schedulers with a small queue are less resilient
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000623to bottlenecks caused by the presence of long data dependencies. The scheduler
624statistics are displayed by using the command option ``-all-stats`` or
625``-scheduler-stats``.
Matt Davisf2603c02018-07-21 18:32:47 +0000626
627The next table, *Retire Control Unit*, presents a histogram displaying a count,
628representing the number of instructions retired on some number of cycles. In
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000629this case, of the 610 simulated cycles, two instructions were retired during the
630same cycle 399 times (65.4%) and there were 109 cycles where no instructions
631were retired. The retire statistics are displayed by using the command option
632``-all-stats`` or ``-retire-stats``.
Matt Davisf2603c02018-07-21 18:32:47 +0000633
634The last table presented is *Register File statistics*. Each physical register
635file (PRF) used by the pipeline is presented in this table. In the case of AMD
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000636Jaguar, there are two register files, one for floating-point registers (JFpuPRF)
637and one for integer registers (JIntegerPRF). The table shows that of the 900
638instructions processed, there were 900 mappings created. Since this dot-product
639example utilized only floating point registers, the JFPuPRF was responsible for
640creating the 900 mappings. However, we see that the pipeline only used a
641maximum of 35 of 72 available register slots at any given time. We can conclude
642that the floating point PRF was the only register file used for the example, and
643that it was never resource constrained. The register file statistics are
644displayed by using the command option ``-all-stats`` or
Matt Davisf2603c02018-07-21 18:32:47 +0000645``-register-file-stats``.
646
647In this example, we can conclude that the IPC is mostly limited by data
648dependencies, and not by resource pressure.
Matt Davis8d253a72018-07-30 22:30:14 +0000649
650Instruction Flow
651^^^^^^^^^^^^^^^^
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000652This section describes the instruction flow through the default pipeline of
653:program:`llvm-mca`, as well as the functional units involved in the process.
Matt Davis8d253a72018-07-30 22:30:14 +0000654
655The default pipeline implements the following sequence of stages used to
656process instructions.
657
658* Dispatch (Instruction is dispatched to the schedulers).
659* Issue (Instruction is issued to the processor pipelines).
660* Write Back (Instruction is executed, and results are written back).
661* Retire (Instruction is retired; writes are architecturally committed).
662
663The default pipeline only models the out-of-order portion of a processor.
664Therefore, the instruction fetch and decode stages are not modeled. Performance
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000665bottlenecks in the frontend are not diagnosed. :program:`llvm-mca` assumes that
666instructions have all been decoded and placed into a queue before the simulation
667start. Also, :program:`llvm-mca` does not model branch prediction.
Matt Davis8d253a72018-07-30 22:30:14 +0000668
669Instruction Dispatch
670""""""""""""""""""""
671During the dispatch stage, instructions are picked in program order from a
672queue of already decoded instructions, and dispatched in groups to the
673simulated hardware schedulers.
674
675The size of a dispatch group depends on the availability of the simulated
676hardware resources. The processor dispatch width defaults to the value
677of the ``IssueWidth`` in LLVM's scheduling model.
678
679An instruction can be dispatched if:
680
681* The size of the dispatch group is smaller than processor's dispatch width.
682* There are enough entries in the reorder buffer.
683* There are enough physical registers to do register renaming.
684* The schedulers are not full.
685
686Scheduling models can optionally specify which register files are available on
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000687the processor. :program:`llvm-mca` uses that information to initialize register
688file descriptors. Users can limit the number of physical registers that are
Matt Davis8d253a72018-07-30 22:30:14 +0000689globally available for register renaming by using the command option
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000690``-register-file-size``. A value of zero for this option means *unbounded*. By
691knowing how many registers are available for renaming, the tool can predict
692dispatch stalls caused by the lack of physical registers.
Matt Davis8d253a72018-07-30 22:30:14 +0000693
694The number of reorder buffer entries consumed by an instruction depends on the
Andrea Di Biagioeaca8ed2018-08-03 12:44:56 +0000695number of micro-opcodes specified for that instruction by the target scheduling
696model. The reorder buffer is responsible for tracking the progress of
697instructions that are "in-flight", and retiring them in program order. The
698number of entries in the reorder buffer defaults to the value specified by field
699`MicroOpBufferSize` in the target scheduling model.
Matt Davis8d253a72018-07-30 22:30:14 +0000700
701Instructions that are dispatched to the schedulers consume scheduler buffer
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000702entries. :program:`llvm-mca` queries the scheduling model to determine the set
703of buffered resources consumed by an instruction. Buffered resources are
704treated like scheduler resources.
Matt Davis8d253a72018-07-30 22:30:14 +0000705
706Instruction Issue
707"""""""""""""""""
708Each processor scheduler implements a buffer of instructions. An instruction
709has to wait in the scheduler's buffer until input register operands become
710available. Only at that point, does the instruction becomes eligible for
711execution and may be issued (potentially out-of-order) for execution.
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000712Instruction latencies are computed by :program:`llvm-mca` with the help of the
713scheduling model.
Matt Davis8d253a72018-07-30 22:30:14 +0000714
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000715:program:`llvm-mca`'s scheduler is designed to simulate multiple processor
716schedulers. The scheduler is responsible for tracking data dependencies, and
717dynamically selecting which processor resources are consumed by instructions.
718It delegates the management of processor resource units and resource groups to a
719resource manager. The resource manager is responsible for selecting resource
720units that are consumed by instructions. For example, if an instruction
721consumes 1cy of a resource group, the resource manager selects one of the
722available units from the group; by default, the resource manager uses a
Matt Davis8d253a72018-07-30 22:30:14 +0000723round-robin selector to guarantee that resource usage is uniformly distributed
724between all units of a group.
725
Andrea Di Biagio1c3bcc62018-08-03 12:55:28 +0000726:program:`llvm-mca`'s scheduler internally groups instructions into three sets:
Matt Davis8d253a72018-07-30 22:30:14 +0000727
Andrea Di Biagio1c3bcc62018-08-03 12:55:28 +0000728* WaitSet: a set of instructions whose operands are not ready.
729* ReadySet: a set of instructions ready to execute.
730* IssuedSet: a set of instructions executing.
Matt Davis8d253a72018-07-30 22:30:14 +0000731
Andrea Di Biagio1c3bcc62018-08-03 12:55:28 +0000732Depending on the operands availability, instructions that are dispatched to the
733scheduler are either placed into the WaitSet or into the ReadySet.
Matt Davis8d253a72018-07-30 22:30:14 +0000734
Andrea Di Biagio1c3bcc62018-08-03 12:55:28 +0000735Every cycle, the scheduler checks if instructions can be moved from the WaitSet
736to the ReadySet, and if instructions from the ReadySet can be issued to the
737underlying pipelines. The algorithm prioritizes older instructions over younger
738instructions.
Matt Davis8d253a72018-07-30 22:30:14 +0000739
740Write-Back and Retire Stage
741"""""""""""""""""""""""""""
Andrea Di Biagio1c3bcc62018-08-03 12:55:28 +0000742Issued instructions are moved from the ReadySet to the IssuedSet. There,
Matt Davis8d253a72018-07-30 22:30:14 +0000743instructions wait until they reach the write-back stage. At that point, they
744get removed from the queue and the retire control unit is notified.
745
Andrea Di Biagio1c3bcc62018-08-03 12:55:28 +0000746When instructions are executed, the retire control unit flags the instruction as
747"ready to retire."
Matt Davis8d253a72018-07-30 22:30:14 +0000748
Andrea Di Biagio1c3bcc62018-08-03 12:55:28 +0000749Instructions are retired in program order. The register file is notified of the
750retirement so that it can free the physical registers that were allocated for
751the instruction during the register renaming stage.
Matt Davis8d253a72018-07-30 22:30:14 +0000752
753Load/Store Unit and Memory Consistency Model
754""""""""""""""""""""""""""""""""""""""""""""
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000755To simulate an out-of-order execution of memory operations, :program:`llvm-mca`
756utilizes a simulated load/store unit (LSUnit) to simulate the speculative
757execution of loads and stores.
Matt Davis8d253a72018-07-30 22:30:14 +0000758
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000759Each load (or store) consumes an entry in the load (or store) queue. Users can
760specify flags ``-lqueue`` and ``-squeue`` to limit the number of entries in the
761load and store queues respectively. The queues are unbounded by default.
Matt Davis8d253a72018-07-30 22:30:14 +0000762
763The LSUnit implements a relaxed consistency model for memory loads and stores.
764The rules are:
765
7661. A younger load is allowed to pass an older load only if there are no
767 intervening stores or barriers between the two loads.
7682. A younger load is allowed to pass an older store provided that the load does
769 not alias with the store.
7703. A younger store is not allowed to pass an older store.
7714. A younger store is not allowed to pass an older load.
772
773By default, the LSUnit optimistically assumes that loads do not alias
774(`-noalias=true`) store operations. Under this assumption, younger loads are
775always allowed to pass older stores. Essentially, the LSUnit does not attempt
776to run any alias analysis to predict when loads and stores do not alias with
777each other.
778
779Note that, in the case of write-combining memory, rule 3 could be relaxed to
780allow reordering of non-aliasing store operations. That being said, at the
781moment, there is no way to further relax the memory model (``-noalias`` is the
782only option). Essentially, there is no option to specify a different memory
783type (e.g., write-back, write-combining, write-through; etc.) and consequently
784to weaken, or strengthen, the memory model.
785
786Other limitations are:
787
788* The LSUnit does not know when store-to-load forwarding may occur.
789* The LSUnit does not know anything about cache hierarchy and memory types.
790* The LSUnit does not know how to identify serializing operations and memory
791 fences.
792
793The LSUnit does not attempt to predict if a load or store hits or misses the L1
794cache. It only knows if an instruction "MayLoad" and/or "MayStore." For
795loads, the scheduling model provides an "optimistic" load-to-use latency (which
796usually matches the load-to-use latency for when there is a hit in the L1D).
797
Andrea Di Biagiobdcf6ad2018-07-31 15:29:10 +0000798:program:`llvm-mca` does not know about serializing operations or memory-barrier
799like instructions. The LSUnit conservatively assumes that an instruction which
800has both "MayLoad" and unmodeled side effects behaves like a "soft"
801load-barrier. That means, it serializes loads without forcing a flush of the
802load queue. Similarly, instructions that "MayStore" and have unmodeled side
803effects are treated like store barriers. A full memory barrier is a "MayLoad"
804and "MayStore" instruction with unmodeled side effects. This is inaccurate, but
805it is the best that we can do at the moment with the current information
806available in LLVM.
Matt Davis8d253a72018-07-30 22:30:14 +0000807
808A load/store barrier consumes one entry of the load/store queue. A load/store
809barrier enforces ordering of loads/stores. A younger load cannot pass a load
810barrier. Also, a younger store cannot pass a store barrier. A younger load
811has to wait for the memory/load barrier to execute. A load/store barrier is
812"executed" when it becomes the oldest entry in the load/store queue(s). That
813also means, by construction, all of the older loads/stores have been executed.
814
815In conclusion, the full set of load/store consistency rules are:
816
817#. A store may not pass a previous store.
818#. A store may not pass a previous load (regardless of ``-noalias``).
819#. A store has to wait until an older store barrier is fully executed.
820#. A load may pass a previous load.
821#. A load may not pass a previous store unless ``-noalias`` is set.
822#. A load has to wait until an older load barrier is fully executed.