<div dir="ltr"><div>Hi, </div><div><br></div><div>We wrote an optimization that eliminates common sub-expressions in a group of similar GEPs for the NVPTX backend. It speeds up some of our benchmarks by up to 20%, which convinces us to try to upstream it. Here's a brief description of why we wrote this optimization, what we did, and how we did it.</div>
<div><br></div><div>Loops in CUDA programs are often extensively unrolled by programmers and compilers, leading to many similar</div><div>GEPs for array accesses.</div><div><br></div><div>e.g., a 2-level loop like</div><div>
<br></div><div>__shared__ float a[32][32];</div><div>unroll for (int i = 0; i < 2; ++i) {</div><div>  unroll for (int j = 0; j < 2; ++j) {</div><div>    ...</div><div>    ... = a[threadIdx.x + i][threadIdx.y + j];</div>
<div>    ...</div><div>  }</div><div>}</div><div><br></div><div>will be unrolled to:</div><div><br></div><div>gep a, 0, tid.x, tid.y; load</div><div>gep a, 0, tid.x, tid.y + 1; load</div><div>gep a, 0, tid.x + 1, tid.y; load</div>
<div>gep a, 0, tid.x + 1, tid.y + 1; load</div><div><br></div><div>The NVPTX backend currently doesn't handle many similar multi-dimensional GEPs</div><div>well enough. It emits PTX code that literally computes the pointer address of</div>
<div>each GEP, wasting tons of registers. e.g., it emits the following PTX for the</div><div>first load and similar PTX for other loads.</div><div><br></div><div>mov.u32         %r1, %tid.x;</div><div>mov.u32         %r2, %tid.y;</div>
<div>mul.wide.u32    %rl2, %r1, 128;</div><div>mov.u64         %rl3, a;</div><div>add.s64         %rl4, %rl3, %rl2;</div><div>mul.wide.u32    %rl5, %r2, 4;</div><div>add.s64         %rl6, %rl4, %rl5;</div><div>ld.shared.f32   %f1, [%rl6];</div>
<div><br></div><div>The resultant register pressure causes up to 20% slowdown on some of our</div><div>benchmarks.</div><div><br></div><div>To reduce register pressure, the optimization implemented in this patch merges</div>
<div>the common subexpression in a group of GEPs, saving many registers used for</div><div>pointer arithmetics. It works by splitting each GEP into a variadic base and a</div><div>constant offset. The variadic base can be computed once and reused by multiple</div>
<div>GEPs, and the constant offsets can be nicely folded into NVPTX's base+offset</div><div>addressing mode without using any extra register. e.g., we transform the four</div><div>GEPs and four loads in the above example conceptually into:</div>
<div><br></div><div>base = gep a, 0, x, y</div><div>load base</div><div>laod base + 1  * sizeof(float)</div><div>load base + 32 * sizeof(float)</div><div>load base + 33 * sizeof(float)</div><div><br></div><div>The resultant PTX code will look like:</div>
<div><br></div><div>mov.u32         %r1, %tid.x;</div><div>mov.u32         %r2, %tid.y;</div><div>mul.wide.u32    %rl2, %r1, 128;</div><div>mov.u64         %rl3, a;</div><div>add.s64         %rl4, %rl3, %rl2;</div><div>mul.wide.u32    %rl5, %r2, 4;</div>
<div>add.s64         %rl6, %rl4, %rl5;</div><div>ld.shared.f32   %f1, [%rl6]; // so far the same as unoptimized PTX</div><div><b>ld.shared.f32   %f2, [%rl6+4]; // much better</b></div><div><b>ld.shared.f32   %f3, [%rl6+128]; // much better</b></div>
<div><b>ld.shared.f32   %f4, [%rl6+132]; // much better</b></div><div><br></div><div>which uses much fewer registers than the unoptimized PTX.</div><div><br></div><div>I am attaching a proof-of-concept patch. It fully implements our idea and contains a contrived test case to demonstrate how it works. It also discusses why our implementation is safe in terms that the optimization won't cause new undefined behavior. There's more work that needs to be done, e.g., adding more tests. If this idea sounds good to you, we will improve the patch and send it out for code review. <br>
</div><div><br></div><div>Thanks, </div><div>Jingyue</div></div>