Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaCUDA.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaCUDA.cpp - Semantic Analysis for CUDA constructs -------------===//
2
//
3
// 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
6
//
7
//===----------------------------------------------------------------------===//
8
/// \file
9
/// This file implements semantic analysis for CUDA constructs.
10
///
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/ASTContext.h"
14
#include "clang/AST/Decl.h"
15
#include "clang/AST/ExprCXX.h"
16
#include "clang/Basic/Cuda.h"
17
#include "clang/Basic/TargetInfo.h"
18
#include "clang/Lex/Preprocessor.h"
19
#include "clang/Sema/Lookup.h"
20
#include "clang/Sema/ScopeInfo.h"
21
#include "clang/Sema/Sema.h"
22
#include "clang/Sema/SemaDiagnostic.h"
23
#include "clang/Sema/SemaInternal.h"
24
#include "clang/Sema/Template.h"
25
#include "llvm/ADT/Optional.h"
26
#include "llvm/ADT/SmallVector.h"
27
using namespace clang;
28
29
163
template <typename AttrT> static bool hasExplicitAttr(const VarDecl *D) {
30
163
  if (!D)
31
0
    return false;
32
163
  if (auto *A = D->getAttr<AttrT>())
33
126
    return !A->isImplicit();
34
37
  return false;
35
163
}
36
37
15
void Sema::PushForceCUDAHostDevice() {
38
15
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
39
0
  ForceCUDAHostDeviceDepth++;
40
15
}
41
42
13
bool Sema::PopForceCUDAHostDevice() {
43
13
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
44
13
  if (ForceCUDAHostDeviceDepth == 0)
45
0
    return false;
46
13
  ForceCUDAHostDeviceDepth--;
47
13
  return true;
48
13
}
49
50
ExprResult Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc,
51
                                         MultiExprArg ExecConfig,
52
157
                                         SourceLocation GGGLoc) {
53
157
  FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl();
54
157
  if (!ConfigDecl)
55
0
    return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use)
56
0
                     << getCudaConfigureFuncName());
57
157
  QualType ConfigQTy = ConfigDecl->getType();
58
59
157
  DeclRefExpr *ConfigDR = new (Context)
60
157
      DeclRefExpr(Context, ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc);
61
157
  MarkFunctionReferenced(LLLLoc, ConfigDecl);
62
63
157
  return BuildCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr,
64
157
                       /*IsExecConfig=*/true);
65
157
}
66
67
Sema::CUDAFunctionTarget
68
30
Sema::IdentifyCUDATarget(const ParsedAttributesView &Attrs) {
69
30
  bool HasHostAttr = false;
70
30
  bool HasDeviceAttr = false;
71
30
  bool HasGlobalAttr = false;
72
30
  bool HasInvalidTargetAttr = false;
73
31
  for (const ParsedAttr &AL : Attrs) {
74
31
    switch (AL.getKind()) {
75
11
    case ParsedAttr::AT_CUDAGlobal:
76
11
      HasGlobalAttr = true;
77
11
      break;
78
10
    case ParsedAttr::AT_CUDAHost:
79
10
      HasHostAttr = true;
80
10
      break;
81
10
    case ParsedAttr::AT_CUDADevice:
82
10
      HasDeviceAttr = true;
83
10
      break;
84
0
    case ParsedAttr::AT_CUDAInvalidTarget:
85
0
      HasInvalidTargetAttr = true;
86
0
      break;
87
0
    default:
88
0
      break;
89
31
    }
90
31
  }
91
92
30
  if (HasInvalidTargetAttr)
93
0
    return CFT_InvalidTarget;
94
95
30
  if (HasGlobalAttr)
96
11
    return CFT_Global;
97
98
19
  if (HasHostAttr && 
HasDeviceAttr10
)
99
6
    return CFT_HostDevice;
100
101
13
  if (HasDeviceAttr)
102
4
    return CFT_Device;
103
104
9
  return CFT_Host;
105
13
}
106
107
template <typename A>
108
297k
static bool hasAttr(const FunctionDecl *D, bool IgnoreImplicitAttr) {
109
356k
  return 
D->hasAttrs()297k
&&
llvm::any_of(D->getAttrs(), [&](Attr *Attribute) 249k
{
110
356k
           return isa<A>(Attribute) &&
111
356k
                  
!(155k
IgnoreImplicitAttr155k
&&
Attribute->isImplicit()76
);
112
356k
         });
SemaCUDA.cpp:bool hasAttr<clang::CUDADeviceAttr>(clang::FunctionDecl const*, bool)::'lambda'(clang::Attr*)::operator()(clang::Attr*) const
Line
Count
Source
109
173k
  return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
110
173k
           return isa<A>(Attribute) &&
111
173k
                  
!(116k
IgnoreImplicitAttr116k
&&
Attribute->isImplicit()38
);
112
173k
         });
SemaCUDA.cpp:bool hasAttr<clang::CUDAHostAttr>(clang::FunctionDecl const*, bool)::'lambda'(clang::Attr*)::operator()(clang::Attr*) const
Line
Count
Source
109
182k
  return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
110
182k
           return isa<A>(Attribute) &&
111
182k
                  
!(39.2k
IgnoreImplicitAttr39.2k
&&
Attribute->isImplicit()38
);
112
182k
         });
113
297k
}
SemaCUDA.cpp:bool hasAttr<clang::CUDADeviceAttr>(clang::FunctionDecl const*, bool)
Line
Count
Source
108
148k
static bool hasAttr(const FunctionDecl *D, bool IgnoreImplicitAttr) {
109
148k
  return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
110
124k
           return isa<A>(Attribute) &&
111
124k
                  !(IgnoreImplicitAttr && Attribute->isImplicit());
112
124k
         });
113
148k
}
SemaCUDA.cpp:bool hasAttr<clang::CUDAHostAttr>(clang::FunctionDecl const*, bool)
Line
Count
Source
108
148k
static bool hasAttr(const FunctionDecl *D, bool IgnoreImplicitAttr) {
109
148k
  return D->hasAttrs() && llvm::any_of(D->getAttrs(), [&](Attr *Attribute) {
110
124k
           return isa<A>(Attribute) &&
111
124k
                  !(IgnoreImplicitAttr && Attribute->isImplicit());
112
124k
         });
113
148k
}
114
115
/// IdentifyCUDATarget - Determine the CUDA compilation target for this function
116
Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D,
117
153k
                                                  bool IgnoreImplicitHDAttr) {
118
  // Code that lives outside a function is run on the host.
119
153k
  if (D == nullptr)
120
1.14k
    return CFT_Host;
121
122
152k
  if (D->hasAttr<CUDAInvalidTargetAttr>())
123
39
    return CFT_InvalidTarget;
124
125
152k
  if (D->hasAttr<CUDAGlobalAttr>())
126
3.91k
    return CFT_Global;
127
128
148k
  if (hasAttr<CUDADeviceAttr>(D, IgnoreImplicitHDAttr)) {
129
116k
    if (hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr))
130
31.3k
      return CFT_HostDevice;
131
84.8k
    return CFT_Device;
132
116k
  } else 
