aboutsummaryrefslogtreecommitdiff
path: root/test/SemaCUDA/function-overload.cu
blob: bd3fb508bfab8fc8c51d7ee121747139d8d2732f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// REQUIRES: x86-registered-target
// REQUIRES: nvptx-registered-target

// Make sure we handle target overloads correctly.
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu \
// RUN:    -fsyntax-only -fcuda-target-overloads -verify %s
// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda \
// RUN:    -fsyntax-only -fcuda-target-overloads -fcuda-is-device -verify %s

// Check target overloads handling with disabled call target checks.
// RUN: %clang_cc1 -DNOCHECKS -triple x86_64-unknown-linux-gnu -fsyntax-only \
// RUN:    -fcuda-disable-target-call-checks -fcuda-target-overloads -verify %s
// RUN: %clang_cc1 -DNOCHECKS -triple nvptx64-nvidia-cuda -fsyntax-only \
// RUN:    -fcuda-disable-target-call-checks -fcuda-target-overloads \
// RUN:    -fcuda-is-device -verify %s

#include "Inputs/cuda.h"

typedef int (*fp_t)(void);
typedef void (*gp_t)(void);

// Host and unattributed functions can't be overloaded
__host__ int hh(void) { return 1; } // expected-note {{previous definition is here}}
int hh(void) { return 1; } // expected-error {{redefinition of 'hh'}}

// H/D overloading is OK
__host__ int dh(void) { return 2; }
__device__ int dh(void) { return 2; }

// H/HD and D/HD are not allowed
__host__ __device__ int hdh(void) { return 5; } // expected-note {{previous definition is here}}
__host__ int hdh(void) { return 4; } // expected-error {{redefinition of 'hdh'}}

__host__ int hhd(void) { return 4; } // expected-note {{previous definition is here}}
__host__ __device__ int hhd(void) { return 5; } // expected-error {{redefinition of 'hhd'}}
// expected-warning@-1 {{attribute declaration must precede definition}}
// expected-note@-3 {{previous definition is here}}

__host__ __device__ int hdd(void) { return 7; } // expected-note {{previous definition is here}}
__device__ int hdd(void) { return 6; } // expected-error {{redefinition of 'hdd'}}

__device__ int dhd(void) { return 6; } // expected-note {{previous definition is here}}
__host__ __device__ int dhd(void) { return 7; } // expected-error {{redefinition of 'dhd'}}
// expected-warning@-1 {{attribute declaration must precede definition}}
// expected-note@-3 {{previous definition is here}}

// Same tests for extern "C" functions
extern "C" __host__ int chh(void) {return 11;} // expected-note {{previous definition is here}}
extern "C" int chh(void) {return 11;} // expected-error {{redefinition of 'chh'}}

// H/D overloading is OK
extern "C" __device__ int cdh(void) {return 10;}
extern "C" __host__ int cdh(void) {return 11;}

// H/HD and D/HD overloading is not allowed.
extern "C" __host__ __device__ int chhd1(void) {return 12;} // expected-note {{previous definition is here}}
extern "C" __host__ int chhd1(void) {return 13;} // expected-error {{redefinition of 'chhd1'}}

extern "C" __host__ int chhd2(void) {return 13;} // expected-note {{previous definition is here}}
extern "C" __host__ __device__ int chhd2(void) {return 12;} // expected-error {{redefinition of 'chhd2'}}
// expected-warning@-1 {{attribute declaration must precede definition}}
// expected-note@-3 {{previous definition is here}}

// Helper functions to verify calling restrictions.
__device__ int d(void) { return 8; }
__host__ int h(void) { return 9; }
__global__ void g(void) {}
extern "C" __device__ int cd(void) {return 10;}
extern "C" __host__ int ch(void) {return 11;}

__host__ void hostf(void) {
  fp_t dp = d;
  fp_t cdp = cd;
#if !defined(NOCHECKS)
  // expected-error@-3 {{reference to __device__ function 'd' in __host__ function}}
  // expected-note@65 {{'d' declared here}}
  // expected-error@-4 {{reference to __device__ function 'cd' in __host__ function}}
  // expected-note@68 {{'cd' declared here}}
#endif
  fp_t hp = h;
  fp_t chp = ch;
  fp_t dhp = dh;
  fp_t cdhp = cdh;
  gp_t gp = g;

  d();
  cd();
#if !defined(NOCHECKS)
  // expected-error@-3 {{no matching function for call to 'd'}}
  // expected-note@65 {{candidate function not viable: call to __device__ function from __host__ function}}
  // expected-error@-4 {{no matching function for call to 'cd'}}
  // expected-note@68 {{candidate function not viable: call to __device__ function from __host__ function}}
#endif
  h();
  ch();
  dh();
  cdh();
  g(); // expected-error {{call to global function g not configured}}
  g<<<0,0>>>();
}


