implicit-device-lambda.cu 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // RUN: %clang_cc1 -std=c++11 -fcuda-is-device -verify -fsyntax-only -verify-ignore-unexpected=warning -verify-ignore-unexpected=note %s
  2. // RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only -verify-ignore-unexpected=warning -verify-ignore-unexpected=note %s
  3. #include "Inputs/cuda.h"
  4. __device__ void device_fn() {
  5. auto f1 = [&] {};
  6. f1(); // implicitly __device__
  7. auto f2 = [&] __device__ {};
  8. f2();
  9. auto f3 = [&] __host__ {};
  10. f3(); // expected-error {{no matching function}}
  11. auto f4 = [&] __host__ __device__ {};
  12. f4();
  13. // Now do it all again with '()'s in the lambda declarations: This is a
  14. // different parse path.
  15. auto g1 = [&]() {};
  16. g1(); // implicitly __device__
  17. auto g2 = [&]() __device__ {};
  18. g2();
  19. auto g3 = [&]() __host__ {};
  20. g3(); // expected-error {{no matching function}}
  21. auto g4 = [&]() __host__ __device__ {};
  22. g4();
  23. // Once more, with the '()'s in a different place.
  24. auto h1 = [&]() {};
  25. h1(); // implicitly __device__
  26. auto h2 = [&] __device__ () {};
  27. h2();
  28. auto h3 = [&] __host__ () {};
  29. h3(); // expected-error {{no matching function}}
  30. auto h4 = [&] __host__ __device__ () {};
  31. h4();
  32. }
  33. // Behaves identically to device_fn.
  34. __global__ void kernel_fn() {
  35. auto f1 = [&] {};
  36. f1(); // implicitly __device__
  37. auto f2 = [&] __device__ {};
  38. f2();
  39. auto f3 = [&] __host__ {};
  40. f3(); // expected-error {{no matching function}}
  41. auto f4 = [&] __host__ __device__ {};
  42. f4();
  43. // No need to re-test all the parser contortions we test in the device
  44. // function.
  45. }
  46. __host__ void host_fn() {
  47. auto f1 = [&] {};
  48. f1(); // implicitly __host__ (i.e., no magic)
  49. auto f2 = [&] __device__ {};
  50. f2(); // expected-error {{no matching function}}
  51. auto f3 = [&] __host__ {};
  52. f3();
  53. auto f4 = [&] __host__ __device__ {};
  54. f4();
  55. }
  56. __host__ __device__ void hd_fn() {
  57. auto f1 = [&] {};
  58. f1(); // implicitly __host__ __device__
  59. auto f2 = [&] __device__ {};
  60. f2();
  61. #ifndef __CUDA_ARCH__
  62. // expected-error@-2 {{reference to __device__ function}}
  63. #endif
  64. auto f3 = [&] __host__ {};
  65. f3();
  66. #ifdef __CUDA_ARCH__
  67. // expected-error@-2 {{reference to __host__ function}}
  68. #endif
  69. auto f4 = [&] __host__ __device__ {};
  70. f4();
  71. }
  72. // The special treatment above only applies to lambdas.
  73. __device__ void foo() {
  74. struct X {
  75. void foo() {}
  76. };
  77. X x;
  78. x.foo(); // expected-error {{reference to __host__ function 'foo' in __device__ function}}
  79. }