llvm-exegesis.rst 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. llvm-exegesis - LLVM Machine Instruction Benchmark
  2. ==================================================
  3. .. program:: llvm-exegesis
  4. SYNOPSIS
  5. --------
  6. :program:`llvm-exegesis` [*options*]
  7. DESCRIPTION
  8. -----------
  9. :program:`llvm-exegesis` is a benchmarking tool that uses information available
  10. in LLVM to measure host machine instruction characteristics like latency,
  11. throughput, or port decomposition.
  12. Given an LLVM opcode name and a benchmarking mode, :program:`llvm-exegesis`
  13. generates a code snippet that makes execution as serial (resp. as parallel) as
  14. possible so that we can measure the latency (resp. inverse throughput/uop decomposition)
  15. of the instruction.
  16. The code snippet is jitted and executed on the host subtarget. The time taken
  17. (resp. resource usage) is measured using hardware performance counters. The
  18. result is printed out as YAML to the standard output.
  19. The main goal of this tool is to automatically (in)validate the LLVM's TableDef
  20. scheduling models. To that end, we also provide analysis of the results.
  21. :program:`llvm-exegesis` can also benchmark arbitrary user-provided code
  22. snippets.
  23. EXAMPLE 1: benchmarking instructions
  24. ------------------------------------
  25. Assume you have an X86-64 machine. To measure the latency of a single
  26. instruction, run:
  27. .. code-block:: bash
  28. $ llvm-exegesis -mode=latency -opcode-name=ADD64rr
  29. Measuring the uop decomposition or inverse throughput of an instruction works similarly:
  30. .. code-block:: bash
  31. $ llvm-exegesis -mode=uops -opcode-name=ADD64rr
  32. $ llvm-exegesis -mode=inverse_throughput -opcode-name=ADD64rr
  33. The output is a YAML document (the default is to write to stdout, but you can
  34. redirect the output to a file using `-benchmarks-file`):
  35. .. code-block:: none
  36. ---
  37. key:
  38. opcode_name: ADD64rr
  39. mode: latency
  40. config: ''
  41. cpu_name: haswell
  42. llvm_triple: x86_64-unknown-linux-gnu
  43. num_repetitions: 10000
  44. measurements:
  45. - { key: latency, value: 1.0058, debug_string: '' }
  46. error: ''
  47. info: 'explicit self cycles, selecting one aliasing configuration.
  48. Snippet:
  49. ADD64rr R8, R8, R10
  50. '
  51. ...
  52. To measure the latency of all instructions for the host architecture, run:
  53. .. code-block:: bash
  54. #!/bin/bash
  55. readonly INSTRUCTIONS=$(($(grep INSTRUCTION_LIST_END build/lib/Target/X86/X86GenInstrInfo.inc | cut -f2 -d=) - 1))
  56. for INSTRUCTION in $(seq 1 ${INSTRUCTIONS});
  57. do
  58. ./build/bin/llvm-exegesis -mode=latency -opcode-index=${INSTRUCTION} | sed -n '/---/,$p'
  59. done
  60. FIXME: Provide an :program:`llvm-exegesis` option to test all instructions.
  61. EXAMPLE 2: benchmarking a custom code snippet
  62. ---------------------------------------------
  63. To measure the latency/uops of a custom piece of code, you can specify the
  64. `snippets-file` option (`-` reads from standard input).
  65. .. code-block:: bash
  66. $ echo "vzeroupper" | llvm-exegesis -mode=uops -snippets-file=-
  67. Real-life code snippets typically depend on registers or memory.
  68. :program:`llvm-exegesis` checks the liveliness of registers (i.e. any register
  69. use has a corresponding def or is a "live in"). If your code depends on the
  70. value of some registers, you have two options:
  71. - Mark the register as requiring a definition. :program:`llvm-exegesis` will
  72. automatically assign a value to the register. This can be done using the
  73. directive `LLVM-EXEGESIS-DEFREG <reg name> <hex_value>`, where `<hex_value>`
  74. is a bit pattern used to fill `<reg_name>`. If `<hex_value>` is smaller than
  75. the register width, it will be sign-extended.
  76. - Mark the register as a "live in". :program:`llvm-exegesis` will benchmark
  77. using whatever value was in this registers on entry. This can be done using
  78. the directive `LLVM-EXEGESIS-LIVEIN <reg name>`.
  79. For example, the following code snippet depends on the values of XMM1 (which
  80. will be set by the tool) and the memory buffer passed in RDI (live in).
  81. .. code-block:: none
  82. # LLVM-EXEGESIS-LIVEIN RDI
  83. # LLVM-EXEGESIS-DEFREG XMM1 42
  84. vmulps (%rdi), %xmm1, %xmm2
  85. vhaddps %xmm2, %xmm2, %xmm3
  86. addq $0x10, %rdi
  87. EXAMPLE 3: analysis
  88. -------------------
  89. Assuming you have a set of benchmarked instructions (either latency or uops) as
  90. YAML in file `/tmp/benchmarks.yaml`, you can analyze the results using the
  91. following command:
  92. .. code-block:: bash
  93. $ llvm-exegesis -mode=analysis \
  94. -benchmarks-file=/tmp/benchmarks.yaml \
  95. -analysis-clusters-output-file=/tmp/clusters.csv \
  96. -analysis-inconsistencies-output-file=/tmp/inconsistencies.html
  97. This will group the instructions into clusters with the same performance
  98. characteristics. The clusters will be written out to `/tmp/clusters.csv` in the
  99. following format:
  100. .. code-block:: none
  101. cluster_id,opcode_name,config,sched_class
  102. ...
  103. 2,ADD32ri8_DB,,WriteALU,1.00
  104. 2,ADD32ri_DB,,WriteALU,1.01
  105. 2,ADD32rr,,WriteALU,1.01
  106. 2,ADD32rr_DB,,WriteALU,1.00
  107. 2,ADD32rr_REV,,WriteALU,1.00
  108. 2,ADD64i32,,WriteALU,1.01
  109. 2,ADD64ri32,,WriteALU,1.01
  110. 2,MOVSX64rr32,,BSWAP32r_BSWAP64r_MOVSX64rr32,1.00
  111. 2,VPADDQYrr,,VPADDBYrr_VPADDDYrr_VPADDQYrr_VPADDWYrr_VPSUBBYrr_VPSUBDYrr_VPSUBQYrr_VPSUBWYrr,1.02
  112. 2,VPSUBQYrr,,VPADDBYrr_VPADDDYrr_VPADDQYrr_VPADDWYrr_VPSUBBYrr_VPSUBDYrr_VPSUBQYrr_VPSUBWYrr,1.01
  113. 2,ADD64ri8,,WriteALU,1.00
  114. 2,SETBr,,WriteSETCC,1.01
  115. ...
  116. :program:`llvm-exegesis` will also analyze the clusters to point out
  117. inconsistencies in the scheduling information. The output is an html file. For
  118. example, `/tmp/inconsistencies.html` will contain messages like the following :
  119. .. image:: llvm-exegesis-analysis.png
  120. :align: center
  121. Note that the scheduling class names will be resolved only when
  122. :program:`llvm-exegesis` is compiled in debug mode, else only the class id will
  123. be shown. This does not invalidate any of the analysis results though.
  124. OPTIONS
  125. -------
  126. .. option:: -help
  127. Print a summary of command line options.
  128. .. option:: -opcode-index=<LLVM opcode index>
  129. Specify the opcode to measure, by index. See example 1 for details.
  130. Either `opcode-index`, `opcode-name` or `snippets-file` must be set.
  131. .. option:: -opcode-name=<opcode name 1>,<opcode name 2>,...
  132. Specify the opcode to measure, by name. Several opcodes can be specified as
  133. a comma-separated list. See example 1 for details.
  134. Either `opcode-index`, `opcode-name` or `snippets-file` must be set.
  135. .. option:: -snippets-file=<filename>
  136. Specify the custom code snippet to measure. See example 2 for details.
  137. Either `opcode-index`, `opcode-name` or `snippets-file` must be set.
  138. .. option:: -mode=[latency|uops|inverse_throughput|analysis]
  139. Specify the run mode. Note that if you pick `analysis` mode, you also need
  140. to specify at least one of the `-analysis-clusters-output-file=` and
  141. `-analysis-inconsistencies-output-file=`.
  142. .. option:: -num-repetitions=<Number of repetition>
  143. Specify the number of repetitions of the asm snippet.
  144. Higher values lead to more accurate measurements but lengthen the benchmark.
  145. .. option:: -benchmarks-file=</path/to/file>
  146. File to read (`analysis` mode) or write (`latency`/`uops`/`inverse_throughput`
  147. modes) benchmark results. "-" uses stdin/stdout.
  148. .. option:: -analysis-clusters-output-file=</path/to/file>
  149. If provided, write the analysis clusters as CSV to this file. "-" prints to
  150. stdout. By default, this analysis is not run.
  151. .. option:: -analysis-inconsistencies-output-file=</path/to/file>
  152. If non-empty, write inconsistencies found during analysis to this file. `-`
  153. prints to stdout. By default, this analysis is not run.
  154. .. option:: -analysis-clustering=[dbscan,naive]
  155. Specify the clustering algorithm to use. By default DBSCAN will be used.
  156. Naive clustering algorithm is better for doing further work on the
  157. `-analysis-inconsistencies-output-file=` output, it will create one cluster
  158. per opcode, and check that the cluster is stable (all points are neighbours).
  159. .. option:: -analysis-numpoints=<dbscan numPoints parameter>
  160. Specify the numPoints parameters to be used for DBSCAN clustering
  161. (`analysis` mode, DBSCAN only).
  162. .. option:: -analysis-clustering-epsilon=<dbscan epsilon parameter>
  163. Specify the epsilon parameter used for clustering of benchmark points
  164. (`analysis` mode).
  165. .. option:: -analysis-inconsistency-epsilon=<epsilon>
  166. Specify the epsilon parameter used for detection of when the cluster
  167. is different from the LLVM schedule profile values (`analysis` mode).
  168. .. option:: -analysis-display-unstable-clusters
  169. If there is more than one benchmark for an opcode, said benchmarks may end up
  170. not being clustered into the same cluster if the measured performance
  171. characteristics are different. by default all such opcodes are filtered out.
  172. This flag will instead show only such unstable opcodes.
  173. .. option:: -ignore-invalid-sched-class=false
  174. If set, ignore instructions that do not have a sched class (class idx = 0).
  175. .. option:: -mcpu=<cpu name>
  176. If set, measure the cpu characteristics using the counters for this CPU. This
  177. is useful when creating new sched models (the host CPU is unknown to LLVM).
  178. .. option:: --dump-object-to-disk=true
  179. By default, llvm-exegesis will dump the generated code to a temporary file to
  180. enable code inspection. You may disable it to speed up the execution and save
  181. disk space.
  182. EXIT STATUS
  183. -----------
  184. :program:`llvm-exegesis` returns 0 on success. Otherwise, an error message is
  185. printed to standard error, and the tool returns a non 0 value.