__device__ void devicef(void) {
  fp_t dp = d;
  fp_t cdp = cd;
  fp_t hp = h;
  fp_t chp = ch;
#if !defined(NOCHECKS)
  // expected-error@-3 {{reference to __host__ function 'h' in __device__ function}}
  // expected-note@66 {{'h' declared here}}
  // expected-error@-4 {{reference to __host__ function 'ch' in __device__ function}}
  // expected-note@69 {{'ch' declared here}}
#endif
  fp_t dhp = dh;
  fp_t cdhp = cdh;
  gp_t gp = g; // expected-error {{reference to __global__ function 'g' in __device__ function}}
               // expected-note@67 {{'g' declared here}}

  d();
  cd();
  h();
  ch();
#if !defined(NOCHECKS)
  // expected-error@-3 {{no matching function for call to 'h'}}
  // expected-note@66 {{candidate function not viable: call to __host__ function from __device__ function}}
  // expected-error@-4 {{no matching function for call to 'ch'}}
  // expected-note@69 {{candidate function not viable: call to __host__ function from __device__ function}}
#endif
  dh();
  cdh();
  g(); // expected-error {{no matching function for call to 'g'}}
  // expected-note@67 {{candidate function not viable: call to __global__ function from __device__ function}}
  g<<<0,0>>>(); // expected-error {{reference to __global__ function 'g' in __device__ function}}
  // expected-note@67 {{'g' declared here}}
}

__global__ void globalf(void) {
  fp_t dp = d;
  fp_t cdp = cd;
  fp_t hp = h;
  fp_t chp = ch;
#if !defined(NOCHECKS)
  // expected-error@-3 {{reference to __host__ function 'h' in __global__ function}}
  // expected-note@66 {{'h' declared here}}
  // expected-error@-4 {{reference to __host__ function 'ch' in __global__ function}}
  // expected-note@69 {{'ch' declared here}}
#endif
  fp_t dhp = dh;
  fp_t cdhp = cdh;
  gp_t gp = g; // expected-error {{reference to __global__ function 'g' in __global__ function}}
               // expected-note@67 {{'g' declared here}}

  d();
  cd();
  h();
  ch();
#if !defined(NOCHECKS)
  // expected-error@-3 {{no matching function for call to 'h'}}
  // expected-note@66 {{candidate function not viable: call to __host__ function from __global__ function}}
  // expected-error@-4 {{no matching function for call to 'ch'}}
  // expected-note@69 {{candidate function not viable: call to __host__ function from __global__ function}}
#endif
  dh();
  cdh();
  g(); // expected-error {{no matching function for call to 'g'}}
  // expected-note@67 {{candidate function not viable: call to __global__ function from __global__ function}}
  g<<<0,0>>>(); // expected-error {{reference to __global__ function 'g' in __global__ function}}
  // expected-note@67 {{'g' declared here}}
}