if (32.3k
hasAttr<CUDAHostAttr>(D, IgnoreImplicitHDAttr)32.3k
) {
133
7.86k
    return CFT_Host;
134
24.5k
  } else if ((D->isImplicit() || 
!D->isUserProvided()23.8k
) &&
135
24.5k
             
!IgnoreImplicitHDAttr654
) {
136
    // Some implicit declarations (like intrinsic functions) are not marked.
137
    // Set the most lenient target on them for maximal flexibility.
138
654
    return CFT_HostDevice;
139
654
  }
140
141
23.8k
  return CFT_Host;
142
148k
}
143
144
/// IdentifyTarget - Determine the CUDA compilation target for this variable.
145
1.23k
Sema::CUDAVariableTarget Sema::IdentifyCUDATarget(const VarDecl *Var) {
146
1.23k
  if (Var->hasAttr<HIPManagedAttr>())
147
64
    return CVT_Unified;
148
1.17k
  if (Var->isConstexpr() && 
!hasExplicitAttr<CUDAConstantAttr>(Var)84
)
149
76
    return CVT_Both;
150
1.09k
  if (Var->getType().isConstQualified() && 
Var->hasAttr<CUDAConstantAttr>()172
&&
151
1.09k
      
!hasExplicitAttr<CUDAConstantAttr>(Var)79
)
152
67
    return CVT_Both;
153
1.02k
  if (Var->hasAttr<CUDADeviceAttr>() || 
Var->hasAttr<CUDAConstantAttr>()606
||
154
1.02k
      
Var->hasAttr<CUDASharedAttr>()491
||
155
1.02k
      
Var->getType()->isCUDADeviceBuiltinSurfaceType()193
||
156
1.02k
      
Var->getType()->isCUDADeviceBuiltinTextureType()191
)
157
840
    return CVT_Device;
158
  // Function-scope static variable without explicit device or constant
159
  // attribute are emitted
160
  //  - on both sides in host device functions
161
  //  - on device side in device or global functions
162
187
  if (auto *FD = dyn_cast<FunctionDecl>(Var->getDeclContext())) {
163
46
    switch (IdentifyCUDATarget(FD)) {
164
14
    case CFT_HostDevice:
165
14
      return CVT_Both;
166
18
    case CFT_Device:
167
30
    case CFT_Global:
168
30
      return CVT_Device;
169
2
    default:
170
2
      return CVT_Host;
171
46
    }
172
46
  }
173
141
  return CVT_Host;
174
187
}
175
176
// * CUDA Call preference table
177
//
178
// F - from,
179
// T - to
180
// Ph - preference in host mode
181
// Pd - preference in device mode
182
// H  - handled in (x)
183
// Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never.
184
//
185
// | F  | T  | Ph  | Pd  |  H  |
186
// |----+----+-----+-----+-----+
187
// | d  | d  | N   | N   | (c) |
188
// | d  | g  | --  | --  | (a) |
189
// | d  | h  | --  | --  | (e) |
190
// | d  | hd | HD  | HD  | (b) |
191
// | g  | d  | N   | N   | (c) |
192
// | g  | g  | --  | --  | (a) |
193
// | g  | h  | --  | --  | (e) |
194
// | g  | hd | HD  | HD  | (b) |
195
// | h  | d  | --  | --  | (e) |
196
// | h  | g  | N   | N   | (c) |
197
// | h  | h  | N   | N   | (c) |
198
// | h  | hd | HD  | HD  | (b) |
199
// | hd | d  | WS  | SS  | (d) |
200
// | hd | g  | SS  | --  |(d/a)|
201
// | hd | h  | SS  | WS  | (d) |
202
// | hd | hd | HD  | HD  | (b) |
203
204
Sema::CUDAFunctionPreference
205
Sema::IdentifyCUDAPreference(const FunctionDecl *Caller,
206
45.8k
                             const FunctionDecl *Callee) {
207
45.8k
  assert(Callee && "Callee must be valid.");
208
0
  CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller);
209
45.8k
  CUDAFunctionTarget CalleeTarget = IdentifyCUDATarget(Callee);
210
211
  // If one of the targets is invalid, the check always fails, no matter what
212
  // the other target is.
213
45.8k
  if (CallerTarget == CFT_InvalidTarget || CalleeTarget == CFT_InvalidTarget)
214
7
    return CFP_Never;
215
216
  // (a) Can't call global from some contexts until we support CUDA's
217
  // dynamic parallelism.
218
45.8k
  if (CalleeTarget == CFT_Global &&
219
45.8k
      
(624
CallerTarget == CFT_Global624
||
CallerTarget == CFT_Device607
))
220
39
    return CFP_Never;
221
222
  // (b) Calling HostDevice is OK for everyone.
223
45.8k
  if (CalleeTarget == CFT_HostDevice)
224
16.1k
    return CFP_HostDevice;
225
226
  // (c) Best case scenarios
227
29.6k
  if (CalleeTarget == CallerTarget ||
228
29.6k
      
(5.33k
CallerTarget == CFT_Host5.33k
&&
CalleeTarget == CFT_Global986
) ||
229
29.6k
      
(4.79k
CallerTarget == CFT_Global4.79k
&&
CalleeTarget == CFT_Device656
))
230
25.4k
    return CFP_Native;
231
232
  // (d) HostDevice behavior depends on compilation mode.
233
4.18k
  if (CallerTarget == CFT_HostDevice) {
234
    // It's OK to call a compilation-mode matching function from an HD one.
235
2.36k
    if ((getLangOpts().CUDAIsDevice && 
CalleeTarget == CFT_Device1.16k
) ||
236
2.36k
        
(1.76k
!getLangOpts().CUDAIsDevice1.76k
&&
237
1.76k
         
(1.20k
CalleeTarget == CFT_Host1.20k
||
CalleeTarget == CFT_Global524
)))
238
1.31k
      return CFP_SameSide;
239
240
    // Calls from HD to non-mode-matching functions (i.e., to host functions
241
    // when compiling in device mode or to device functions when compiling in
242
    // host mode) are allowed at the sema level, but eventually rejected if
243
    // they're ever codegened.  TODO: Reject said calls earlier.
244
1.05k
    return CFP_WrongSide;
245
2.36k
  }
