<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - alloca in local memory not promoted to registers"
   href="https://llvm.org/bugs/show_bug.cgi?id=31333">31333</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>alloca in local memory not promoted to registers
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: PTX
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>andrew.b.adams@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=17747" name="attach_17747" title="ll that reproduces">attachment 17747</a> <a href="attachment.cgi?id=17747&action=edit" title="ll that reproduces">[details]</a></span>
ll that reproduces

The attached .ll uses an alloca of 64 floats to do a matrix multiply block.
Every access to this array is at a constant offset. The generated ptx includes
a ton of traffic to and from .local:

...
    st.local.f32     [%rd5+184], %f143;
    ld.local.f32     %f144, [%rd5+188];
    fma.rn.f32     %f145, %f139, %f93, %f144;
    st.local.f32     [%rd5+188], %f145;
    ld.local.f32     %f146, [%rd5+208];
    fma.rn.f32     %f147, %f126, %f108, %f146;
    st.local.f32     [%rd5+208], %f147;
    ld.local.f32     %f148, [%rd5+212];
    fma.rn.f32     %f149, %f129, %f108, %f148;
    st.local.f32     [%rd5+212], %f149;
    ld.local.f32     %f150, [%rd5+240];
    fma.rn.f32     %f151, %f126, %f113, %f150;
    st.local.f32     [%rd5+240], %f151;
    ld.local.f32     %f152, [%rd5+244];
    fma.rn.f32     %f153, %f129, %f113, %f152;
    st.local.f32     [%rd5+244], %f153;
    ld.local.f32     %f154, [%rd5+216];
    fma.rn.f32     %f155, %f136, %f108, %f154;
    st.local.f32     [%rd5+216], %f155;
    ld.local.f32     %f156, [%rd5+220];
...

If you instead do 64 scalar allocas of one float each, all of this traffic goes
away, and the kernel gets 10 times faster! 

The kernel uses vector types, but the problem persists without them too.

Interestingly, if I reduce the size of the alloca to 4 (computing a 2x2 block
of the output matrix instead of 8x8), then the loads from local go away, but
the stores remain. So it's keeping two copies of the values. I've included this
simpler kernel too.

I'm compiling the kernel with:

llc kernel.ll -filetype=asm -march=nvptx -O3  -o -</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>