1*9880d681SAndroid Build Coastguard Worker================================ 2*9880d681SAndroid Build Coastguard WorkerLLVM Block Frequency Terminology 3*9880d681SAndroid Build Coastguard Worker================================ 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard Worker.. contents:: 6*9880d681SAndroid Build Coastguard Worker :local: 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard WorkerIntroduction 9*9880d681SAndroid Build Coastguard Worker============ 10*9880d681SAndroid Build Coastguard Worker 11*9880d681SAndroid Build Coastguard WorkerBlock Frequency is a metric for estimating the relative frequency of different 12*9880d681SAndroid Build Coastguard Workerbasic blocks. This document describes the terminology that the 13*9880d681SAndroid Build Coastguard Worker``BlockFrequencyInfo`` and ``MachineBlockFrequencyInfo`` analysis passes use. 14*9880d681SAndroid Build Coastguard Worker 15*9880d681SAndroid Build Coastguard WorkerBranch Probability 16*9880d681SAndroid Build Coastguard Worker================== 17*9880d681SAndroid Build Coastguard Worker 18*9880d681SAndroid Build Coastguard WorkerBlocks with multiple successors have probabilities associated with each 19*9880d681SAndroid Build Coastguard Workeroutgoing edge. These are called branch probabilities. For a given block, the 20*9880d681SAndroid Build Coastguard Workersum of its outgoing branch probabilities should be 1.0. 21*9880d681SAndroid Build Coastguard Worker 22*9880d681SAndroid Build Coastguard WorkerBranch Weight 23*9880d681SAndroid Build Coastguard Worker============= 24*9880d681SAndroid Build Coastguard Worker 25*9880d681SAndroid Build Coastguard WorkerRather than storing fractions on each edge, we store an integer weight. 26*9880d681SAndroid Build Coastguard WorkerWeights are relative to the other edges of a given predecessor block. The 27*9880d681SAndroid Build Coastguard Workerbranch probability associated with a given edge is its own weight divided by 28*9880d681SAndroid Build Coastguard Workerthe sum of the weights on the predecessor's outgoing edges. 29*9880d681SAndroid Build Coastguard Worker 30*9880d681SAndroid Build Coastguard WorkerFor example, consider this IR: 31*9880d681SAndroid Build Coastguard Worker 32*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm 33*9880d681SAndroid Build Coastguard Worker 34*9880d681SAndroid Build Coastguard Worker define void @foo() { 35*9880d681SAndroid Build Coastguard Worker ; ... 36*9880d681SAndroid Build Coastguard Worker A: 37*9880d681SAndroid Build Coastguard Worker br i1 %cond, label %B, label %C, !prof !0 38*9880d681SAndroid Build Coastguard Worker ; ... 39*9880d681SAndroid Build Coastguard Worker } 40*9880d681SAndroid Build Coastguard Worker !0 = metadata !{metadata !"branch_weights", i32 7, i32 8} 41*9880d681SAndroid Build Coastguard Worker 42*9880d681SAndroid Build Coastguard Workerand this simple graph representation:: 43*9880d681SAndroid Build Coastguard Worker 44*9880d681SAndroid Build Coastguard Worker A -> B (edge-weight: 7) 45*9880d681SAndroid Build Coastguard Worker A -> C (edge-weight: 8) 46*9880d681SAndroid Build Coastguard Worker 47*9880d681SAndroid Build Coastguard WorkerThe probability of branching from block A to block B is 7/15, and the 48*9880d681SAndroid Build Coastguard Workerprobability of branching from block A to block C is 8/15. 49*9880d681SAndroid Build Coastguard Worker 50*9880d681SAndroid Build Coastguard WorkerSee :doc:`BranchWeightMetadata` for details about the branch weight IR 51*9880d681SAndroid Build Coastguard Workerrepresentation. 52*9880d681SAndroid Build Coastguard Worker 53*9880d681SAndroid Build Coastguard WorkerBlock Frequency 54*9880d681SAndroid Build Coastguard Worker=============== 55*9880d681SAndroid Build Coastguard Worker 56*9880d681SAndroid Build Coastguard WorkerBlock frequency is a relative metric that represents the number of times a 57*9880d681SAndroid Build Coastguard Workerblock executes. The ratio of a block frequency to the entry block frequency is 58*9880d681SAndroid Build Coastguard Workerthe expected number of times the block will execute per entry to the function. 59*9880d681SAndroid Build Coastguard Worker 60*9880d681SAndroid Build Coastguard WorkerBlock frequency is the main output of the ``BlockFrequencyInfo`` and 61*9880d681SAndroid Build Coastguard Worker``MachineBlockFrequencyInfo`` analysis passes. 62*9880d681SAndroid Build Coastguard Worker 63*9880d681SAndroid Build Coastguard WorkerImplementation: a series of DAGs 64*9880d681SAndroid Build Coastguard Worker================================ 65*9880d681SAndroid Build Coastguard Worker 66*9880d681SAndroid Build Coastguard WorkerThe implementation of the block frequency calculation analyses each loop, 67*9880d681SAndroid Build Coastguard Workerbottom-up, ignoring backedges; i.e., as a DAG. After each loop is processed, 68*9880d681SAndroid Build Coastguard Workerit's packaged up to act as a pseudo-node in its parent loop's (or the 69*9880d681SAndroid Build Coastguard Workerfunction's) DAG analysis. 70*9880d681SAndroid Build Coastguard Worker 71*9880d681SAndroid Build Coastguard WorkerBlock Mass 72*9880d681SAndroid Build Coastguard Worker========== 73*9880d681SAndroid Build Coastguard Worker 74*9880d681SAndroid Build Coastguard WorkerFor each DAG, the entry node is assigned a mass of ``UINT64_MAX`` and mass is 75*9880d681SAndroid Build Coastguard Workerdistributed to successors according to branch weights. Block Mass uses a 76*9880d681SAndroid Build Coastguard Workerfixed-point representation where ``UINT64_MAX`` represents ``1.0`` and ``0`` 77*9880d681SAndroid Build Coastguard Workerrepresents a number just above ``0.0``. 78*9880d681SAndroid Build Coastguard Worker 79*9880d681SAndroid Build Coastguard WorkerAfter mass is fully distributed, in any cut of the DAG that separates the exit 80*9880d681SAndroid Build Coastguard Workernodes from the entry node, the sum of the block masses of the nodes succeeded 81*9880d681SAndroid Build Coastguard Workerby a cut edge should equal ``UINT64_MAX``. In other words, mass is conserved 82*9880d681SAndroid Build Coastguard Workeras it "falls" through the DAG. 83*9880d681SAndroid Build Coastguard Worker 84*9880d681SAndroid Build Coastguard WorkerIf a function's basic block graph is a DAG, then block masses are valid block 85*9880d681SAndroid Build Coastguard Workerfrequencies. This works poorly in practise though, since downstream users rely 86*9880d681SAndroid Build Coastguard Workeron adding block frequencies together without hitting the maximum. 87*9880d681SAndroid Build Coastguard Worker 88*9880d681SAndroid Build Coastguard WorkerLoop Scale 89*9880d681SAndroid Build Coastguard Worker========== 90*9880d681SAndroid Build Coastguard Worker 91*9880d681SAndroid Build Coastguard WorkerLoop scale is a metric that indicates how many times a loop iterates per entry. 92*9880d681SAndroid Build Coastguard WorkerAs mass is distributed through the loop's DAG, the (otherwise ignored) backedge 93*9880d681SAndroid Build Coastguard Workermass is collected. This backedge mass is used to compute the exit frequency, 94*9880d681SAndroid Build Coastguard Workerand thus the loop scale. 95*9880d681SAndroid Build Coastguard Worker 96*9880d681SAndroid Build Coastguard WorkerImplementation: Getting from mass and scale to frequency 97*9880d681SAndroid Build Coastguard Worker======================================================== 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard WorkerAfter analysing the complete series of DAGs, each block has a mass (local to 100*9880d681SAndroid Build Coastguard Workerits containing loop, if any), and each loop pseudo-node has a loop scale and 101*9880d681SAndroid Build Coastguard Workerits own mass (from its parent's DAG). 102*9880d681SAndroid Build Coastguard Worker 103*9880d681SAndroid Build Coastguard WorkerWe can get an initial frequency assignment (with entry frequency of 1.0) by 104*9880d681SAndroid Build Coastguard Workermultiplying these masses and loop scales together. A given block's frequency 105*9880d681SAndroid Build Coastguard Workeris the product of its mass, the mass of containing loops' pseudo nodes, and the 106*9880d681SAndroid Build Coastguard Workercontaining loops' loop scales. 107*9880d681SAndroid Build Coastguard Worker 108*9880d681SAndroid Build Coastguard WorkerSince downstream users need integers (not floating point), this initial 109*9880d681SAndroid Build Coastguard Workerfrequency assignment is shifted as necessary into the range of ``uint64_t``. 110*9880d681SAndroid Build Coastguard Worker 111*9880d681SAndroid Build Coastguard WorkerBlock Bias 112*9880d681SAndroid Build Coastguard Worker========== 113*9880d681SAndroid Build Coastguard Worker 114*9880d681SAndroid Build Coastguard WorkerBlock bias is a proposed *absolute* metric to indicate a bias toward or away 115*9880d681SAndroid Build Coastguard Workerfrom a given block during a function's execution. The idea is that bias can be 116*9880d681SAndroid Build Coastguard Workerused in isolation to indicate whether a block is relatively hot or cold, or to 117*9880d681SAndroid Build Coastguard Workercompare two blocks to indicate whether one is hotter or colder than the other. 118*9880d681SAndroid Build Coastguard Worker 119*9880d681SAndroid Build Coastguard WorkerThe proposed calculation involves calculating a *reference* block frequency, 120*9880d681SAndroid Build Coastguard Workerwhere: 121*9880d681SAndroid Build Coastguard Worker 122*9880d681SAndroid Build Coastguard Worker* every branch weight is assumed to be 1 (i.e., every branch probability 123*9880d681SAndroid Build Coastguard Worker distribution is even) and 124*9880d681SAndroid Build Coastguard Worker 125*9880d681SAndroid Build Coastguard Worker* loop scales are ignored. 126*9880d681SAndroid Build Coastguard Worker 127*9880d681SAndroid Build Coastguard WorkerThis reference frequency represents what the block frequency would be in an 128*9880d681SAndroid Build Coastguard Workerunbiased graph. 129*9880d681SAndroid Build Coastguard Worker 130*9880d681SAndroid Build Coastguard WorkerThe bias is the ratio of the block frequency to this reference block frequency. 131