246
247
  // (e) Calling across device/host boundary is not something you should do.
248
1.82k
  if ((CallerTarget == CFT_Host && 
CalleeTarget == CFT_Device447
) ||
249
1.82k
      
(1.37k
CallerTarget == CFT_Device1.37k
&&
CalleeTarget == CFT_Host1.32k
) ||
250
1.82k
      
(54
CallerTarget == CFT_Global54
&&
CalleeTarget == CFT_Host54
))
251
1.82k
    return CFP_Never;
252
253
0
  llvm_unreachable("All cases should've been handled by now.");
254
0
}
255
256
2.83k
template <typename AttrT> static bool hasImplicitAttr(const FunctionDecl *D) {
257
2.83k
  if (!D)
258
0
    return false;
259
2.83k
  if (auto *A = D->getAttr<AttrT>())
260
2.23k
    return A->isImplicit();
261
601
  return D->isImplicit();
262
2.83k
}
SemaCUDA.cpp:bool hasImplicitAttr<clang::CUDADeviceAttr>(clang::FunctionDecl const*)
Line
Count
Source
256
1.41k
template <typename AttrT> static bool hasImplicitAttr(const FunctionDecl *D) {
257
1.41k
  if (!D)
258
0
    return false;
259
1.41k
  if (auto *A = D->getAttr<AttrT>())
260
1.15k
    return A->isImplicit();
261
264
  return D->isImplicit();
262
1.41k
}
SemaCUDA.cpp:bool hasImplicitAttr<clang::CUDAHostAttr>(clang::FunctionDecl const*)
Line
Count
Source
256
1.41k
template <typename AttrT> static bool hasImplicitAttr(const FunctionDecl *D) {
257
1.41k
  if (!D)
258
0
    return false;
259
1.41k
  if (auto *A = D->getAttr<AttrT>())
260
1.08k
    return A->isImplicit();
261
337
  return D->isImplicit();
262
1.41k
}
263
264
1.41k
bool Sema::isCUDAImplicitHostDeviceFunction(const FunctionDecl *D) {
265
1.41k
  bool IsImplicitDevAttr = hasImplicitAttr<CUDADeviceAttr>(D);
266
1.41k
  bool IsImplicitHostAttr = hasImplicitAttr<CUDAHostAttr>(D);
267
1.41k
  return IsImplicitDevAttr && 
IsImplicitHostAttr703
;
268
1.41k
}
269
270
void Sema::EraseUnwantedCUDAMatches(
271
    const FunctionDecl *Caller,
272
4
    SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) {
273
4
  if (Matches.size() <= 1)
274
0
    return;
275
276
4
  using Pair = std::pair<DeclAccessPair, FunctionDecl*>;
277
278
  // Gets the CUDA function preference for a call from Caller to Match.
279
20
  auto GetCFP = [&](const Pair &Match) {
280
20
    return IdentifyCUDAPreference(Caller, Match.second);
281
20
  };
282
283
  // Find the best call preference among the functions in Matches.
284
4
  CUDAFunctionPreference BestCFP = GetCFP(*std::max_element(
285
4
      Matches.begin(), Matches.end(),
286
4
      [&](const Pair &M1, const Pair &M2) { return GetCFP(M1) < GetCFP(M2); }));
287
288
  // Erase all functions with lower priority.
289
4
  llvm::erase_if(Matches,
290
8
                 [&](const Pair &Match) { return GetCFP(Match) < BestCFP; });
291
4
}
292
293
/// When an implicitly-declared special member has to invoke more than one
294
/// base/field special member, conflicts may occur in the targets of these
295
/// members. For example, if one base's member __host__ and another's is
296
/// __device__, it's a conflict.
297
/// This function figures out if the given targets \param Target1 and
298
/// \param Target2 conflict, and if they do not it fills in
299
/// \param ResolvedTarget with a target that resolves for both calls.
300
/// \return true if there's a conflict, false otherwise.
301
static bool
302
resolveCalleeCUDATargetConflict(Sema::CUDAFunctionTarget Target1,
303
                                Sema::CUDAFunctionTarget Target2,
304
83
                                Sema::CUDAFunctionTarget *ResolvedTarget) {
305
  // Only free functions and static member functions may be global.
306
83
  assert(Target1 != Sema::CFT_Global);
307
0
  assert(Target2 != Sema::CFT_Global);
308
309
83
  if (Target1 == Sema::CFT_HostDevice) {
310
53
    *ResolvedTarget = Target2;
311
53
  } else 
if (30
Target2 == Sema::CFT_HostDevice30
) {
312
1
    *ResolvedTarget = Target1;
313
29
  } else if (Target1 != Target2) {
314
25
    return true;
315
25
  } else {
316
4
    *ResolvedTarget = Target1;
317
4
  }
318
319
58
  return false;
320
83
}
321
322
bool Sema::inferCUDATargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
323
                                                   CXXSpecialMember CSM,
324
                                                   CXXMethodDecl *MemberDecl,
325
                                                   bool ConstRHS,
