function-target-hd.cu 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Test the Sema analysis of caller-callee relationships of host device
  2. // functions when compiling CUDA code. There are 4 permutations of this test as
  3. // host and device compilation are separate compilation passes, and clang has
  4. // an option to allow host calls from host device functions. __CUDA_ARCH__ is
  5. // defined when compiling for the device and TEST_WARN_HD when host calls are
  6. // allowed from host device functions. So for example, if __CUDA_ARCH__ is
  7. // defined and TEST_WARN_HD is not then device compilation is happening but
  8. // host device functions are not allowed to call device functions.
  9. // RUN: %clang_cc1 -fsyntax-only -verify %s
  10. // RUN: %clang_cc1 -fsyntax-only -fcuda-is-device -verify %s
  11. // RUN: %clang_cc1 -fsyntax-only -fcuda-allow-host-calls-from-host-device -verify %s -DTEST_WARN_HD
  12. // RUN: %clang_cc1 -fsyntax-only -fcuda-is-device -fcuda-allow-host-calls-from-host-device -verify %s -DTEST_WARN_HD
  13. #include "Inputs/cuda.h"
  14. __host__ void hd1h(void);
  15. #if defined(__CUDA_ARCH__) && !defined(TEST_WARN_HD)
  16. // expected-note@-2 {{candidate function not viable: call to __host__ function from __host__ __device__ function}}
  17. #endif
  18. __device__ void hd1d(void);
  19. #ifndef __CUDA_ARCH__
  20. // expected-note@-2 {{candidate function not viable: call to __device__ function from __host__ __device__ function}}
  21. #endif
  22. __host__ void hd1hg(void);
  23. __device__ void hd1dg(void);
  24. #ifdef __CUDA_ARCH__
  25. __host__ void hd1hig(void);
  26. #if !defined(TEST_WARN_HD)
  27. // expected-note@-2 {{candidate function not viable: call to __host__ function from __host__ __device__ function}}
  28. #endif
  29. #else
  30. __device__ void hd1dig(void); // expected-note {{candidate function not viable: call to __device__ function from __host__ __device__ function}}
  31. #endif
  32. __host__ __device__ void hd1hd(void);
  33. __global__ void hd1g(void); // expected-note {{'hd1g' declared here}}
  34. __host__ __device__ void hd1(void) {
  35. #if defined(TEST_WARN_HD) && defined(__CUDA_ARCH__)
  36. // expected-warning@-2 {{calling __host__ function hd1h from __host__ __device__ function hd1}}
  37. // expected-warning@-3 {{calling __host__ function hd1hig from __host__ __device__ function hd1}}
  38. #endif
  39. hd1d();
  40. #ifndef __CUDA_ARCH__
  41. // expected-error@-2 {{no matching function}}
  42. #endif
  43. hd1h();
  44. #if defined(__CUDA_ARCH__) && !defined(TEST_WARN_HD)
  45. // expected-error@-2 {{no matching function}}
  46. #endif
  47. // No errors as guarded
  48. #ifdef __CUDA_ARCH__
  49. hd1d();
  50. #else
  51. hd1h();
  52. #endif
  53. // Errors as incorrectly guarded
  54. #ifndef __CUDA_ARCH__
  55. hd1dig(); // expected-error {{no matching function}}
  56. #else
  57. hd1hig();
  58. #ifndef TEST_WARN_HD
  59. // expected-error@-2 {{no matching function}}
  60. #endif
  61. #endif
  62. hd1hd();
  63. hd1g<<<1, 1>>>(); // expected-error {{reference to __global__ function 'hd1g' in __host__ __device__ function}}
  64. }