BlockFrequencyTerminology.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ================================
  2. LLVM Block Frequency Terminology
  3. ================================
  4. .. contents::
  5. :local:
  6. Introduction
  7. ============
  8. Block Frequency is a metric for estimating the relative frequency of different
  9. basic blocks. This document describes the terminology that the
  10. ``BlockFrequencyInfo`` and ``MachineBlockFrequencyInfo`` analysis passes use.
  11. Branch Probability
  12. ==================
  13. Blocks with multiple successors have probabilities associated with each
  14. outgoing edge. These are called branch probabilities. For a given block, the
  15. sum of its outgoing branch probabilities should be 1.0.
  16. Branch Weight
  17. =============
  18. Rather than storing fractions on each edge, we store an integer weight.
  19. Weights are relative to the other edges of a given predecessor block. The
  20. branch probability associated with a given edge is its own weight divided by
  21. the sum of the weights on the predecessor's outgoing edges.
  22. For example, consider this IR:
  23. .. code-block:: llvm
  24. define void @foo() {
  25. ; ...
  26. A:
  27. br i1 %cond, label %B, label %C, !prof !0
  28. ; ...
  29. }
  30. !0 = metadata !{metadata !"branch_weights", i32 7, i32 8}
  31. and this simple graph representation::
  32. A -> B (edge-weight: 7)
  33. A -> C (edge-weight: 8)
  34. The probability of branching from block A to block B is 7/15, and the
  35. probability of branching from block A to block C is 8/15.
  36. See :doc:`BranchWeightMetadata` for details about the branch weight IR
  37. representation.
  38. Block Frequency
  39. ===============
  40. Block frequency is a relative metric that represents the number of times a
  41. block executes. The ratio of a block frequency to the entry block frequency is
  42. the expected number of times the block will execute per entry to the function.
  43. Block frequency is the main output of the ``BlockFrequencyInfo`` and
  44. ``MachineBlockFrequencyInfo`` analysis passes.
  45. Implementation: a series of DAGs
  46. ================================
  47. The implementation of the block frequency calculation analyses each loop,
  48. bottom-up, ignoring backedges; i.e., as a DAG. After each loop is processed,
  49. it's packaged up to act as a pseudo-node in its parent loop's (or the
  50. function's) DAG analysis.
  51. Block Mass
  52. ==========
  53. For each DAG, the entry node is assigned a mass of ``UINT64_MAX`` and mass is
  54. distributed to successors according to branch weights. Block Mass uses a
  55. fixed-point representation where ``UINT64_MAX`` represents ``1.0`` and ``0``
  56. represents a number just above ``0.0``.
  57. After mass is fully distributed, in any cut of the DAG that separates the exit
  58. nodes from the entry node, the sum of the block masses of the nodes succeeded
  59. by a cut edge should equal ``UINT64_MAX``. In other words, mass is conserved
  60. as it "falls" through the DAG.
  61. If a function's basic block graph is a DAG, then block masses are valid block
  62. frequencies. This works poorly in practise though, since downstream users rely
  63. on adding block frequencies together without hitting the maximum.
  64. Loop Scale
  65. ==========
  66. Loop scale is a metric that indicates how many times a loop iterates per entry.
  67. As mass is distributed through the loop's DAG, the (otherwise ignored) backedge
  68. mass is collected. This backedge mass is used to compute the exit frequency,
  69. and thus the loop scale.
  70. Implementation: Getting from mass and scale to frequency
  71. ========================================================
  72. After analysing the complete series of DAGs, each block has a mass (local to
  73. its containing loop, if any), and each loop pseudo-node has a loop scale and
  74. its own mass (from its parent's DAG).
  75. We can get an initial frequency assignment (with entry frequency of 1.0) by
  76. multiplying these masses and loop scales together. A given block's frequency
  77. is the product of its mass, the mass of containing loops' pseudo nodes, and the
  78. containing loops' loop scales.
  79. Since downstream users need integers (not floating point), this initial
  80. frequency assignment is shifted as necessary into the range of ``uint64_t``.
  81. Block Bias
  82. ==========
  83. Block bias is a proposed *absolute* metric to indicate a bias toward or away
  84. from a given block during a function's execution. The idea is that bias can be
  85. used in isolation to indicate whether a block is relatively hot or cold, or to
  86. compare two blocks to indicate whether one is hotter or colder than the other.
  87. The proposed calculation involves calculating a *reference* block frequency,
  88. where:
  89. * every branch weight is assumed to be 1 (i.e., every branch probability
  90. distribution is even) and
  91. * loop scales are ignored.
  92. This reference frequency represents what the block frequency would be in an
  93. unbiased graph.
  94. The bias is the ratio of the block frequency to this reference block frequency.