326
4.55k
                                                   bool Diagnose) {
327
  // If the defaulted special member is defined lexically outside of its
328
  // owning class, or the special member already has explicit device or host
329
  // attributes, do not infer.
330
4.55k
  bool InClass = MemberDecl->getLexicalParent() == MemberDecl->getParent();
331
4.55k
  bool HasH = MemberDecl->hasAttr<CUDAHostAttr>();
332
4.55k
  bool HasD = MemberDecl->hasAttr<CUDADeviceAttr>();
333
4.55k
  bool HasExplicitAttr =
334
4.55k
      (HasD && 
!MemberDecl->getAttr<CUDADeviceAttr>()->isImplicit()2.23k
) ||
335
4.55k
      
(4.54k
HasH4.54k
&&
!MemberDecl->getAttr<CUDAHostAttr>()->isImplicit()2.19k
);
336
4.55k
  if (!InClass || 
HasExplicitAttr4.54k
)
337
14
    return false;
338
339
4.54k
  llvm::Optional<CUDAFunctionTarget> InferredTarget;
340
341
  // We're going to invoke special member lookup; mark that these special
342
  // members are called from this one, and not from its caller.
343
4.54k
  ContextRAII MethodContext(*this, MemberDecl);
344
345
  // Look for special members in base classes that should be invoked from here.
346
  // Infer the target of this member base on the ones it should call.
347
  // Skip direct and indirect virtual bases for abstract classes.
348
4.54k
  llvm::SmallVector<const CXXBaseSpecifier *, 16> Bases;
349
4.54k
  for (const auto &B : ClassDecl->bases()) {
350
647
    if (!B.isVirtual()) {
351
587
      Bases.push_back(&B);
352
587
    }
353
647
  }
354
355
4.54k
  if (!ClassDecl->isAbstract()) {
356
4.49k
    for (const auto &VB : ClassDecl->vbases()) {
357
60
      Bases.push_back(&VB);
358
60
    }
359
4.49k
  }
360
361
4.54k
  for (const auto *B : Bases) {
362
647
    const RecordType *BaseType = B->getType()->getAs<RecordType>();
363
647
    if (!BaseType) {
364
0
      continue;
365
0
    }
366
367
647
    CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl());
368
647
    Sema::SpecialMemberOverloadResult SMOR =
369
647
        LookupSpecialMember(BaseClassDecl, CSM,
370
647
                            /* ConstArg */ ConstRHS,
371
647
                            /* VolatileArg */ false,
372
647
                            /* RValueThis */ false,
373
647
                            /* ConstThis */ false,
374
647
                            /* VolatileThis */ false);
375
376
647
    if (!SMOR.getMethod())
377
5
      continue;
378
379
642
    CUDAFunctionTarget BaseMethodTarget = IdentifyCUDATarget(SMOR.getMethod());
380
642
    if (!InferredTarget.hasValue()) {
381
612
      InferredTarget = BaseMethodTarget;
382
612
    } else {
383
30
      bool ResolutionError = resolveCalleeCUDATargetConflict(
384
30
          InferredTarget.getValue(), BaseMethodTarget,
385
30
          InferredTarget.getPointer());
386
30
      if (ResolutionError) {
387
13
        if (Diagnose) {
388
3
          Diag(ClassDecl->getLocation(),
389
3
               diag::note_implicit_member_target_infer_collision)
390
3
              << (unsigned)CSM << InferredTarget.getValue() << BaseMethodTarget;
391
3
        }
392
13
        MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
393
13
        return true;
394
13
      }
395
30
    }
396
642
  }
397
398
  // Same as for bases, but now for special members of fields.
399
4.52k
  for (const auto *F : ClassDecl->fields()) {
400
3.51k
    if (F->isInvalidDecl()) {
401
0
      continue;
402
0
    }
403
404
3.51k
    const RecordType *FieldType =
405
3.51k
        Context.getBaseElementType(F->getType())->getAs<RecordType>();
406
3.51k
    if (!FieldType) {
407
3.03k
      continue;
408
3.03k
    }
409
410
486
    CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(FieldType->getDecl());
411
486
    Sema::SpecialMemberOverloadResult SMOR =
412
486
        LookupSpecialMember(FieldRecDecl, CSM,
413
486
                            /* ConstArg */ ConstRHS && 
!F->isMutable()166
,
414
486
                            /* VolatileArg */ false,
415
486
                            /* RValueThis */ false,
416
486
                            /* ConstThis */ false,
417
486
                            /* VolatileThis */ false);
418
419
486
    if (!SMOR.getMethod())
420
12
      continue;
421
422
474
    CUDAFunctionTarget FieldMethodTarget =
423
474
        IdentifyCUDATarget(SMOR.getMethod());
424
474
    if (!InferredTarget.hasValue()) {
425
421
      InferredTarget = FieldMethodTarget;
426
421
    } else {
427
53
      bool ResolutionError = resolveCalleeCUDATargetConflict(
428
53
          InferredTarget.getValue(), FieldMethodTarget,
429
53
          InferredTarget.getPointer());
430
53
      if (ResolutionError) {
431
12
        if (Diagnose) {
432
4
          Diag(ClassDecl->getLocation(),
433
4
               diag::note_implicit_member_target_infer_collision)
434
4
              << (unsigned)CSM << InferredTarget.getValue()
435
4
              << FieldMethodTarget;
436
4
        }
437
12
        MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context));
438
12
        return true;
439
12
      }
440
53
    }
441
474
  }
442
443
444
  // If no target was inferred, mark this member as __host__ __device__;
445
  // it's the least restrictive option that can be invoked from any target.
446
4.51k
  bool NeedsH = true, NeedsD = true;
447
4.51k
  if (InferredTarget.hasValue()) {
448
1.00k
    if (InferredTarget.getValue() == CFT_Device)
449
118
      NeedsH = false;
450
890
    else if (InferredTarget.getValue() == CFT_Host)
451
65
      NeedsD = false;
452
1.00k
  }
453
454
  // We either setting attributes first time, or the inferred ones must match
455
  // previously set ones.
456
4.51k
  if (NeedsD && 
!HasD4.45k
)
457
2.23k
    MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
458
4.51k
  if (NeedsH && 
!HasH4.39k
)
459
2.20k
    MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
460
461
4.51k
  return false;
462
4.52k
}
463
464
484
bool Sema::isEmptyCudaConstructor(SourceLocation Loc, CXXConstructorDecl *CD) {
465
484
  if (!CD->isDefined() && 
CD->isTemplateInstantiation()47
)
466
33
    InstantiateFunctionDefinition(Loc, CD->getFirstDecl());
467
468
  // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered
469
  // empty at a point in the translation unit, if it is either a
470
  // trivial constructor
471
484
  if (CD->isTrivial())
472
113
    return true;
473
474
  // ... or it satisfies all of the following conditions:
475
  // The constructor function has been defined.
476
  // The constructor function has no parameters,
477
  // and the function body is an empty compound statement.
478
371
  if (!(CD->hasTrivialBody() && 
CD->getNumParams() == 0252
))
479
181
    return false;
480
481
  // Its class has no virtual functions and no virtual base classes.
482
190
  if (CD->getParent()->isDynamicClass())
483
36
    return false;
484
485
  // Union ctor does not call ctors of its data members.
486
154
  if (CD->getParent()->isUnion())
487
4
    return true;
488
489
  // The only form of initializer allowed is an empty constructor.
490
  // This will recursively check all base classes and member initializers
491
150
  if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) {
492
95
        if (const CXXConstructExpr *CE =
493
95
                dyn_cast<CXXConstructExpr>(CI->getInit()))
494
59
          return isEmptyCudaConstructor(Loc, CE->getConstructor());
495
36
        return false;
496
95
      }))
497
86
    return false;
498
499
64
  return true;
