brintos

brintos / llvm-project-archived public Read only

0
0
Text · 21.1 KiB · 5996026 Raw
490 lines · plain
1llvm-exegesis - LLVM Machine Instruction Benchmark2==================================================3 4.. program:: llvm-exegesis5 6SYNOPSIS7--------8 9:program:`llvm-exegesis` [*options*]10 11DESCRIPTION12-----------13 14:program:`llvm-exegesis` is a benchmarking tool that uses information available15in LLVM to measure host machine instruction characteristics like latency,16throughput, or port decomposition.17 18Given an LLVM opcode name and a benchmarking mode, :program:`llvm-exegesis`19generates a code snippet that makes execution as serial (resp. as parallel) as20possible so that we can measure the latency (resp. inverse throughput/uop decomposition)21of the instruction.22The code snippet is jitted and, unless requested not to, executed on the23host subtarget. The time taken (resp. resource usage) is measured using24hardware performance counters. The result is printed out as YAML25to the standard output.26 27The main goal of this tool is to automatically (in)validate the LLVM's TableDef28scheduling models. To that end, we also provide analysis of the results.29 30:program:`llvm-exegesis` can also benchmark arbitrary user-provided code31snippets.32 33SUPPORTED PLATFORMS34-------------------35 36:program:`llvm-exegesis` currently only supports X86 (64-bit only), ARM37(AArch64 only, snippet generation is sparse), MIPS, PowerPC (PowerPC64LE38only) and RISC-V (RV64I/E and RV32I/E) on Linux for benchmarking. Not all39benchmarking functionality is guaranteed to work on every platform.40:program:`llvm-exegesis` also has a separate analysis mode that is supported on41every platform that LLVM is.42 43To enable benchmarking in llvm-exegesis, LLVM must be configured and built with44`LLVM_ENABLE_LIBPFM` enabled, as :program:`llvm-exegesis` depends on libpfm445for accessing performance counters. Benchmarking may fail if the target CPU is46unsupported by libpfm. This can be verified by setting `LIBPFM_VERBOSE` and47`LIBPFM_DEBUG` environment variables to enable verbose or debug mode for48libpfm. If libpfm is installed in a non-standard directory, LLVM can be49configured to locate the necessary library and header files by setting50`LIBRARY_PATH`, `C_INCLUDE_PATH`, and `CPLUS_INCLUDE_PATH` environment51variables. Additionally, `LD_LIBRARY_PATH` should be set so that52:program:`llvm-exegesis` can locate the libpfm library during execution.53 54SNIPPET ANNOTATIONS55-------------------56 57:program:`llvm-exegesis` supports benchmarking arbitrary snippets of assembly.58However, benchmarking these snippets often requires some setup so that they59can execute properly. :program:`llvm-exegesis` has five annotations and some60additional utilities to help with setup so that snippets can be benchmarked61properly.62 63* `LLVM-EXEGESIS-DEFREG <register name>` - Adding this annotation to the text64  assembly snippet to be benchmarked marks the register as requiring a definition.65  A value will automatically be provided unless a second parameter, a hex value,66  is passed in. This is done with the `LLVM-EXEGESIS-DEFREG <register name> <hex value>`67  format. `<hex value>` is a bit pattern used to fill the register. If it is a68  value smaller than the register, it is sign extended to match the size of the69  register.70* `LLVM-EXEGESIS-LIVEIN <register name>` - This annotation allows specifying71  registers that should keep their value upon starting the benchmark. Values72  can be passed through registers from the benchmarking setup in some cases.73  The registers and the values assigned to them that can be utilized in the74  benchmarking script with a `LLVM-EXEGESIS-LIVEIN` are as follows:75 76  * Scratch memory register - The specific register that this value is put in77    is platform dependent (e.g., it is the RDI register on X86 Linux). Setting78    this register as a live in ensures that a pointer to a block of memory (1MB)79    is placed within this register that can be used by the snippet.80* `LLVM-EXEGESIS-MEM-DEF <value name> <size> <value>` - This annotation allows81  specifying memory definitions that can later be mapped into the execution82  process of a snippet with the `LLVM-EXEGESIS-MEM-MAP` annotation. Each83  value is named using the `<value name>` argument so that it can be referenced84  later within a map annotation. The size is specified in a decimal number of85  bytes and the value is given in hexadecimal. If the size of the value is less86  than the specified size, the value will be repeated until it fills the entire87  section of memory. Using this annotation requires using the subprocess execution88  mode.89* `LLVM-EXEGESIS-MEM-MAP <value name> <address>` - This annotation allows for90  mapping previously defined memory definitions into the execution context of a91  process. The value name refers to a previously defined memory definition and92  the address is a decimal number that specifies the address the memory93  definition should start at. Note that a single memory definition can be94  mapped multiple times. Using this annotation requires the subprocess95  execution mode.96* `LLVM-EXEGESIS-SNIPPET-ADDRESS <address>` - This annotation allows for97  setting the address where the beginning of the snippet to be executed will98  be mapped in at. The address is given in hexadecimal. Note that the snippet99  also includes setup code, so the instruction exactly at the specified100  address will not be the first instruction in the snippet. Using this101  annotation requires the subprocess execution mode. This is useful in102  cases where the memory accessed by the snippet depends on the location103  of the snippet, like RIP-relative addressing.104* `LLVM-EXEGESIS-LOOP-REGISTER <register name>` - This annotation specifies105  the loop register to use for keeping track of the current iteration when106  using the loop repetition mode. :program:`llvm-exegesis` needs to keep track107  of the current loop iteration within the loop repetition mode in a performant108  manner (i.e., no memory accesses), and uses a register to do this. This register109  has an architecture-specific default (e.g., `R8` on X86), but this might conflict110  with some snippets. This annotation allows changing the register to prevent111  interference between the loop index register and the snippet.112 113EXAMPLE 1: benchmarking instructions114------------------------------------115 116Assume you have an X86-64 machine. To measure the latency of a single117instruction, run:118 119.. code-block:: bash120 121    $ llvm-exegesis --mode=latency --opcode-name=ADD64rr122 123Measuring the uop decomposition or inverse throughput of an instruction works similarly:124 125.. code-block:: bash126 127    $ llvm-exegesis --mode=uops --opcode-name=ADD64rr128    $ llvm-exegesis --mode=inverse_throughput --opcode-name=ADD64rr129 130 131The output is a YAML document (the default is to write to stdout, but you can132redirect the output to a file using `--benchmarks-file`):133 134.. code-block:: none135 136  ---137  key:138    opcode_name:     ADD64rr139    mode:            latency140    config:          ''141  cpu_name:        haswell142  llvm_triple:     x86_64-unknown-linux-gnu143  num_repetitions: 10000144  measurements:145    - { key: latency, value: 1.0058, debug_string: '' }146  error:           ''147  info:            'explicit self cycles, selecting one aliasing configuration.148  Snippet:149  ADD64rr R8, R8, R10150  '151  ...152 153To measure the latency of all instructions for the host architecture, run:154 155.. code-block:: bash156 157    $ llvm-exegesis --mode=latency --opcode-index=-1158 159 160EXAMPLE 2: benchmarking a custom code snippet161---------------------------------------------162 163To measure the latency/uops of a custom piece of code, you can specify the164`snippets-file` option (`-` reads from standard input).165 166.. code-block:: bash167 168    $ echo "vzeroupper" | llvm-exegesis --mode=uops --snippets-file=-169 170Real-life code snippets typically depend on registers or memory.171:program:`llvm-exegesis` checks the liveliness of registers (i.e. any register172use has a corresponding def or is a "live in"). If your code depends on the173value of some registers, you need to use snippet annotations to ensure setup174is performed properly.175 176For example, the following code snippet depends on the values of XMM1 (which177will be set by the tool) and the memory buffer passed in RDI (live in).178 179.. code-block:: none180 181  # LLVM-EXEGESIS-LIVEIN RDI182  # LLVM-EXEGESIS-DEFREG XMM1 42183  vmulps	(%rdi), %xmm1, %xmm2184  vhaddps	%xmm2, %xmm2, %xmm3185  addq $0x10, %rdi186 187 188Example 3: benchmarking with memory annotations189-----------------------------------------------190 191Some snippets require memory setup in specific places to execute without192crashing. Setting up memory can be accomplished with the `LLVM-EXEGESIS-MEM-DEF`193and `LLVM-EXEGESIS-MEM-MAP` annotations. To execute the following snippet:194 195.. code-block:: none196 197    movq $8192, %rax198    movq (%rax), %rdi199 200We need to have at least eight bytes of memory allocated starting `0x2000`.201We can create the necessary execution environment with the following202annotations added to the snippet:203 204.. code-block:: none205 206  # LLVM-EXEGESIS-MEM-DEF test1 4096 7fffffff207  # LLVM-EXEGESIS-MEM-MAP test1 8192208 209  movq $8192, %rax210  movq (%rax), %rdi211 212EXAMPLE 4: analysis213-------------------214 215Assuming you have a set of benchmarked instructions (either latency or uops) as216YAML in file `/tmp/benchmarks.yaml`, you can analyze the results using the217following command:218 219.. code-block:: bash220 221    $ llvm-exegesis --mode=analysis \222  --benchmarks-file=/tmp/benchmarks.yaml \223  --analysis-clusters-output-file=/tmp/clusters.csv \224  --analysis-inconsistencies-output-file=/tmp/inconsistencies.html225 226This will group the instructions into clusters with the same performance227characteristics. The clusters will be written out to `/tmp/clusters.csv` in the228following format:229 230.. code-block:: none231 232  cluster_id,opcode_name,config,sched_class233  ...234  2,ADD32ri8_DB,,WriteALU,1.00235  2,ADD32ri_DB,,WriteALU,1.01236  2,ADD32rr,,WriteALU,1.01237  2,ADD32rr_DB,,WriteALU,1.00238  2,ADD32rr_REV,,WriteALU,1.00239  2,ADD64i32,,WriteALU,1.01240  2,ADD64ri32,,WriteALU,1.01241  2,MOVSX64rr32,,BSWAP32r_BSWAP64r_MOVSX64rr32,1.00242  2,VPADDQYrr,,VPADDBYrr_VPADDDYrr_VPADDQYrr_VPADDWYrr_VPSUBBYrr_VPSUBDYrr_VPSUBQYrr_VPSUBWYrr,1.02243  2,VPSUBQYrr,,VPADDBYrr_VPADDDYrr_VPADDQYrr_VPADDWYrr_VPSUBBYrr_VPSUBDYrr_VPSUBQYrr_VPSUBWYrr,1.01244  2,ADD64ri8,,WriteALU,1.00245  2,SETBr,,WriteSETCC,1.01246  ...247 248:program:`llvm-exegesis` will also analyze the clusters to point out249inconsistencies in the scheduling information. The output is an html file. For250example, `/tmp/inconsistencies.html` will contain messages like the following :251 252.. image:: llvm-exegesis-analysis.png253  :align: center254 255Note that the scheduling class names will be resolved only when256:program:`llvm-exegesis` is compiled in debug mode, else only the class id will257be shown. This does not invalidate any of the analysis results though.258 259OPTIONS260-------261 262.. option:: --help263 264 Print a summary of command line options.265 266.. option:: --opcode-index=<LLVM opcode index>267 268 Specify the opcode to measure, by index. Specifying `-1` will result269 in measuring every existing opcode. See example 1 for details.270 Either `opcode-index`, `opcode-name` or `snippets-file` must be set.271 272.. option:: --opcode-name=<opcode name 1>,<opcode name 2>,...273 274 Specify the opcode to measure, by name. Several opcodes can be specified as275 a comma-separated list. See example 1 for details.276 Either `opcode-index`, `opcode-name` or `snippets-file` must be set.277 278.. option:: --snippets-file=<filename>279 280 Specify the custom code snippet to measure. See example 2 for details.281 Either `opcode-index`, `opcode-name` or `snippets-file` must be set.282 283.. option:: --mode=[latency|uops|inverse_throughput|analysis]284 285 Specify the run mode. Note that some modes have additional requirements and options.286 287 `latency` mode can be  make use of either RDTSC or LBR.288 `latency[LBR]` is only available on X86 (at least `Skylake`).289 To run in `latency` mode, a positive value must be specified290 for `x86-lbr-sample-period` and `--repetition-mode=loop`.291 292 In `analysis` mode, you also need to specify at least one of the293 `-analysis-clusters-output-file=` and `-analysis-inconsistencies-output-file=`.294 295.. option:: --benchmark-phase=[prepare-snippet|prepare-and-assemble-snippet|assemble-measured-code|measure]296 297  By default, when `-mode=` is specified, the generated snippet will be executed298  and measured, and that requires that we are running on the hardware for which299  the snippet was generated, and that supports performance measurements.300  However, it is possible to stop at some stage before measuring. Choices are:301  * ``prepare-snippet``: Only generate the minimal instruction sequence.302  * ``prepare-and-assemble-snippet``: Same as ``prepare-snippet``, but also dumps an excerpt of the sequence (hex encoded).303  * ``assemble-measured-code``: Same as ``prepare-and-assemble-snippet``. but also creates the full sequence that can be dumped to a file using ``--dump-object-to-disk``.304  * ``measure``: Same as ``assemble-measured-code``, but also runs the measurement.305 306.. option:: --x86-lbr-sample-period=<nBranches/sample>307 308  Specify the LBR sampling period - how many branches before we take a sample.309  When a positive value is specified for this option and when the mode is `latency`,310  we will use LBRs for measuring.311  On choosing the "right" sampling period, a small value is preferred, but throttling312  could occur if the sampling is too frequent. A prime number should be used to313  avoid consistently skipping certain blocks.314 315.. option:: --x86-disable-upper-sse-registers316 317  Using the upper xmm registers (xmm8-xmm15) forces a longer instruction encoding318  which may put greater pressure on the frontend fetch and decode stages,319  potentially reducing the rate that instructions are dispatched to the backend,320  particularly on older hardware. Comparing baseline results with this mode321  enabled can help determine the effects of the frontend and can be used to322  improve latency and throughput estimates.323 324.. option:: --repetition-mode=[duplicate|loop|min|middle-half-duplicate|middle-half-loop]325 326 Specify the repetition mode. `duplicate` will create a large, straight line327 basic block with `min-instructions` instructions (repeating the snippet328 `min-instructions`/`snippet size` times). `loop` will, optionally, duplicate the329 snippet until the loop body contains at least `loop-body-size` instructions,330 and then wrap the result in a loop which will execute `min-instructions`331 instructions (thus, again, repeating the snippet332 `min-instructions`/`snippet size` times). The `loop` mode, especially with loop333 unrolling tends to better hide the effects of the CPU frontend on architectures334 that cache decoded instructions, but consumes a register for counting335 iterations. If performing an analysis over many opcodes, it may be best to336 instead use the `min` mode, which will run each other mode,337 and produce the minimal measured result. The middle half repetition modes338 will either duplicate or run the snippet in a loop depending upon the specific339 mode. The middle half repetition modes will run two benchmarks, one twice the340 length of the first one, and then subtract the difference between them to get341 values without overhead.342 343.. option:: --min-instructions=<Number of instructions>344 345 Specify the target number of executed instructions. Note that the actual346 repetition count of the snippet will be `min-instructions`/`snippet size`.347 Higher values lead to more accurate measurements but lengthen the benchmark.348 349.. option:: --loop-body-size=<Preferred loop body size>350 351 Only effective for `-repetition-mode=[loop|min]`.352 Instead of looping over the snippet directly, first duplicate it so that the353 loop body contains at least this many instructions. This potentially results354 in loop body being cached in the CPU Op Cache / Loop Cache, which allows to355 which may have higher throughput than the CPU decoders.356 357.. option:: --max-configs-per-opcode=<value>358 359 Specify the maximum configurations that can be generated for each opcode.360 By default this is `1`, meaning that we assume that a single measurement is361 enough to characterize an opcode. This might not be true of all instructions:362 for example, the performance characteristics of the LEA instruction on X86363 depends on the value of assigned registers and immediates. Setting a value of364 `-max-configs-per-opcode` larger than `1` allows `llvm-exegesis` to explore365 more configurations to discover if some register or immediate assignments366 lead to different performance characteristics.367 368 369.. option:: --benchmarks-file=</path/to/file>370 371 File to read (`analysis` mode) or write (`latency`/`uops`/`inverse_throughput`372 modes) benchmark results. "-" uses stdin/stdout.373 374.. option:: --analysis-clusters-output-file=</path/to/file>375 376 If provided, write the analysis clusters as CSV to this file. "-" prints to377 stdout. By default, this analysis is not run.378 379.. option:: --analysis-inconsistencies-output-file=</path/to/file>380 381 If non-empty, write inconsistencies found during analysis to this file. `-`382 prints to stdout. By default, this analysis is not run.383 384.. option:: --analysis-filter=[all|reg-only|mem-only]385 386 By default, all benchmark results are analysed, but sometimes it may be useful387 to only look at those that to not involve memory, or vice versa. This option388 allows to either keep all benchmarks, or filter out (ignore) either all the389 ones that do involve memory (involve instructions that may read or write to390 memory), or the opposite, to only keep such benchmarks.391 392.. option:: --analysis-clustering=[dbscan,naive]393 394 Specify the clustering algorithm to use. By default DBSCAN will be used.395 Naive clustering algorithm is better for doing further work on the396 `-analysis-inconsistencies-output-file=` output, it will create one cluster397 per opcode, and check that the cluster is stable (all points are neighbours).398 399.. option:: --analysis-numpoints=<dbscan numPoints parameter>400 401 Specify the numPoints parameters to be used for DBSCAN clustering402 (`analysis` mode, DBSCAN only).403 404.. option:: --analysis-clustering-epsilon=<dbscan epsilon parameter>405 406 Specify the epsilon parameter used for clustering of benchmark points407 (`analysis` mode).408 409.. option:: --analysis-inconsistency-epsilon=<epsilon>410 411 Specify the epsilon parameter used for detection of when the cluster412 is different from the LLVM schedule profile values (`analysis` mode).413 414.. option:: --analysis-display-unstable-clusters415 416 If there is more than one benchmark for an opcode, said benchmarks may end up417 not being clustered into the same cluster if the measured performance418 characteristics are different. by default all such opcodes are filtered out.419 This flag will instead show only such unstable opcodes.420 421.. option:: --ignore-invalid-sched-class=false422 423 If set, ignore instructions that do not have a sched class (class idx = 0).424 425.. option:: --mtriple=<triple name>426 427 Target triple. See `-version` for available targets.428 429.. option:: --mcpu=<cpu name>430 431 If set, measure the cpu characteristics using the counters for this CPU. This432 is useful when creating new sched models (the host CPU is unknown to LLVM).433 (`-mcpu=help` for details)434 435.. option:: --analysis-override-benchmark-triple-and-cpu436 437  By default, llvm-exegesis will analyze the benchmarks for the triple/CPU they438  were measured for, but if you want to analyze them for some other combination439  (specified via `-mtriple`/`-mcpu`), you can pass this flag.440 441.. option:: --dump-object-to-disk=<filename>442 443 If set,  llvm-exegesis will dump the generated code to a temporary file to444 enable code inspection. Disabled by default.445 446.. option:: --use-dummy-perf-counters447 448 If set, llvm-exegesis will not read any real performance counters and449 return a dummy value instead. This can be used to ensure a snippet doesn't450 crash when hardware performance counters are unavailable and for451 debugging :program:`llvm-exegesis` itself.452 453.. option:: --execution-mode=[inprocess,subprocess]454 455  This option specifies what execution mode to use. The `inprocess` execution456  mode is the default. The `subprocess` execution mode allows for additional457  features such as memory annotations but is currently restricted to X86-64458  on Linux.459 460.. option:: --benchmark-repeat-count=<repeat-count>461 462  This option enables specifying the number of times to repeat the measurement463  when performing latency measurements. By default, llvm-exegesis will repeat464  a latency measurement enough times to balance run-time and noise reduction.465 466.. option:: --validation-counter=[instructions-retired,l1d-cache-load-misses,467   l1d-cache-store-misses,l1i-cache-load-misses,data-tlb-load-misses,468   data-tld-store-misses,instruction-tlb-load-misses]469 470   This option enables the use of validation counters, which measure additional471   microarchitectural events like cache misses to validate snippet execution472   conditions. These events are measured using the perf subsystem in a group473   with the performance counter used to measure the value of interest. This474   flag can be specified multiple times to measure multiple events. The maximum475   number of validation counters is platform dependent.476 477.. option:: --benchmark-process-cpu=<cpu id>478 479  This option specifies the number of the CPU that should be used to run the480  benchmarking subprocess. When starting the subprocess,481  :program:`llvm-exegesis` will set the affinity of the subprocess to only482  include the specified CPU. This option only works in the subprocess execution483  mode.484 485EXIT STATUS486-----------487 488:program:`llvm-exegesis` returns 0 on success. Otherwise, an error message is489printed to standard error, and the tool returns a non 0 value.490