__host__ __device__ void hostdevicef(void) {
  fp_t dp = d;
  fp_t cdp = cd;
  fp_t hp = h;
  fp_t chp = ch;
#if !defined(NOCHECKS)
#if !defined(__CUDA_ARCH__)
  // expected-error@-6 {{reference to __device__ function 'd' in __host__ __device__ function}}
  // expected-note@65 {{'d' declared here}}
  // expected-error@-7 {{reference to __device__ function 'cd' in __host__ __device__ function}}
  // expected-note@68 {{'cd' declared here}}
#else
  // expected-error@-9 {{reference to __host__ function 'h' in __host__ __device__ function}}
  // expected-note@66 {{'h' declared here}}
  // expected-error@-10 {{reference to __host__ function 'ch' in __host__ __device__ function}}
  // expected-note@69 {{'ch' declared here}}
#endif
#endif
  fp_t dhp = dh;
  fp_t cdhp = cdh;
  gp_t gp = g;
#if defined(__CUDA_ARCH__)
  // expected-error@-2 {{reference to __global__ function 'g' in __host__ __device__ function}}
  // expected-note@67 {{'g' declared here}}
#endif

  d();
  cd();
  h();
  ch();
#if !defined(NOCHECKS)
#if !defined(__CUDA_ARCH__)
  // expected-error@-6 {{no matching function for call to 'd'}}
  // expected-note@65 {{candidate function not viable: call to __device__ function from __host__ __device__ function}}
  // expected-error@-7 {{no matching function for call to 'cd'}}
  // expected-note@68 {{candidate function not viable: call to __device__ function from __host__ __device__ function}}
#else
  // expected-error@-9 {{no matching function for call to 'h'}}
  // expected-note@66 {{candidate function not viable: call to __host__ function from __host__ __device__ function}}
  // expected-error@-10 {{no matching function for call to 'ch'}}
  // expected-note@69 {{candidate function not viable: call to __host__ function from __host__ __device__ function}}
#endif
#endif

  dh();
  cdh();
  g();
  g<<<0,0>>>();
#if !defined(__CUDA_ARCH__)
  // expected-error@-3 {{call to global function g not configured}}
#else
  // expected-error@-5 {{no matching function for call to 'g'}}
  // expected-note@67 {{candidate function not viable: call to __global__ function from __host__ __device__ function}}
  // expected-error@-6 {{reference to __global__ function 'g' in __host__ __device__ function}}
  // expected-note@67 {{'g' declared here}}
#endif  // __CUDA_ARCH__
}

// Test for address of overloaded function resolution in the global context.
fp_t hp = h;
fp_t chp = ch;
fp_t dhp = dh;
fp_t cdhp = cdh;
gp_t gp = g;


// Test overloading of destructors
// Can't mix H and unattributed destructors
struct d_h {
  ~d_h() {} // expected-note {{previous declaration is here}}
  __host__ ~d_h() {} // expected-error {{destructor cannot be redeclared}}
};

// H/D overloading is OK
struct d_dh {
  __device__ ~d_dh() {}
  __host__ ~d_dh() {}
};

// HD is OK
struct d_hd {
  __host__ __device__ ~d_hd() {}
};

// Mixing H/D and HD is not allowed.
struct d_dhhd {
  __device__ ~d_dhhd() {}
  __host__ ~d_dhhd() {} // expected-note {{previous declaration is here}}
  __host__ __device__ ~d_dhhd() {} // expected-error {{destructor cannot be redeclared}}
};

struct d_hhd {
  __host__ ~d_hhd() {} // expected-note {{previous declaration is here}}
  __host__ __device__ ~d_hhd() {} // expected-error {{destructor cannot be redeclared}}
};

struct d_hdh {
  __host__ __device__ ~d_hdh() {} // expected-note {{previous declaration is here}}
  __host__ ~d_hdh() {} // expected-error {{destructor cannot be redeclared}}
};

struct d_dhd {
  __device__ ~d_dhd() {} // expected-note {{previous declaration is here}}
  __host__ __device__ ~d_dhd() {} // expected-error {{destructor cannot be redeclared}}
};

struct d_hdd {
  __host__ __device__ ~d_hdd() {} // expected-note {{previous declaration is here}}
  __device__ ~d_hdd() {} // expected-error {{destructor cannot be redeclared}}
};

// Test overloading of member functions
struct m_h {
  void operator delete(void *ptr); // expected-note {{previous declaration is here}}
  __host__ void operator delete(void *ptr); // expected-error {{class member cannot be redeclared}}
};

// D/H overloading is OK
struct m_dh {
  __device__ void operator delete(void *ptr);
  __host__ void operator delete(void *ptr);
};

// HD by itself is OK
struct m_hd {
  __device__ __host__ void operator delete(void *ptr);
};

struct m_hhd {
  __host__ void operator delete(void *ptr) {} // expected-note {{previous declaration is here}}
  __host__ __device__ void operator delete(void *ptr) {} // expected-error {{class member cannot be redeclared}}
};

struct m_hdh {
  __host__ __device__ void operator delete(void *ptr) {} // expected-note {{previous declaration is here}}
  __host__ void operator delete(void *ptr) {} // expected-error {{class member cannot be redeclared}}
};

struct m_dhd {
  __device__ void operator delete(void *ptr) {} // expected-note {{previous declaration is here}}
  __host__ __device__ void operator delete(void *ptr) {} // expected-error {{class member cannot be redeclared}}
};

struct m_hdd {
  __host__ __device__ void operator delete(void *ptr) {} // expected-note {{previous declaration is here}}
  __device__ void operator delete(void *ptr) {} // expected-error {{class member cannot be redeclared}}
};