500
150
}
501
502
235
bool Sema::isEmptyCudaDestructor(SourceLocation Loc, CXXDestructorDecl *DD) {
503
  // No destructor -> no problem.
504
235
  if (!DD)
505
74
    return true;
506
507
161
  if (!DD->isDefined() && 
DD->isTemplateInstantiation()12
)
508
0
    InstantiateFunctionDefinition(Loc, DD->getFirstDecl());
509
510
  // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered
511
  // empty at a point in the translation unit, if it is either a
512
  // trivial constructor
513
161
  if (DD->isTrivial())
514
30
    return true;
515
516
  // ... or it satisfies all of the following conditions:
517
  // The destructor function has been defined.
518
  // and the function body is an empty compound statement.
519
131
  if (!DD->hasTrivialBody())
520
63
    return false;
521
522
68
  const CXXRecordDecl *ClassDecl = DD->getParent();
523
524
  // Its class has no virtual functions and no virtual base classes.
525
68
  if (ClassDecl->isDynamicClass())
526
0
    return false;
527
528
  // Union does not have base class and union dtor does not call dtors of its
529
  // data members.
530
68
  if (DD->getParent()->isUnion())
531
2
    return true;
532
533
  // Only empty destructors are allowed. This will recursively check
534
  // destructors for all base classes...
535
66
  if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) {
536
12
        if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl())
537
12
          return isEmptyCudaDestructor(Loc, RD->getDestructor());
538
0
        return true;
539
12
      }))
540
12
    return false;
541
542
  // ... and member fields.
543
54
  if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) {
544
24
        if (CXXRecordDecl *RD = Field->getType()
545
24
                                    ->getBaseElementTypeUnsafe()
546
24
                                    ->getAsCXXRecordDecl())
547
24
          return isEmptyCudaDestructor(Loc, RD->getDestructor());
548
0
        return true;
549
24
      }))
550
24
    return false;
551
552
30
  return true;
553
54
}
554
555
namespace {
556
enum CUDAInitializerCheckKind {
557
  CICK_DeviceOrConstant, // Check initializer for device/constant variable
558
  CICK_Shared,           // Check initializer for shared variable
559
};
560
561
2.04k
bool IsDependentVar(VarDecl *VD) {
562
2.04k
  if (VD->getType()->isDependentType())
563
28
    return true;
564
2.01k
  if (const auto *Init = VD->getInit())
565
1.87k
    return Init->isValueDependent();
566
146
  return false;
567
2.01k
}
568
569
// Check whether a variable has an allowed initializer for a CUDA device side
570
// variable with global storage. \p VD may be a host variable to be checked for
571
// potential promotion to device side variable.
572
//
573
// CUDA/HIP allows only empty constructors as initializers for global
574
// variables (see E.2.3.1, CUDA 7.5). The same restriction also applies to all
575
// __shared__ variables whether they are local or not (they all are implicitly
576
// static in CUDA). One exception is that CUDA allows constant initializers
577
// for __constant__ and __device__ variables.
578
bool HasAllowedCUDADeviceStaticInitializer(Sema &S, VarDecl *VD,
579
758
                                           CUDAInitializerCheckKind CheckKind) {
580
758
  assert(!VD->isInvalidDecl() && VD->hasGlobalStorage());
581
0
  assert(!IsDependentVar(VD) && "do not check dependent var");
582
0
  const Expr *Init = VD->getInit();
583
758
  auto IsEmptyInit = [&](const Expr *Init) {
584
757
    if (!Init)
585
0
      return true;
586
757
    if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {
587
425
      return S.isEmptyCudaConstructor(VD->getLocation(), CE->getConstructor());
588
425
    }
589
332
    return false;
590
757
  };
591
758
  auto IsConstantInit = [&](const Expr *Init) {
592
492
    assert(Init);
593
0
    return Init->isConstantInitializer(S.Context,
594
492
                                       VD->getType()->isReferenceType());
595
492
  };
596
758
  auto HasEmptyDtor = [&](VarDecl *VD) {
597
485
    if (const auto *RD = VD->getType()->getAsCXXRecordDecl())
598
199
      return S.isEmptyCudaDestructor(VD->getLocation(), RD->getDestructor());
599
286
    return true;
600
485
  };
601
758
  if (CheckKind == CICK_Shared)
602
154
    return IsEmptyInit(Init) && 
HasEmptyDtor(VD)61
;
603
604
  return S.LangOpts.GPUAllowDeviceInit ||
604
604
         
(603
(603
IsEmptyInit(Init)603
||
IsConstantInit(Init)492
) &&
HasEmptyDtor(VD)424
);
605
758
}
606
} // namespace
607
608
3.51k
void Sema::checkAllowedCUDAInitializer(VarDecl *VD) {
609
  // Do not check dependent variables since the ctor/dtor/initializer are not
610
  // determined. Do it after instantiation.
611
3.51k
  if (VD->isInvalidDecl() || 
!VD->hasInit()3.51k
||
!VD->hasGlobalStorage()2.46k
||
612
3.51k
      
IsDependentVar(VD)898
)
613
2.65k
    return;
614
861
  const Expr *Init = VD->getInit();
615
861
  bool IsSharedVar = VD->hasAttr<CUDASharedAttr>();
616
861
  bool IsDeviceOrConstantVar =
617
861
      !IsSharedVar &&
618
861
      
(707
VD->hasAttr<CUDADeviceAttr>()707
||
VD->hasAttr<CUDAConstantAttr>()478
);
619
861
  if (IsDeviceOrConstantVar || 
IsSharedVar318
) {
620
697
    if (HasAllowedCUDADeviceStaticInitializer(
621
697
            *this, VD, IsSharedVar ? 
CICK_Shared154
:
CICK_DeviceOrConstant543
))
622
369
      return;
623
328
    Diag(VD->getLocation(),
624
328
         IsSharedVar ? 
diag::err_shared_var_init114
:
diag::err_dynamic_var_init214
)
625
328
        << Init->getSourceRange();
626
328
    VD->setInvalidDecl();
627
328
  } else {
628
    // This is a host-side global variable.  Check that the initializer is
629
    // callable from the host side.
630
164
    const FunctionDecl *InitFn = nullptr;
631
164
    if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Init)) {
632
31
      InitFn = CE->getConstructor();
633
133
    } else if (const CallExpr *CE = dyn_cast<CallExpr>(Init)) {
634
3
      InitFn = CE->getDirectCallee();
635
3
    }
