Coverage Report

Created: 2021-01-23 06:44

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