<div dir="ltr"><div dir="auto"><div><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Dec 19, 2018, 2:45 PM Justin Lebar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div>Hi from one of the people who works/worked on the NVPTX backend.</div><div dir="ltr"><br></div><div dir="ltr">> The key issue is that *the set of threads participating in the exchange of data is implicitly defined by control flow*.</div><div dir="ltr"><br></div><div>I agree that this is the root of the problem.</div><div><br></div><div>The way nvidia GPUs "solve" this is that in Volta and newer, it's up to the user to track explicitly which set of threads participate in warp-synchronous functions. You cannot safely ballot(). Problem "solved". :)</div></div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">Well, that certainly works for Nvidia, but from what I understand, you can't efficiently do this without special hardware support to properly synchronize the threads when you get to the ballot or whatever, so the rest of us are unfortunately out of luck :)<br></div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div><br></div><div>We already have the notion of "convergent" functions like syncthreads(), to which we cannot add control-flow dependencies. That is, it's legal to hoist syncthreads out of an "if", but it's not legal to sink it into an "if". It's not clear to me why we can't have "anticonvergent" (terrible name) functions which cannot have control-flow dependencies removed from them? ballot() would be both convergent and anticonvergent.</div><div><br></div><div>Would that solve your problem?</div></div></div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">I think it's important to note that we already have such an attribute, although with the opposite sense - it's impossible to remove control flow dependencies from a call unless you mark it as "speculatable". However, this doesn't prevent<br></div><div dir="auto"><br></div><div dir="auto">if (...) {</div><div dir="auto">} else {</div><div dir="auto">}</div><div dir="auto">foo = ballot();</div><div dir="auto"><br></div><div dir="auto">from being turned into </div><div dir="auto"><br></div><div dir="auto">if (...) {</div><div dir="auto"> foo1 = ballot();</div><div dir="auto">} else {</div><div dir="auto"> foo2 = ballot();</div><div dir="auto">}</div><div dir="auto">foo = phi(foo1, foo2)</div><div dir="auto"><br></div><div dir="auto">and vice versa. We have a "noduplicate" attribute which prevents transforming the first into the second, but not the other way around. Of course we could keep going this way and add a "nocombine" attribute to complement noduplicate. But even then, there are even still problematic transforms. For example, take this program, which is simplified from a real game that doesn't work with the AMDGPU backend:</div><div dir="auto"><br></div><div dir="auto"><div>while (cond1 /* uniform */) {</div><div> ballot();<br></div><div> ...</div><div> if (cond2 /* non-uniform */) continue;</div><div> ...</div><div>}</div></div><div dir="auto"><br></div><div>In SPIR-V, when using structured control flow, the semantics of this are pretty clearly defined. In particular, there's a continue block after the body of the loop where control flow re-converges, and the only back edge is from the continue block, so the ballot is in uniform control flow. But LLVM will get rid of the continue block since it's empty, and re-analyze the loop as two nested loops, splitting the loop header in two, producing a CFG which corresponds to this:<br></div><div><br></div><div><div dir="auto">while (cond1 /* uniform */) {</div><div dir="auto"> do {</div><div> ballot();<br></div><div dir="auto"> ...</div><div dir="auto"> } while (cond2 /* non-uniform */);</div><div dir="auto"> ...</div><div dir="auto">}</div><div dir="auto"><br></div><div>Now, in an implementation where control flow re-converges at the immediate post-dominator, this won't do the right thing anymore. In order to handle it correctly, you'd effectively need to always flatten nested loops, which will probably be really bad for performance if the programmer actually wanted the second thing. It also makes it impossible when translating a high-level language to LLVM to get the "natural" behavior which game developers actually expect. This is exactly the sort of "spooky action at a distance" which makes me think that everything we've done so far is really insufficient, and we need to add an explicit notion of control-flow divergence and reconvergence to the IR. We need a way to say that control flow re-converges at the continue block, so that LLVM won't eliminate it, and we can vectorize it correctly without penalizing cases where it's better for control flow not to re-converge.<br></div></div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div><br></div><div>> However, the basic block containing the ballot call in the natural lowering to LLVM IR is not part of the loop at all. The information that it was intended to be run as part of the loop is currently lost forever.</div><div><br></div><div>Sounds like the natural lowering of this example is not respecting anticonvergence and just needs to be made more unnatural?</div></div></div></blockquote></div></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div><br></div><div>I also think it's worthwhile to consider the reasons behind nvidia's move away from functions like ballot() towards explicit tracking of which threads are active. <a class="gmail_plusreply" id="m_8926645893797330667m_-5922374205997674957m_3836388116597113399m_-6885807592573695332plusReplyChip-3" href="mailto:ogiroux@gmail.com" rel="noreferrer noreferrer" target="_blank">+Olivier Giroux</a> told me a while ago that he was working on a paper which showed that the old way of doing things is even more fraught/broken/difficult-to-specify than I thought; I'm not sure if anything ended up being published.</div></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Dec 19, 2018 at 11:32 AM Nicolai Hähnle via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi all,<br>
<br>
LLVM needs a solution to the long-standing problem that the IR is unable <br>
to express certain semantics expected by high-level programming <br>
languages that target GPUs.<br>
<br>
Solving this issue is necessary both for upstream use of LLVM as a <br>
compiler backend for GPUs and for correctly supporting LLVM IR <-> <br>
SPIR-V roundtrip translation. It may also be useful for compilers <br>
targeting SPMD-on-CPU a la ispc -- after all, some GPU hardware really <br>
just looks like a CPU with extra-wide SIMD.<br>
<br>
After thinking and talking about the problem on and off for more than <br>
two years now, I'm convinced that it can only be solved by adding <br>
dedicated semantics to LLVM IR, which take the shape of:<br>
<br>
- a new function (and call) attribute similar to `convergent`,<br>
- explicitly adding language to the LangRef that talks about groups of <br>
threads and their communication with each other via special functions,<br>
- including how these groups of threads are impacted (split and merged) <br>
by branches etc., and<br>
- new target-independent intrinsic(s) which manipulate subgroups of <br>
threads, mostly by marking locations in the program where threads reconverge<br>
<br>
Details to be determined, of course.<br>
<br>
In this mail, I would mostly like to make initial contact with the <br>
larger community. First, to explain the problem to CPU-centric folks and <br>
maybe help get over the initial shock. Second, to reach out to other <br>
people thinking about GPUs: Has anybody else given this issue much <br>
thought? How can we proceed to get this solved?<br>
<br>
<br>
The Problem<br>
===========<br>
Programming languages for GPUs have "cross-lane" or "subgroup" <br>
operations which allow fine-grained exchange of data between threads <br>
being executed together in a "wave" or "warp".<br>
<br>
The poster-child is ballot, which takes a boolean argument and returns a <br>
bitmask of the value of the argument across the <br>
"subgroup"/"wave"/"warp", but more complex operations exist as well e.g. <br>
for reducing a value across all active lanes of a wave or for computing <br>
a prefix scan.<br>
<br>
The key issue is that *the set of threads participating in the exchange <br>
of data is implicitly defined by control flow*.<br>
<br>
Two examples to demonstrate the resulting problem and the limitation of <br>
the existing LLVM IR semantics. The first one:<br>
<br>
bool value = ...;<br>
<br>
if (condition) {<br>
bitmask0 = ballot(value);<br>
foo(bitmask0);<br>
} else {<br>
bitmask1 = ballot(value);<br>
bar(bitmask1);<br>
}<br>
<br>
The semantics of high-level languages demand that `bitmask0` only <br>
contains set bits for threads (lanes) for which `condition` is true, and <br>
analogously for `bitmask1`. However, there is no reasonable way in LLVM <br>
IR to prevent the ballot call from being hoisted above the if-statement, <br>
which changes the behavior.<br>
<br>
(Existing frontends for the AMDGPU target currently implement a gross <br>
hack where `value` is passed through a call to a unique chunk of no-op <br>
inline assembly that is marked as having unspecified side effects...)<br>
<br>
The second example:<br>
<br>
uint64_t bitmask;<br>
for (;;) {<br>
...<br>
if (...) {<br>
bool value = ...;<br>
bitmask = ballot(value);<br>
break;<br>
}<br>
...<br>
}<br>
<br>
The semantics of high-level languages expect that `bitmask` only <br>
contains set bits for threads (lanes) which break from the loop in the <br>
same iteration. However, the basic block containing the ballot call in <br>
the natural lowering to LLVM IR is not part of the loop at all. The <br>
information that it was intended to be run as part of the loop is <br>
currently lost forever.<br>
<br>
<br>
The Design Space of Solutions<br>
=============================<br>
The relevant high-level languages are structured programming languages, <br>
where the behavior of subgroups falls out quite naturally. Needless to <br>
say, we cannot rely on structured control flow in LLVM IR.<br>
<br>
HSAIL defines subgroups as forking at branches and joining at the <br>
immediate post-dominator. It also attempts to define restrictions on <br>
program transformations in terms of immediate dominators and <br>
post-dominators. I am not certain that this definition is sound in all <br>
cases, and it is too restrictive in places.<br>
<br>
Its main disadvantage is that describing restrictions on transformations <br>
purely in terms of dominators and post-dominators causes non-local <br>
effects. For example, jump threading can change the <br>
dominator/post-dominator tree, but verifying whether the corresponding <br>
transform is legal in the face of subgroup operations requires <br>
inspecting distant parts of the code that jump threading would not have <br>
to look at for a CPU target.<br>
<br>
So I reject HSAIL-like approaches based on the fact that they would <br>
require invasive changes in generic middle-end passes.<br>
<br>
There is a type of approach that most people who come into contact with <br>
this problem eventually at least think about, which suggests replacing <br>
the implicit dependence on control flow by an explicit one. That is, <br>
augment subgroup intrinsics with an additional argument that represents <br>
the subgroup of threads which participate in the exchange of data <br>
described by this intrinsic, which results in code that looks similar to <br>
the co-operative groups in new versions of Cuda.<br>
<br>
This kind of approach can be a valid solution to the problem of <br>
preserving the correct semantics, although it imposes annoying <br>
restrictions on function call ABIs.<br>
<br>
The major problem with this kind of approach is that it does not <br>
actually restrict the transforms made by middle-end passes, and so the <br>
final IR before code generation might end up containing code patterns <br>
that cannot be natively expressed on a target that implements SPMD with <br>
lock-step execution. The burden on the backend to reliably catch and <br>
implement the rare cases when this happens is excessive. Even for <br>
targets without lock-step execution, these transforms may have unwanted <br>
performance impacts. So I reject this type of proposal as well.<br>
<br>
The literature from practicioners on SPMD/SIMT control flow (a lot of it <br>
targeting a hardware audience rather than a compiler audience) does not <br>
concern itself with this problem to my knowledge, but there is a <br>
commonly recurring theme of reconverging or rejoining threads at <br>
explicit instructions and/or post-dominators.<br>
<br>
This suggests a viable path towards a solution to me.<br>
<br>
The SPIR-V spec has a notion of explicitly structured control flow with <br>
merge basic blocks. It also defines "dynamic instances" of instructions <br>
that are distinguished by control flow path, which provides a decent <br>
option for modeling the set of threads which participate in subgroup <br>
operations.<br>
<br>
The SPIR-V spec itself is IMO quite lacking, in that the formalism is <br>
very incomplete, the concrete structured control flow constructs are <br>
very complex, and the details of how they are expressed lead to the same <br>
non-local effects you get with the HSAIL approach. Nevertheless, I think <br>
there's another viable path towards a solution hidden there.<br>
<br>
Finally and for completeness, it should be noted that if we were to <br>
forbid irreducible control flow in LLVM IR entirely, that would open up <br>
more freedom in the design since we could properly define some things in <br>
terms of loop structure.<br>
<br>
<br>
Final Thoughts<br>
==============<br>
Formalizing these semantics could also help put divergence analysis on a <br>
more solid foundation.<br>
<br>
Mostly I'm interested in general feedback at this point. Is there an <br>
important part of the design space that I missed? What do people think <br>
about explicitly talking about thread groups and e.g. dynamic instances <br>
as in SPIR-V as part of the LLVM LangRef? Are people generally happy <br>
with the notion? If not, how can we get to a place where people are <br>
happy with it?<br>
<br>
Thanks,<br>
Nicolai<br>
-- <br>
Lerne, wie die Welt wirklich ist,<br>
Aber vergiss niemals, wie sie sein sollte.<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div>
</div>