636
164
    if (InitFn) {
637
34
      CUDAFunctionTarget InitFnTarget = IdentifyCUDATarget(InitFn);
638
34
      if (InitFnTarget != CFT_Host && 
InitFnTarget != CFT_HostDevice20
) {
639
3
        Diag(VD->getLocation(), diag::err_ref_bad_target_global_initializer)
640
3
            << InitFnTarget << InitFn;
641
3
        Diag(InitFn->getLocation(), diag::note_previous_decl) << InitFn;
642
3
        VD->setInvalidDecl();
643
3
      }
644
34
    }
645
164
  }
646
861
}
647
648
// With -fcuda-host-device-constexpr, an unattributed constexpr function is
649
// treated as implicitly __host__ __device__, unless:
650
//  * it is a variadic function (device-side variadic functions are not
651
//    allowed), or
652
//  * a __device__ function with this signature was already declared, in which
653
//    case in which case we output an error, unless the __device__ decl is in a
654
//    system header, in which case we leave the constexpr function unattributed.
655
//
656
// In addition, all function decls are treated as __host__ __device__ when
657
// ForceCUDAHostDeviceDepth > 0 (corresponding to code within a
658
//   #pragma clang force_cuda_host_device_begin/end
659
// pair).
660
void Sema::maybeAddCUDAHostDeviceAttrs(FunctionDecl *NewD,
661
11.3k
                                       const LookupResult &Previous) {
662
11.3k
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
663
664
11.3k
  if (ForceCUDAHostDeviceDepth > 0) {
665
23
    if (!NewD->hasAttr<CUDAHostAttr>())
666
21
      NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
667
23
    if (!NewD->hasAttr<CUDADeviceAttr>())
668
21
      NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
669
23
    return;
670
23
  }
671
672
11.3k
  if (!getLangOpts().CUDAHostDeviceConstexpr || 
!NewD->isConstexpr()11.3k
||
673
11.3k
      
NewD->isVariadic()61
||
NewD->hasAttr<CUDAHostAttr>()57
||
674
11.3k
      
NewD->hasAttr<CUDADeviceAttr>()53
||
NewD->hasAttr<CUDAGlobalAttr>()49
)
675
11.2k
    return;
676
677
  // Is D a __device__ function with the same signature as NewD, ignoring CUDA
678
  // attributes?
679
49
  auto IsMatchingDeviceFn = [&](NamedDecl *D) {
680
40
    if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D))
681
4
      D = Using->getTargetDecl();
682
40
    FunctionDecl *OldD = D->getAsFunction();
683
40
    return OldD && OldD->hasAttr<CUDADeviceAttr>() &&
684
40
           
!OldD->hasAttr<CUDAHostAttr>()24
&&
685
40
           !IsOverload(NewD, OldD, /* UseMemberUsingDeclRules = */ false,
686
12
                       /* ConsiderCudaAttrs = */ false);
687
40
  };
688
49
  auto It = llvm::find_if(Previous, IsMatchingDeviceFn);
689
49
  if (It != Previous.end()) {
690
    // We found a __device__ function with the same name and signature as NewD
691
    // (ignoring CUDA attrs).  This is an error unless that function is defined
692
    // in a system header, in which case we simply return without making NewD
693
    // host+device.
694
12
    NamedDecl *Match = *It;
695
12
    if (!getSourceManager().isInSystemHeader(Match->getLocation())) {
696
4
      Diag(NewD->getLocation(),
697
4
           diag::err_cuda_unattributed_constexpr_cannot_overload_device)
698
4
          << NewD;
699
4
      Diag(Match->getLocation(),
700
4
           diag::note_cuda_conflicting_device_function_declared_here);
701
4
    }
702
12
    return;
703
12
  }
704
705
37
  NewD->addAttr(CUDAHostAttr::CreateImplicit(Context));
706
37
  NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context));
707
37
}
708
709
// TODO: `__constant__` memory may be a limited resource for certain targets.
710
// A safeguard may be needed at the end of compilation pipeline if
711
// `__constant__` memory usage goes beyond limit.
712
2.35M
void Sema::MaybeAddCUDAConstantAttr(VarDecl *VD) {
713
  // Do not promote dependent variables since the cotr/dtor/initializer are
714
  // not determined. Do it after instantiation.
715
2.35M
  if (getLangOpts().CUDAIsDevice && 
!VD->hasAttr<CUDAConstantAttr>()2.15k
&&
716
2.35M
      
!VD->hasAttr<CUDAConstantAttr>()1.91k
&&
!VD->hasAttr<CUDASharedAttr>()1.91k
&&
717
2.35M
      
(1.71k
VD->isFileVarDecl()1.71k
||
VD->isStaticDataMember()1.32k
) &&
718
2.35M
      
!IsDependentVar(VD)390
&&
719
2.35M
      
(374
VD->isConstexpr()374
||
(332
VD->getType().isConstQualified()332
&&
720
332
                             HasAllowedCUDADeviceStaticInitializer(
721
96
                                 *this, VD, CICK_DeviceOrConstant)))) {
722
96
    VD->addAttr(CUDAConstantAttr::CreateImplicit(getASTContext()));
723
96
  }
724
2.35M
}
725
726
Sema::SemaDiagnosticBuilder Sema::CUDADiagIfDeviceCode(SourceLocation Loc,
727
126
                                                       unsigned DiagID) {
728
126
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
729
126
  SemaDiagnosticBuilder::Kind DiagKind = [&] {
730
126
    if (!isa<FunctionDecl>(CurContext))
731
1
      return SemaDiagnosticBuilder::K_Nop;
732
125
    switch (CurrentCUDATarget()) {
733
13
    case CFT_Global:
734
58
    case CFT_Device:
735
58
      return SemaDiagnosticBuilder::K_Immediate;
736
55
    case CFT_HostDevice:
737
      // An HD function counts as host code if we're compiling for host, and
738
      // device code if we're compiling for device.  Defer any errors in device
739
      // mode until the function is known-emitted.
740
55
      if (!getLangOpts().CUDAIsDevice)
741
6
        return SemaDiagnosticBuilder::K_Nop;
742
49
      if (IsLastErrorImmediate && Diags.getDiagnosticIDs()->isBuiltinNote(DiagID))
743
7
        return SemaDiagnosticBuilder::K_Immediate;
744
42
      return (getEmissionStatus(cast<FunctionDecl>(CurContext)) ==
745
42
              FunctionEmissionStatus::Emitted)
746
42
                 ? 
SemaDiagnosticBuilder::K_ImmediateWithCallStack6
747
42
                 : 
SemaDiagnosticBuilder::K_Deferred36
;
748
12
    default:
749
12
      return SemaDiagnosticBuilder::K_Nop;
750
125
    }
751
125
  }();
752
126
  return SemaDiagnosticBuilder(DiagKind, Loc, DiagID,
753
126
                               dyn_cast<FunctionDecl>(CurContext), *this);
754
126
}
755
756
Sema::SemaDiagnosticBuilder Sema::CUDADiagIfHostCode(SourceLocation Loc,
757
56
                                                     unsigned DiagID) {
758
56
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
759
56
  SemaDiagnosticBuilder::Kind DiagKind = [&] {
760
56
    if (!isa<FunctionDecl>(CurContext))
761
0
      return SemaDiagnosticBuilder::K_Nop;
762
56
    switch (CurrentCUDATarget()) {
763
8
    case CFT_Host:
764
8
      return SemaDiagnosticBuilder::K_Immediate;
765
3
    case CFT_HostDevice:
766
      // An HD function counts as host code if we're compiling for host, and
767
      // device code if we're compiling for device.  Defer any errors in device
768
      // mode until the function is known-emitted.
769
3
      if (getLangOpts().CUDAIsDevice)
770
0
        return SemaDiagnosticBuilder::K_Nop;
771
3
      if (IsLastErrorImmediate && Diags.getDiagnosticIDs()->isBuiltinNote(DiagID))
772
0
        return SemaDiagnosticBuilder::K_Immediate;
773
3
      return (getEmissionStatus(cast<FunctionDecl>(CurContext)) ==
774
3
              FunctionEmissionStatus::Emitted)
775
3
                 ? 
SemaDiagnosticBuilder::K_ImmediateWithCallStack0
776
3
                 : SemaDiagnosticBuilder::K_Deferred;
777
45
    default:
778
45
      return SemaDiagnosticBuilder::K_Nop;
779
56
    }
780
56
  }();
781
56
  return SemaDiagnosticBuilder(DiagKind, Loc, DiagID,
782
56
                               dyn_cast<FunctionDecl>(CurContext), *this);
783
56
}
784
785
24.6k
bool Sema::CheckCUDACall(SourceLocation Loc, FunctionDecl *Callee) {
786
24.6k
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
787
0
  assert(Callee && "Callee may not be null.");
788
789
0
  auto &ExprEvalCtx = ExprEvalContexts.back();
790
24.6k
  if (ExprEvalCtx.isUnevaluated() || 
ExprEvalCtx.isConstantEvaluated()24.6k
)
791
107
    return true;
792
793
  // FIXME: Is bailing out early correct here?  Should we instead assume that
794
  // the caller is a global initializer?
795
24.5k
  FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext);
