xref: /llvm-project/clang/test/SemaCUDA/lambda.cu (revision 26e492e134c006c63b3d9f9f9eabdeba014b1d2c)
11eaad010SYaxun (Sam) Liu // RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify=com %s
2*26e492e1SYaxun (Sam) Liu // RUN: %clang_cc1 -std=c++17 -fsyntax-only -fcuda-is-device -verify=com,dev,warn %s
3*26e492e1SYaxun (Sam) Liu // RUN: %clang_cc1 -std=c++17 -fsyntax-only -fcuda-is-device -verify=com,dev,warn \
4*26e492e1SYaxun (Sam) Liu // RUN:   -triple amdgcn-amd-amdhsa -target-cpu gfx906 -x hip %s
5*26e492e1SYaxun (Sam) Liu // RUN: %clang_cc1 -std=c++17 -fsyntax-only -fcuda-is-device -verify=com,dev \
6*26e492e1SYaxun (Sam) Liu // RUN:   -Wno-gpu-maybe-wrong-side %s
71eaad010SYaxun (Sam) Liu 
81eaad010SYaxun (Sam) Liu #include "Inputs/cuda.h"
91eaad010SYaxun (Sam) Liu 
__anon795605be0102() 101eaad010SYaxun (Sam) Liu auto global_lambda = [] () { return 123; };
111eaad010SYaxun (Sam) Liu 
121eaad010SYaxun (Sam) Liu template<class F>
kernel(F f)131eaad010SYaxun (Sam) Liu __global__ void kernel(F f) { f(); }
14*26e492e1SYaxun (Sam) Liu // dev-note@-1 3{{called by 'kernel<(lambda}}
15*26e492e1SYaxun (Sam) Liu // warn-note@-2 5{{called by 'kernel<(lambda}}
161eaad010SYaxun (Sam) Liu 
171eaad010SYaxun (Sam) Liu __host__ __device__ void hd(int x);
181eaad010SYaxun (Sam) Liu 
191eaad010SYaxun (Sam) Liu class A {
201eaad010SYaxun (Sam) Liu   int b;
211eaad010SYaxun (Sam) Liu public:
test()221eaad010SYaxun (Sam) Liu   void test() {
231eaad010SYaxun (Sam) Liu     [=](){ hd(b); }();
241eaad010SYaxun (Sam) Liu 
251eaad010SYaxun (Sam) Liu     [&](){ hd(b); }();
261eaad010SYaxun (Sam) Liu 
271eaad010SYaxun (Sam) Liu     kernel<<<1,1>>>([](){ hd(0); });
281eaad010SYaxun (Sam) Liu 
291eaad010SYaxun (Sam) Liu     kernel<<<1,1>>>([=](){ hd(b); });
30*26e492e1SYaxun (Sam) Liu     // warn-warning@-1 {{capture host side class data member by this pointer in device or host device lambda function may result in invalid memory access if this pointer is not accessible on device side}}
311eaad010SYaxun (Sam) Liu 
321eaad010SYaxun (Sam) Liu     kernel<<<1,1>>>([&](){ hd(b); });
33*26e492e1SYaxun (Sam) Liu     // warn-warning@-1 {{capture host side class data member by this pointer in device or host device lambda function may result in invalid memory access if this pointer is not accessible on device side}}
341eaad010SYaxun (Sam) Liu 
351eaad010SYaxun (Sam) Liu     kernel<<<1,1>>>([&] __device__ (){ hd(b); });
36*26e492e1SYaxun (Sam) Liu     // warn-warning@-1 {{capture host side class data member by this pointer in device or host device lambda function may result in invalid memory access if this pointer is not accessible on device side}}
371eaad010SYaxun (Sam) Liu 
381eaad010SYaxun (Sam) Liu     kernel<<<1,1>>>([&](){
391eaad010SYaxun (Sam) Liu       auto f = [&]{ hd(b); };
40*26e492e1SYaxun (Sam) Liu       // warn-warning@-1 {{capture host side class data member by this pointer in device or host device lambda function may result in invalid memory access if this pointer is not accessible on device side}}
411eaad010SYaxun (Sam) Liu       f();
421eaad010SYaxun (Sam) Liu     });
43*26e492e1SYaxun (Sam) Liu 
44*26e492e1SYaxun (Sam) Liu     auto lambda1 = [this] __device__ { hd(this->b); };
45*26e492e1SYaxun (Sam) Liu     // warn-warning@-1 {{capture host side class data member by this pointer in device or host device lambda function may result in invalid memory access if this pointer is not accessible on device side}}
46*26e492e1SYaxun (Sam) Liu     kernel<<<1,1>>>(lambda1);
471eaad010SYaxun (Sam) Liu   }
481eaad010SYaxun (Sam) Liu };
491eaad010SYaxun (Sam) Liu 
main(void)501eaad010SYaxun (Sam) Liu int main(void) {
511eaad010SYaxun (Sam) Liu   auto lambda_kernel = [&]__global__(){};
521eaad010SYaxun (Sam) Liu   // com-error@-1 {{kernel function 'operator()' must be a free function or static member function}}
531eaad010SYaxun (Sam) Liu 
541eaad010SYaxun (Sam) Liu   int b;
551eaad010SYaxun (Sam) Liu   [&](){ hd(b); }();
561eaad010SYaxun (Sam) Liu 
571eaad010SYaxun (Sam) Liu   [=, &b](){ hd(b); }();
581eaad010SYaxun (Sam) Liu 
591eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>(global_lambda);
601eaad010SYaxun (Sam) Liu 
611eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>([](){ hd(0); });
621eaad010SYaxun (Sam) Liu 
631eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>([=](){ hd(b); });
641eaad010SYaxun (Sam) Liu 
651eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>([b](){ hd(b); });
661eaad010SYaxun (Sam) Liu 
671eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>([&](){ hd(b); });
681eaad010SYaxun (Sam) Liu   // dev-error@-1 {{capture host variable 'b' by reference in device or host device lambda function}}
691eaad010SYaxun (Sam) Liu 
701eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>([=, &b](){ hd(b); });
711eaad010SYaxun (Sam) Liu   // dev-error@-1 {{capture host variable 'b' by reference in device or host device lambda function}}
721eaad010SYaxun (Sam) Liu 
731eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>([&, b](){ hd(b); });
741eaad010SYaxun (Sam) Liu 
751eaad010SYaxun (Sam) Liu   kernel<<<1,1>>>([&](){
761eaad010SYaxun (Sam) Liu       auto f = [&]{ hd(b); };
771eaad010SYaxun (Sam) Liu       // dev-error@-1 {{capture host variable 'b' by reference in device or host device lambda function}}
781eaad010SYaxun (Sam) Liu       f();
791eaad010SYaxun (Sam) Liu   });
801eaad010SYaxun (Sam) Liu 
811eaad010SYaxun (Sam) Liu   return 0;
821eaad010SYaxun (Sam) Liu }
83