Coverage Report

Created: 2020-09-22 08:39

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