796
24.5k
  if (!Caller)
797
1.14k
    return true;
798
799
  // If the caller is known-emitted, mark the callee as known-emitted.
800
  // Otherwise, mark the call in our call graph so we can traverse it later.
801
23.4k
  bool CallerKnownEmitted =
802
23.4k
      getEmissionStatus(Caller) == FunctionEmissionStatus::Emitted;
803
23.4k
  SemaDiagnosticBuilder::Kind DiagKind = [this, Caller, Callee,
804
23.4k
                                          CallerKnownEmitted] {
805
23.4k
    switch (IdentifyCUDAPreference(Caller, Callee)) {
806
102
    case CFP_Never:
807
326
    case CFP_WrongSide:
808
326
      assert(Caller && "Never/wrongSide calls require a non-null caller");
809
      // If we know the caller will be emitted, we know this wrong-side call
810
      // will be emitted, so it's an immediate error.  Otherwise, defer the
811
      // error until we know the caller is emitted.
812
326
      return CallerKnownEmitted
813
326
                 ? 
SemaDiagnosticBuilder::K_ImmediateWithCallStack116
814
326
                 : 
SemaDiagnosticBuilder::K_Deferred210
;
815
23.1k
    default:
816
23.1k
      return SemaDiagnosticBuilder::K_Nop;
817
23.4k
    }
818
23.4k
  }();
819
820
23.4k
  if (DiagKind == SemaDiagnosticBuilder::K_Nop)
821
23.1k
    return true;
822
823
  // Avoid emitting this error twice for the same location.  Using a hashtable
824
  // like this is unfortunate, but because we must continue parsing as normal
825
  // after encountering a deferred error, it's otherwise very tricky for us to
826
  // ensure that we only emit this deferred error once.
827
326
  if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second)
828
122
    return true;
829
830
204
  SemaDiagnosticBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller, *this)
831
204
      << IdentifyCUDATarget(Callee) << /*function*/ 0 << Callee
832
204
      << IdentifyCUDATarget(Caller);
833
204
  if (!Callee->getBuiltinID())
834
192
    SemaDiagnosticBuilder(DiagKind, Callee->getLocation(),
835
192
                          diag::note_previous_decl, Caller, *this)
836
192
        << Callee;
837
204
  return DiagKind != SemaDiagnosticBuilder::K_Immediate &&
838
204
         DiagKind != SemaDiagnosticBuilder::K_ImmediateWithCallStack;
839
326
}
840
841
// Check the wrong-sided reference capture of lambda for CUDA/HIP.
842
// A lambda function may capture a stack variable by reference when it is
843
// defined and uses the capture by reference when the lambda is called. When
844
// the capture and use happen on different sides, the capture is invalid and
845
// should be diagnosed.
846
void Sema::CUDACheckLambdaCapture(CXXMethodDecl *Callee,
847
98
                                  const sema::Capture &Capture) {
848
  // In host compilation we only need to check lambda functions emitted on host
849
  // side. In such lambda functions, a reference capture is invalid only
850
  // if the lambda structure is populated by a device function or kernel then
851
  // is passed to and called by a host function. However that is impossible,
852
  // since a device function or kernel can only call a device function, also a
853
  // kernel cannot pass a lambda back to a host function since we cannot
854
  // define a kernel argument type which can hold the lambda before the lambda
855
  // itself is defined.
856
98
  if (!LangOpts.CUDAIsDevice)
857
32
    return;
858
859
  // File-scope lambda can only do init captures for global variables, which
860
  // results in passing by value for these global variables.
861
66
  FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext);
862
66
  if (!Caller)
863
0
    return;
864
865
  // In device compilation, we only need to check lambda functions which are
866
  // emitted on device side. For such lambdas, a reference capture is invalid
867
  // only if the lambda structure is populated by a host function then passed
868
  // to and called in a device function or kernel.
869
66
  bool CalleeIsDevice = Callee->hasAttr<CUDADeviceAttr>();
870
66
  bool CallerIsHost =
