<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 --- - CUDA __device__ lambdas generating incorrect code"
   href="https://llvm.org/bugs/show_bug.cgi?id=26483">26483</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>CUDA __device__ lambdas generating incorrect code
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>justin.lebar@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>The following CUDA code, which passes a __device__ functor to a kernel,
compiles fine but does not give the correct output.

When I compile and run it, I get the following output:

Error 3: invalid device function
Error: 0 2883584
Error: 1 0
Error: 2 2883584
Error: 3 0
Error: 4 3105024
Error: 5 0
Error: 6 0
Error: 7 0
Error: 8 0
Error: 9 0
Error: 10 0
Error: 11 0
Error: 12 0
Error: 13 0
Error: 14 0
Error: 15 0

The "error 3 invalid device function" is probably the root of the problem; it
seems like we may not be running any device code at all.

#include <cuda_runtime.h>
#include <cuda.h>
#include <cstdio>

template<class Functor>
__global__ void foo(const Functor f) {
   f(blockIdx.x*blockDim.x+threadIdx.x);
}

template<class Functor>
void run(const Functor& f) {
 foo<Functor> <<<256,256>>> (f);   
}


int main() {
  int* h_a = new int[256*256];

  int d_c = 9;
  int* d_a;
  cudaMalloc(&d_a,256*256*sizeof(int));

  run([=] (int i) __device__ {
    d_a[i] = d_c;
  });

  cudaMemcpy(h_a,d_a,256*256*sizeof(int),cudaMemcpyDeviceToHost);
  printf("Error 3: %s\n",cudaGetErrorString(cudaGetLastError()));

  for(int i=0;i<16;i++)
    if(h_a[i]!=d_c)
     printf("Error: %i %i\n",i,h_a[i]); 
}</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>