blob: 6f272ec839f5330ede8dd59ab338e3f8f54fe9f3 [file] [log] [blame]
Eli Bendersky7325e562014-09-03 15:27:03 +00001//===--- SemaCUDA.cpp - Semantic Analysis for CUDA constructs -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for CUDA constructs.
11///
12//===----------------------------------------------------------------------===//
13
Eli Bendersky7325e562014-09-03 15:27:03 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Artem Belevich97c01c32016-02-02 22:29:48 +000016#include "clang/AST/ExprCXX.h"
Reid Klecknerbbc01782014-12-03 21:53:36 +000017#include "clang/Lex/Preprocessor.h"
Justin Lebarba122ab2016-03-30 23:30:21 +000018#include "clang/Sema/Lookup.h"
19#include "clang/Sema/Sema.h"
Eli Bendersky7325e562014-09-03 15:27:03 +000020#include "clang/Sema/SemaDiagnostic.h"
Justin Lebar179bdce2016-10-13 18:45:08 +000021#include "clang/Sema/SemaInternal.h"
Justin Lebarba122ab2016-03-30 23:30:21 +000022#include "clang/Sema/Template.h"
Eli Bendersky9a220fc2014-09-29 20:38:29 +000023#include "llvm/ADT/Optional.h"
24#include "llvm/ADT/SmallVector.h"
Eli Bendersky7325e562014-09-03 15:27:03 +000025using namespace clang;
26
Justin Lebar67a78a62016-10-08 22:15:58 +000027void Sema::PushForceCUDAHostDevice() {
28 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
29 ForceCUDAHostDeviceDepth++;
30}
31
32bool Sema::PopForceCUDAHostDevice() {
33 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
34 if (ForceCUDAHostDeviceDepth == 0)
35 return false;
36 ForceCUDAHostDeviceDepth--;
37 return true;
38}
39
Eli Bendersky7325e562014-09-03 15:27:03 +000040ExprResult Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc,
41 MultiExprArg ExecConfig,
42 SourceLocation GGGLoc) {
43 FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl();
44 if (!ConfigDecl)
45 return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)
46 << "cudaConfigureCall");
47 QualType ConfigQTy = ConfigDecl->getType();
48
49 DeclRefExpr *ConfigDR = new (Context)
50 DeclRefExpr(ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc);
51 MarkFunctionReferenced(LLLLoc, ConfigDecl);
52
53 return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr,
54 /*IsExecConfig=*/true);
55}
56
Artem Belevich13e9b4d2016-12-07 19:27:16 +000057Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const AttributeList *Attr) {
58 bool HasHostAttr = false;
59 bool HasDeviceAttr = false;
60 bool HasGlobalAttr = false;
61 bool HasInvalidTargetAttr = false;
62 while (Attr) {
63 switch(Attr->getKind()){
64 case AttributeList::AT_CUDAGlobal:
65 HasGlobalAttr = true;
66 break;
67 case AttributeList::AT_CUDAHost:
68 HasHostAttr = true;
69 break;
70 case AttributeList::AT_CUDADevice:
71 HasDeviceAttr = true;
72 break;
73 case AttributeList::AT_CUDAInvalidTarget:
74 HasInvalidTargetAttr = true;
75 break;
76 default:
77 break;
78 }
79 Attr = Attr->getNext();
80 }
81 if (HasInvalidTargetAttr)
82 return CFT_InvalidTarget;
83
84 if (HasGlobalAttr)
85 return CFT_Global;
86
87 if (HasHostAttr && HasDeviceAttr)
88 return CFT_HostDevice;
89
90 if (HasDeviceAttr)
91 return CFT_Device;
92
93 return CFT_Host;
94}
95
Artem Belevich64135c32016-12-08 19:38:13 +000096template <typename A>
97static bool hasAttr(const FunctionDecl *D, bool IgnoreImplicitAttr) {
98 return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
99 return isa<A>(Attribute) &&
100 !(IgnoreImplicitAttr && Attribute->isImplicit());
101 });
102}
103
Eli Bendersky7325e562014-09-03 15:27:03 +0000104/// IdentifyCUDATarget - Determine the CUDA compilation target for this function
Artem Belevich64135c32016-12-08 19:38:13 +0000105Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D,
106 bool IgnoreImplicitHDAttr) {
Justin Lebar179bdce2016-10-13 18:45:08 +0000107 // Code that lives outside a function is run on the host.
108 if (D == nullptr)
109 return CFT_Host;
110
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000111 if (D->hasAttr<CUDAInvalidTargetAttr>())
112 return CFT_InvalidTarget;
Eli Bendersky7325e562014-09-03 15:27:03 +0000113
114 if (D->hasAttr<CUDAGlobalAttr>())
115 return CFT_Global;
116
Artem Belevich64135c32016-12-08 19:38:13 +0000117 if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitHDAttr)) {
118 if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr))
Eli Bendersky7325e562014-09-03 15:27:03 +0000119 return CFT_HostDevice;
120 return CFT_Device;
Artem Belevich64135c32016-12-08 19:38:13 +0000121 } else if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr)) {
Eli Benderskyf2787a02014-09-30 17:38:34 +0000122 return CFT_Host;
Artem Belevich64135c32016-12-08 19:38:13 +0000123 } else if (D->isImplicit() && !IgnoreImplicitHDAttr) {
Eli Benderskyf2787a02014-09-30 17:38:34 +0000124 // Some implicit declarations (like intrinsic functions) are not marked.
125 // Set the most lenient target on them for maximal flexibility.
126 return CFT_HostDevice;
Eli Bendersky7325e562014-09-03 15:27:03 +0000127 }
128
129 return CFT_Host;
130}
131
Artem Belevich94a55e82015-09-22 17:22:59 +0000132// * CUDA Call preference table
133//
134// F - from,
135// T - to
136// Ph - preference in host mode
137// Pd - preference in device mode
138// H - handled in (x)
Justin Lebar39186472016-03-29 16:24:22 +0000139// Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never.
Artem Belevich94a55e82015-09-22 17:22:59 +0000140//
Artem Belevich18609102016-02-12 18:29:18 +0000141// | F | T | Ph | Pd | H |
142// |----+----+-----+-----+-----+
143// | d | d | N | N | (c) |
144// | d | g | -- | -- | (a) |
145// | d | h | -- | -- | (e) |
146// | d | hd | HD | HD | (b) |
147// | g | d | N | N | (c) |
148// | g | g | -- | -- | (a) |
149// | g | h | -- | -- | (e) |
150// | g | hd | HD | HD | (b) |
151// | h | d | -- | -- | (e) |
152// | h | g | N | N | (c) |
153// | h | h | N | N | (c) |
154// | h | hd | HD | HD | (b) |
155// | hd | d | WS | SS | (d) |
156// | hd | g | SS | -- |(d/a)|
157// | hd | h | SS | WS | (d) |
158// | hd | hd | HD | HD | (b) |
Artem Belevich94a55e82015-09-22 17:22:59 +0000159
160Sema::CUDAFunctionPreference
161Sema::IdentifyCUDAPreference(const FunctionDecl *Caller,
162 const FunctionDecl *Callee) {
Artem Belevich94a55e82015-09-22 17:22:59 +0000163 assert(Callee && "Callee must be valid.");
Justin Lebar179bdce2016-10-13 18:45:08 +0000164 CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller);
Artem Belevich94a55e82015-09-22 17:22:59 +0000165 CUDAFunctionTarget CalleeTarget = IdentifyCUDATarget(Callee);
Artem Belevich94a55e82015-09-22 17:22:59 +0000166
167 // If one of the targets is invalid, the check always fails, no matter what
168 // the other target is.
169 if (CallerTarget == CFT_InvalidTarget || CalleeTarget == CFT_InvalidTarget)
170 return CFP_Never;
171
172 // (a) Can't call global from some contexts until we support CUDA's
173 // dynamic parallelism.
174 if (CalleeTarget == CFT_Global &&
Justin Lebar0254c462016-10-12 01:30:08 +0000175 (CallerTarget == CFT_Global || CallerTarget == CFT_Device))
Artem Belevich94a55e82015-09-22 17:22:59 +0000176 return CFP_Never;
177
Artem Belevich18609102016-02-12 18:29:18 +0000178 // (b) Calling HostDevice is OK for everyone.
179 if (CalleeTarget == CFT_HostDevice)
180 return CFP_HostDevice;
181
182 // (c) Best case scenarios
Artem Belevich94a55e82015-09-22 17:22:59 +0000183 if (CalleeTarget == CallerTarget ||
184 (CallerTarget == CFT_Host && CalleeTarget == CFT_Global) ||
185 (CallerTarget == CFT_Global && CalleeTarget == CFT_Device))
Artem Belevich18609102016-02-12 18:29:18 +0000186 return CFP_Native;
Artem Belevich94a55e82015-09-22 17:22:59 +0000187
188 // (d) HostDevice behavior depends on compilation mode.
189 if (CallerTarget == CFT_HostDevice) {
Artem Belevich18609102016-02-12 18:29:18 +0000190 // It's OK to call a compilation-mode matching function from an HD one.
191 if ((getLangOpts().CUDAIsDevice && CalleeTarget == CFT_Device) ||
192 (!getLangOpts().CUDAIsDevice &&
193 (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global)))
194 return CFP_SameSide;
195
Justin Lebar25c4a812016-03-29 16:24:16 +0000196 // Calls from HD to non-mode-matching functions (i.e., to host functions
197 // when compiling in device mode or to device functions when compiling in
198 // host mode) are allowed at the sema level, but eventually rejected if
199 // they're ever codegened. TODO: Reject said calls earlier.
200 return CFP_WrongSide;
Artem Belevich94a55e82015-09-22 17:22:59 +0000201 }
202
203 // (e) Calling across device/host boundary is not something you should do.
204 if ((CallerTarget == CFT_Host && CalleeTarget == CFT_Device) ||
205 (CallerTarget == CFT_Device && CalleeTarget == CFT_Host) ||
206 (CallerTarget == CFT_Global && CalleeTarget == CFT_Host))
Artem Belevich18609102016-02-12 18:29:18 +0000207 return CFP_Never;
Artem Belevich94a55e82015-09-22 17:22:59 +0000208
209 llvm_unreachable("All cases should've been handled by now.");
210}
211
Richard Smithf75dcbe2016-10-11 00:21:10 +0000212void Sema::EraseUnwantedCUDAMatches(
213 const FunctionDecl *Caller,
214 SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) {
Artem Belevich94a55e82015-09-22 17:22:59 +0000215 if (Matches.size() <= 1)
216 return;
217
Richard Smithf75dcbe2016-10-11 00:21:10 +0000218 using Pair = std::pair<DeclAccessPair, FunctionDecl*>;
219
Justin Lebare6a2cc12016-03-22 00:09:25 +0000220 // Gets the CUDA function preference for a call from Caller to Match.
Richard Smithf75dcbe2016-10-11 00:21:10 +0000221 auto GetCFP = [&](const Pair &Match) {
222 return IdentifyCUDAPreference(Caller, Match.second);
Justin Lebare6a2cc12016-03-22 00:09:25 +0000223 };
224
Artem Belevich94a55e82015-09-22 17:22:59 +0000225 // Find the best call preference among the functions in Matches.
Richard Smithf75dcbe2016-10-11 00:21:10 +0000226 CUDAFunctionPreference BestCFP = GetCFP(*std::max_element(
Justin Lebare6a2cc12016-03-22 00:09:25 +0000227 Matches.begin(), Matches.end(),
Richard Smithf75dcbe2016-10-11 00:21:10 +0000228 [&](const Pair &M1, const Pair &M2) { return GetCFP(M1) < GetCFP(M2); }));
Artem Belevich94a55e82015-09-22 17:22:59 +0000229
230 // Erase all functions with lower priority.
Justin Lebare71c08f2016-07-12 23:23:13 +0000231 Matches.erase(
Richard Smithf75dcbe2016-10-11 00:21:10 +0000232 llvm::remove_if(
233 Matches, [&](const Pair &Match) { return GetCFP(Match) < BestCFP; }),
Justin Lebare71c08f2016-07-12 23:23:13 +0000234 Matches.end());
Artem Belevich94a55e82015-09-22 17:22:59 +0000235}
236
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000237/// When an implicitly-declared special member has to invoke more than one
238/// base/field special member, conflicts may occur in the targets of these
239/// members. For example, if one base's member __host__ and another's is
240/// __device__, it's a conflict.
241/// This function figures out if the given targets \param Target1 and
242/// \param Target2 conflict, and if they do not it fills in
243/// \param ResolvedTarget with a target that resolves for both calls.
244/// \return true if there's a conflict, false otherwise.
245static bool
246resolveCalleeCUDATargetConflict(Sema::CUDAFunctionTarget Target1,
247 Sema::CUDAFunctionTarget Target2,
248 Sema::CUDAFunctionTarget *ResolvedTarget) {
Justin Lebarc66a1062016-01-20 00:26:57 +0000249 // Only free functions and static member functions may be global.
250 assert(Target1 != Sema::CFT_Global);
251 assert(Target2 != Sema::CFT_Global);
Eli Bendersky9a220fc2014-09-29 20:38:29 +0000252
253 if (Target1 == Sema::CFT_HostDevice) {
254 *ResolvedTarget = Target2;
255 } else if (Target2 == Sema::CFT_HostDevice) {
256 *ResolvedTarget = Target1;
257 } else if (Target1 != Target2) {
258 return true;
259 } else {
260 *ResolvedTarget = Target1;
261 }
262
263 return false;
264}
265
266bool Sema::inferCUDATargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
267 CXXSpecialMember CSM,
268 CXXMethodDecl *MemberDecl,
269 bool ConstRHS,
270 bool Diagnose) {
271 llvm::Optional<CUDAFunctionTarget> InferredTarget;
272
273 // We're going to invoke special member lookup; mark that these special
274 // members are called from this one, and not from its caller.
275 ContextRAII MethodContext(*this, MemberDecl);
276
277 // Look for special members in base classes that should be invoked from here.
278 // Infer the target of this member base on the ones it should call.
279 // Skip direct and indirect virtual bases for abstract classes.
280 llvm::SmallVector<const CXXBaseSpecifier *, 16> Bases;
281 for (const auto &B : ClassDecl->bases()) {
282 if (!B.isVirtual()) {
283 Bases.push_back(&B);
284 }
285 }
286
287 if (!ClassDecl->isAbstract()) {
288 for (const auto &VB : ClassDecl->vbases()) {
289 Bases.push_back(&VB);
290 }
291 }
292
293 for (const auto *B : Bases) {
294 const RecordType *BaseType = B->getType()->getAs<RecordType>();
295 if (!BaseType) {
296 continue;
297 }
298
299 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl());
300 Sema::SpecialMemberOverloadResult *SMOR =
301 LookupSpecialMember(BaseClassDecl, CSM,
302 /* ConstArg */ ConstRHS,
303 /* VolatileArg */ false,
304 /* RValueThis */ false,
305 /* ConstThis */ false,
306 /* VolatileThis */ false);
307
308 if (!SMOR || !SMOR->getMethod()) {
309 continue;
310 }
311
312 CUDAFunctionTarget BaseMethodTarget = IdentifyCUDATarget(SMOR->getMethod());
313 if (!InferredTarget.hasValue()) {
314 InferredTarget = BaseMethodTarget;
315 } else {
316 bool ResolutionError = resolveCalleeCUDATargetConflict(
317 InferredTarget.getValue(), BaseMethodTarget,
318 InferredTarget.getPointer());
319 if (ResolutionError) {
320 if (Diagnose) {
321 Diag(ClassDecl->getLocation(),
322 diag::note_implicit_member_target_infer_collision)
323 << (unsigned)CSM << InferredTarget.getValue() << BaseMethodTarget;
324 }
325 MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
326 return true;
327 }
328 }
329 }
330
331 // Same as for bases, but now for special members of fields.
332 for (const auto *F : ClassDecl->fields()) {
333 if (F->isInvalidDecl()) {
334 continue;
335 }
336
337 const RecordType *FieldType =
338 Context.getBaseElementType(F->getType())->getAs<RecordType>();
339 if (!FieldType) {
340 continue;
341 }
342
343 CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(FieldType->getDecl());
344 Sema::SpecialMemberOverloadResult *SMOR =
345 LookupSpecialMember(FieldRecDecl, CSM,
346 /* ConstArg */ ConstRHS && !F->isMutable(),
347 /* VolatileArg */ false,
348 /* RValueThis */ false,
349 /* ConstThis */ false,
350 /* VolatileThis */ false);
351
352 if (!SMOR || !SMOR->getMethod()) {
353 continue;
354 }
355
356 CUDAFunctionTarget FieldMethodTarget =
357 IdentifyCUDATarget(SMOR->getMethod());
358 if (!InferredTarget.hasValue()) {
359 InferredTarget = FieldMethodTarget;
360 } else {
361 bool ResolutionError = resolveCalleeCUDATargetConflict(
362 InferredTarget.getValue(), FieldMethodTarget,
363 InferredTarget.getPointer());
364 if (ResolutionError) {
365 if (Diagnose) {
366 Diag(ClassDecl->getLocation(),
367 diag::note_implicit_member_target_infer_collision)
368 << (unsigned)CSM << InferredTarget.getValue()
369 << FieldMethodTarget;
370 }
371 MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
372 return true;
373 }
374 }
375 }
376
377 if (InferredTarget.hasValue()) {
378 if (InferredTarget.getValue() == CFT_Device) {
379 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
380 } else if (InferredTarget.getValue() == CFT_Host) {
381 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
382 } else {
383 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
384 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
385 }
386 } else {
387 // If no target was inferred, mark this member as __host__ __device__;
388 // it's the least restrictive option that can be invoked from any target.
389 MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
390 MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
391 }
392
393 return false;
394}
Artem Belevich97c01c32016-02-02 22:29:48 +0000395
396bool Sema::isEmptyCudaConstructor(SourceLocation Loc, CXXConstructorDecl *CD) {
397 if (!CD->isDefined() && CD->isTemplateInstantiation())
398 InstantiateFunctionDefinition(Loc, CD->getFirstDecl());
399
400 // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered
401 // empty at a point in the translation unit, if it is either a
402 // trivial constructor
403 if (CD->isTrivial())
404 return true;
405
406 // ... or it satisfies all of the following conditions:
407 // The constructor function has been defined.
408 // The constructor function has no parameters,
409 // and the function body is an empty compound statement.
410 if (!(CD->hasTrivialBody() && CD->getNumParams() == 0))
411 return false;
412
413 // Its class has no virtual functions and no virtual base classes.
414 if (CD->getParent()->isDynamicClass())
415 return false;
416
417 // The only form of initializer allowed is an empty constructor.
Artem Belevich3650bbe2016-05-19 20:13:53 +0000418 // This will recursively check all base classes and member initializers
Artem Belevich97c01c32016-02-02 22:29:48 +0000419 if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) {
420 if (const CXXConstructExpr *CE =
421 dyn_cast<CXXConstructExpr>(CI->getInit()))
422 return isEmptyCudaConstructor(Loc, CE->getConstructor());
423 return false;
424 }))
425 return false;
426
427 return true;
428}
Justin Lebarba122ab2016-03-30 23:30:21 +0000429
Artem Belevich3650bbe2016-05-19 20:13:53 +0000430bool Sema::isEmptyCudaDestructor(SourceLocation Loc, CXXDestructorDecl *DD) {
431 // No destructor -> no problem.
432 if (!DD)
433 return true;
434
435 if (!DD->isDefined() && DD->isTemplateInstantiation())
436 InstantiateFunctionDefinition(Loc, DD->getFirstDecl());
437
438 // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered
439 // empty at a point in the translation unit, if it is either a
440 // trivial constructor
441 if (DD->isTrivial())
442 return true;
443
444 // ... or it satisfies all of the following conditions:
445 // The destructor function has been defined.
446 // and the function body is an empty compound statement.
447 if (!DD->hasTrivialBody())
448 return false;
449
450 const CXXRecordDecl *ClassDecl = DD->getParent();
451
452 // Its class has no virtual functions and no virtual base classes.
453 if (ClassDecl->isDynamicClass())
454 return false;
455
456 // Only empty destructors are allowed. This will recursively check
457 // destructors for all base classes...
458 if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) {
459 if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())
460 return isEmptyCudaDestructor(Loc, RD->getDestructor());
461 return true;
462 }))
463 return false;
464
465 // ... and member fields.
466 if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) {
467 if (CXXRecordDecl *RD = Field->getType()
468 ->getBaseElementTypeUnsafe()
469 ->getAsCXXRecordDecl())
470 return isEmptyCudaDestructor(Loc, RD->getDestructor());
471 return true;
472 }))
473 return false;
474
475 return true;
476}
477
Justin Lebarba122ab2016-03-30 23:30:21 +0000478// With -fcuda-host-device-constexpr, an unattributed constexpr function is
479// treated as implicitly __host__ __device__, unless:
480// * it is a variadic function (device-side variadic functions are not
481// allowed), or
482// * a __device__ function with this signature was already declared, in which
483// case in which case we output an error, unless the __device__ decl is in a
484// system header, in which case we leave the constexpr function unattributed.
Justin Lebar67a78a62016-10-08 22:15:58 +0000485//
486// In addition, all function decls are treated as __host__ __device__ when
487// ForceCUDAHostDeviceDepth > 0 (corresponding to code within a
488// #pragma clang force_cuda_host_device_begin/end
489// pair).
Artem Belevich9fb40e32016-10-21 17:15:46 +0000490void Sema::maybeAddCUDAHostDeviceAttrs(FunctionDecl *NewD,
Justin Lebarba122ab2016-03-30 23:30:21 +0000491 const LookupResult &Previous) {
Justin Lebar9d4ed262016-09-30 23:57:38 +0000492 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar67a78a62016-10-08 22:15:58 +0000493
494 if (ForceCUDAHostDeviceDepth > 0) {
495 if (!NewD->hasAttr<CUDAHostAttr>())
496 NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
497 if (!NewD->hasAttr<CUDADeviceAttr>())
498 NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
499 return;
500 }
501
Justin Lebarba122ab2016-03-30 23:30:21 +0000502 if (!getLangOpts().CUDAHostDeviceConstexpr || !NewD->isConstexpr() ||
503 NewD->isVariadic() || NewD->hasAttr<CUDAHostAttr>() ||
504 NewD->hasAttr<CUDADeviceAttr>() || NewD->hasAttr<CUDAGlobalAttr>())
505 return;
506
507 // Is D a __device__ function with the same signature as NewD, ignoring CUDA
508 // attributes?
509 auto IsMatchingDeviceFn = [&](NamedDecl *D) {
510 if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D))
511 D = Using->getTargetDecl();
512 FunctionDecl *OldD = D->getAsFunction();
513 return OldD && OldD->hasAttr<CUDADeviceAttr>() &&
514 !OldD->hasAttr<CUDAHostAttr>() &&
515 !IsOverload(NewD, OldD, /* UseMemberUsingDeclRules = */ false,
516 /* ConsiderCudaAttrs = */ false);
517 };
518 auto It = llvm::find_if(Previous, IsMatchingDeviceFn);
519 if (It != Previous.end()) {
520 // We found a __device__ function with the same name and signature as NewD
521 // (ignoring CUDA attrs). This is an error unless that function is defined
522 // in a system header, in which case we simply return without making NewD
523 // host+device.
524 NamedDecl *Match = *It;
525 if (!getSourceManager().isInSystemHeader(Match->getLocation())) {
526 Diag(NewD->getLocation(),
527 diag::err_cuda_unattributed_constexpr_cannot_overload_device)
528 << NewD->getName();
529 Diag(Match->getLocation(),
530 diag::note_cuda_conflicting_device_function_declared_here);
531 }
532 return;
533 }
534
535 NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
536 NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
537}
Justin Lebar18e2d822016-08-15 23:00:49 +0000538
Justin Lebar23d95422016-10-13 20:52:12 +0000539// In CUDA, there are some constructs which may appear in semantically-valid
540// code, but trigger errors if we ever generate code for the function in which
541// they appear. Essentially every construct you're not allowed to use on the
542// device falls into this category, because you are allowed to use these
543// constructs in a __host__ __device__ function, but only if that function is
544// never codegen'ed on the device.
545//
546// To handle semantic checking for these constructs, we keep track of the set of
547// functions we know will be emitted, either because we could tell a priori that
548// they would be emitted, or because they were transitively called by a
549// known-emitted function.
550//
551// We also keep a partial call graph of which not-known-emitted functions call
552// which other not-known-emitted functions.
553//
554// When we see something which is illegal if the current function is emitted
555// (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or
556// CheckCUDACall), we first check if the current function is known-emitted. If
557// so, we immediately output the diagnostic.
558//
559// Otherwise, we "defer" the diagnostic. It sits in Sema::CUDADeferredDiags
560// until we discover that the function is known-emitted, at which point we take
561// it out of this map and emit the diagnostic.
562
Justin Lebar6c86e912016-10-19 21:15:01 +0000563Sema::CUDADiagBuilder::CUDADiagBuilder(Kind K, SourceLocation Loc,
564 unsigned DiagID, FunctionDecl *Fn,
565 Sema &S)
566 : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn),
567 ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) {
568 switch (K) {
569 case K_Nop:
570 break;
571 case K_Immediate:
572 case K_ImmediateWithCallStack:
573 ImmediateDiag.emplace(S.Diag(Loc, DiagID));
574 break;
575 case K_Deferred:
576 assert(Fn && "Must have a function to attach the deferred diag to.");
577 PartialDiag.emplace(S.PDiag(DiagID));
578 break;
579 }
580}
581
582// Print notes showing how we can reach FD starting from an a priori
583// known-callable function.
584static void EmitCallStackNotes(Sema &S, FunctionDecl *FD) {
585 auto FnIt = S.CUDAKnownEmittedFns.find(FD);
586 while (FnIt != S.CUDAKnownEmittedFns.end()) {
587 DiagnosticBuilder Builder(
588 S.Diags.Report(FnIt->second.Loc, diag::note_called_by));
589 Builder << FnIt->second.FD;
590 Builder.setForceEmit();
591
592 FnIt = S.CUDAKnownEmittedFns.find(FnIt->second.FD);
593 }
594}
595
596Sema::CUDADiagBuilder::~CUDADiagBuilder() {
597 if (ImmediateDiag) {
598 // Emit our diagnostic and, if it was a warning or error, output a callstack
599 // if Fn isn't a priori known-emitted.
600 bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel(
601 DiagID, Loc) >= DiagnosticsEngine::Warning;
602 ImmediateDiag.reset(); // Emit the immediate diag.
603 if (IsWarningOrError && ShowCallStack)
604 EmitCallStackNotes(S, Fn);
605 } else if (PartialDiag) {
606 assert(ShowCallStack && "Must always show call stack for deferred diags.");
607 S.CUDADeferredDiags[Fn].push_back({Loc, std::move(*PartialDiag)});
608 }
609}
610
Justin Lebar23d95422016-10-13 20:52:12 +0000611// Do we know that we will eventually codegen the given function?
612static bool IsKnownEmitted(Sema &S, FunctionDecl *FD) {
613 // Templates are emitted when they're instantiated.
614 if (FD->isDependentContext())
615 return false;
616
617 // When compiling for device, host functions are never emitted. Similarly,
618 // when compiling for host, device and global functions are never emitted.
619 // (Technically, we do emit a host-side stub for global functions, but this
620 // doesn't count for our purposes here.)
621 Sema::CUDAFunctionTarget T = S.IdentifyCUDATarget(FD);
622 if (S.getLangOpts().CUDAIsDevice && T == Sema::CFT_Host)
623 return false;
624 if (!S.getLangOpts().CUDAIsDevice &&
625 (T == Sema::CFT_Device || T == Sema::CFT_Global))
626 return false;
627
Justin Lebar2d56c262016-11-08 23:45:51 +0000628 // Check whether this function is externally visible -- if so, it's
629 // known-emitted.
630 //
631 // We have to check the GVA linkage of the function's *definition* -- if we
632 // only have a declaration, we don't know whether or not the function will be
633 // emitted, because (say) the definition could include "inline".
634 FunctionDecl *Def = FD->getDefinition();
635
636 // We may currently be parsing the body of FD, in which case
637 // FD->getDefinition() will be null, but we still want to treat FD as though
638 // it's a definition.
639 if (!Def && FD->willHaveBody())
640 Def = FD;
641
642 if (Def &&
643 !isDiscardableGVALinkage(S.getASTContext().GetGVALinkageForFunction(Def)))
Justin Lebar23d95422016-10-13 20:52:12 +0000644 return true;
645
646 // Otherwise, the function is known-emitted if it's in our set of
647 // known-emitted functions.
648 return S.CUDAKnownEmittedFns.count(FD) > 0;
649}
650
Justin Lebar179bdce2016-10-13 18:45:08 +0000651Sema::CUDADiagBuilder Sema::CUDADiagIfDeviceCode(SourceLocation Loc,
652 unsigned DiagID) {
653 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar23d95422016-10-13 20:52:12 +0000654 CUDADiagBuilder::Kind DiagKind = [&] {
655 switch (CurrentCUDATarget()) {
656 case CFT_Global:
657 case CFT_Device:
658 return CUDADiagBuilder::K_Immediate;
659 case CFT_HostDevice:
660 // An HD function counts as host code if we're compiling for host, and
661 // device code if we're compiling for device. Defer any errors in device
662 // mode until the function is known-emitted.
663 if (getLangOpts().CUDAIsDevice) {
664 return IsKnownEmitted(*this, dyn_cast<FunctionDecl>(CurContext))
Justin Lebar6c86e912016-10-19 21:15:01 +0000665 ? CUDADiagBuilder::K_ImmediateWithCallStack
Justin Lebar23d95422016-10-13 20:52:12 +0000666 : CUDADiagBuilder::K_Deferred;
667 }
668 return CUDADiagBuilder::K_Nop;
669
670 default:
671 return CUDADiagBuilder::K_Nop;
672 }
673 }();
Justin Lebar179bdce2016-10-13 18:45:08 +0000674 return CUDADiagBuilder(DiagKind, Loc, DiagID,
675 dyn_cast<FunctionDecl>(CurContext), *this);
676}
677
678Sema::CUDADiagBuilder Sema::CUDADiagIfHostCode(SourceLocation Loc,
679 unsigned DiagID) {
680 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar23d95422016-10-13 20:52:12 +0000681 CUDADiagBuilder::Kind DiagKind = [&] {
682 switch (CurrentCUDATarget()) {
683 case CFT_Host:
684 return CUDADiagBuilder::K_Immediate;
685 case CFT_HostDevice:
686 // An HD function counts as host code if we're compiling for host, and
687 // device code if we're compiling for device. Defer any errors in device
688 // mode until the function is known-emitted.
689 if (getLangOpts().CUDAIsDevice)
690 return CUDADiagBuilder::K_Nop;
691
692 return IsKnownEmitted(*this, dyn_cast<FunctionDecl>(CurContext))
Justin Lebar6c86e912016-10-19 21:15:01 +0000693 ? CUDADiagBuilder::K_ImmediateWithCallStack
Justin Lebar23d95422016-10-13 20:52:12 +0000694 : CUDADiagBuilder::K_Deferred;
695 default:
696 return CUDADiagBuilder::K_Nop;
697 }
698 }();
Justin Lebar179bdce2016-10-13 18:45:08 +0000699 return CUDADiagBuilder(DiagKind, Loc, DiagID,
700 dyn_cast<FunctionDecl>(CurContext), *this);
701}
702
Justin Lebar23d95422016-10-13 20:52:12 +0000703// Emit any deferred diagnostics for FD and erase them from the map in which
704// they're stored.
705static void EmitDeferredDiags(Sema &S, FunctionDecl *FD) {
706 auto It = S.CUDADeferredDiags.find(FD);
707 if (It == S.CUDADeferredDiags.end())
708 return;
Justin Lebar6c86e912016-10-19 21:15:01 +0000709 bool HasWarningOrError = false;
Justin Lebar23d95422016-10-13 20:52:12 +0000710 for (PartialDiagnosticAt &PDAt : It->second) {
711 const SourceLocation &Loc = PDAt.first;
712 const PartialDiagnostic &PD = PDAt.second;
Justin Lebar6c86e912016-10-19 21:15:01 +0000713 HasWarningOrError |= S.getDiagnostics().getDiagnosticLevel(
714 PD.getDiagID(), Loc) >= DiagnosticsEngine::Warning;
Justin Lebar23d95422016-10-13 20:52:12 +0000715 DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID()));
716 Builder.setForceEmit();
717 PD.Emit(Builder);
718 }
719 S.CUDADeferredDiags.erase(It);
Justin Lebar6c86e912016-10-19 21:15:01 +0000720
721 // FIXME: Should this be called after every warning/error emitted in the loop
722 // above, instead of just once per function? That would be consistent with
723 // how we handle immediate errors, but it also seems like a bit much.
724 if (HasWarningOrError)
725 EmitCallStackNotes(S, FD);
Justin Lebar23d95422016-10-13 20:52:12 +0000726}
727
728// Indicate that this function (and thus everything it transtively calls) will
729// be codegen'ed, and emit any deferred diagnostics on this function and its
730// (transitive) callees.
Justin Lebar6c86e912016-10-19 21:15:01 +0000731static void MarkKnownEmitted(Sema &S, FunctionDecl *OrigCaller,
732 FunctionDecl *OrigCallee, SourceLocation OrigLoc) {
Justin Lebar23d95422016-10-13 20:52:12 +0000733 // Nothing to do if we already know that FD is emitted.
Justin Lebar6c86e912016-10-19 21:15:01 +0000734 if (IsKnownEmitted(S, OrigCallee)) {
735 assert(!S.CUDACallGraph.count(OrigCallee));
Justin Lebar23d95422016-10-13 20:52:12 +0000736 return;
737 }
738
Justin Lebar6c86e912016-10-19 21:15:01 +0000739 // We've just discovered that OrigCallee is known-emitted. Walk our call
740 // graph to see what else we can now discover also must be emitted.
741
742 struct CallInfo {
743 FunctionDecl *Caller;
744 FunctionDecl *Callee;
745 SourceLocation Loc;
746 };
747 llvm::SmallVector<CallInfo, 4> Worklist = {{OrigCaller, OrigCallee, OrigLoc}};
748 llvm::SmallSet<CanonicalDeclPtr<FunctionDecl>, 4> Seen;
749 Seen.insert(OrigCallee);
Justin Lebar23d95422016-10-13 20:52:12 +0000750 while (!Worklist.empty()) {
Justin Lebar6c86e912016-10-19 21:15:01 +0000751 CallInfo C = Worklist.pop_back_val();
752 assert(!IsKnownEmitted(S, C.Callee) &&
Justin Lebar23d95422016-10-13 20:52:12 +0000753 "Worklist should not contain known-emitted functions.");
Justin Lebar6c86e912016-10-19 21:15:01 +0000754 S.CUDAKnownEmittedFns[C.Callee] = {C.Caller, C.Loc};
755 EmitDeferredDiags(S, C.Callee);
Justin Lebar23d95422016-10-13 20:52:12 +0000756
Justin Lebard692dfb2016-10-17 02:25:55 +0000757 // If this is a template instantiation, explore its callgraph as well:
758 // Non-dependent calls are part of the template's callgraph, while dependent
759 // calls are part of to the instantiation's call graph.
Justin Lebar6c86e912016-10-19 21:15:01 +0000760 if (auto *Templ = C.Callee->getPrimaryTemplate()) {
Justin Lebard692dfb2016-10-17 02:25:55 +0000761 FunctionDecl *TemplFD = Templ->getAsFunction();
762 if (!Seen.count(TemplFD) && !S.CUDAKnownEmittedFns.count(TemplFD)) {
763 Seen.insert(TemplFD);
Justin Lebar6c86e912016-10-19 21:15:01 +0000764 Worklist.push_back(
765 {/* Caller = */ C.Caller, /* Callee = */ TemplFD, C.Loc});
Justin Lebard692dfb2016-10-17 02:25:55 +0000766 }
767 }
Justin Lebar23d95422016-10-13 20:52:12 +0000768
Justin Lebar6c86e912016-10-19 21:15:01 +0000769 // Add all functions called by Callee to our worklist.
770 auto CGIt = S.CUDACallGraph.find(C.Callee);
Justin Lebar23d95422016-10-13 20:52:12 +0000771 if (CGIt == S.CUDACallGraph.end())
772 continue;
773
Justin Lebar6c86e912016-10-19 21:15:01 +0000774 for (std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation> FDLoc :
775 CGIt->second) {
776 FunctionDecl *NewCallee = FDLoc.first;
777 SourceLocation CallLoc = FDLoc.second;
778 if (Seen.count(NewCallee) || IsKnownEmitted(S, NewCallee))
Justin Lebar23d95422016-10-13 20:52:12 +0000779 continue;
Justin Lebar6c86e912016-10-19 21:15:01 +0000780 Seen.insert(NewCallee);
781 Worklist.push_back(
782 {/* Caller = */ C.Callee, /* Callee = */ NewCallee, CallLoc});
Justin Lebar23d95422016-10-13 20:52:12 +0000783 }
784
Justin Lebar6c86e912016-10-19 21:15:01 +0000785 // C.Callee is now known-emitted, so we no longer need to maintain its list
786 // of callees in CUDACallGraph.
Justin Lebar23d95422016-10-13 20:52:12 +0000787 S.CUDACallGraph.erase(CGIt);
788 }
789}
790
Justin Lebar18e2d822016-08-15 23:00:49 +0000791bool Sema::CheckCUDACall(SourceLocation Loc, FunctionDecl *Callee) {
Justin Lebar9d4ed262016-09-30 23:57:38 +0000792 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar18e2d822016-08-15 23:00:49 +0000793 assert(Callee && "Callee may not be null.");
Justin Lebar23d95422016-10-13 20:52:12 +0000794 // FIXME: Is bailing out early correct here? Should we instead assume that
795 // the caller is a global initializer?
Justin Lebar18e2d822016-08-15 23:00:49 +0000796 FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext);
797 if (!Caller)
798 return true;
799
Justin Lebard692dfb2016-10-17 02:25:55 +0000800 // If the caller is known-emitted, mark the callee as known-emitted.
801 // Otherwise, mark the call in our call graph so we can traverse it later.
Justin Lebar23d95422016-10-13 20:52:12 +0000802 bool CallerKnownEmitted = IsKnownEmitted(*this, Caller);
803 if (CallerKnownEmitted)
Justin Lebar6c86e912016-10-19 21:15:01 +0000804 MarkKnownEmitted(*this, Caller, Callee, Loc);
Justin Lebard692dfb2016-10-17 02:25:55 +0000805 else {
806 // If we have
807 // host fn calls kernel fn calls host+device,
808 // the HD function does not get instantiated on the host. We model this by
809 // omitting at the call to the kernel from the callgraph. This ensures
810 // that, when compiling for host, only HD functions actually called from the
811 // host get marked as known-emitted.
812 if (getLangOpts().CUDAIsDevice || IdentifyCUDATarget(Callee) != CFT_Global)
Justin Lebar6c86e912016-10-19 21:15:01 +0000813 CUDACallGraph[Caller].insert({Callee, Loc});
Justin Lebard692dfb2016-10-17 02:25:55 +0000814 }
Justin Lebar23d95422016-10-13 20:52:12 +0000815
816 CUDADiagBuilder::Kind DiagKind = [&] {
817 switch (IdentifyCUDAPreference(Caller, Callee)) {
818 case CFP_Never:
819 return CUDADiagBuilder::K_Immediate;
820 case CFP_WrongSide:
821 assert(Caller && "WrongSide calls require a non-null caller");
822 // If we know the caller will be emitted, we know this wrong-side call
823 // will be emitted, so it's an immediate error. Otherwise, defer the
824 // error until we know the caller is emitted.
Justin Lebar6c86e912016-10-19 21:15:01 +0000825 return CallerKnownEmitted ? CUDADiagBuilder::K_ImmediateWithCallStack
Justin Lebar23d95422016-10-13 20:52:12 +0000826 : CUDADiagBuilder::K_Deferred;
827 default:
828 return CUDADiagBuilder::K_Nop;
829 }
830 }();
Justin Lebar9fdb46e2016-10-08 01:07:11 +0000831
Justin Lebar9730ae92016-10-19 21:03:38 +0000832 if (DiagKind == CUDADiagBuilder::K_Nop)
833 return true;
834
Justin Lebar179bdce2016-10-13 18:45:08 +0000835 // Avoid emitting this error twice for the same location. Using a hashtable
836 // like this is unfortunate, but because we must continue parsing as normal
837 // after encountering a deferred error, it's otherwise very tricky for us to
838 // ensure that we only emit this deferred error once.
Justin Lebar6f727372016-10-21 20:08:52 +0000839 if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second)
Justin Lebar18e2d822016-08-15 23:00:49 +0000840 return true;
Justin Lebar2a8db342016-09-28 22:45:54 +0000841
Justin Lebar9730ae92016-10-19 21:03:38 +0000842 CUDADiagBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller, *this)
Justin Lebar179bdce2016-10-13 18:45:08 +0000843 << IdentifyCUDATarget(Callee) << Callee << IdentifyCUDATarget(Caller);
844 CUDADiagBuilder(DiagKind, Callee->getLocation(), diag::note_previous_decl,
845 Caller, *this)
846 << Callee;
Justin Lebar6c86e912016-10-19 21:15:01 +0000847 return DiagKind != CUDADiagBuilder::K_Immediate &&
848 DiagKind != CUDADiagBuilder::K_ImmediateWithCallStack;
Justin Lebarb17840d2016-09-28 22:45:58 +0000849}
Justin Lebar7ca116c2016-09-30 17:14:53 +0000850
851void Sema::CUDASetLambdaAttrs(CXXMethodDecl *Method) {
Justin Lebar9d4ed262016-09-30 23:57:38 +0000852 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
Justin Lebar7ca116c2016-09-30 17:14:53 +0000853 if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>())
854 return;
855 FunctionDecl *CurFn = dyn_cast<FunctionDecl>(CurContext);
856 if (!CurFn)
857 return;
858 CUDAFunctionTarget Target = IdentifyCUDATarget(CurFn);
859 if (Target == CFT_Global || Target == CFT_Device) {
860 Method->addAttr(CUDADeviceAttr::CreateImplicit(Context));
861 } else if (Target == CFT_HostDevice) {
862 Method->addAttr(CUDADeviceAttr::CreateImplicit(Context));
863 Method->addAttr(CUDAHostAttr::CreateImplicit(Context));
864 }
Justin Lebar7ca116c2016-09-30 17:14:53 +0000865}
Artem Belevich13e9b4d2016-12-07 19:27:16 +0000866
867void Sema::checkCUDATargetOverload(FunctionDecl *NewFD,
Artem Belevich64135c32016-12-08 19:38:13 +0000868 const LookupResult &Previous) {
Artem Belevich13e9b4d2016-12-07 19:27:16 +0000869 assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
870 CUDAFunctionTarget NewTarget = IdentifyCUDATarget(NewFD);
871 for (NamedDecl *OldND : Previous) {
872 FunctionDecl *OldFD = OldND->getAsFunction();
873 if (!OldFD)
874 continue;
875
876 CUDAFunctionTarget OldTarget = IdentifyCUDATarget(OldFD);
877 // Don't allow HD and global functions to overload other functions with the
878 // same signature. We allow overloading based on CUDA attributes so that
879 // functions can have different implementations on the host and device, but
880 // HD/global functions "exist" in some sense on both the host and device, so
881 // should have the same implementation on both sides.
882 if (NewTarget != OldTarget &&
883 ((NewTarget == CFT_HostDevice) || (OldTarget == CFT_HostDevice) ||
884 (NewTarget == CFT_Global) || (OldTarget == CFT_Global)) &&
885 !IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false,
886 /* ConsiderCudaAttrs = */ false)) {
887 Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)
888 << NewTarget << NewFD->getDeclName() << OldTarget << OldFD;
889 Diag(OldFD->getLocation(), diag::note_previous_declaration);
890 NewFD->setInvalidDecl();
891 break;
892 }
893 }
894}
Artem Belevich64135c32016-12-08 19:38:13 +0000895
896template <typename AttrTy>
897static void copyAttrIfPresent(Sema &S, FunctionDecl *FD,
898 const FunctionDecl &TemplateFD) {
899 if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
900 AttrTy *Clone = Attribute->clone(S.Context);
901 Clone->setInherited(true);
902 FD->addAttr(Clone);
903 }
904}
905
906void Sema::inheritCUDATargetAttrs(FunctionDecl *FD,
907 const FunctionTemplateDecl &TD) {
908 const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();
909 copyAttrIfPresent<CUDAGlobalAttr>(*this, FD, TemplateFD);
910 copyAttrIfPresent<CUDAHostAttr>(*this, FD, TemplateFD);
911 copyAttrIfPresent<CUDADeviceAttr>(*this, FD, TemplateFD);
912}