871
66
      !Caller->hasAttr<CUDAGlobalAttr>() && !Caller->hasAttr<CUDADeviceAttr>();
872
66
  bool ShouldCheck = CalleeIsDevice && CallerIsHost;
873
66
  if (!ShouldCheck || 
!Capture.isReferenceCapture()56
)
874
26
    return;
875
40
  auto DiagKind = SemaDiagnosticBuilder::K_Deferred;
876
40
  if (Capture.isVariableCapture()) {
877
19
    SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),
878
19
                          diag::err_capture_bad_target, Callee, *this)
879
19
        << Capture.getVariable();
880
21
  } else if (Capture.isThisCapture()) {
881
    // Capture of this pointer is allowed since this pointer may be pointing to
882
    // managed memory which is accessible on both device and host sides. It only
883
    // results in invalid memory access if this pointer points to memory not
884
    // accessible on device side.
885
21
    SemaDiagnosticBuilder(DiagKind, Capture.getLocation(),
886
21
                          diag::warn_maybe_capture_bad_target_this_ptr, Callee,
887
21
                          *this);
888
21
  }
889
40
  return;
890
66
}
891
892
228
void Sema::CUDASetLambdaAttrs(CXXMethodDecl *Method) {
893
228
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
894
228
  if (Method->hasAttr<CUDAHostAttr>() || 
Method->hasAttr<CUDADeviceAttr>()188
)
895
90
    return;
896
138
  Method->addAttr(CUDADeviceAttr::CreateImplicit(Context));
897
138
  Method->addAttr(CUDAHostAttr::CreateImplicit(Context));
898
138
}
899
900
void Sema::checkCUDATargetOverload(FunctionDecl *NewFD,
901
13.7k
                                   const LookupResult &Previous) {
902
13.7k
  assert(getLangOpts().CUDA && "Should only be called during CUDA compilation");
903
0
  CUDAFunctionTarget NewTarget = IdentifyCUDATarget(NewFD);
904
13.7k
  for (NamedDecl *OldND : Previous) {
905
10.5k
    FunctionDecl *OldFD = OldND->getAsFunction();
906
10.5k
    if (!OldFD)
907
326
      continue;
908
909
10.2k
    CUDAFunctionTarget OldTarget = IdentifyCUDATarget(OldFD);
910
    // Don't allow HD and global functions to overload other functions with the
911
    // same signature.  We allow overloading based on CUDA attributes so that
912
    // functions can have different implementations on the host and device, but
913
    // HD/global functions "exist" in some sense on both the host and device, so
914
    // should have the same implementation on both sides.
915
10.2k
    if (NewTarget != OldTarget &&
916
10.2k
        
(3.69k
(NewTarget == CFT_HostDevice)3.69k
||
(OldTarget == CFT_HostDevice)3.29k
||
917
3.69k
         
(NewTarget == CFT_Global)3.25k
||
(OldTarget == CFT_Global)3.25k
) &&
918
10.2k
        !IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false,
919
441
                    /* ConsiderCudaAttrs = */ false)) {
920
48
      Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)
921
48
          << NewTarget << NewFD->getDeclName() << OldTarget << OldFD;
922
48
      Diag(OldFD->getLocation(), diag::note_previous_declaration);
923
48
      NewFD->setInvalidDecl();
924
48
      break;
925
48
    }
926
10.2k
  }
927
13.7k
}
928
929
template <typename AttrTy>
930
static void copyAttrIfPresent(Sema &S, FunctionDecl *FD,
931
33
                              const FunctionDecl &TemplateFD) {
932
33
  if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
933
18
    AttrTy *Clone = Attribute->clone(S.Context);
934
18
    Clone->setInherited(true);
935
18
    FD->addAttr(Clone);
936
18
  }
937
33
}
SemaCUDA.cpp:void copyAttrIfPresent<clang::CUDAGlobalAttr>(clang::Sema&, clang::FunctionDecl*, clang::FunctionDecl const&)
Line
Count
Source
931
11
                              const FunctionDecl &TemplateFD) {
932
11
  if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
933
0
    AttrTy *Clone = Attribute->clone(S.Context);
934
0
    Clone->setInherited(true);
935
0
    FD->addAttr(Clone);
936
0
  }
937
11
}
SemaCUDA.cpp:void copyAttrIfPresent<clang::CUDAHostAttr>(clang::Sema&, clang::FunctionDecl*, clang::FunctionDecl const&)
Line
Count
Source
931
11
                              const FunctionDecl &TemplateFD) {
932
11
  if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
933
9
    AttrTy *Clone = Attribute->clone(S.Context);
934
9
    Clone->setInherited(true);
935
9
    FD->addAttr(Clone);
936
9
  }
937
11
}
SemaCUDA.cpp:void copyAttrIfPresent<clang::CUDADeviceAttr>(clang::Sema&, clang::FunctionDecl*, clang::FunctionDecl const&)
Line
Count
Source
931
11
                              const FunctionDecl &TemplateFD) {
932
11
  if (AttrTy *Attribute = TemplateFD.getAttr<AttrTy>()) {
933
9
    AttrTy *Clone = Attribute->clone(S.Context);
934
9
    Clone->setInherited(true);
935
9
    FD->addAttr(Clone);
936
9
  }
937
11
}
938
939
void Sema::inheritCUDATargetAttrs(FunctionDecl *FD,
940
11
                                  const FunctionTemplateDecl &TD) {
941
11
  const FunctionDecl &TemplateFD = *TD.getTemplatedDecl();
942
11
  copyAttrIfPresent<CUDAGlobalAttr>(*this, FD, TemplateFD);
943
11
  copyAttrIfPresent<CUDAHostAttr>(*this, FD, TemplateFD);
944
11
  copyAttrIfPresent<CUDADeviceAttr>(*this, FD, TemplateFD);
945
11
}
946
947
9.87k
std::string Sema::getCudaConfigureFuncName() const {
948
9.87k
  if (getLangOpts().HIP)
949
4.15k
    return getLangOpts().HIPUseNewLaunchAPI ? 
"__hipPushCallConfiguration"6
950
4.15k
                                            : 
"hipConfigureCall"4.14k
;
951
952
  // New CUDA kernel launch sequence.
953
5.72k
  if (CudaFeatureEnabled(Context.getTargetInfo().getSDKVersion(),
954
5.72k
                         CudaFeature::CUDA_USES_NEW_LAUNCH))
955
53
    return "__cudaPushCallConfiguration";
956
957
  // Legacy CUDA kernel configuration call
958
5.67k
  return "cudaConfigureCall";
959
5.72k
}