blob: ec926eaf85170e3ad845468f21dcb6f9d3416519 [file] [log] [blame]
Eli Bendersky7325e562014-09-03 15:27:03 +00001//===--- SemaCUDA.cpp - Semantic Analysis for CUDA constructs -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Eli Bendersky7325e562014-09-03 15:27:03 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements semantic analysis for CUDA constructs.
Eli Bendersky7325e562014-09-03 15:27:03 +000010///
11//===----------------------------------------------------------------------===//
12
Eli Bendersky7325e562014-09-03 15:27:03 +000013#include "clang/AST/ASTContext.h"
14#include "clang/AST/Decl.h"
Artem Belevich97c01c32016-02-02 22:29:48 +000015#include "clang/AST/ExprCXX.h"
Reid Klecknerbbc01782014-12-03 21:53:36 +000016#include "clang/Lex/Preprocessor.h"
Justin Lebarba122ab2016-03-30 23:30:21 +000017#include "clang/Sema/Lookup.h"
18#include "clang/Sema/Sema.h"
Eli Bendersky7325e562014-09-03 15:27:03 +000019#include "clang/Sema/SemaDiagnostic.h"
Justin Lebar179bdce2016-10-13 18:45:08 +000020#include "clang/Sema/SemaInternal.h"
Justin Lebarba122ab2016-03-30 23:30:21 +000021#include "clang/Sema/Template.h"
Eli Bendersky9a220fc2014-09-29 20:38:29 +000022#include "llvm/ADT/Optional.h"
23#include "llvm/ADT/SmallVector.h"
Eli Bendersky7325e562014-09-03 15:27:03 +000024using namespace clang;
25
Justin Lebar67a78a62016-10-08 22:15:58 +000026void Sema::PushForceCUDAHostDevice() {
27 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
28 ForceCUDAHostDeviceDepth++;
29}
30
31bool Sema::PopForceCUDAHostDevice() {
32 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
33 if (ForceCUDAHostDeviceDepth == 0)
34 return false;
35 ForceCUDAHostDeviceDepth--;
36 return true;
37}
38
Eli Bendersky7325e562014-09-03 15:27:03 +000039ExprResult Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc,
40 MultiExprArg ExecConfig,
41 SourceLocation GGGLoc) {
42 FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl();
43 if (!ConfigDecl)
Yaxun Liu887c5692018-04-25 01:10:37 +000044 return ExprError(
45 Diag(LLLLoc, diag::err_undeclared_var_use)
46 << (getLangOpts().HIP ? "hipConfigureCall" : "cudaConfigureCall"));
Eli Bendersky7325e562014-09-03 15:27:03 +000047 QualType ConfigQTy = ConfigDecl->getType();
48
49 DeclRefExpr *ConfigDR = new (Context)
Bruno Ricci5fc4db72018-12-21 14:10:18 +000050 DeclRefExpr(Context, ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc);
Eli Bendersky7325e562014-09-03 15:27:03 +000051 MarkFunctionReferenced(LLLLoc, ConfigDecl);
52
53 return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr,
54 /*IsExecConfig=*/true);
55}
56
Erich Keanec480f302018-07-12 21:09:05 +000057Sema::CUDAFunctionTarget
58Sema::IdentifyCUDATarget(const ParsedAttributesView &Attrs) {
Artem Belevich13e9b4d2016-12-07 19:27:16 +000059 bool HasHostAttr = false;
60 bool HasDeviceAttr = false;
61 bool HasGlobalAttr = false;
62 bool HasInvalidTargetAttr = false;
Erich Keanee891aa92018-07-13 15:07:47 +000063 for (const ParsedAttr &AL : Attrs) {
Erich Keanec480f302018-07-12 21:09:05 +000064 switch (AL.getKind()) {
Erich Keanee891aa92018-07-13 15:07:47 +000065 case ParsedAttr::AT_CUDAGlobal:
Artem Belevich13e9b4d2016-12-07 19:27:16 +000066 HasGlobalAttr = true;
67 break;
Erich Keanee891aa92018-07-13 15:07:47 +000068 case ParsedAttr::AT_CUDAHost:
Artem Belevich13e9b4d2016-12-07 19:27:16 +000069 HasHostAttr = true;
70 break;
Erich Keanee891aa92018-07-13 15:07:47 +000071 case ParsedAttr::AT_CUDADevice:
Artem Belevich13e9b4d2016-12-07 19:27:16 +000072 HasDeviceAttr = true;
73 break;
Erich Keanee891aa92018-07-13 15:07:47 +000074 case ParsedAttr::AT_CUDAInvalidTarget:
Artem Belevich13e9b4d2016-12-07 19:27:16 +000075 HasInvalidTargetAttr = true;
76 break;
77 default:
78 break;
79 }
Artem Belevich13e9b4d2016-12-07 19:27:16 +000080 }
Erich Keanec480f302018-07-12 21:09:05 +000081
Artem Belevich13e9b4d2016-12-07 19:27:16 +000082 if (HasInvalidTargetAttr)
83 return CFT_InvalidTarget;
84
85 if (HasGlobalAttr)
86 return CFT_Global;
87
88 if (HasHostAttr && HasDeviceAttr)
89 return CFT_HostDevice;
90
91 if (HasDeviceAttr)
92 return CFT_Device;
93
94 return CFT_Host;
95}
96
Artem Belevich64135c32016-12-08 19:38:13 +000097template <typename A>
98static bool hasAttr(const FunctionDecl *D, bool IgnoreImplicitAttr) {
99 return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
100 return isa<A>(Attribute) &&
101 !(IgnoreImplicitAttr && Attribute->isImplicit());
102 });
103}
104
Eli Bendersky7325e562014-09-03 15:27:03 +0000105/// IdentifyCUDATarget - Determine the CUDA compilation target for this function
Artem Belevich64135c32016-12-08 19:38:13 +0000106Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D,
107 bool IgnoreImplicitHDAttr) {
Justin Lebar179bdce2016-10-13 18:45:08 +0000108 // Code that lives outside a function is run on the host.
109 if (D == nullptr)
110 return CFT_Host;
111
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000112 if (D->hasAttr<CUDAInvalidTargetAttr>())
113 return CFT_InvalidTarget;
Eli Bendersky7325e562014-09-03 15:27:03 +0000114
115 if (D->hasAttr<CUDAGlobalAttr>())
116 return CFT_Global;
117
Artem Belevich64135c32016-12-08 19:38:13 +0000118 if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitHDAttr)) {
119 if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr))
Eli Bendersky7325e562014-09-03 15:27:03 +0000120 return CFT_HostDevice;
121 return CFT_Device;
Artem Belevich64135c32016-12-08 19:38:13 +0000122 } else if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr)) {
Eli Benderskyf2787a02014-09-30 17:38:34 +0000123 return CFT_Host;
Artem Belevich64135c32016-12-08 19:38:13 +0000124 } else if (D->isImplicit() && !IgnoreImplicitHDAttr) {
Eli Benderskyf2787a02014-09-30 17:38:34 +0000125 // Some implicit declarations (like intrinsic functions) are not marked.
126 // Set the most lenient target on them for maximal flexibility.
127 return CFT_HostDevice;
Eli Bendersky7325e562014-09-03 15:27:03 +0000128 }
129
130 return CFT_Host;
131}
132
Artem Belevich94a55e82015-09-22 17:22:59 +0000133// * CUDA Call preference table
134//
135// F - from,
136// T - to
137// Ph - preference in host mode
138// Pd - preference in device mode
139// H - handled in (x)
Justin Lebar39186472016-03-29 16:24:22 +0000140// Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never.
Artem Belevich94a55e82015-09-22 17:22:59 +0000141//
Artem Belevich18609102016-02-12 18:29:18 +0000142// | F | T | Ph | Pd | H |
143// |----+----+-----+-----+-----+
144// | d | d | N | N | (c) |
145// | d | g | -- | -- | (a) |
146// | d | h | -- | -- | (e) |
147// | d | hd | HD | HD | (b) |
148// | g | d | N | N | (c) |
149// | g | g | -- | -- | (a) |
150// | g | h | -- | -- | (e) |
151// | g | hd | HD | HD | (b) |
152// | h | d | -- | -- | (e) |
153// | h | g | N | N | (c) |
154// | h | h | N | N | (c) |
155// | h | hd | HD | HD | (b) |
156// | hd | d | WS | SS | (d) |
157// | hd | g | SS | -- |(d/a)|
158// | hd | h | SS | WS | (d) |
159// | hd | hd | HD | HD | (b) |
Artem Belevich94a55e82015-09-22 17:22:59 +0000160
161Sema::CUDAFunctionPreference
162Sema::IdentifyCUDAPreference(const FunctionDecl *Caller,
163 const FunctionDecl *Callee) {
Artem Belevich94a55e82015-09-22 17:22:59 +0000164 assert(Callee && "Callee must be valid.");
Justin Lebar179bdce2016-10-13 18:45:08 +0000165 CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller);
Artem Belevich94a55e82015-09-22 17:22:59 +0000166 CUDAFunctionTarget CalleeTarget = IdentifyCUDATarget(Callee);
Artem Belevich94a55e82015-09-22 17:22:59 +0000167
168 // If one of the targets is invalid, the check always fails, no matter what
169 // the other target is.
170 if (CallerTarget == CFT_InvalidTarget || CalleeTarget == CFT_InvalidTarget)
171 return CFP_Never;
172
173 // (a) Can't call global from some contexts until we support CUDA's
174 // dynamic parallelism.
175 if (CalleeTarget == CFT_Global &&
Justin Lebar0254c462016-10-12 01:30:08 +0000176 (CallerTarget == CFT_Global || CallerTarget == CFT_Device))
Artem Belevich94a55e82015-09-22 17:22:59 +0000177 return CFP_Never;
178
Artem Belevich18609102016-02-12 18:29:18 +0000179 // (b) Calling HostDevice is OK for everyone.
180 if (CalleeTarget == CFT_HostDevice)
181 return CFP_HostDevice;
182
183 // (c) Best case scenarios
Artem Belevich94a55e82015-09-22 17:22:59 +0000184 if (CalleeTarget == CallerTarget ||
185 (CallerTarget == CFT_Host && CalleeTarget == CFT_Global) ||
186 (CallerTarget == CFT_Global && CalleeTarget == CFT_Device))
Artem Belevich18609102016-02-12 18:29:18 +0000187 return CFP_Native;
Artem Belevich94a55e82015-09-22 17:22:59 +0000188
189 // (d) HostDevice behavior depends on compilation mode.
190 if (CallerTarget == CFT_HostDevice) {
Artem Belevich18609102016-02-12 18:29:18 +0000191 // It's OK to call a compilation-mode matching function from an HD one.
192 if ((getLangOpts().CUDAIsDevice && CalleeTarget == CFT_Device) ||
193 (!getLangOpts().CUDAIsDevice &&
194 (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)))
195 return CFP_SameSide;
196
Justin Lebar25c4a812016-03-29 16:24:16 +0000197 // Calls from HD to non-mode-matching functions (i.e., to host functions
198 // when compiling in device mode or to device functions when compiling in
199 // host mode) are allowed at the sema level, but eventually rejected if
200 // they're ever codegened. TODO: Reject said calls earlier.
201 return CFP_WrongSide;
Artem Belevich94a55e82015-09-22 17:22:59 +0000202 }
203
204 // (e) Calling across device/host boundary is not something you should do.
205 if ((CallerTarget == CFT_Host && CalleeTarget == CFT_Device) ||
206 (CallerTarget == CFT_Device && CalleeTarget == CFT_Host) ||
207 (CallerTarget == CFT_Global && CalleeTarget == CFT_Host))
Artem Belevich18609102016-02-12 18:29:18 +0000208 return CFP_Never;
Artem Belevich94a55e82015-09-22 17:22:59 +0000209
210 llvm_unreachable("All cases should've been handled by now.");
211}
212
Richard Smithf75dcbe2016-10-11 00:21:10 +0000213void Sema::EraseUnwantedCUDAMatches(
214 const FunctionDecl *Caller,
215 SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) {
Artem Belevich94a55e82015-09-22 17:22:59 +0000216 if (Matches.size() <= 1)
217 return;
218
Richard Smithf75dcbe2016-10-11 00:21:10 +0000219 using Pair = std::pair<DeclAccessPair, FunctionDecl*>;
220
Justin Lebare6a2cc12016-03-22 00:09:25 +0000221 // Gets the CUDA function preference for a call from Caller to Match.
Richard Smithf75dcbe2016-10-11 00:21:10 +0000222 auto GetCFP = [&](const Pair &Match) {
223 return IdentifyCUDAPreference(Caller, Match.second);
Justin Lebare6a2cc12016-03-22 00:09:25 +0000224 };
225
Artem Belevich94a55e82015-09-22 17:22:59 +0000226 // Find the best call preference among the functions in Matches.
Richard Smithf75dcbe2016-10-11 00:21:10 +0000227 CUDAFunctionPreference BestCFP = GetCFP(*std::max_element(
Justin Lebare6a2cc12016-03-22 00:09:25 +0000228 Matches.begin(), Matches.end(),
Richard Smithf75dcbe2016-10-11 00:21:10 +0000229 [&](const Pair &M1, const Pair &M2) { return GetCFP(M1) < GetCFP(M2); }));
Artem Belevich94a55e82015-09-22 17:22:59 +0000230
231 // Erase all functions with lower priority.
George Burgess IV8684b032017-01-04 19:16:29 +0000232 llvm::erase_if(Matches,
233 [&](const Pair &Match) { return GetCFP(Match) < BestCFP; });
Artem Belevich94a55e82015-09-22 17:22:59 +0000234}
235
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000236/// When an implicitly-declared special member has to invoke more than one
237/// base/field special member, conflicts may occur in the targets of these
238/// members. For example, if one base's member __host__ and another's is
239/// __device__, it's a conflict.
240/// This function figures out if the given targets \param Target1 and
241/// \param Target2 conflict, and if they do not it fills in
242/// \param ResolvedTarget with a target that resolves for both calls.
243/// \return true if there's a conflict, false otherwise.
244static bool
245resolveCalleeCUDATargetConflict(Sema::CUDAFunctionTarget Target1,
246 Sema::CUDAFunctionTarget Target2,
247 Sema::CUDAFunctionTarget *ResolvedTarget) {
Justin Lebarc66a1062016-01-20 00:26:57 +0000248 // Only free functions and static member functions may be global.
249 assert(Target1 != Sema::CFT_Global);
250 assert(Target2 != Sema::CFT_Global);
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000251
252 if (Target1 == Sema::CFT_HostDevice) {
253 *ResolvedTarget = Target2;
254 } else if (Target2 == Sema::CFT_HostDevice) {
255 *ResolvedTarget = Target1;
256 } else if (Target1 != Target2) {
257 return true;
258 } else {
259 *ResolvedTarget = Target1;
260 }
261
262 return false;
263}
264
265bool Sema::inferCUDATargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
266 CXXSpecialMember CSM,
267 CXXMethodDecl *MemberDecl,
268 bool ConstRHS,
269 bool Diagnose) {
270 llvm::Optional<CUDAFunctionTarget> InferredTarget;
271
272 // We're going to invoke special member lookup; mark that these special
273 // members are called from this one, and not from its caller.
274 ContextRAII MethodContext(*this, MemberDecl);
275
276 // Look for special members in base classes that should be invoked from here.
277 // Infer the target of this member base on the ones it should call.
278 // Skip direct and indirect virtual bases for abstract classes.
279 llvm::SmallVector<const CXXBaseSpecifier *, 16> Bases;
280 for (const auto &B : ClassDecl->bases()) {
281 if (!B.isVirtual()) {
282 Bases.push_back(&B);
283 }
284 }
285
286 if (!ClassDecl->isAbstract()) {
287 for (const auto &VB : ClassDecl->vbases()) {
288 Bases.push_back(&VB);
289 }
290 }
291
292 for (const auto *B : Bases) {
293 const RecordType *BaseType = B->getType()->getAs<RecordType>();
294 if (!BaseType) {
295 continue;
296 }
297
298 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl());
Richard Smith8bae1be2017-02-24 02:07:20 +0000299 Sema::SpecialMemberOverloadResult SMOR =
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000300 LookupSpecialMember(BaseClassDecl, CSM,
301 /* ConstArg */ ConstRHS,
302 /* VolatileArg */ false,
303 /* RValueThis */ false,
304 /* ConstThis */ false,
305 /* VolatileThis */ false);
306
Richard Smith8bae1be2017-02-24 02:07:20 +0000307 if (!SMOR.getMethod())
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000308 continue;
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000309
Richard Smith8bae1be2017-02-24 02:07:20 +0000310 CUDAFunctionTarget BaseMethodTarget = IdentifyCUDATarget(SMOR.getMethod());
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000311 if (!InferredTarget.hasValue()) {
312 InferredTarget = BaseMethodTarget;
313 } else {
314 bool ResolutionError = resolveCalleeCUDATargetConflict(
315 InferredTarget.getValue(), BaseMethodTarget,
316 InferredTarget.getPointer());
317 if (ResolutionError) {
318 if (Diagnose) {
319 Diag(ClassDecl->getLocation(),
320 diag::note_implicit_member_target_infer_collision)
321 << (unsigned)CSM << InferredTarget.getValue() << BaseMethodTarget;
322 }
323 MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
324 return true;
325 }
326 }
327 }
328
329 // Same as for bases, but now for special members of fields.
330 for (const auto *F : ClassDecl->fields()) {
331 if (F->isInvalidDecl()) {
332 continue;
333 }
334
335 const RecordType *FieldType =
336 Context.getBaseElementType(F->getType())->getAs<RecordType>();
337 if (!FieldType) {
338 continue;
339 }
340
341 CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(FieldType->getDecl());
Richard Smith8bae1be2017-02-24 02:07:20 +0000342 Sema::SpecialMemberOverloadResult SMOR =
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000343 LookupSpecialMember(FieldRecDecl, CSM,
344 /* ConstArg */ ConstRHS && !F->isMutable(),
345 /* VolatileArg */ false,
346 /* RValueThis */ false,
347 /* ConstThis */ false,
348 /* VolatileThis */ false);
349
Richard Smith8bae1be2017-02-24 02:07:20 +0000350 if (!SMOR.getMethod())
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000351 continue;
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000352
353 CUDAFunctionTarget FieldMethodTarget =
Richard Smith8bae1be2017-02-24 02:07:20 +0000354 IdentifyCUDATarget(SMOR.getMethod());
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000355 if (!InferredTarget.hasValue()) {
356 InferredTarget = FieldMethodTarget;
357 } else {
358 bool ResolutionError = resolveCalleeCUDATargetConflict(
359 InferredTarget.getValue(), FieldMethodTarget,
360 InferredTarget.getPointer());
361 if (ResolutionError) {
362 if (Diagnose) {
363 Diag(ClassDecl->getLocation(),
364 diag::note_implicit_member_target_infer_collision)
365 << (unsigned)CSM << InferredTarget.getValue()
366 << FieldMethodTarget;
367 }
368 MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
369 return true;
370 }
371 }
372 }
373
374 if (InferredTarget.hasValue()) {
375 if (InferredTarget.getValue() == CFT_Device) {
376 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
377 } else if (InferredTarget.getValue() == CFT_Host) {
378 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
379 } else {
380 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
381 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
382 }
383 } else {
384 // If no target was inferred, mark this member as __host__ __device__;
385 // it's the least restrictive option that can be invoked from any target.
386 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
387 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
388 }
389
390 return false;
391}
Artem Belevich97c01c32016-02-02 22:29:48 +0000392
393bool Sema::isEmptyCudaConstructor(SourceLocation Loc, CXXConstructorDecl *CD) {
394 if (!CD->isDefined() && CD->isTemplateInstantiation())
395 InstantiateFunctionDefinition(Loc, CD->getFirstDecl());
396
397 // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered
398 // empty at a point in the translation unit, if it is either a
399 // trivial constructor
400 if (CD->isTrivial())
401 return true;
402
403 // ... or it satisfies all of the following conditions:
404 // The constructor function has been defined.
405 // The constructor function has no parameters,
406 // and the function body is an empty compound statement.
407 if (!(CD->hasTrivialBody() && CD->getNumParams() == 0))
408 return false;
409
410 // Its class has no virtual functions and no virtual base classes.
411 if (CD->getParent()->isDynamicClass())
412 return false;
413
414 // The only form of initializer allowed is an empty constructor.
Artem Belevich3650bbe2016-05-19 20:13:53 +0000415 // This will recursively check all base classes and member initializers
Artem Belevich97c01c32016-02-02 22:29:48 +0000416 if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) {
417 if (const CXXConstructExpr *CE =
418 dyn_cast<CXXConstructExpr>(CI->getInit()))
419 return isEmptyCudaConstructor(Loc, CE->getConstructor());
420 return false;
421 }))
422 return false;
423
424 return true;
425}
Justin Lebarba122ab2016-03-30 23:30:21 +0000426
Artem Belevich3650bbe2016-05-19 20:13:53 +0000427bool Sema::isEmptyCudaDestructor(SourceLocation Loc, CXXDestructorDecl *DD) {
428 // No destructor -> no problem.
429 if (!DD)
430 return true;
431
432 if (!DD->isDefined() && DD->isTemplateInstantiation())
433 InstantiateFunctionDefinition(Loc, DD->getFirstDecl());
434
435 // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered
436 // empty at a point in the translation unit, if it is either a
437 // trivial constructor
438 if (DD->isTrivial())
439 return true;
440
441 // ... or it satisfies all of the following conditions:
442 // The destructor function has been defined.
443 // and the function body is an empty compound statement.
444 if (!DD->hasTrivialBody())
445 return false;
446
447 const CXXRecordDecl *ClassDecl = DD->getParent();
448
449 // Its class has no virtual functions and no virtual base classes.
450 if (ClassDecl->isDynamicClass())
451 return false;
452
453 // Only empty destructors are allowed. This will recursively check
454 // destructors for all base classes...
455 if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) {
456 if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())
457 return isEmptyCudaDestructor(Loc, RD->getDestructor());
458 return true;
459 }))
460 return false;
461
462 // ... and member fields.
463 if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) {
464 if (CXXRecordDecl *RD = Field->getType()
465 ->getBaseElementTypeUnsafe()
466 ->getAsCXXRecordDecl())
467 return isEmptyCudaDestructor(Loc, RD->getDestructor());
468 return true;
469 }))
470 return false;
471
472 return true;
473}
474
Artem Beleviche9fa53a2018-06-06 22:37:25 +0000475void Sema::checkAllowedCUDAInitializer(VarDecl *VD) {
476 if (VD->isInvalidDecl() || !VD->hasInit() || !VD->hasGlobalStorage())
477 return;
478 const Expr *Init = VD->getInit();
479 if (VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>() ||
480 VD->hasAttr<CUDASharedAttr>()) {
481 assert(!VD->isStaticLocal() || VD->hasAttr<CUDASharedAttr>());
482 bool AllowedInit = false;
483 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Init))
484 AllowedInit =
485 isEmptyCudaConstructor(VD->getLocation(), CE->getConstructor());
486 // We'll allow constant initializers even if it's a non-empty
487 // constructor according to CUDA rules. This deviates from NVCC,
488 // but allows us to handle things like constexpr constructors.
489 if (!AllowedInit &&
490 (VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>()))
491 AllowedInit = VD->getInit()->isConstantInitializer(
492 Context, VD->getType()->isReferenceType());
493
494 // Also make sure that destructor, if there is one, is empty.
495 if (AllowedInit)
496 if (CXXRecordDecl *RD = VD->getType()->getAsCXXRecordDecl())
497 AllowedInit =
498 isEmptyCudaDestructor(VD->getLocation(), RD->getDestructor());
499
500 if (!AllowedInit) {
501 Diag(VD->getLocation(), VD->hasAttr<CUDASharedAttr>()
502 ? diag::err_shared_var_init
503 : diag::err_dynamic_var_init)
504 << Init->getSourceRange();
505 VD->setInvalidDecl();
506 }
507 } else {
508 // This is a host-side global variable. Check that the initializer is
509 // callable from the host side.
510 const FunctionDecl *InitFn = nullptr;
511 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Init)) {
512 InitFn = CE->getConstructor();
513 } else if (const CallExpr *CE = dyn_cast<CallExpr>(Init)) {
514 InitFn = CE->getDirectCallee();
515 }
516 if (InitFn) {
517 CUDAFunctionTarget InitFnTarget = IdentifyCUDATarget(InitFn);
518 if (InitFnTarget != CFT_Host && InitFnTarget != CFT_HostDevice) {
519 Diag(VD->getLocation(), diag::err_ref_bad_target_global_initializer)
520 << InitFnTarget << InitFn;
521 Diag(InitFn->getLocation(), diag::note_previous_decl) << InitFn;
522 VD->setInvalidDecl();
523 }
524 }
525 }
526}
527
Justin Lebarba122ab2016-03-30 23:30:21 +0000528// With -fcuda-host-device-constexpr, an unattributed constexpr function is
529// treated as implicitly __host__ __device__, unless:
530// * it is a variadic function (device-side variadic functions are not
531// allowed), or
532// * a __device__ function with this signature was already declared, in which
533// case in which case we output an error, unless the __device__ decl is in a
534// system header, in which case we leave the constexpr function unattributed.
Justin Lebar67a78a62016-10-08 22:15:58 +0000535//
536// In addition, all function decls are treated as __host__ __device__ when
537// ForceCUDAHostDeviceDepth > 0 (corresponding to code within a
538// #pragma clang force_cuda_host_device_begin/end
539// pair).
Artem Belevich9fb40e32016-10-21 17:15:46 +0000540void Sema::maybeAddCUDAHostDeviceAttrs(FunctionDecl *NewD,
Justin Lebarba122ab2016-03-30 23:30:21 +0000541 const LookupResult &Previous) {
Justin Lebar9d4ed262016-09-30 23:57:38 +0000542 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar67a78a62016-10-08 22:15:58 +0000543
544 if (ForceCUDAHostDeviceDepth > 0) {
545 if (!NewD->hasAttr<CUDAHostAttr>())
546 NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
547 if (!NewD->hasAttr<CUDADeviceAttr>())
548 NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
549 return;
550 }
551
Justin Lebarba122ab2016-03-30 23:30:21 +0000552 if (!getLangOpts().CUDAHostDeviceConstexpr || !NewD->isConstexpr() ||
553 NewD->isVariadic() || NewD->hasAttr<CUDAHostAttr>() ||
554 NewD->hasAttr<CUDADeviceAttr>() || NewD->hasAttr<CUDAGlobalAttr>())
555 return;
556
557 // Is D a __device__ function with the same signature as NewD, ignoring CUDA
558 // attributes?
559 auto IsMatchingDeviceFn = [&](NamedDecl *D) {
560 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D))
561 D = Using->getTargetDecl();
562 FunctionDecl *OldD = D->getAsFunction();
563 return OldD && OldD->hasAttr<CUDADeviceAttr>() &&
564 !OldD->hasAttr<CUDAHostAttr>() &&
565 !IsOverload(NewD, OldD, /* UseMemberUsingDeclRules = */ false,
566 /* ConsiderCudaAttrs = */ false);
567 };
568 auto It = llvm::find_if(Previous, IsMatchingDeviceFn);
569 if (It != Previous.end()) {
570 // We found a __device__ function with the same name and signature as NewD
571 // (ignoring CUDA attrs). This is an error unless that function is defined
572 // in a system header, in which case we simply return without making NewD
573 // host+device.
574 NamedDecl *Match = *It;
575 if (!getSourceManager().isInSystemHeader(Match->getLocation())) {
576 Diag(NewD->getLocation(),
577 diag::err_cuda_unattributed_constexpr_cannot_overload_device)
Richard Trieub4025802018-03-28 04:16:13 +0000578 << NewD;
Justin Lebarba122ab2016-03-30 23:30:21 +0000579 Diag(Match->getLocation(),
580 diag::note_cuda_conflicting_device_function_declared_here);
581 }
582 return;
583 }
584
585 NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
586 NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
587}
Justin Lebar18e2d822016-08-15 23:00:49 +0000588
Justin Lebar23d95422016-10-13 20:52:12 +0000589// In CUDA, there are some constructs which may appear in semantically-valid
590// code, but trigger errors if we ever generate code for the function in which
591// they appear. Essentially every construct you're not allowed to use on the
592// device falls into this category, because you are allowed to use these
593// constructs in a __host__ __device__ function, but only if that function is
594// never codegen'ed on the device.
595//
596// To handle semantic checking for these constructs, we keep track of the set of
597// functions we know will be emitted, either because we could tell a priori that
598// they would be emitted, or because they were transitively called by a
599// known-emitted function.
600//
601// We also keep a partial call graph of which not-known-emitted functions call
602// which other not-known-emitted functions.
603//
604// When we see something which is illegal if the current function is emitted
605// (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or
606// CheckCUDACall), we first check if the current function is known-emitted. If
607// so, we immediately output the diagnostic.
608//
609// Otherwise, we "defer" the diagnostic. It sits in Sema::CUDADeferredDiags
610// until we discover that the function is known-emitted, at which point we take
611// it out of this map and emit the diagnostic.
612
Justin Lebar6c86e912016-10-19 21:15:01 +0000613Sema::CUDADiagBuilder::CUDADiagBuilder(Kind K, SourceLocation Loc,
614 unsigned DiagID, FunctionDecl *Fn,
615 Sema &S)
616 : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn),
617 ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) {
618 switch (K) {
619 case K_Nop:
620 break;
621 case K_Immediate:
622 case K_ImmediateWithCallStack:
623 ImmediateDiag.emplace(S.Diag(Loc, DiagID));
624 break;
625 case K_Deferred:
626 assert(Fn && "Must have a function to attach the deferred diag to.");
627 PartialDiag.emplace(S.PDiag(DiagID));
628 break;
629 }
630}
631
632// Print notes showing how we can reach FD starting from an a priori
633// known-callable function.
634static void EmitCallStackNotes(Sema &S, FunctionDecl *FD) {
635 auto FnIt = S.CUDAKnownEmittedFns.find(FD);
636 while (FnIt != S.CUDAKnownEmittedFns.end()) {
637 DiagnosticBuilder Builder(
638 S.Diags.Report(FnIt->second.Loc, diag::note_called_by));
639 Builder << FnIt->second.FD;
640 Builder.setForceEmit();
641
642 FnIt = S.CUDAKnownEmittedFns.find(FnIt->second.FD);
643 }
644}
645
646Sema::CUDADiagBuilder::~CUDADiagBuilder() {
647 if (ImmediateDiag) {
648 // Emit our diagnostic and, if it was a warning or error, output a callstack
649 // if Fn isn't a priori known-emitted.
650 bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel(
651 DiagID, Loc) >= DiagnosticsEngine::Warning;
652 ImmediateDiag.reset(); // Emit the immediate diag.
653 if (IsWarningOrError && ShowCallStack)
654 EmitCallStackNotes(S, Fn);
655 } else if (PartialDiag) {
656 assert(ShowCallStack && "Must always show call stack for deferred diags.");
657 S.CUDADeferredDiags[Fn].push_back({Loc, std::move(*PartialDiag)});
658 }
659}
660
Justin Lebar23d95422016-10-13 20:52:12 +0000661// Do we know that we will eventually codegen the given function?
662static bool IsKnownEmitted(Sema &S, FunctionDecl *FD) {
663 // Templates are emitted when they're instantiated.
664 if (FD->isDependentContext())
665 return false;
666
667 // When compiling for device, host functions are never emitted. Similarly,
668 // when compiling for host, device and global functions are never emitted.
669 // (Technically, we do emit a host-side stub for global functions, but this
670 // doesn't count for our purposes here.)
671 Sema::CUDAFunctionTarget T = S.IdentifyCUDATarget(FD);
672 if (S.getLangOpts().CUDAIsDevice && T == Sema::CFT_Host)
673 return false;
674 if (!S.getLangOpts().CUDAIsDevice &&
675 (T == Sema::CFT_Device || T == Sema::CFT_Global))
676 return false;
677
Justin Lebar2d56c262016-11-08 23:45:51 +0000678 // Check whether this function is externally visible -- if so, it's
679 // known-emitted.
680 //
681 // We have to check the GVA linkage of the function's *definition* -- if we
682 // only have a declaration, we don't know whether or not the function will be
683 // emitted, because (say) the definition could include "inline".
684 FunctionDecl *Def = FD->getDefinition();
685
Justin Lebar2d56c262016-11-08 23:45:51 +0000686 if (Def &&
687 !isDiscardableGVALinkage(S.getASTContext().GetGVALinkageForFunction(Def)))
Justin Lebar23d95422016-10-13 20:52:12 +0000688 return true;
689
690 // Otherwise, the function is known-emitted if it's in our set of
691 // known-emitted functions.
692 return S.CUDAKnownEmittedFns.count(FD) > 0;
693}
694
Justin Lebar179bdce2016-10-13 18:45:08 +0000695Sema::CUDADiagBuilder Sema::CUDADiagIfDeviceCode(SourceLocation Loc,
696 unsigned DiagID) {
697 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar23d95422016-10-13 20:52:12 +0000698 CUDADiagBuilder::Kind DiagKind = [&] {
699 switch (CurrentCUDATarget()) {
700 case CFT_Global:
701 case CFT_Device:
702 return CUDADiagBuilder::K_Immediate;
703 case CFT_HostDevice:
704 // An HD function counts as host code if we're compiling for host, and
705 // device code if we're compiling for device. Defer any errors in device
706 // mode until the function is known-emitted.
707 if (getLangOpts().CUDAIsDevice) {
708 return IsKnownEmitted(*this, dyn_cast<FunctionDecl>(CurContext))
Justin Lebar6c86e912016-10-19 21:15:01 +0000709 ? CUDADiagBuilder::K_ImmediateWithCallStack
Justin Lebar23d95422016-10-13 20:52:12 +0000710 : CUDADiagBuilder::K_Deferred;
711 }
712 return CUDADiagBuilder::K_Nop;
713
714 default:
715 return CUDADiagBuilder::K_Nop;
716 }
717 }();
Justin Lebar179bdce2016-10-13 18:45:08 +0000718 return CUDADiagBuilder(DiagKind, Loc, DiagID,
719 dyn_cast<FunctionDecl>(CurContext), *this);
720}
721
722Sema::CUDADiagBuilder Sema::CUDADiagIfHostCode(SourceLocation Loc,
723 unsigned DiagID) {
724 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar23d95422016-10-13 20:52:12 +0000725 CUDADiagBuilder::Kind DiagKind = [&] {
726 switch (CurrentCUDATarget()) {
727 case CFT_Host:
728 return CUDADiagBuilder::K_Immediate;
729 case CFT_HostDevice:
730 // An HD function counts as host code if we're compiling for host, and
731 // device code if we're compiling for device. Defer any errors in device
732 // mode until the function is known-emitted.
733 if (getLangOpts().CUDAIsDevice)
734 return CUDADiagBuilder::K_Nop;
735
736 return IsKnownEmitted(*this, dyn_cast<FunctionDecl>(CurContext))
Justin Lebar6c86e912016-10-19 21:15:01 +0000737 ? CUDADiagBuilder::K_ImmediateWithCallStack
Justin Lebar23d95422016-10-13 20:52:12 +0000738 : CUDADiagBuilder::K_Deferred;
739 default:
740 return CUDADiagBuilder::K_Nop;
741 }
742 }();
Justin Lebar179bdce2016-10-13 18:45:08 +0000743 return CUDADiagBuilder(DiagKind, Loc, DiagID,
744 dyn_cast<FunctionDecl>(CurContext), *this);
745}
746
Justin Lebar23d95422016-10-13 20:52:12 +0000747// Emit any deferred diagnostics for FD and erase them from the map in which
748// they're stored.
749static void EmitDeferredDiags(Sema &S, FunctionDecl *FD) {
750 auto It = S.CUDADeferredDiags.find(FD);
751 if (It == S.CUDADeferredDiags.end())
752 return;
Justin Lebar6c86e912016-10-19 21:15:01 +0000753 bool HasWarningOrError = false;
Justin Lebar23d95422016-10-13 20:52:12 +0000754 for (PartialDiagnosticAt &PDAt : It->second) {
755 const SourceLocation &Loc = PDAt.first;
756 const PartialDiagnostic &PD = PDAt.second;
Justin Lebar6c86e912016-10-19 21:15:01 +0000757 HasWarningOrError |= S.getDiagnostics().getDiagnosticLevel(
758 PD.getDiagID(), Loc) >= DiagnosticsEngine::Warning;
Justin Lebar23d95422016-10-13 20:52:12 +0000759 DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID()));
760 Builder.setForceEmit();
761 PD.Emit(Builder);
762 }
763 S.CUDADeferredDiags.erase(It);
Justin Lebar6c86e912016-10-19 21:15:01 +0000764
765 // FIXME: Should this be called after every warning/error emitted in the loop
766 // above, instead of just once per function? That would be consistent with
767 // how we handle immediate errors, but it also seems like a bit much.
768 if (HasWarningOrError)
769 EmitCallStackNotes(S, FD);
Justin Lebar23d95422016-10-13 20:52:12 +0000770}
771
772// Indicate that this function (and thus everything it transtively calls) will
773// be codegen'ed, and emit any deferred diagnostics on this function and its
774// (transitive) callees.
Justin Lebar6c86e912016-10-19 21:15:01 +0000775static void MarkKnownEmitted(Sema &S, FunctionDecl *OrigCaller,
776 FunctionDecl *OrigCallee, SourceLocation OrigLoc) {
Justin Lebar23d95422016-10-13 20:52:12 +0000777 // Nothing to do if we already know that FD is emitted.
Justin Lebar6c86e912016-10-19 21:15:01 +0000778 if (IsKnownEmitted(S, OrigCallee)) {
779 assert(!S.CUDACallGraph.count(OrigCallee));
Justin Lebar23d95422016-10-13 20:52:12 +0000780 return;
781 }
782
Justin Lebar6c86e912016-10-19 21:15:01 +0000783 // We've just discovered that OrigCallee is known-emitted. Walk our call
784 // graph to see what else we can now discover also must be emitted.
785
786 struct CallInfo {
787 FunctionDecl *Caller;
788 FunctionDecl *Callee;
789 SourceLocation Loc;
790 };
791 llvm::SmallVector<CallInfo, 4> Worklist = {{OrigCaller, OrigCallee, OrigLoc}};
792 llvm::SmallSet<CanonicalDeclPtr<FunctionDecl>, 4> Seen;
793 Seen.insert(OrigCallee);
Justin Lebar23d95422016-10-13 20:52:12 +0000794 while (!Worklist.empty()) {
Justin Lebar6c86e912016-10-19 21:15:01 +0000795 CallInfo C = Worklist.pop_back_val();
796 assert(!IsKnownEmitted(S, C.Callee) &&
Justin Lebar23d95422016-10-13 20:52:12 +0000797 "Worklist should not contain known-emitted functions.");
Justin Lebar6c86e912016-10-19 21:15:01 +0000798 S.CUDAKnownEmittedFns[C.Callee] = {C.Caller, C.Loc};
799 EmitDeferredDiags(S, C.Callee);
Justin Lebar23d95422016-10-13 20:52:12 +0000800
Justin Lebard692dfb2016-10-17 02:25:55 +0000801 // If this is a template instantiation, explore its callgraph as well:
802 // Non-dependent calls are part of the template's callgraph, while dependent
803 // calls are part of to the instantiation's call graph.
Justin Lebar6c86e912016-10-19 21:15:01 +0000804 if (auto *Templ = C.Callee->getPrimaryTemplate()) {
Justin Lebard692dfb2016-10-17 02:25:55 +0000805 FunctionDecl *TemplFD = Templ->getAsFunction();
806 if (!Seen.count(TemplFD) && !S.CUDAKnownEmittedFns.count(TemplFD)) {
807 Seen.insert(TemplFD);
Justin Lebar6c86e912016-10-19 21:15:01 +0000808 Worklist.push_back(
809 {/* Caller = */ C.Caller, /* Callee = */ TemplFD, C.Loc});
Justin Lebard692dfb2016-10-17 02:25:55 +0000810 }
811 }
Justin Lebar23d95422016-10-13 20:52:12 +0000812
Justin Lebar6c86e912016-10-19 21:15:01 +0000813 // Add all functions called by Callee to our worklist.
814 auto CGIt = S.CUDACallGraph.find(C.Callee);
Justin Lebar23d95422016-10-13 20:52:12 +0000815 if (CGIt == S.CUDACallGraph.end())
816 continue;
817
Justin Lebar6c86e912016-10-19 21:15:01 +0000818 for (std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation> FDLoc :
819 CGIt->second) {
820 FunctionDecl *NewCallee = FDLoc.first;
821 SourceLocation CallLoc = FDLoc.second;
822 if (Seen.count(NewCallee) || IsKnownEmitted(S, NewCallee))
Justin Lebar23d95422016-10-13 20:52:12 +0000823 continue;
Justin Lebar6c86e912016-10-19 21:15:01 +0000824 Seen.insert(NewCallee);
825 Worklist.push_back(
826 {/* Caller = */ C.Callee, /* Callee = */ NewCallee, CallLoc});
Justin Lebar23d95422016-10-13 20:52:12 +0000827 }
828
Justin Lebar6c86e912016-10-19 21:15:01 +0000829 // C.Callee is now known-emitted, so we no longer need to maintain its list
830 // of callees in CUDACallGraph.
Justin Lebar23d95422016-10-13 20:52:12 +0000831 S.CUDACallGraph.erase(CGIt);
832 }
833}
834
Justin Lebar18e2d822016-08-15 23:00:49 +0000835bool Sema::CheckCUDACall(SourceLocation Loc, FunctionDecl *Callee) {
Justin Lebar9d4ed262016-09-30 23:57:38 +0000836 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar18e2d822016-08-15 23:00:49 +0000837 assert(Callee && "Callee may not be null.");
Justin Lebar23d95422016-10-13 20:52:12 +0000838 // FIXME: Is bailing out early correct here? Should we instead assume that
839 // the caller is a global initializer?
Justin Lebar18e2d822016-08-15 23:00:49 +0000840 FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext);
841 if (!Caller)
842 return true;
843
Justin Lebard692dfb2016-10-17 02:25:55 +0000844 // If the caller is known-emitted, mark the callee as known-emitted.
845 // Otherwise, mark the call in our call graph so we can traverse it later.
Justin Lebar23d95422016-10-13 20:52:12 +0000846 bool CallerKnownEmitted = IsKnownEmitted(*this, Caller);
Artem Beleviche2ae8b52018-03-23 19:49:03 +0000847 if (CallerKnownEmitted) {
848 // Host-side references to a __global__ function refer to the stub, so the
849 // function itself is never emitted and therefore should not be marked.
850 if (getLangOpts().CUDAIsDevice || IdentifyCUDATarget(Callee) != CFT_Global)
851 MarkKnownEmitted(*this, Caller, Callee, Loc);
852 } else {
Justin Lebard692dfb2016-10-17 02:25:55 +0000853 // If we have
854 // host fn calls kernel fn calls host+device,
855 // the HD function does not get instantiated on the host. We model this by
856 // omitting at the call to the kernel from the callgraph. This ensures
857 // that, when compiling for host, only HD functions actually called from the
858 // host get marked as known-emitted.
859 if (getLangOpts().CUDAIsDevice || IdentifyCUDATarget(Callee) != CFT_Global)
Justin Lebar6c86e912016-10-19 21:15:01 +0000860 CUDACallGraph[Caller].insert({Callee, Loc});
Justin Lebard692dfb2016-10-17 02:25:55 +0000861 }
Justin Lebar23d95422016-10-13 20:52:12 +0000862
863 CUDADiagBuilder::Kind DiagKind = [&] {
864 switch (IdentifyCUDAPreference(Caller, Callee)) {
865 case CFP_Never:
866 return CUDADiagBuilder::K_Immediate;
867 case CFP_WrongSide:
868 assert(Caller && "WrongSide calls require a non-null caller");
869 // If we know the caller will be emitted, we know this wrong-side call
870 // will be emitted, so it's an immediate error. Otherwise, defer the
871 // error until we know the caller is emitted.
Justin Lebar6c86e912016-10-19 21:15:01 +0000872 return CallerKnownEmitted ? CUDADiagBuilder::K_ImmediateWithCallStack
Justin Lebar23d95422016-10-13 20:52:12 +0000873 : CUDADiagBuilder::K_Deferred;
874 default:
875 return CUDADiagBuilder::K_Nop;
876 }
877 }();
Justin Lebar9fdb46e2016-10-08 01:07:11 +0000878
Justin Lebar9730ae92016-10-19 21:03:38 +0000879 if (DiagKind == CUDADiagBuilder::K_Nop)
880 return true;
881
Justin Lebar179bdce2016-10-13 18:45:08 +0000882 // Avoid emitting this error twice for the same location. Using a hashtable
883 // like this is unfortunate, but because we must continue parsing as normal
884 // after encountering a deferred error, it's otherwise very tricky for us to
885 // ensure that we only emit this deferred error once.
Justin Lebar6f727372016-10-21 20:08:52 +0000886 if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second)
Justin Lebar18e2d822016-08-15 23:00:49 +0000887 return true;
Justin Lebar2a8db342016-09-28 22:45:54 +0000888
Justin Lebar9730ae92016-10-19 21:03:38 +0000889 CUDADiagBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller, *this)
Justin Lebar179bdce2016-10-13 18:45:08 +0000890 << IdentifyCUDATarget(Callee) << Callee << IdentifyCUDATarget(Caller);
891 CUDADiagBuilder(DiagKind, Callee->getLocation(), diag::note_previous_decl,
892 Caller, *this)
893 << Callee;
Justin Lebar6c86e912016-10-19 21:15:01 +0000894 return DiagKind != CUDADiagBuilder::K_Immediate &&
895 DiagKind != CUDADiagBuilder::K_ImmediateWithCallStack;
Justin Lebarb17840d2016-09-28 22:45:58 +0000896}
Justin Lebar7ca116c2016-09-30 17:14:53 +0000897
898void Sema::CUDASetLambdaAttrs(CXXMethodDecl *Method) {
Justin Lebar9d4ed262016-09-30 23:57:38 +0000899 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar7ca116c2016-09-30 17:14:53 +0000900 if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>())
901 return;
902 FunctionDecl *CurFn = dyn_cast<FunctionDecl>(CurContext);
903 if (!CurFn)
904 return;
905 CUDAFunctionTarget Target = IdentifyCUDATarget(CurFn);
906 if (Target == CFT_Global || Target == CFT_Device) {
907 Method->addAttr(CUDADeviceAttr::CreateImplicit(Context));
908 } else if (Target == CFT_HostDevice) {
909 Method->addAttr(CUDADeviceAttr::CreateImplicit(Context));
910 Method->addAttr(CUDAHostAttr::CreateImplicit(Context));
911 }
Justin Lebar7ca116c2016-09-30 17:14:53 +0000912}
Artem Belevich13e9b4d2016-12-07 19:27:16 +0000913
914void Sema::checkCUDATargetOverload(FunctionDecl *NewFD,
Artem Belevich64135c32016-12-08 19:38:13 +0000915 const LookupResult &Previous) {
Artem Belevich13e9b4d2016-12-07 19:27:16 +0000916 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
917 CUDAFunctionTarget NewTarget = IdentifyCUDATarget(NewFD);
918 for (NamedDecl *OldND : Previous) {
919 FunctionDecl *OldFD = OldND->getAsFunction();
920 if (!OldFD)
921 continue;
922
923 CUDAFunctionTarget OldTarget = IdentifyCUDATarget(OldFD);
924 // Don't allow HD and global functions to overload other functions with the
925 // same signature. We allow overloading based on CUDA attributes so that
926 // functions can have different implementations on the host and device, but
927 // HD/global functions "exist" in some sense on both the host and device, so
928 // should have the same implementation on both sides.
929 if (NewTarget != OldTarget &&
930 ((NewTarget == CFT_HostDevice) || (OldTarget == CFT_HostDevice) ||
931 (NewTarget == CFT_Global) || (OldTarget == CFT_Global)) &&
932 !IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false,
933 /* ConsiderCudaAttrs = */ false)) {
934 Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)
935 << NewTarget << NewFD->getDeclName() << OldTarget << OldFD;
936 Diag(OldFD->getLocation(), diag::note_previous_declaration);
937 NewFD->setInvalidDecl();
938 break;
939 }
940 }
941}
Artem Belevich64135c32016-12-08 19:38:13 +0000942
943template <typename AttrTy>
944static void copyAttrIfPresent(Sema &S, FunctionDecl *FD,
945 const FunctionDecl &TemplateFD) {
946 if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
947 AttrTy *Clone = Attribute->clone(S.Context);
948 Clone->setInherited(true);
949 FD->addAttr(Clone);
950 }
951}
952
953void Sema::inheritCUDATargetAttrs(FunctionDecl *FD,
954 const FunctionTemplateDecl &TD) {
955 const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();
956 copyAttrIfPresent<CUDAGlobalAttr>(*this, FD, TemplateFD);
957 copyAttrIfPresent<CUDAHostAttr>(*this, FD, TemplateFD);
958 copyAttrIfPresent<CUDADeviceAttr>(*this, FD, TemplateFD);
959}