llvm-mca.rst 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. llvm-mca - LLVM Machine Code Analyzer
  2. =====================================
  3. .. program:: llvm-mca
  4. SYNOPSIS
  5. --------
  6. :program:`llvm-mca` [*options*] [input]
  7. DESCRIPTION
  8. -----------
  9. :program:`llvm-mca` is a performance analysis tool that uses information
  10. available in LLVM (e.g. scheduling models) to statically measure the performance
  11. of machine code in a specific CPU.
  12. Performance is measured in terms of throughput as well as processor resource
  13. consumption. The tool currently works for processors with an out-of-order
  14. backend, for which there is a scheduling model available in LLVM.
  15. The main goal of this tool is not just to predict the performance of the code
  16. when run on the target, but also help with diagnosing potential performance
  17. issues.
  18. Given an assembly code sequence, :program:`llvm-mca` estimates the Instructions
  19. Per Cycle (IPC), as well as hardware resource pressure. The analysis and
  20. reporting style were inspired by the IACA tool from Intel.
  21. For example, you can compile code with clang, output assembly, and pipe it
  22. directly into :program:`llvm-mca` for analysis:
  23. .. code-block:: bash
  24. $ clang foo.c -O2 -target x86_64-unknown-unknown -S -o - | llvm-mca -mcpu=btver2
  25. Or for Intel syntax:
  26. .. code-block:: bash
  27. $ clang foo.c -O2 -target x86_64-unknown-unknown -mllvm -x86-asm-syntax=intel -S -o - | llvm-mca -mcpu=btver2
  28. Scheduling models are not just used to compute instruction latencies and
  29. throughput, but also to understand what processor resources are available
  30. and how to simulate them.
  31. By design, the quality of the analysis conducted by :program:`llvm-mca` is
  32. inevitably affected by the quality of the scheduling models in LLVM.
  33. If you see that the performance report is not accurate for a processor,
  34. please `file a bug <https://bugs.llvm.org/enter_bug.cgi?product=libraries>`_
  35. against the appropriate backend.
  36. OPTIONS
  37. -------
  38. If ``input`` is "``-``" or omitted, :program:`llvm-mca` reads from standard
  39. input. Otherwise, it will read from the specified filename.
  40. If the :option:`-o` option is omitted, then :program:`llvm-mca` will send its output
  41. to standard output if the input is from standard input. If the :option:`-o`
  42. option specifies "``-``", then the output will also be sent to standard output.
  43. .. option:: -help
  44. Print a summary of command line options.
  45. .. option:: -o <filename>
  46. Use ``<filename>`` as the output filename. See the summary above for more
  47. details.
  48. .. option:: -mtriple=<target triple>
  49. Specify a target triple string.
  50. .. option:: -march=<arch>
  51. Specify the architecture for which to analyze the code. It defaults to the
  52. host default target.
  53. .. option:: -mcpu=<cpuname>
  54. Specify the processor for which to analyze the code. By default, the cpu name
  55. is autodetected from the host.
  56. .. option:: -output-asm-variant=<variant id>
  57. Specify the output assembly variant for the report generated by the tool.
  58. On x86, possible values are [0, 1]. A value of 0 (vic. 1) for this flag enables
  59. the AT&T (vic. Intel) assembly format for the code printed out by the tool in
  60. the analysis report.
  61. .. option:: -print-imm-hex
  62. Prefer hex format for numeric literals in the output assembly printed as part
  63. of the report.
  64. .. option:: -dispatch=<width>
  65. Specify a different dispatch width for the processor. The dispatch width
  66. defaults to field 'IssueWidth' in the processor scheduling model. If width is
  67. zero, then the default dispatch width is used.
  68. .. option:: -register-file-size=<size>
  69. Specify the size of the register file. When specified, this flag limits how
  70. many physical registers are available for register renaming purposes. A value
  71. of zero for this flag means "unlimited number of physical registers".
  72. .. option:: -iterations=<number of iterations>
  73. Specify the number of iterations to run. If this flag is set to 0, then the
  74. tool sets the number of iterations to a default value (i.e. 100).
  75. .. option:: -noalias=<bool>
  76. If set, the tool assumes that loads and stores don't alias. This is the
  77. default behavior.
  78. .. option:: -lqueue=<load queue size>
  79. Specify the size of the load queue in the load/store unit emulated by the tool.
  80. By default, the tool assumes an unbound number of entries in the load queue.
  81. A value of zero for this flag is ignored, and the default load queue size is
  82. used instead.
  83. .. option:: -squeue=<store queue size>
  84. Specify the size of the store queue in the load/store unit emulated by the
  85. tool. By default, the tool assumes an unbound number of entries in the store
  86. queue. A value of zero for this flag is ignored, and the default store queue
  87. size is used instead.
  88. .. option:: -timeline
  89. Enable the timeline view.
  90. .. option:: -timeline-max-iterations=<iterations>
  91. Limit the number of iterations to print in the timeline view. By default, the
  92. timeline view prints information for up to 10 iterations.
  93. .. option:: -timeline-max-cycles=<cycles>
  94. Limit the number of cycles in the timeline view. By default, the number of
  95. cycles is set to 80.
  96. .. option:: -resource-pressure
  97. Enable the resource pressure view. This is enabled by default.
  98. .. option:: -register-file-stats
  99. Enable register file usage statistics.
  100. .. option:: -dispatch-stats
  101. Enable extra dispatch statistics. This view collects and analyzes instruction
  102. dispatch events, as well as static/dynamic dispatch stall events. This view
  103. is disabled by default.
  104. .. option:: -scheduler-stats
  105. Enable extra scheduler statistics. This view collects and analyzes instruction
  106. issue events. This view is disabled by default.
  107. .. option:: -retire-stats
  108. Enable extra retire control unit statistics. This view is disabled by default.
  109. .. option:: -instruction-info
  110. Enable the instruction info view. This is enabled by default.
  111. .. option:: -show-encoding
  112. Enable the printing of instruction encodings within the instruction info view.
  113. .. option:: -all-stats
  114. Print all hardware statistics. This enables extra statistics related to the
  115. dispatch logic, the hardware schedulers, the register file(s), and the retire
  116. control unit. This option is disabled by default.
  117. .. option:: -all-views
  118. Enable all the view.
  119. .. option:: -instruction-tables
  120. Prints resource pressure information based on the static information
  121. available from the processor model. This differs from the resource pressure
  122. view because it doesn't require that the code is simulated. It instead prints
  123. the theoretical uniform distribution of resource pressure for every
  124. instruction in sequence.
  125. .. option:: -bottleneck-analysis
  126. Print information about bottlenecks that affect the throughput. This analysis
  127. can be expensive, and it is disabled by default. Bottlenecks are highlighted
  128. in the summary view.
  129. EXIT STATUS
  130. -----------
  131. :program:`llvm-mca` returns 0 on success. Otherwise, an error message is printed
  132. to standard error, and the tool returns 1.
  133. USING MARKERS TO ANALYZE SPECIFIC CODE BLOCKS
  134. ---------------------------------------------
  135. :program:`llvm-mca` allows for the optional usage of special code comments to
  136. mark regions of the assembly code to be analyzed. A comment starting with
  137. substring ``LLVM-MCA-BEGIN`` marks the beginning of a code region. A comment
  138. starting with substring ``LLVM-MCA-END`` marks the end of a code region. For
  139. example:
  140. .. code-block:: none
  141. # LLVM-MCA-BEGIN
  142. ...
  143. # LLVM-MCA-END
  144. If no user-defined region is specified, then :program:`llvm-mca` assumes a
  145. default region which contains every instruction in the input file. Every region
  146. is analyzed in isolation, and the final performance report is the union of all
  147. the reports generated for every code region.
  148. Code regions can have names. For example:
  149. .. code-block:: none
  150. # LLVM-MCA-BEGIN A simple example
  151. add %eax, %eax
  152. # LLVM-MCA-END
  153. The code from the example above defines a region named "A simple example" with a
  154. single instruction in it. Note how the region name doesn't have to be repeated
  155. in the ``LLVM-MCA-END`` directive. In the absence of overlapping regions,
  156. an anonymous ``LLVM-MCA-END`` directive always ends the currently active user
  157. defined region.
  158. Example of nesting regions:
  159. .. code-block:: none
  160. # LLVM-MCA-BEGIN foo
  161. add %eax, %edx
  162. # LLVM-MCA-BEGIN bar
  163. sub %eax, %edx
  164. # LLVM-MCA-END bar
  165. # LLVM-MCA-END foo
  166. Example of overlapping regions:
  167. .. code-block:: none
  168. # LLVM-MCA-BEGIN foo
  169. add %eax, %edx
  170. # LLVM-MCA-BEGIN bar
  171. sub %eax, %edx
  172. # LLVM-MCA-END foo
  173. add %eax, %edx
  174. # LLVM-MCA-END bar
  175. Note that multiple anonymous regions cannot overlap. Also, overlapping regions
  176. cannot have the same name.
  177. There is no support for marking regions from high-level source code, like C or
  178. C++. As a workaround, inline assembly directives may be used:
  179. .. code-block:: c++
  180. int foo(int a, int b) {
  181. __asm volatile("# LLVM-MCA-BEGIN foo");
  182. a += 42;
  183. __asm volatile("# LLVM-MCA-END");
  184. a *= b;
  185. return a;
  186. }
  187. However, this interferes with optimizations like loop vectorization and may have
  188. an impact on the code generated. This is because the ``__asm`` statements are
  189. seen as real code having important side effects, which limits how the code
  190. around them can be transformed. If users want to make use of inline assembly
  191. to emit markers, then the recommendation is to always verify that the output
  192. assembly is equivalent to the assembly generated in the absence of markers.
  193. The `Clang options to emit optimization reports <https://clang.llvm.org/docs/UsersManual.html#options-to-emit-optimization-reports>`_
  194. can also help in detecting missed optimizations.
  195. HOW LLVM-MCA WORKS
  196. ------------------
  197. :program:`llvm-mca` takes assembly code as input. The assembly code is parsed
  198. into a sequence of MCInst with the help of the existing LLVM target assembly
  199. parsers. The parsed sequence of MCInst is then analyzed by a ``Pipeline`` module
  200. to generate a performance report.
  201. The Pipeline module simulates the execution of the machine code sequence in a
  202. loop of iterations (default is 100). During this process, the pipeline collects
  203. a number of execution related statistics. At the end of this process, the
  204. pipeline generates and prints a report from the collected statistics.
  205. Here is an example of a performance report generated by the tool for a
  206. dot-product of two packed float vectors of four elements. The analysis is
  207. conducted for target x86, cpu btver2. The following result can be produced via
  208. the following command using the example located at
  209. ``test/tools/llvm-mca/X86/BtVer2/dot-product.s``:
  210. .. code-block:: bash
  211. $ llvm-mca -mtriple=x86_64-unknown-unknown -mcpu=btver2 -iterations=300 dot-product.s
  212. .. code-block:: none
  213. Iterations: 300
  214. Instructions: 900
  215. Total Cycles: 610
  216. Total uOps: 900
  217. Dispatch Width: 2
  218. uOps Per Cycle: 1.48
  219. IPC: 1.48
  220. Block RThroughput: 2.0
  221. Instruction Info:
  222. [1]: #uOps
  223. [2]: Latency
  224. [3]: RThroughput
  225. [4]: MayLoad
  226. [5]: MayStore
  227. [6]: HasSideEffects (U)
  228. [1] [2] [3] [4] [5] [6] Instructions:
  229. 1 2 1.00 vmulps %xmm0, %xmm1, %xmm2
  230. 1 3 1.00 vhaddps %xmm2, %xmm2, %xmm3
  231. 1 3 1.00 vhaddps %xmm3, %xmm3, %xmm4
  232. Resources:
  233. [0] - JALU0
  234. [1] - JALU1
  235. [2] - JDiv
  236. [3] - JFPA
  237. [4] - JFPM
  238. [5] - JFPU0
  239. [6] - JFPU1
  240. [7] - JLAGU
  241. [8] - JMul
  242. [9] - JSAGU
  243. [10] - JSTC
  244. [11] - JVALU0
  245. [12] - JVALU1
  246. [13] - JVIMUL
  247. Resource pressure per iteration:
  248. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
  249. - - - 2.00 1.00 2.00 1.00 - - - - - - -
  250. Resource pressure by instruction:
  251. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] Instructions:
  252. - - - - 1.00 - 1.00 - - - - - - - vmulps %xmm0, %xmm1, %xmm2
  253. - - - 1.00 - 1.00 - - - - - - - - vhaddps %xmm2, %xmm2, %xmm3
  254. - - - 1.00 - 1.00 - - - - - - - - vhaddps %xmm3, %xmm3, %xmm4
  255. According to this report, the dot-product kernel has been executed 300 times,
  256. for a total of 900 simulated instructions. The total number of simulated micro
  257. opcodes (uOps) is also 900.
  258. The report is structured in three main sections. The first section collects a
  259. few performance numbers; the goal of this section is to give a very quick
  260. overview of the performance throughput. Important performance indicators are
  261. **IPC**, **uOps Per Cycle**, and **Block RThroughput** (Block Reciprocal
  262. Throughput).
  263. Field *DispatchWidth* is the maximum number of micro opcodes that are dispatched
  264. to the out-of-order backend every simulated cycle.
  265. IPC is computed dividing the total number of simulated instructions by the total
  266. number of cycles.
  267. Field *Block RThroughput* is the reciprocal of the block throughput. Block
  268. throuhgput is a theoretical quantity computed as the maximum number of blocks
  269. (i.e. iterations) that can be executed per simulated clock cycle in the absence
  270. of loop carried dependencies. Block throughput is is superiorly
  271. limited by the dispatch rate, and the availability of hardware resources.
  272. In the absence of loop-carried data dependencies, the observed IPC tends to a
  273. theoretical maximum which can be computed by dividing the number of instructions
  274. of a single iteration by the `Block RThroughput`.
  275. Field 'uOps Per Cycle' is computed dividing the total number of simulated micro
  276. opcodes by the total number of cycles. A delta between Dispatch Width and this
  277. field is an indicator of a performance issue. In the absence of loop-carried
  278. data dependencies, the observed 'uOps Per Cycle' should tend to a theoretical
  279. maximum throughput which can be computed by dividing the number of uOps of a
  280. single iteration by the `Block RThroughput`.
  281. Field *uOps Per Cycle* is bounded from above by the dispatch width. That is
  282. because the dispatch width limits the maximum size of a dispatch group. Both IPC
  283. and 'uOps Per Cycle' are limited by the amount of hardware parallelism. The
  284. availability of hardware resources affects the resource pressure distribution,
  285. and it limits the number of instructions that can be executed in parallel every
  286. cycle. A delta between Dispatch Width and the theoretical maximum uOps per
  287. Cycle (computed by dividing the number of uOps of a single iteration by the
  288. `Block RThroughput`) is an indicator of a performance bottleneck caused by the
  289. lack of hardware resources.
  290. In general, the lower the Block RThroughput, the better.
  291. In this example, ``uOps per iteration/Block RThroughput`` is 1.50. Since there
  292. are no loop-carried dependencies, the observed `uOps Per Cycle` is expected to
  293. approach 1.50 when the number of iterations tends to infinity. The delta between
  294. the Dispatch Width (2.00), and the theoretical maximum uOp throughput (1.50) is
  295. an indicator of a performance bottleneck caused by the lack of hardware
  296. resources, and the *Resource pressure view* can help to identify the problematic
  297. resource usage.
  298. The second section of the report is the `instruction info view`. It shows the
  299. latency and reciprocal throughput of every instruction in the sequence. It also
  300. reports extra information related to the number of micro opcodes, and opcode
  301. properties (i.e., 'MayLoad', 'MayStore', and 'HasSideEffects').
  302. Field *RThroughput* is the reciprocal of the instruction throughput. Throughput
  303. is computed as the maximum number of instructions of a same type that can be
  304. executed per clock cycle in the absence of operand dependencies. In this
  305. example, the reciprocal throughput of a vector float multiply is 1
  306. cycles/instruction. That is because the FP multiplier JFPM is only available
  307. from pipeline JFPU1.
  308. Instruction encodings are displayed within the instruction info view when flag
  309. `-show-encoding` is specified.
  310. Below is an example of `-show-encoding` output for the dot-product kernel:
  311. .. code-block:: none
  312. Instruction Info:
  313. [1]: #uOps
  314. [2]: Latency
  315. [3]: RThroughput
  316. [4]: MayLoad
  317. [5]: MayStore
  318. [6]: HasSideEffects (U)
  319. [7]: Encoding Size
  320. [1] [2] [3] [4] [5] [6] [7] Encodings: Instructions:
  321. 1 2 1.00 4 c5 f0 59 d0 vmulps %xmm0, %xmm1, %xmm2
  322. 1 4 1.00 4 c5 eb 7c da vhaddps %xmm2, %xmm2, %xmm3
  323. 1 4 1.00 4 c5 e3 7c e3 vhaddps %xmm3, %xmm3, %xmm4
  324. The `Encoding Size` column shows the size in bytes of instructions. The
  325. `Encodings` column shows the actual instruction encodings (byte sequences in
  326. hex).
  327. The third section is the *Resource pressure view*. This view reports
  328. the average number of resource cycles consumed every iteration by instructions
  329. for every processor resource unit available on the target. Information is
  330. structured in two tables. The first table reports the number of resource cycles
  331. spent on average every iteration. The second table correlates the resource
  332. cycles to the machine instruction in the sequence. For example, every iteration
  333. of the instruction vmulps always executes on resource unit [6]
  334. (JFPU1 - floating point pipeline #1), consuming an average of 1 resource cycle
  335. per iteration. Note that on AMD Jaguar, vector floating-point multiply can
  336. only be issued to pipeline JFPU1, while horizontal floating-point additions can
  337. only be issued to pipeline JFPU0.
  338. The resource pressure view helps with identifying bottlenecks caused by high
  339. usage of specific hardware resources. Situations with resource pressure mainly
  340. concentrated on a few resources should, in general, be avoided. Ideally,
  341. pressure should be uniformly distributed between multiple resources.
  342. Timeline View
  343. ^^^^^^^^^^^^^
  344. The timeline view produces a detailed report of each instruction's state
  345. transitions through an instruction pipeline. This view is enabled by the
  346. command line option ``-timeline``. As instructions transition through the
  347. various stages of the pipeline, their states are depicted in the view report.
  348. These states are represented by the following characters:
  349. * D : Instruction dispatched.
  350. * e : Instruction executing.
  351. * E : Instruction executed.
  352. * R : Instruction retired.
  353. * = : Instruction already dispatched, waiting to be executed.
  354. * \- : Instruction executed, waiting to be retired.
  355. Below is the timeline view for a subset of the dot-product example located in
  356. ``test/tools/llvm-mca/X86/BtVer2/dot-product.s`` and processed by
  357. :program:`llvm-mca` using the following command:
  358. .. code-block:: bash
  359. $ llvm-mca -mtriple=x86_64-unknown-unknown -mcpu=btver2 -iterations=3 -timeline dot-product.s
  360. .. code-block:: none
  361. Timeline view:
  362. 012345
  363. Index 0123456789
  364. [0,0] DeeER. . . vmulps %xmm0, %xmm1, %xmm2
  365. [0,1] D==eeeER . . vhaddps %xmm2, %xmm2, %xmm3
  366. [0,2] .D====eeeER . vhaddps %xmm3, %xmm3, %xmm4
  367. [1,0] .DeeE-----R . vmulps %xmm0, %xmm1, %xmm2
  368. [1,1] . D=eeeE---R . vhaddps %xmm2, %xmm2, %xmm3
  369. [1,2] . D====eeeER . vhaddps %xmm3, %xmm3, %xmm4
  370. [2,0] . DeeE-----R . vmulps %xmm0, %xmm1, %xmm2
  371. [2,1] . D====eeeER . vhaddps %xmm2, %xmm2, %xmm3
  372. [2,2] . D======eeeER vhaddps %xmm3, %xmm3, %xmm4
  373. Average Wait times (based on the timeline view):
  374. [0]: Executions
  375. [1]: Average time spent waiting in a scheduler's queue
  376. [2]: Average time spent waiting in a scheduler's queue while ready
  377. [3]: Average time elapsed from WB until retire stage
  378. [0] [1] [2] [3]
  379. 0. 3 1.0 1.0 3.3 vmulps %xmm0, %xmm1, %xmm2
  380. 1. 3 3.3 0.7 1.0 vhaddps %xmm2, %xmm2, %xmm3
  381. 2. 3 5.7 0.0 0.0 vhaddps %xmm3, %xmm3, %xmm4
  382. 3 3.3 0.5 1.4 <total>
  383. The timeline view is interesting because it shows instruction state changes
  384. during execution. It also gives an idea of how the tool processes instructions
  385. executed on the target, and how their timing information might be calculated.
  386. The timeline view is structured in two tables. The first table shows
  387. instructions changing state over time (measured in cycles); the second table
  388. (named *Average Wait times*) reports useful timing statistics, which should
  389. help diagnose performance bottlenecks caused by long data dependencies and
  390. sub-optimal usage of hardware resources.
  391. An instruction in the timeline view is identified by a pair of indices, where
  392. the first index identifies an iteration, and the second index is the
  393. instruction index (i.e., where it appears in the code sequence). Since this
  394. example was generated using 3 iterations: ``-iterations=3``, the iteration
  395. indices range from 0-2 inclusively.
  396. Excluding the first and last column, the remaining columns are in cycles.
  397. Cycles are numbered sequentially starting from 0.
  398. From the example output above, we know the following:
  399. * Instruction [1,0] was dispatched at cycle 1.
  400. * Instruction [1,0] started executing at cycle 2.
  401. * Instruction [1,0] reached the write back stage at cycle 4.
  402. * Instruction [1,0] was retired at cycle 10.
  403. Instruction [1,0] (i.e., vmulps from iteration #1) does not have to wait in the
  404. scheduler's queue for the operands to become available. By the time vmulps is
  405. dispatched, operands are already available, and pipeline JFPU1 is ready to
  406. serve another instruction. So the instruction can be immediately issued on the
  407. JFPU1 pipeline. That is demonstrated by the fact that the instruction only
  408. spent 1cy in the scheduler's queue.
  409. There is a gap of 5 cycles between the write-back stage and the retire event.
  410. That is because instructions must retire in program order, so [1,0] has to wait
  411. for [0,2] to be retired first (i.e., it has to wait until cycle 10).
  412. In the example, all instructions are in a RAW (Read After Write) dependency
  413. chain. Register %xmm2 written by vmulps is immediately used by the first
  414. vhaddps, and register %xmm3 written by the first vhaddps is used by the second
  415. vhaddps. Long data dependencies negatively impact the ILP (Instruction Level
  416. Parallelism).
  417. In the dot-product example, there are anti-dependencies introduced by
  418. instructions from different iterations. However, those dependencies can be
  419. removed at register renaming stage (at the cost of allocating register aliases,
  420. and therefore consuming physical registers).
  421. Table *Average Wait times* helps diagnose performance issues that are caused by
  422. the presence of long latency instructions and potentially long data dependencies
  423. which may limit the ILP. Last row, ``<total>``, shows a global average over all
  424. instructions measured. Note that :program:`llvm-mca`, by default, assumes at
  425. least 1cy between the dispatch event and the issue event.
  426. When the performance is limited by data dependencies and/or long latency
  427. instructions, the number of cycles spent while in the *ready* state is expected
  428. to be very small when compared with the total number of cycles spent in the
  429. scheduler's queue. The difference between the two counters is a good indicator
  430. of how large of an impact data dependencies had on the execution of the
  431. instructions. When performance is mostly limited by the lack of hardware
  432. resources, the delta between the two counters is small. However, the number of
  433. cycles spent in the queue tends to be larger (i.e., more than 1-3cy),
  434. especially when compared to other low latency instructions.
  435. Bottleneck Analysis
  436. ^^^^^^^^^^^^^^^^^^^
  437. The ``-bottleneck-analysis`` command line option enables the analysis of
  438. performance bottlenecks.
  439. This analysis is potentially expensive. It attempts to correlate increases in
  440. backend pressure (caused by pipeline resource pressure and data dependencies) to
  441. dynamic dispatch stalls.
  442. Below is an example of ``-bottleneck-analysis`` output generated by
  443. :program:`llvm-mca` for 500 iterations of the dot-product example on btver2.
  444. .. code-block:: none
  445. Cycles with backend pressure increase [ 48.07% ]
  446. Throughput Bottlenecks:
  447. Resource Pressure [ 47.77% ]
  448. - JFPA [ 47.77% ]
  449. - JFPU0 [ 47.77% ]
  450. Data Dependencies: [ 0.30% ]
  451. - Register Dependencies [ 0.30% ]
  452. - Memory Dependencies [ 0.00% ]
  453. Critical sequence based on the simulation:
  454. Instruction Dependency Information
  455. +----< 2. vhaddps %xmm3, %xmm3, %xmm4
  456. |
  457. | < loop carried >
  458. |
  459. | 0. vmulps %xmm0, %xmm1, %xmm2
  460. +----> 1. vhaddps %xmm2, %xmm2, %xmm3 ## RESOURCE interference: JFPA [ probability: 74% ]
  461. +----> 2. vhaddps %xmm3, %xmm3, %xmm4 ## REGISTER dependency: %xmm3
  462. |
  463. | < loop carried >
  464. |
  465. +----> 1. vhaddps %xmm2, %xmm2, %xmm3 ## RESOURCE interference: JFPA [ probability: 74% ]
  466. According to the analysis, throughput is limited by resource pressure and not by
  467. data dependencies. The analysis observed increases in backend pressure during
  468. 48.07% of the simulated run. Almost all those pressure increase events were
  469. caused by contention on processor resources JFPA/JFPU0.
  470. The `critical sequence` is the most expensive sequence of instructions according
  471. to the simulation. It is annotated to provide extra information about critical
  472. register dependencies and resource interferences between instructions.
  473. Instructions from the critical sequence are expected to significantly impact
  474. performance. By construction, the accuracy of this analysis is strongly
  475. dependent on the simulation and (as always) by the quality of the processor
  476. model in llvm.
  477. Extra Statistics to Further Diagnose Performance Issues
  478. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  479. The ``-all-stats`` command line option enables extra statistics and performance
  480. counters for the dispatch logic, the reorder buffer, the retire control unit,
  481. and the register file.
  482. Below is an example of ``-all-stats`` output generated by :program:`llvm-mca`
  483. for 300 iterations of the dot-product example discussed in the previous
  484. sections.
  485. .. code-block:: none
  486. Dynamic Dispatch Stall Cycles:
  487. RAT - Register unavailable: 0
  488. RCU - Retire tokens unavailable: 0
  489. SCHEDQ - Scheduler full: 272 (44.6%)
  490. LQ - Load queue full: 0
  491. SQ - Store queue full: 0
  492. GROUP - Static restrictions on the dispatch group: 0
  493. Dispatch Logic - number of cycles where we saw N micro opcodes dispatched:
  494. [# dispatched], [# cycles]
  495. 0, 24 (3.9%)
  496. 1, 272 (44.6%)
  497. 2, 314 (51.5%)
  498. Schedulers - number of cycles where we saw N micro opcodes issued:
  499. [# issued], [# cycles]
  500. 0, 7 (1.1%)
  501. 1, 306 (50.2%)
  502. 2, 297 (48.7%)
  503. Scheduler's queue usage:
  504. [1] Resource name.
  505. [2] Average number of used buffer entries.
  506. [3] Maximum number of used buffer entries.
  507. [4] Total number of buffer entries.
  508. [1] [2] [3] [4]
  509. JALU01 0 0 20
  510. JFPU01 17 18 18
  511. JLSAGU 0 0 12
  512. Retire Control Unit - number of cycles where we saw N instructions retired:
  513. [# retired], [# cycles]
  514. 0, 109 (17.9%)
  515. 1, 102 (16.7%)
  516. 2, 399 (65.4%)
  517. Total ROB Entries: 64
  518. Max Used ROB Entries: 35 ( 54.7% )
  519. Average Used ROB Entries per cy: 32 ( 50.0% )
  520. Register File statistics:
  521. Total number of mappings created: 900
  522. Max number of mappings used: 35
  523. * Register File #1 -- JFpuPRF:
  524. Number of physical registers: 72
  525. Total number of mappings created: 900
  526. Max number of mappings used: 35
  527. * Register File #2 -- JIntegerPRF:
  528. Number of physical registers: 64
  529. Total number of mappings created: 0
  530. Max number of mappings used: 0
  531. If we look at the *Dynamic Dispatch Stall Cycles* table, we see the counter for
  532. SCHEDQ reports 272 cycles. This counter is incremented every time the dispatch
  533. logic is unable to dispatch a full group because the scheduler's queue is full.
  534. Looking at the *Dispatch Logic* table, we see that the pipeline was only able to
  535. dispatch two micro opcodes 51.5% of the time. The dispatch group was limited to
  536. one micro opcode 44.6% of the cycles, which corresponds to 272 cycles. The
  537. dispatch statistics are displayed by either using the command option
  538. ``-all-stats`` or ``-dispatch-stats``.
  539. The next table, *Schedulers*, presents a histogram displaying a count,
  540. representing the number of micro opcodes issued on some number of cycles. In
  541. this case, of the 610 simulated cycles, single opcodes were issued 306 times
  542. (50.2%) and there were 7 cycles where no opcodes were issued.
  543. The *Scheduler's queue usage* table shows that the average and maximum number of
  544. buffer entries (i.e., scheduler queue entries) used at runtime. Resource JFPU01
  545. reached its maximum (18 of 18 queue entries). Note that AMD Jaguar implements
  546. three schedulers:
  547. * JALU01 - A scheduler for ALU instructions.
  548. * JFPU01 - A scheduler floating point operations.
  549. * JLSAGU - A scheduler for address generation.
  550. The dot-product is a kernel of three floating point instructions (a vector
  551. multiply followed by two horizontal adds). That explains why only the floating
  552. point scheduler appears to be used.
  553. A full scheduler queue is either caused by data dependency chains or by a
  554. sub-optimal usage of hardware resources. Sometimes, resource pressure can be
  555. mitigated by rewriting the kernel using different instructions that consume
  556. different scheduler resources. Schedulers with a small queue are less resilient
  557. to bottlenecks caused by the presence of long data dependencies. The scheduler
  558. statistics are displayed by using the command option ``-all-stats`` or
  559. ``-scheduler-stats``.
  560. The next table, *Retire Control Unit*, presents a histogram displaying a count,
  561. representing the number of instructions retired on some number of cycles. In
  562. this case, of the 610 simulated cycles, two instructions were retired during the
  563. same cycle 399 times (65.4%) and there were 109 cycles where no instructions
  564. were retired. The retire statistics are displayed by using the command option
  565. ``-all-stats`` or ``-retire-stats``.
  566. The last table presented is *Register File statistics*. Each physical register
  567. file (PRF) used by the pipeline is presented in this table. In the case of AMD
  568. Jaguar, there are two register files, one for floating-point registers (JFpuPRF)
  569. and one for integer registers (JIntegerPRF). The table shows that of the 900
  570. instructions processed, there were 900 mappings created. Since this dot-product
  571. example utilized only floating point registers, the JFPuPRF was responsible for
  572. creating the 900 mappings. However, we see that the pipeline only used a
  573. maximum of 35 of 72 available register slots at any given time. We can conclude
  574. that the floating point PRF was the only register file used for the example, and
  575. that it was never resource constrained. The register file statistics are
  576. displayed by using the command option ``-all-stats`` or
  577. ``-register-file-stats``.
  578. In this example, we can conclude that the IPC is mostly limited by data
  579. dependencies, and not by resource pressure.
  580. Instruction Flow
  581. ^^^^^^^^^^^^^^^^
  582. This section describes the instruction flow through the default pipeline of
  583. :program:`llvm-mca`, as well as the functional units involved in the process.
  584. The default pipeline implements the following sequence of stages used to
  585. process instructions.
  586. * Dispatch (Instruction is dispatched to the schedulers).
  587. * Issue (Instruction is issued to the processor pipelines).
  588. * Write Back (Instruction is executed, and results are written back).
  589. * Retire (Instruction is retired; writes are architecturally committed).
  590. The default pipeline only models the out-of-order portion of a processor.
  591. Therefore, the instruction fetch and decode stages are not modeled. Performance
  592. bottlenecks in the frontend are not diagnosed. :program:`llvm-mca` assumes that
  593. instructions have all been decoded and placed into a queue before the simulation
  594. start. Also, :program:`llvm-mca` does not model branch prediction.
  595. Instruction Dispatch
  596. """"""""""""""""""""
  597. During the dispatch stage, instructions are picked in program order from a
  598. queue of already decoded instructions, and dispatched in groups to the
  599. simulated hardware schedulers.
  600. The size of a dispatch group depends on the availability of the simulated
  601. hardware resources. The processor dispatch width defaults to the value
  602. of the ``IssueWidth`` in LLVM's scheduling model.
  603. An instruction can be dispatched if:
  604. * The size of the dispatch group is smaller than processor's dispatch width.
  605. * There are enough entries in the reorder buffer.
  606. * There are enough physical registers to do register renaming.
  607. * The schedulers are not full.
  608. Scheduling models can optionally specify which register files are available on
  609. the processor. :program:`llvm-mca` uses that information to initialize register
  610. file descriptors. Users can limit the number of physical registers that are
  611. globally available for register renaming by using the command option
  612. ``-register-file-size``. A value of zero for this option means *unbounded*. By
  613. knowing how many registers are available for renaming, the tool can predict
  614. dispatch stalls caused by the lack of physical registers.
  615. The number of reorder buffer entries consumed by an instruction depends on the
  616. number of micro-opcodes specified for that instruction by the target scheduling
  617. model. The reorder buffer is responsible for tracking the progress of
  618. instructions that are "in-flight", and retiring them in program order. The
  619. number of entries in the reorder buffer defaults to the value specified by field
  620. `MicroOpBufferSize` in the target scheduling model.
  621. Instructions that are dispatched to the schedulers consume scheduler buffer
  622. entries. :program:`llvm-mca` queries the scheduling model to determine the set
  623. of buffered resources consumed by an instruction. Buffered resources are
  624. treated like scheduler resources.
  625. Instruction Issue
  626. """""""""""""""""
  627. Each processor scheduler implements a buffer of instructions. An instruction
  628. has to wait in the scheduler's buffer until input register operands become
  629. available. Only at that point, does the instruction becomes eligible for
  630. execution and may be issued (potentially out-of-order) for execution.
  631. Instruction latencies are computed by :program:`llvm-mca` with the help of the
  632. scheduling model.
  633. :program:`llvm-mca`'s scheduler is designed to simulate multiple processor
  634. schedulers. The scheduler is responsible for tracking data dependencies, and
  635. dynamically selecting which processor resources are consumed by instructions.
  636. It delegates the management of processor resource units and resource groups to a
  637. resource manager. The resource manager is responsible for selecting resource
  638. units that are consumed by instructions. For example, if an instruction
  639. consumes 1cy of a resource group, the resource manager selects one of the
  640. available units from the group; by default, the resource manager uses a
  641. round-robin selector to guarantee that resource usage is uniformly distributed
  642. between all units of a group.
  643. :program:`llvm-mca`'s scheduler internally groups instructions into three sets:
  644. * WaitSet: a set of instructions whose operands are not ready.
  645. * ReadySet: a set of instructions ready to execute.
  646. * IssuedSet: a set of instructions executing.
  647. Depending on the operands availability, instructions that are dispatched to the
  648. scheduler are either placed into the WaitSet or into the ReadySet.
  649. Every cycle, the scheduler checks if instructions can be moved from the WaitSet
  650. to the ReadySet, and if instructions from the ReadySet can be issued to the
  651. underlying pipelines. The algorithm prioritizes older instructions over younger
  652. instructions.
  653. Write-Back and Retire Stage
  654. """""""""""""""""""""""""""
  655. Issued instructions are moved from the ReadySet to the IssuedSet. There,
  656. instructions wait until they reach the write-back stage. At that point, they
  657. get removed from the queue and the retire control unit is notified.
  658. When instructions are executed, the retire control unit flags the instruction as
  659. "ready to retire."
  660. Instructions are retired in program order. The register file is notified of the
  661. retirement so that it can free the physical registers that were allocated for
  662. the instruction during the register renaming stage.
  663. Load/Store Unit and Memory Consistency Model
  664. """"""""""""""""""""""""""""""""""""""""""""
  665. To simulate an out-of-order execution of memory operations, :program:`llvm-mca`
  666. utilizes a simulated load/store unit (LSUnit) to simulate the speculative
  667. execution of loads and stores.
  668. Each load (or store) consumes an entry in the load (or store) queue. Users can
  669. specify flags ``-lqueue`` and ``-squeue`` to limit the number of entries in the
  670. load and store queues respectively. The queues are unbounded by default.
  671. The LSUnit implements a relaxed consistency model for memory loads and stores.
  672. The rules are:
  673. 1. A younger load is allowed to pass an older load only if there are no
  674. intervening stores or barriers between the two loads.
  675. 2. A younger load is allowed to pass an older store provided that the load does
  676. not alias with the store.
  677. 3. A younger store is not allowed to pass an older store.
  678. 4. A younger store is not allowed to pass an older load.
  679. By default, the LSUnit optimistically assumes that loads do not alias
  680. (`-noalias=true`) store operations. Under this assumption, younger loads are
  681. always allowed to pass older stores. Essentially, the LSUnit does not attempt
  682. to run any alias analysis to predict when loads and stores do not alias with
  683. each other.
  684. Note that, in the case of write-combining memory, rule 3 could be relaxed to
  685. allow reordering of non-aliasing store operations. That being said, at the
  686. moment, there is no way to further relax the memory model (``-noalias`` is the
  687. only option). Essentially, there is no option to specify a different memory
  688. type (e.g., write-back, write-combining, write-through; etc.) and consequently
  689. to weaken, or strengthen, the memory model.
  690. Other limitations are:
  691. * The LSUnit does not know when store-to-load forwarding may occur.
  692. * The LSUnit does not know anything about cache hierarchy and memory types.
  693. * The LSUnit does not know how to identify serializing operations and memory
  694. fences.
  695. The LSUnit does not attempt to predict if a load or store hits or misses the L1
  696. cache. It only knows if an instruction "MayLoad" and/or "MayStore." For
  697. loads, the scheduling model provides an "optimistic" load-to-use latency (which
  698. usually matches the load-to-use latency for when there is a hit in the L1D).
  699. :program:`llvm-mca` does not know about serializing operations or memory-barrier
  700. like instructions. The LSUnit conservatively assumes that an instruction which
  701. has both "MayLoad" and unmodeled side effects behaves like a "soft"
  702. load-barrier. That means, it serializes loads without forcing a flush of the
  703. load queue. Similarly, instructions that "MayStore" and have unmodeled side
  704. effects are treated like store barriers. A full memory barrier is a "MayLoad"
  705. and "MayStore" instruction with unmodeled side effects. This is inaccurate, but
  706. it is the best that we can do at the moment with the current information
  707. available in LLVM.
  708. A load/store barrier consumes one entry of the load/store queue. A load/store
  709. barrier enforces ordering of loads/stores. A younger load cannot pass a load
  710. barrier. Also, a younger store cannot pass a store barrier. A younger load
  711. has to wait for the memory/load barrier to execute. A load/store barrier is
  712. "executed" when it becomes the oldest entry in the load/store queue(s). That
  713. also means, by construction, all of the older loads/stores have been executed.
  714. In conclusion, the full set of load/store consistency rules are:
  715. #. A store may not pass a previous store.
  716. #. A store may not pass a previous load (regardless of ``-noalias``).
  717. #. A store has to wait until an older store barrier is fully executed.
  718. #. A load may pass a previous load.
  719. #. A load may not pass a previous store unless ``-noalias`` is set.
  720. #. A load has to wait until an older load barrier is fully executed.