Convergence And Uniformity¶
Introduction¶
In some environments, groups of threads execute the same program in parallel, where efficient communication within a group is established using special primitives called convergent operations. The outcome of a convergent operation is sensitive to the set of threads that participate in it.
The intuitive picture of convergence is built around threads executing in “lock step” — a set of threads is thought of as converged if they are all executing “the same sequence of instructions together”. Such threads may diverge at a divergent branch, and they may later reconverge at some common program point.
In this intuitive picture, when converged threads execute an instruction, the resulting value is said to be uniform if it is the same in those threads, and divergent otherwise. Correspondingly, a branch is said to be a uniform branch if its condition is uniform, and it is a divergent branch otherwise.
But the assumption of lock-step execution is not necessary for describing communication at convergent operations. It also constrains the implementation (compiler as well as hardware) by overspecifying how threads execute in such a parallel environment. To eliminate this assumption:
We define convergence as a relation between the execution of each instruction by different threads and not as a relation between the threads themselves. This definition is reasonable for known targets and is compatible with the semantics of convergent operations in LLVM IR.
We also define uniformity in terms of this convergence. The output of an instruction can be examined for uniformity across multiple threads only if the corresponding executions of that instruction are converged.
This document decribes a static analysis for determining convergence at each instruction in a function. The analysis extends previous work on divergence analysis [DivergenceSPMD] to cover irreducible control-flow. The described analysis is used in LLVM to implement a UniformityAnalysis that determines the uniformity of value(s) computed at each instruction in an LLVM IR or MIR function.
Julian Rosemann, Simon Moll, and Sebastian Hack. 2021. An Abstract Interpretation for SPMD Divergence on Reducible Control Flow Graphs. Proc. ACM Program. Lang. 5, POPL, Article 31 (January 2021), 35 pages. https://doi.org/10.1145/3434312
Motivation¶
Divergent branches constrain program transforms such as changing the CFG or moving a convergent operation to a different point of the CFG. Performing these transformations across a divergent branch can change the sets of threads that execute convergent operations convergently. While these constraints are out of scope for this document, uniformity analysis allows these transformations to identify uniform branches where these constraints do not hold.
Uniformity is also useful by itself on targets that execute threads in groups with shared execution resources (e.g. waves, warps, or subgroups):
Uniform outputs can potentially be computed or stored on shared resources.
These targets must “linearize” a divergent branch to ensure that each side of the branch is followed by the corresponding threads in the same group. But linearization is unnecessary at uniform branches, since the whole group of threads follows either one side of the branch or the other.
Terminology¶
- Cycles
Described in LLVM Cycle Terminology.
- Closed path
Described in Closed Paths and Cycles.
- Disjoint paths
Two paths in a CFG are said to be disjoint if the only nodes common to both are the start node or the end node, or both.
- Join node
A join node of a branch is a node reachable along disjoint paths starting from that branch.
- Diverged path
A diverged path is a path that starts from a divergent branch and either reaches a join node of the branch or reaches the end of the function without passing through any join node of the branch.
Threads and Dynamic Instances¶
Each occurrence of an instruction in the program source is called a static instance. When a thread executes a program, each execution of a static instance produces a distinct dynamic instance of that instruction.
Each thread produces a unique sequence of dynamic instances:
The sequence is generated along branch decisions and loop traversals.
Starts with a dynamic instance of a “first” instruction.
Continues with dynamic instances of successive “next” instructions.
Threads are independent; some targets may choose to execute them in groups in order to share resources when possible.
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|||
Thread 1 |
Entry1 |
H1 |
B1 |
L1 |
H3 |
L3 |
Exit |
||||
Thread 2 |
Entry1 |
H2 |
L2 |
H4 |
B2 |
L4 |
H5 |
B3 |
L5 |
Exit |
In the above table, each row is a different thread, listing the
dynamic instances produced by that thread from left to right. Each
thread executes the same program that starts with an Entry node
and ends with an Exit node, but different threads may take
different paths through the control flow of the program. The columns
are numbered merely for convenience, and empty cells have no special
meaning. Dynamic instances listed in the same column are converged.
Convergence¶
Convergence-before is a strict partial order over dynamic instances that is defined as the transitive closure of:
If dynamic instance
Pis executed strictly beforeQin the same thread, thenPis convergence-beforeQ.If dynamic instance
Pis executed strictly beforeQ1in the same thread, andQ1is converged-withQ2, thenPis convergence-beforeQ2.If dynamic instance
P1is converged-withP2, andP2is executed strictly beforeQin the same thread, thenP1is convergence-beforeQ.
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|
Thread 1 |
Entry |
… |
S2 |
T |
… |
Exit |
|||
Thread 2 |
Entry |
… |
Q2 |
R |
S1 |
… |
Exit |
||
Thread 3 |
Entry |
… |
P |
Q1 |
… |
The above table shows partial sequences of dynamic instances from
different threads. Dynamic instances in the same column are assumed
to be converged (i.e., related to each other in the converged-with
relation). The resulting convergence order includes the edges P ->
Q2, Q1 -> R, P -> R, P -> T, etc.
Converged-with is a transitive symmetric relation over dynamic instances produced by different threads for the same static instance.
It is impractical to provide any one definition for the converged-with relation, since different environments may wish to relate dynamic instances in different ways. The fact that convergence-before is a strict partial order is a constraint on the converged-with relation. It is trivially satisfied if different dynamic instances are never converged. Below, we provide a relation called maximal converged-with, which satisifies convergence-before and is suitable for known targets.
Note
The convergence-before relation is not directly observable. Program transforms are in general free to change the order of instructions, even though that obviously changes the convergence-before relation.
Converged dynamic instances need not be executed at the same time or even on the same resource. Converged dynamic instances of a convergent operation may appear to do so but that is an implementation detail.
The fact that
Pis convergence-beforeQdoes not automatically imply thatPhappens-beforeQin a memory model sense.
Maximal Convergence¶
This section defines a constraint that may be used to produce a maximal converged-with relation without violating the strict convergence-before order. This maximal converged-with relation is reasonable for real targets and is compatible with convergent operations.
The maximal converged-with relation is defined in terms of cycle headers, with the assumption that threads converge at the header on every “iteration” of the cycle. Informally, two threads execute the same iteration of a cycle if they both previously executed the cycle header the same number of times after they entered that cycle. In general, this needs to account for the iterations of parent cycles as well.
Maximal converged-with:
Dynamic instances
X1andX2produced by different threads for the same static instanceXare converged in the maximal converged-with relation if and only if:
Xis not contained in any cycle, or,For every cycle
Cwith headerHthat containsX:
every dynamic instance
H1ofHthat precedesX1in the respective thread is convergence-beforeX2, and,every dynamic instance
H2ofHthat precedesX2in the respective thread is convergence-beforeX1,without assuming that
X1is converged withX2.
Note
Cycle headers may not be unique to a given CFG if it is irreducible. Each cycle hierarchy for the same CFG results in a different maximal converged-with relation.
For brevity, the rest of the document restricts the term converged to mean “related under the maximal converged-with relation for the given cycle hierarchy”.
Maximal convergence can now be demonstrated in the earlier example as follows:
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|||
Thread 1 |
Entry1 |
H1 |
B1 |
L1 |
H3 |
L3 |
Exit |
||||
Thread 2 |
Entry2 |
H2 |
L2 |
H4 |
B2 |
L4 |
H5 |
B3 |
L5 |
Exit |
Entry1andEntry2are converged.H1andH2are converged.B1andB2are not converged due toH4which is not convergence-beforeB1.H3andH4are converged.H3is not converged withH5due toH4which is not convergence-beforeH3.L1andL2are converged.L3andL4are converged.L3is not converged withL5due toH5which is not convergence-beforeL3.
Dependence on Cycles Headers¶
Contradictions in convergence-before are possible only between two nodes that are inside some cycle. The dynamic instances of such nodes may be interleaved in the same thread, and this interleaving may be different for different threads.
When a thread executes a node X once and then executes it again,
it must have followed a closed path in the CFG that includes X.
Such a path must pass through the header of at least one cycle — the
smallest cycle that includes the entire closed path. In a given
thread, two dynamic instances of X are either separated by the
execution of at least one cycle header, or X itself is a cycle
header.
In reducible cycles (natural loops), each execution of the header is equivalent to the start of a new iteration of the cycle. But this analogy breaks down in the presence of explicit constraints on the converged-with relation, such as those described in future work. Instead, cycle headers should be treated as implicit points of convergence in a maximal converged-with relation.
Consider a sequence of nested cycles C1, C2, …, Ck such
that C1 is the outermost cycle and Ck is the innermost cycle,
with headers H1, H2, …, Hk respectively. When a thread
enters the cycle Ck, any of the following is possible:
The thread directly entered cycle
Ckwithout having executed any of the headersH1toHk.The thread executed some or all of the nested headers one or more times.
The maximal converged-with relation captures the following intuition about cycles:
When two threads enter a top-level cycle
C1, they execute converged dynamic instances of every node that is a child ofC1.When two threads enter a nested cycle
Ck, they execute converged dynamic instances of every node that is a child ofCk, until either thread exitsCk, if and only if they executed converged dynamic instances of the last nested header that either thread encountered.Note that when a thread exits a nested cycle
Ck, it must follow a closed path outsideCkto reenter it. This requires executing the header of some outer cycle, as described earlier.
Consider two dynamic instances X1 and X2 produced by threads T1
and T2 for a node X that is a child of nested cycle Ck.
Maximal convergence relates X1 and X2 as follows:
If neither thread executed any header from
H1toHk, thenX1andX2are converged.Otherwise, if there are no converged dynamic instances
Q1andQ2of any headerQfromH1toHk(whereQis possibly the same asX), such thatQ1precedesX1andQ2precedesX2in the respective threads, thenX1andX2are not converged.Otherwise, consider the pair
Q1andQ2of converged dynamic instances of a headerQfromH1toHkthat occur most recently beforeX1andX2in the respective threads. ThenX1andX2are converged if and only if there is no dynamic instance of any header fromH1toHkthat occurs betweenQ1andX1in threadT1, or betweenQ2andX2in threadT2. In other words,Q1andQ2represent the last point of convergence, with no other header being executed before executingX.
Example:
The above figure shows two nested irreducible cycles with headers
R and S. The nodes Entry and Q have divergent
branches. The table below shows the convergence between three threads
taking different paths through the CFG. Dynamic instances listed in
the same column are converged.
1
2
3
4
5
6
7
8
10
Thread1
Entry
P1
Q1
S1
P3
Q3
R1
S2
Exit
Thread2
Entry
P2
Q2
R2
S3
Exit
Thread3
Entry
R3
S4
Exit
P2andP3are not converged due toS1Q2andQ3are not converged due toS1S1andS3are not converged due toR2S1andS4are not converged due toR3
Informally, T1 and T2 execute the inner cycle a different
number of times, without executing the header of the outer cycle. All
threads converge in the outer cycle when they first execute the header
of the outer cycle.
Uniformity¶
The output of two converged dynamic instances is uniform if and only if it compares equal for those two dynamic instances.
The output of a static instance
Xis uniform for a given set of threads if and only if it is uniform for every pair of converged dynamic instances ofXproduced by those threads.
A non-uniform value is said to be
