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