blob: c727dc8cbeb7110eaf5fbc015ca4be62b55a902f [file] [log] [blame]
Justin Lebar23d95422016-10-13 20:52:12 +00001// RUN: %clang_cc1 %s --std=c++11 -triple nvptx-unknown-unknown -fcuda-is-device \
Justin Lebar6c86e912016-10-19 21:15:01 +00002// RUN: -emit-llvm -o /dev/null -verify -verify-ignore-unexpected=note
Justin Lebar18e2d822016-08-15 23:00:49 +00003
4// Note: This test won't work with -fsyntax-only, because some of these errors
5// are emitted during codegen.
6
7#include "Inputs/cuda.h"
8
9extern "C" void host_fn() {}
Justin Lebar4d38a5c2016-10-21 20:50:47 +000010// expected-note@-1 7 {{'host_fn' declared here}}
Justin Lebar9fdb46e2016-10-08 01:07:11 +000011
12struct Dummy {};
Justin Lebar18e2d822016-08-15 23:00:49 +000013
14struct S {
15 S() {}
Justin Lebar4d38a5c2016-10-21 20:50:47 +000016 // expected-note@-1 2 {{'S' declared here}}
Justin Lebar18e2d822016-08-15 23:00:49 +000017 ~S() { host_fn(); }
Justin Lebar26bb3112016-08-16 00:48:21 +000018 // expected-note@-1 {{'~S' declared here}}
Justin Lebar18e2d822016-08-15 23:00:49 +000019 int x;
20};
21
22struct T {
23 __host__ __device__ void hd() { host_fn(); }
24 // expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
25
26 // No error; this is (implicitly) inline and is never called, so isn't
27 // codegen'ed.
28 __host__ __device__ void hd2() { host_fn(); }
29
30 __host__ __device__ void hd3();
31
32 void h() {}
Justin Lebar26bb3112016-08-16 00:48:21 +000033 // expected-note@-1 {{'h' declared here}}
Justin Lebar9fdb46e2016-10-08 01:07:11 +000034
35 void operator+();
36 // expected-note@-1 {{'operator+' declared here}}
37
38 void operator-(const T&) {}
39 // expected-note@-1 {{'operator-' declared here}}
40
41 operator Dummy() { return Dummy(); }
42 // expected-note@-1 {{'operator Dummy' declared here}}
Richard Smithf75dcbe2016-10-11 00:21:10 +000043
44 __host__ void operator delete(void*);
45 __device__ void operator delete(void*, size_t);
46};
47
48struct U {
49 __device__ void operator delete(void*, size_t) = delete;
50 __host__ __device__ void operator delete(void*);
Justin Lebar18e2d822016-08-15 23:00:49 +000051};
52
53__host__ __device__ void T::hd3() {
54 host_fn();
55 // expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
56}
57
58template <typename T> __host__ __device__ void hd2() { host_fn(); }
59// expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
60__global__ void kernel() { hd2<int>(); }
61
62__host__ __device__ void hd() { host_fn(); }
63// expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
64
65template <typename T> __host__ __device__ void hd3() { host_fn(); }
66// expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
67__device__ void device_fn() { hd3<int>(); }
68
69// No error because this is never instantiated.
70template <typename T> __host__ __device__ void hd4() { host_fn(); }
71
72__host__ __device__ void local_var() {
73 S s;
74 // expected-error@-1 {{reference to __host__ function 'S' in __host__ __device__ function}}
75}
76
77__host__ __device__ void placement_new(char *ptr) {
78 ::new(ptr) S();
79 // expected-error@-1 {{reference to __host__ function 'S' in __host__ __device__ function}}
80}
81
82__host__ __device__ void explicit_destructor(S *s) {
83 s->~S();
84 // expected-error@-1 {{reference to __host__ function '~S' in __host__ __device__ function}}
85}
86
Richard Smithf75dcbe2016-10-11 00:21:10 +000087__host__ __device__ void class_specific_delete(T *t, U *u) {
88 delete t; // ok, call sized device delete even though host has preferable non-sized version
89 delete u; // ok, call non-sized HD delete rather than sized D delete
90}
91
Justin Lebar18e2d822016-08-15 23:00:49 +000092__host__ __device__ void hd_member_fn() {
93 T t;
94 // Necessary to trigger an error on T::hd. It's (implicitly) inline, so
95 // isn't codegen'ed until we call it.
96 t.hd();
97}
98
99__host__ __device__ void h_member_fn() {
100 T t;
101 t.h();
102 // expected-error@-1 {{reference to __host__ function 'h' in __host__ __device__ function}}
103}
104
105__host__ __device__ void fn_ptr() {
106 auto* ptr = &host_fn;
107 // expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
108}
109
110template <typename T>
111__host__ __device__ void fn_ptr_template() {
112 auto* ptr = &host_fn; // Not an error because the template isn't instantiated.
113}
Justin Lebar9fdb46e2016-10-08 01:07:11 +0000114
115__host__ __device__ void unaryOp() {
116 T t;
117 (void) +t; // expected-error {{reference to __host__ function 'operator+' in __host__ __device__ function}}
118}
119
120__host__ __device__ void binaryOp() {
121 T t;
122 (void) (t - t); // expected-error {{reference to __host__ function 'operator-' in __host__ __device__ function}}
123}
124
125__host__ __device__ void implicitConversion() {
126 T t;
127 Dummy d = t; // expected-error {{reference to __host__ function 'operator Dummy' in __host__ __device__ function}}
128}
129
130template <typename T>
131struct TmplStruct {
132 template <typename U> __host__ __device__ void fn() {}
133};
134
135template <>
136template <>
137__host__ __device__ void TmplStruct<int>::fn<int>() { host_fn(); }
138// expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
139
140__device__ void double_specialization() { TmplStruct<int>().fn<int>(); }