Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 1 | //===--- SemaCUDA.cpp - Semantic Analysis for CUDA constructs -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// \brief This file implements semantic analysis for CUDA constructs. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/Decl.h" |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprCXX.h" |
Reid Kleckner | bbc0178 | 2014-12-03 21:53:36 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Lookup.h" |
| 19 | #include "clang/Sema/Sema.h" |
Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 20 | #include "clang/Sema/SemaDiagnostic.h" |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 21 | #include "clang/Sema/SemaInternal.h" |
Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Template.h" |
Eli Bendersky | 9a220fc | 2014-09-29 20:38:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Optional.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 27 | void Sema::PushForceCUDAHostDevice() { |
| 28 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
| 29 | ForceCUDAHostDeviceDepth++; |
| 30 | } |
| 31 | |
| 32 | bool Sema::PopForceCUDAHostDevice() { |
| 33 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
| 34 | if (ForceCUDAHostDeviceDepth == 0) |
| 35 | return false; |
| 36 | ForceCUDAHostDeviceDepth--; |
| 37 | return true; |
| 38 | } |
| 39 | |
Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 40 | ExprResult Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 41 | MultiExprArg ExecConfig, |
| 42 | SourceLocation GGGLoc) { |
| 43 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 44 | if (!ConfigDecl) |
| 45 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 46 | << "cudaConfigureCall"); |
| 47 | QualType ConfigQTy = ConfigDecl->getType(); |
| 48 | |
| 49 | DeclRefExpr *ConfigDR = new (Context) |
| 50 | DeclRefExpr(ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc); |
| 51 | MarkFunctionReferenced(LLLLoc, ConfigDecl); |
| 52 | |
| 53 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, nullptr, |
| 54 | /*IsExecConfig=*/true); |
| 55 | } |
| 56 | |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame^] | 57 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const AttributeList *Attr) { |
| 58 | bool HasHostAttr = false; |
| 59 | bool HasDeviceAttr = false; |
| 60 | bool HasGlobalAttr = false; |
| 61 | bool HasInvalidTargetAttr = false; |
| 62 | while (Attr) { |
| 63 | switch(Attr->getKind()){ |
| 64 | case AttributeList::AT_CUDAGlobal: |
| 65 | HasGlobalAttr = true; |
| 66 | break; |
| 67 | case AttributeList::AT_CUDAHost: |
| 68 | HasHostAttr = true; |
| 69 | break; |
| 70 | case AttributeList::AT_CUDADevice: |
| 71 | HasDeviceAttr = true; |
| 72 | break; |
| 73 | case AttributeList::AT_CUDAInvalidTarget: |
| 74 | HasInvalidTargetAttr = true; |
| 75 | break; |
| 76 | default: |
| 77 | break; |
| 78 | } |
| 79 | Attr = Attr->getNext(); |
| 80 | } |
| 81 | if (HasInvalidTargetAttr) |
| 82 | return CFT_InvalidTarget; |
| 83 | |
| 84 | if (HasGlobalAttr) |
| 85 | return CFT_Global; |
| 86 | |
| 87 | if (HasHostAttr && HasDeviceAttr) |
| 88 | return CFT_HostDevice; |
| 89 | |
| 90 | if (HasDeviceAttr) |
| 91 | return CFT_Device; |
| 92 | |
| 93 | return CFT_Host; |
| 94 | } |
| 95 | |
Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 96 | /// IdentifyCUDATarget - Determine the CUDA compilation target for this function |
| 97 | Sema::CUDAFunctionTarget Sema::IdentifyCUDATarget(const FunctionDecl *D) { |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 98 | // Code that lives outside a function is run on the host. |
| 99 | if (D == nullptr) |
| 100 | return CFT_Host; |
| 101 | |
Eli Bendersky | 9a220fc | 2014-09-29 20:38:29 +0000 | [diff] [blame] | 102 | if (D->hasAttr<CUDAInvalidTargetAttr>()) |
| 103 | return CFT_InvalidTarget; |
Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 104 | |
| 105 | if (D->hasAttr<CUDAGlobalAttr>()) |
| 106 | return CFT_Global; |
| 107 | |
| 108 | if (D->hasAttr<CUDADeviceAttr>()) { |
| 109 | if (D->hasAttr<CUDAHostAttr>()) |
| 110 | return CFT_HostDevice; |
| 111 | return CFT_Device; |
Eli Bendersky | f2787a0 | 2014-09-30 17:38:34 +0000 | [diff] [blame] | 112 | } else if (D->hasAttr<CUDAHostAttr>()) { |
| 113 | return CFT_Host; |
| 114 | } else if (D->isImplicit()) { |
| 115 | // Some implicit declarations (like intrinsic functions) are not marked. |
| 116 | // Set the most lenient target on them for maximal flexibility. |
| 117 | return CFT_HostDevice; |
Eli Bendersky | 7325e56 | 2014-09-03 15:27:03 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return CFT_Host; |
| 121 | } |
| 122 | |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 123 | // * CUDA Call preference table |
| 124 | // |
| 125 | // F - from, |
| 126 | // T - to |
| 127 | // Ph - preference in host mode |
| 128 | // Pd - preference in device mode |
| 129 | // H - handled in (x) |
Justin Lebar | 3918647 | 2016-03-29 16:24:22 +0000 | [diff] [blame] | 130 | // Preferences: N:native, SS:same side, HD:host-device, WS:wrong side, --:never. |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 131 | // |
Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 132 | // | F | T | Ph | Pd | H | |
| 133 | // |----+----+-----+-----+-----+ |
| 134 | // | d | d | N | N | (c) | |
| 135 | // | d | g | -- | -- | (a) | |
| 136 | // | d | h | -- | -- | (e) | |
| 137 | // | d | hd | HD | HD | (b) | |
| 138 | // | g | d | N | N | (c) | |
| 139 | // | g | g | -- | -- | (a) | |
| 140 | // | g | h | -- | -- | (e) | |
| 141 | // | g | hd | HD | HD | (b) | |
| 142 | // | h | d | -- | -- | (e) | |
| 143 | // | h | g | N | N | (c) | |
| 144 | // | h | h | N | N | (c) | |
| 145 | // | h | hd | HD | HD | (b) | |
| 146 | // | hd | d | WS | SS | (d) | |
| 147 | // | hd | g | SS | -- |(d/a)| |
| 148 | // | hd | h | SS | WS | (d) | |
| 149 | // | hd | hd | HD | HD | (b) | |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 150 | |
| 151 | Sema::CUDAFunctionPreference |
| 152 | Sema::IdentifyCUDAPreference(const FunctionDecl *Caller, |
| 153 | const FunctionDecl *Callee) { |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 154 | assert(Callee && "Callee must be valid."); |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 155 | CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller); |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 156 | CUDAFunctionTarget CalleeTarget = IdentifyCUDATarget(Callee); |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 157 | |
| 158 | // If one of the targets is invalid, the check always fails, no matter what |
| 159 | // the other target is. |
| 160 | if (CallerTarget == CFT_InvalidTarget || CalleeTarget == CFT_InvalidTarget) |
| 161 | return CFP_Never; |
| 162 | |
| 163 | // (a) Can't call global from some contexts until we support CUDA's |
| 164 | // dynamic parallelism. |
| 165 | if (CalleeTarget == CFT_Global && |
Justin Lebar | 0254c46 | 2016-10-12 01:30:08 +0000 | [diff] [blame] | 166 | (CallerTarget == CFT_Global || CallerTarget == CFT_Device)) |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 167 | return CFP_Never; |
| 168 | |
Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 169 | // (b) Calling HostDevice is OK for everyone. |
| 170 | if (CalleeTarget == CFT_HostDevice) |
| 171 | return CFP_HostDevice; |
| 172 | |
| 173 | // (c) Best case scenarios |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 174 | if (CalleeTarget == CallerTarget || |
| 175 | (CallerTarget == CFT_Host && CalleeTarget == CFT_Global) || |
| 176 | (CallerTarget == CFT_Global && CalleeTarget == CFT_Device)) |
Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 177 | return CFP_Native; |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 178 | |
| 179 | // (d) HostDevice behavior depends on compilation mode. |
| 180 | if (CallerTarget == CFT_HostDevice) { |
Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 181 | // It's OK to call a compilation-mode matching function from an HD one. |
| 182 | if ((getLangOpts().CUDAIsDevice && CalleeTarget == CFT_Device) || |
| 183 | (!getLangOpts().CUDAIsDevice && |
| 184 | (CalleeTarget == CFT_Host || CalleeTarget == CFT_Global))) |
| 185 | return CFP_SameSide; |
| 186 | |
Justin Lebar | 25c4a81 | 2016-03-29 16:24:16 +0000 | [diff] [blame] | 187 | // Calls from HD to non-mode-matching functions (i.e., to host functions |
| 188 | // when compiling in device mode or to device functions when compiling in |
| 189 | // host mode) are allowed at the sema level, but eventually rejected if |
| 190 | // they're ever codegened. TODO: Reject said calls earlier. |
| 191 | return CFP_WrongSide; |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // (e) Calling across device/host boundary is not something you should do. |
| 195 | if ((CallerTarget == CFT_Host && CalleeTarget == CFT_Device) || |
| 196 | (CallerTarget == CFT_Device && CalleeTarget == CFT_Host) || |
| 197 | (CallerTarget == CFT_Global && CalleeTarget == CFT_Host)) |
Artem Belevich | 1860910 | 2016-02-12 18:29:18 +0000 | [diff] [blame] | 198 | return CFP_Never; |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 199 | |
| 200 | llvm_unreachable("All cases should've been handled by now."); |
| 201 | } |
| 202 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 203 | void Sema::EraseUnwantedCUDAMatches( |
| 204 | const FunctionDecl *Caller, |
| 205 | SmallVectorImpl<std::pair<DeclAccessPair, FunctionDecl *>> &Matches) { |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 206 | if (Matches.size() <= 1) |
| 207 | return; |
| 208 | |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 209 | using Pair = std::pair<DeclAccessPair, FunctionDecl*>; |
| 210 | |
Justin Lebar | e6a2cc1 | 2016-03-22 00:09:25 +0000 | [diff] [blame] | 211 | // Gets the CUDA function preference for a call from Caller to Match. |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 212 | auto GetCFP = [&](const Pair &Match) { |
| 213 | return IdentifyCUDAPreference(Caller, Match.second); |
Justin Lebar | e6a2cc1 | 2016-03-22 00:09:25 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 216 | // Find the best call preference among the functions in Matches. |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 217 | CUDAFunctionPreference BestCFP = GetCFP(*std::max_element( |
Justin Lebar | e6a2cc1 | 2016-03-22 00:09:25 +0000 | [diff] [blame] | 218 | Matches.begin(), Matches.end(), |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 219 | [&](const Pair &M1, const Pair &M2) { return GetCFP(M1) < GetCFP(M2); })); |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 220 | |
| 221 | // Erase all functions with lower priority. |
Justin Lebar | e71c08f | 2016-07-12 23:23:13 +0000 | [diff] [blame] | 222 | Matches.erase( |
Richard Smith | f75dcbe | 2016-10-11 00:21:10 +0000 | [diff] [blame] | 223 | llvm::remove_if( |
| 224 | Matches, [&](const Pair &Match) { return GetCFP(Match) < BestCFP; }), |
Justin Lebar | e71c08f | 2016-07-12 23:23:13 +0000 | [diff] [blame] | 225 | Matches.end()); |
Artem Belevich | 94a55e8 | 2015-09-22 17:22:59 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Eli Bendersky | 9a220fc | 2014-09-29 20:38:29 +0000 | [diff] [blame] | 228 | /// When an implicitly-declared special member has to invoke more than one |
| 229 | /// base/field special member, conflicts may occur in the targets of these |
| 230 | /// members. For example, if one base's member __host__ and another's is |
| 231 | /// __device__, it's a conflict. |
| 232 | /// This function figures out if the given targets \param Target1 and |
| 233 | /// \param Target2 conflict, and if they do not it fills in |
| 234 | /// \param ResolvedTarget with a target that resolves for both calls. |
| 235 | /// \return true if there's a conflict, false otherwise. |
| 236 | static bool |
| 237 | resolveCalleeCUDATargetConflict(Sema::CUDAFunctionTarget Target1, |
| 238 | Sema::CUDAFunctionTarget Target2, |
| 239 | Sema::CUDAFunctionTarget *ResolvedTarget) { |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 240 | // Only free functions and static member functions may be global. |
| 241 | assert(Target1 != Sema::CFT_Global); |
| 242 | assert(Target2 != Sema::CFT_Global); |
Eli Bendersky | 9a220fc | 2014-09-29 20:38:29 +0000 | [diff] [blame] | 243 | |
| 244 | if (Target1 == Sema::CFT_HostDevice) { |
| 245 | *ResolvedTarget = Target2; |
| 246 | } else if (Target2 == Sema::CFT_HostDevice) { |
| 247 | *ResolvedTarget = Target1; |
| 248 | } else if (Target1 != Target2) { |
| 249 | return true; |
| 250 | } else { |
| 251 | *ResolvedTarget = Target1; |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | bool Sema::inferCUDATargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl, |
| 258 | CXXSpecialMember CSM, |
| 259 | CXXMethodDecl *MemberDecl, |
| 260 | bool ConstRHS, |
| 261 | bool Diagnose) { |
| 262 | llvm::Optional<CUDAFunctionTarget> InferredTarget; |
| 263 | |
| 264 | // We're going to invoke special member lookup; mark that these special |
| 265 | // members are called from this one, and not from its caller. |
| 266 | ContextRAII MethodContext(*this, MemberDecl); |
| 267 | |
| 268 | // Look for special members in base classes that should be invoked from here. |
| 269 | // Infer the target of this member base on the ones it should call. |
| 270 | // Skip direct and indirect virtual bases for abstract classes. |
| 271 | llvm::SmallVector<const CXXBaseSpecifier *, 16> Bases; |
| 272 | for (const auto &B : ClassDecl->bases()) { |
| 273 | if (!B.isVirtual()) { |
| 274 | Bases.push_back(&B); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (!ClassDecl->isAbstract()) { |
| 279 | for (const auto &VB : ClassDecl->vbases()) { |
| 280 | Bases.push_back(&VB); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | for (const auto *B : Bases) { |
| 285 | const RecordType *BaseType = B->getType()->getAs<RecordType>(); |
| 286 | if (!BaseType) { |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseType->getDecl()); |
| 291 | Sema::SpecialMemberOverloadResult *SMOR = |
| 292 | LookupSpecialMember(BaseClassDecl, CSM, |
| 293 | /* ConstArg */ ConstRHS, |
| 294 | /* VolatileArg */ false, |
| 295 | /* RValueThis */ false, |
| 296 | /* ConstThis */ false, |
| 297 | /* VolatileThis */ false); |
| 298 | |
| 299 | if (!SMOR || !SMOR->getMethod()) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | CUDAFunctionTarget BaseMethodTarget = IdentifyCUDATarget(SMOR->getMethod()); |
| 304 | if (!InferredTarget.hasValue()) { |
| 305 | InferredTarget = BaseMethodTarget; |
| 306 | } else { |
| 307 | bool ResolutionError = resolveCalleeCUDATargetConflict( |
| 308 | InferredTarget.getValue(), BaseMethodTarget, |
| 309 | InferredTarget.getPointer()); |
| 310 | if (ResolutionError) { |
| 311 | if (Diagnose) { |
| 312 | Diag(ClassDecl->getLocation(), |
| 313 | diag::note_implicit_member_target_infer_collision) |
| 314 | << (unsigned)CSM << InferredTarget.getValue() << BaseMethodTarget; |
| 315 | } |
| 316 | MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context)); |
| 317 | return true; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // Same as for bases, but now for special members of fields. |
| 323 | for (const auto *F : ClassDecl->fields()) { |
| 324 | if (F->isInvalidDecl()) { |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | const RecordType *FieldType = |
| 329 | Context.getBaseElementType(F->getType())->getAs<RecordType>(); |
| 330 | if (!FieldType) { |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | CXXRecordDecl *FieldRecDecl = cast<CXXRecordDecl>(FieldType->getDecl()); |
| 335 | Sema::SpecialMemberOverloadResult *SMOR = |
| 336 | LookupSpecialMember(FieldRecDecl, CSM, |
| 337 | /* ConstArg */ ConstRHS && !F->isMutable(), |
| 338 | /* VolatileArg */ false, |
| 339 | /* RValueThis */ false, |
| 340 | /* ConstThis */ false, |
| 341 | /* VolatileThis */ false); |
| 342 | |
| 343 | if (!SMOR || !SMOR->getMethod()) { |
| 344 | continue; |
| 345 | } |
| 346 | |
| 347 | CUDAFunctionTarget FieldMethodTarget = |
| 348 | IdentifyCUDATarget(SMOR->getMethod()); |
| 349 | if (!InferredTarget.hasValue()) { |
| 350 | InferredTarget = FieldMethodTarget; |
| 351 | } else { |
| 352 | bool ResolutionError = resolveCalleeCUDATargetConflict( |
| 353 | InferredTarget.getValue(), FieldMethodTarget, |
| 354 | InferredTarget.getPointer()); |
| 355 | if (ResolutionError) { |
| 356 | if (Diagnose) { |
| 357 | Diag(ClassDecl->getLocation(), |
| 358 | diag::note_implicit_member_target_infer_collision) |
| 359 | << (unsigned)CSM << InferredTarget.getValue() |
| 360 | << FieldMethodTarget; |
| 361 | } |
| 362 | MemberDecl->addAttr(CUDAInvalidTargetAttr::CreateImplicit(Context)); |
| 363 | return true; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (InferredTarget.hasValue()) { |
| 369 | if (InferredTarget.getValue() == CFT_Device) { |
| 370 | MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context)); |
| 371 | } else if (InferredTarget.getValue() == CFT_Host) { |
| 372 | MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context)); |
| 373 | } else { |
| 374 | MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context)); |
| 375 | MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context)); |
| 376 | } |
| 377 | } else { |
| 378 | // If no target was inferred, mark this member as __host__ __device__; |
| 379 | // it's the least restrictive option that can be invoked from any target. |
| 380 | MemberDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context)); |
| 381 | MemberDecl->addAttr(CUDAHostAttr::CreateImplicit(Context)); |
| 382 | } |
| 383 | |
| 384 | return false; |
| 385 | } |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 386 | |
| 387 | bool Sema::isEmptyCudaConstructor(SourceLocation Loc, CXXConstructorDecl *CD) { |
| 388 | if (!CD->isDefined() && CD->isTemplateInstantiation()) |
| 389 | InstantiateFunctionDefinition(Loc, CD->getFirstDecl()); |
| 390 | |
| 391 | // (E.2.3.1, CUDA 7.5) A constructor for a class type is considered |
| 392 | // empty at a point in the translation unit, if it is either a |
| 393 | // trivial constructor |
| 394 | if (CD->isTrivial()) |
| 395 | return true; |
| 396 | |
| 397 | // ... or it satisfies all of the following conditions: |
| 398 | // The constructor function has been defined. |
| 399 | // The constructor function has no parameters, |
| 400 | // and the function body is an empty compound statement. |
| 401 | if (!(CD->hasTrivialBody() && CD->getNumParams() == 0)) |
| 402 | return false; |
| 403 | |
| 404 | // Its class has no virtual functions and no virtual base classes. |
| 405 | if (CD->getParent()->isDynamicClass()) |
| 406 | return false; |
| 407 | |
| 408 | // The only form of initializer allowed is an empty constructor. |
Artem Belevich | 3650bbe | 2016-05-19 20:13:53 +0000 | [diff] [blame] | 409 | // This will recursively check all base classes and member initializers |
Artem Belevich | 97c01c3 | 2016-02-02 22:29:48 +0000 | [diff] [blame] | 410 | if (!llvm::all_of(CD->inits(), [&](const CXXCtorInitializer *CI) { |
| 411 | if (const CXXConstructExpr *CE = |
| 412 | dyn_cast<CXXConstructExpr>(CI->getInit())) |
| 413 | return isEmptyCudaConstructor(Loc, CE->getConstructor()); |
| 414 | return false; |
| 415 | })) |
| 416 | return false; |
| 417 | |
| 418 | return true; |
| 419 | } |
Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 420 | |
Artem Belevich | 3650bbe | 2016-05-19 20:13:53 +0000 | [diff] [blame] | 421 | bool Sema::isEmptyCudaDestructor(SourceLocation Loc, CXXDestructorDecl *DD) { |
| 422 | // No destructor -> no problem. |
| 423 | if (!DD) |
| 424 | return true; |
| 425 | |
| 426 | if (!DD->isDefined() && DD->isTemplateInstantiation()) |
| 427 | InstantiateFunctionDefinition(Loc, DD->getFirstDecl()); |
| 428 | |
| 429 | // (E.2.3.1, CUDA 7.5) A destructor for a class type is considered |
| 430 | // empty at a point in the translation unit, if it is either a |
| 431 | // trivial constructor |
| 432 | if (DD->isTrivial()) |
| 433 | return true; |
| 434 | |
| 435 | // ... or it satisfies all of the following conditions: |
| 436 | // The destructor function has been defined. |
| 437 | // and the function body is an empty compound statement. |
| 438 | if (!DD->hasTrivialBody()) |
| 439 | return false; |
| 440 | |
| 441 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 442 | |
| 443 | // Its class has no virtual functions and no virtual base classes. |
| 444 | if (ClassDecl->isDynamicClass()) |
| 445 | return false; |
| 446 | |
| 447 | // Only empty destructors are allowed. This will recursively check |
| 448 | // destructors for all base classes... |
| 449 | if (!llvm::all_of(ClassDecl->bases(), [&](const CXXBaseSpecifier &BS) { |
| 450 | if (CXXRecordDecl *RD = BS.getType()->getAsCXXRecordDecl()) |
| 451 | return isEmptyCudaDestructor(Loc, RD->getDestructor()); |
| 452 | return true; |
| 453 | })) |
| 454 | return false; |
| 455 | |
| 456 | // ... and member fields. |
| 457 | if (!llvm::all_of(ClassDecl->fields(), [&](const FieldDecl *Field) { |
| 458 | if (CXXRecordDecl *RD = Field->getType() |
| 459 | ->getBaseElementTypeUnsafe() |
| 460 | ->getAsCXXRecordDecl()) |
| 461 | return isEmptyCudaDestructor(Loc, RD->getDestructor()); |
| 462 | return true; |
| 463 | })) |
| 464 | return false; |
| 465 | |
| 466 | return true; |
| 467 | } |
| 468 | |
Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 469 | // With -fcuda-host-device-constexpr, an unattributed constexpr function is |
| 470 | // treated as implicitly __host__ __device__, unless: |
| 471 | // * it is a variadic function (device-side variadic functions are not |
| 472 | // allowed), or |
| 473 | // * a __device__ function with this signature was already declared, in which |
| 474 | // case in which case we output an error, unless the __device__ decl is in a |
| 475 | // system header, in which case we leave the constexpr function unattributed. |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 476 | // |
| 477 | // In addition, all function decls are treated as __host__ __device__ when |
| 478 | // ForceCUDAHostDeviceDepth > 0 (corresponding to code within a |
| 479 | // #pragma clang force_cuda_host_device_begin/end |
| 480 | // pair). |
Artem Belevich | 9fb40e3 | 2016-10-21 17:15:46 +0000 | [diff] [blame] | 481 | void Sema::maybeAddCUDAHostDeviceAttrs(FunctionDecl *NewD, |
Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 482 | const LookupResult &Previous) { |
Justin Lebar | 9d4ed26 | 2016-09-30 23:57:38 +0000 | [diff] [blame] | 483 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
Justin Lebar | 67a78a6 | 2016-10-08 22:15:58 +0000 | [diff] [blame] | 484 | |
| 485 | if (ForceCUDAHostDeviceDepth > 0) { |
| 486 | if (!NewD->hasAttr<CUDAHostAttr>()) |
| 487 | NewD->addAttr(CUDAHostAttr::CreateImplicit(Context)); |
| 488 | if (!NewD->hasAttr<CUDADeviceAttr>()) |
| 489 | NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context)); |
| 490 | return; |
| 491 | } |
| 492 | |
Justin Lebar | ba122ab | 2016-03-30 23:30:21 +0000 | [diff] [blame] | 493 | if (!getLangOpts().CUDAHostDeviceConstexpr || !NewD->isConstexpr() || |
| 494 | NewD->isVariadic() || NewD->hasAttr<CUDAHostAttr>() || |
| 495 | NewD->hasAttr<CUDADeviceAttr>() || NewD->hasAttr<CUDAGlobalAttr>()) |
| 496 | return; |
| 497 | |
| 498 | // Is D a __device__ function with the same signature as NewD, ignoring CUDA |
| 499 | // attributes? |
| 500 | auto IsMatchingDeviceFn = [&](NamedDecl *D) { |
| 501 | if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(D)) |
| 502 | D = Using->getTargetDecl(); |
| 503 | FunctionDecl *OldD = D->getAsFunction(); |
| 504 | return OldD && OldD->hasAttr<CUDADeviceAttr>() && |
| 505 | !OldD->hasAttr<CUDAHostAttr>() && |
| 506 | !IsOverload(NewD, OldD, /* UseMemberUsingDeclRules = */ false, |
| 507 | /* ConsiderCudaAttrs = */ false); |
| 508 | }; |
| 509 | auto It = llvm::find_if(Previous, IsMatchingDeviceFn); |
| 510 | if (It != Previous.end()) { |
| 511 | // We found a __device__ function with the same name and signature as NewD |
| 512 | // (ignoring CUDA attrs). This is an error unless that function is defined |
| 513 | // in a system header, in which case we simply return without making NewD |
| 514 | // host+device. |
| 515 | NamedDecl *Match = *It; |
| 516 | if (!getSourceManager().isInSystemHeader(Match->getLocation())) { |
| 517 | Diag(NewD->getLocation(), |
| 518 | diag::err_cuda_unattributed_constexpr_cannot_overload_device) |
| 519 | << NewD->getName(); |
| 520 | Diag(Match->getLocation(), |
| 521 | diag::note_cuda_conflicting_device_function_declared_here); |
| 522 | } |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | NewD->addAttr(CUDAHostAttr::CreateImplicit(Context)); |
| 527 | NewD->addAttr(CUDADeviceAttr::CreateImplicit(Context)); |
| 528 | } |
Justin Lebar | 18e2d82 | 2016-08-15 23:00:49 +0000 | [diff] [blame] | 529 | |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 530 | // In CUDA, there are some constructs which may appear in semantically-valid |
| 531 | // code, but trigger errors if we ever generate code for the function in which |
| 532 | // they appear. Essentially every construct you're not allowed to use on the |
| 533 | // device falls into this category, because you are allowed to use these |
| 534 | // constructs in a __host__ __device__ function, but only if that function is |
| 535 | // never codegen'ed on the device. |
| 536 | // |
| 537 | // To handle semantic checking for these constructs, we keep track of the set of |
| 538 | // functions we know will be emitted, either because we could tell a priori that |
| 539 | // they would be emitted, or because they were transitively called by a |
| 540 | // known-emitted function. |
| 541 | // |
| 542 | // We also keep a partial call graph of which not-known-emitted functions call |
| 543 | // which other not-known-emitted functions. |
| 544 | // |
| 545 | // When we see something which is illegal if the current function is emitted |
| 546 | // (usually by way of CUDADiagIfDeviceCode, CUDADiagIfHostCode, or |
| 547 | // CheckCUDACall), we first check if the current function is known-emitted. If |
| 548 | // so, we immediately output the diagnostic. |
| 549 | // |
| 550 | // Otherwise, we "defer" the diagnostic. It sits in Sema::CUDADeferredDiags |
| 551 | // until we discover that the function is known-emitted, at which point we take |
| 552 | // it out of this map and emit the diagnostic. |
| 553 | |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 554 | Sema::CUDADiagBuilder::CUDADiagBuilder(Kind K, SourceLocation Loc, |
| 555 | unsigned DiagID, FunctionDecl *Fn, |
| 556 | Sema &S) |
| 557 | : S(S), Loc(Loc), DiagID(DiagID), Fn(Fn), |
| 558 | ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) { |
| 559 | switch (K) { |
| 560 | case K_Nop: |
| 561 | break; |
| 562 | case K_Immediate: |
| 563 | case K_ImmediateWithCallStack: |
| 564 | ImmediateDiag.emplace(S.Diag(Loc, DiagID)); |
| 565 | break; |
| 566 | case K_Deferred: |
| 567 | assert(Fn && "Must have a function to attach the deferred diag to."); |
| 568 | PartialDiag.emplace(S.PDiag(DiagID)); |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // Print notes showing how we can reach FD starting from an a priori |
| 574 | // known-callable function. |
| 575 | static void EmitCallStackNotes(Sema &S, FunctionDecl *FD) { |
| 576 | auto FnIt = S.CUDAKnownEmittedFns.find(FD); |
| 577 | while (FnIt != S.CUDAKnownEmittedFns.end()) { |
| 578 | DiagnosticBuilder Builder( |
| 579 | S.Diags.Report(FnIt->second.Loc, diag::note_called_by)); |
| 580 | Builder << FnIt->second.FD; |
| 581 | Builder.setForceEmit(); |
| 582 | |
| 583 | FnIt = S.CUDAKnownEmittedFns.find(FnIt->second.FD); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | Sema::CUDADiagBuilder::~CUDADiagBuilder() { |
| 588 | if (ImmediateDiag) { |
| 589 | // Emit our diagnostic and, if it was a warning or error, output a callstack |
| 590 | // if Fn isn't a priori known-emitted. |
| 591 | bool IsWarningOrError = S.getDiagnostics().getDiagnosticLevel( |
| 592 | DiagID, Loc) >= DiagnosticsEngine::Warning; |
| 593 | ImmediateDiag.reset(); // Emit the immediate diag. |
| 594 | if (IsWarningOrError && ShowCallStack) |
| 595 | EmitCallStackNotes(S, Fn); |
| 596 | } else if (PartialDiag) { |
| 597 | assert(ShowCallStack && "Must always show call stack for deferred diags."); |
| 598 | S.CUDADeferredDiags[Fn].push_back({Loc, std::move(*PartialDiag)}); |
| 599 | } |
| 600 | } |
| 601 | |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 602 | // Do we know that we will eventually codegen the given function? |
| 603 | static bool IsKnownEmitted(Sema &S, FunctionDecl *FD) { |
| 604 | // Templates are emitted when they're instantiated. |
| 605 | if (FD->isDependentContext()) |
| 606 | return false; |
| 607 | |
| 608 | // When compiling for device, host functions are never emitted. Similarly, |
| 609 | // when compiling for host, device and global functions are never emitted. |
| 610 | // (Technically, we do emit a host-side stub for global functions, but this |
| 611 | // doesn't count for our purposes here.) |
| 612 | Sema::CUDAFunctionTarget T = S.IdentifyCUDATarget(FD); |
| 613 | if (S.getLangOpts().CUDAIsDevice && T == Sema::CFT_Host) |
| 614 | return false; |
| 615 | if (!S.getLangOpts().CUDAIsDevice && |
| 616 | (T == Sema::CFT_Device || T == Sema::CFT_Global)) |
| 617 | return false; |
| 618 | |
Justin Lebar | 2d56c26 | 2016-11-08 23:45:51 +0000 | [diff] [blame] | 619 | // Check whether this function is externally visible -- if so, it's |
| 620 | // known-emitted. |
| 621 | // |
| 622 | // We have to check the GVA linkage of the function's *definition* -- if we |
| 623 | // only have a declaration, we don't know whether or not the function will be |
| 624 | // emitted, because (say) the definition could include "inline". |
| 625 | FunctionDecl *Def = FD->getDefinition(); |
| 626 | |
| 627 | // We may currently be parsing the body of FD, in which case |
| 628 | // FD->getDefinition() will be null, but we still want to treat FD as though |
| 629 | // it's a definition. |
| 630 | if (!Def && FD->willHaveBody()) |
| 631 | Def = FD; |
| 632 | |
| 633 | if (Def && |
| 634 | !isDiscardableGVALinkage(S.getASTContext().GetGVALinkageForFunction(Def))) |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 635 | return true; |
| 636 | |
| 637 | // Otherwise, the function is known-emitted if it's in our set of |
| 638 | // known-emitted functions. |
| 639 | return S.CUDAKnownEmittedFns.count(FD) > 0; |
| 640 | } |
| 641 | |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 642 | Sema::CUDADiagBuilder Sema::CUDADiagIfDeviceCode(SourceLocation Loc, |
| 643 | unsigned DiagID) { |
| 644 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 645 | CUDADiagBuilder::Kind DiagKind = [&] { |
| 646 | switch (CurrentCUDATarget()) { |
| 647 | case CFT_Global: |
| 648 | case CFT_Device: |
| 649 | return CUDADiagBuilder::K_Immediate; |
| 650 | 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 | if (getLangOpts().CUDAIsDevice) { |
| 655 | return IsKnownEmitted(*this, dyn_cast<FunctionDecl>(CurContext)) |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 656 | ? CUDADiagBuilder::K_ImmediateWithCallStack |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 657 | : CUDADiagBuilder::K_Deferred; |
| 658 | } |
| 659 | return CUDADiagBuilder::K_Nop; |
| 660 | |
| 661 | default: |
| 662 | return CUDADiagBuilder::K_Nop; |
| 663 | } |
| 664 | }(); |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 665 | return CUDADiagBuilder(DiagKind, Loc, DiagID, |
| 666 | dyn_cast<FunctionDecl>(CurContext), *this); |
| 667 | } |
| 668 | |
| 669 | Sema::CUDADiagBuilder Sema::CUDADiagIfHostCode(SourceLocation Loc, |
| 670 | unsigned DiagID) { |
| 671 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 672 | CUDADiagBuilder::Kind DiagKind = [&] { |
| 673 | switch (CurrentCUDATarget()) { |
| 674 | case CFT_Host: |
| 675 | return CUDADiagBuilder::K_Immediate; |
| 676 | case CFT_HostDevice: |
| 677 | // An HD function counts as host code if we're compiling for host, and |
| 678 | // device code if we're compiling for device. Defer any errors in device |
| 679 | // mode until the function is known-emitted. |
| 680 | if (getLangOpts().CUDAIsDevice) |
| 681 | return CUDADiagBuilder::K_Nop; |
| 682 | |
| 683 | return IsKnownEmitted(*this, dyn_cast<FunctionDecl>(CurContext)) |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 684 | ? CUDADiagBuilder::K_ImmediateWithCallStack |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 685 | : CUDADiagBuilder::K_Deferred; |
| 686 | default: |
| 687 | return CUDADiagBuilder::K_Nop; |
| 688 | } |
| 689 | }(); |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 690 | return CUDADiagBuilder(DiagKind, Loc, DiagID, |
| 691 | dyn_cast<FunctionDecl>(CurContext), *this); |
| 692 | } |
| 693 | |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 694 | // Emit any deferred diagnostics for FD and erase them from the map in which |
| 695 | // they're stored. |
| 696 | static void EmitDeferredDiags(Sema &S, FunctionDecl *FD) { |
| 697 | auto It = S.CUDADeferredDiags.find(FD); |
| 698 | if (It == S.CUDADeferredDiags.end()) |
| 699 | return; |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 700 | bool HasWarningOrError = false; |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 701 | for (PartialDiagnosticAt &PDAt : It->second) { |
| 702 | const SourceLocation &Loc = PDAt.first; |
| 703 | const PartialDiagnostic &PD = PDAt.second; |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 704 | HasWarningOrError |= S.getDiagnostics().getDiagnosticLevel( |
| 705 | PD.getDiagID(), Loc) >= DiagnosticsEngine::Warning; |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 706 | DiagnosticBuilder Builder(S.Diags.Report(Loc, PD.getDiagID())); |
| 707 | Builder.setForceEmit(); |
| 708 | PD.Emit(Builder); |
| 709 | } |
| 710 | S.CUDADeferredDiags.erase(It); |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 711 | |
| 712 | // FIXME: Should this be called after every warning/error emitted in the loop |
| 713 | // above, instead of just once per function? That would be consistent with |
| 714 | // how we handle immediate errors, but it also seems like a bit much. |
| 715 | if (HasWarningOrError) |
| 716 | EmitCallStackNotes(S, FD); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | // Indicate that this function (and thus everything it transtively calls) will |
| 720 | // be codegen'ed, and emit any deferred diagnostics on this function and its |
| 721 | // (transitive) callees. |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 722 | static void MarkKnownEmitted(Sema &S, FunctionDecl *OrigCaller, |
| 723 | FunctionDecl *OrigCallee, SourceLocation OrigLoc) { |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 724 | // Nothing to do if we already know that FD is emitted. |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 725 | if (IsKnownEmitted(S, OrigCallee)) { |
| 726 | assert(!S.CUDACallGraph.count(OrigCallee)); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 730 | // We've just discovered that OrigCallee is known-emitted. Walk our call |
| 731 | // graph to see what else we can now discover also must be emitted. |
| 732 | |
| 733 | struct CallInfo { |
| 734 | FunctionDecl *Caller; |
| 735 | FunctionDecl *Callee; |
| 736 | SourceLocation Loc; |
| 737 | }; |
| 738 | llvm::SmallVector<CallInfo, 4> Worklist = {{OrigCaller, OrigCallee, OrigLoc}}; |
| 739 | llvm::SmallSet<CanonicalDeclPtr<FunctionDecl>, 4> Seen; |
| 740 | Seen.insert(OrigCallee); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 741 | while (!Worklist.empty()) { |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 742 | CallInfo C = Worklist.pop_back_val(); |
| 743 | assert(!IsKnownEmitted(S, C.Callee) && |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 744 | "Worklist should not contain known-emitted functions."); |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 745 | S.CUDAKnownEmittedFns[C.Callee] = {C.Caller, C.Loc}; |
| 746 | EmitDeferredDiags(S, C.Callee); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 747 | |
Justin Lebar | d692dfb | 2016-10-17 02:25:55 +0000 | [diff] [blame] | 748 | // If this is a template instantiation, explore its callgraph as well: |
| 749 | // Non-dependent calls are part of the template's callgraph, while dependent |
| 750 | // calls are part of to the instantiation's call graph. |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 751 | if (auto *Templ = C.Callee->getPrimaryTemplate()) { |
Justin Lebar | d692dfb | 2016-10-17 02:25:55 +0000 | [diff] [blame] | 752 | FunctionDecl *TemplFD = Templ->getAsFunction(); |
| 753 | if (!Seen.count(TemplFD) && !S.CUDAKnownEmittedFns.count(TemplFD)) { |
| 754 | Seen.insert(TemplFD); |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 755 | Worklist.push_back( |
| 756 | {/* Caller = */ C.Caller, /* Callee = */ TemplFD, C.Loc}); |
Justin Lebar | d692dfb | 2016-10-17 02:25:55 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 759 | |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 760 | // Add all functions called by Callee to our worklist. |
| 761 | auto CGIt = S.CUDACallGraph.find(C.Callee); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 762 | if (CGIt == S.CUDACallGraph.end()) |
| 763 | continue; |
| 764 | |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 765 | for (std::pair<CanonicalDeclPtr<FunctionDecl>, SourceLocation> FDLoc : |
| 766 | CGIt->second) { |
| 767 | FunctionDecl *NewCallee = FDLoc.first; |
| 768 | SourceLocation CallLoc = FDLoc.second; |
| 769 | if (Seen.count(NewCallee) || IsKnownEmitted(S, NewCallee)) |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 770 | continue; |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 771 | Seen.insert(NewCallee); |
| 772 | Worklist.push_back( |
| 773 | {/* Caller = */ C.Callee, /* Callee = */ NewCallee, CallLoc}); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 776 | // C.Callee is now known-emitted, so we no longer need to maintain its list |
| 777 | // of callees in CUDACallGraph. |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 778 | S.CUDACallGraph.erase(CGIt); |
| 779 | } |
| 780 | } |
| 781 | |
Justin Lebar | 18e2d82 | 2016-08-15 23:00:49 +0000 | [diff] [blame] | 782 | bool Sema::CheckCUDACall(SourceLocation Loc, FunctionDecl *Callee) { |
Justin Lebar | 9d4ed26 | 2016-09-30 23:57:38 +0000 | [diff] [blame] | 783 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
Justin Lebar | 18e2d82 | 2016-08-15 23:00:49 +0000 | [diff] [blame] | 784 | assert(Callee && "Callee may not be null."); |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 785 | // FIXME: Is bailing out early correct here? Should we instead assume that |
| 786 | // the caller is a global initializer? |
Justin Lebar | 18e2d82 | 2016-08-15 23:00:49 +0000 | [diff] [blame] | 787 | FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext); |
| 788 | if (!Caller) |
| 789 | return true; |
| 790 | |
Justin Lebar | d692dfb | 2016-10-17 02:25:55 +0000 | [diff] [blame] | 791 | // If the caller is known-emitted, mark the callee as known-emitted. |
| 792 | // Otherwise, mark the call in our call graph so we can traverse it later. |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 793 | bool CallerKnownEmitted = IsKnownEmitted(*this, Caller); |
| 794 | if (CallerKnownEmitted) |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 795 | MarkKnownEmitted(*this, Caller, Callee, Loc); |
Justin Lebar | d692dfb | 2016-10-17 02:25:55 +0000 | [diff] [blame] | 796 | else { |
| 797 | // If we have |
| 798 | // host fn calls kernel fn calls host+device, |
| 799 | // the HD function does not get instantiated on the host. We model this by |
| 800 | // omitting at the call to the kernel from the callgraph. This ensures |
| 801 | // that, when compiling for host, only HD functions actually called from the |
| 802 | // host get marked as known-emitted. |
| 803 | if (getLangOpts().CUDAIsDevice || IdentifyCUDATarget(Callee) != CFT_Global) |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 804 | CUDACallGraph[Caller].insert({Callee, Loc}); |
Justin Lebar | d692dfb | 2016-10-17 02:25:55 +0000 | [diff] [blame] | 805 | } |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 806 | |
| 807 | CUDADiagBuilder::Kind DiagKind = [&] { |
| 808 | switch (IdentifyCUDAPreference(Caller, Callee)) { |
| 809 | case CFP_Never: |
| 810 | return CUDADiagBuilder::K_Immediate; |
| 811 | case CFP_WrongSide: |
| 812 | assert(Caller && "WrongSide calls require a non-null caller"); |
| 813 | // If we know the caller will be emitted, we know this wrong-side call |
| 814 | // will be emitted, so it's an immediate error. Otherwise, defer the |
| 815 | // error until we know the caller is emitted. |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 816 | return CallerKnownEmitted ? CUDADiagBuilder::K_ImmediateWithCallStack |
Justin Lebar | 23d9542 | 2016-10-13 20:52:12 +0000 | [diff] [blame] | 817 | : CUDADiagBuilder::K_Deferred; |
| 818 | default: |
| 819 | return CUDADiagBuilder::K_Nop; |
| 820 | } |
| 821 | }(); |
Justin Lebar | 9fdb46e | 2016-10-08 01:07:11 +0000 | [diff] [blame] | 822 | |
Justin Lebar | 9730ae9 | 2016-10-19 21:03:38 +0000 | [diff] [blame] | 823 | if (DiagKind == CUDADiagBuilder::K_Nop) |
| 824 | return true; |
| 825 | |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 826 | // Avoid emitting this error twice for the same location. Using a hashtable |
| 827 | // like this is unfortunate, but because we must continue parsing as normal |
| 828 | // after encountering a deferred error, it's otherwise very tricky for us to |
| 829 | // ensure that we only emit this deferred error once. |
Justin Lebar | 6f72737 | 2016-10-21 20:08:52 +0000 | [diff] [blame] | 830 | if (!LocsWithCUDACallDiags.insert({Caller, Loc}).second) |
Justin Lebar | 18e2d82 | 2016-08-15 23:00:49 +0000 | [diff] [blame] | 831 | return true; |
Justin Lebar | 2a8db34 | 2016-09-28 22:45:54 +0000 | [diff] [blame] | 832 | |
Justin Lebar | 9730ae9 | 2016-10-19 21:03:38 +0000 | [diff] [blame] | 833 | CUDADiagBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller, *this) |
Justin Lebar | 179bdce | 2016-10-13 18:45:08 +0000 | [diff] [blame] | 834 | << IdentifyCUDATarget(Callee) << Callee << IdentifyCUDATarget(Caller); |
| 835 | CUDADiagBuilder(DiagKind, Callee->getLocation(), diag::note_previous_decl, |
| 836 | Caller, *this) |
| 837 | << Callee; |
Justin Lebar | 6c86e91 | 2016-10-19 21:15:01 +0000 | [diff] [blame] | 838 | return DiagKind != CUDADiagBuilder::K_Immediate && |
| 839 | DiagKind != CUDADiagBuilder::K_ImmediateWithCallStack; |
Justin Lebar | b17840d | 2016-09-28 22:45:58 +0000 | [diff] [blame] | 840 | } |
Justin Lebar | 7ca116c | 2016-09-30 17:14:53 +0000 | [diff] [blame] | 841 | |
| 842 | void Sema::CUDASetLambdaAttrs(CXXMethodDecl *Method) { |
Justin Lebar | 9d4ed26 | 2016-09-30 23:57:38 +0000 | [diff] [blame] | 843 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
Justin Lebar | 7ca116c | 2016-09-30 17:14:53 +0000 | [diff] [blame] | 844 | if (Method->hasAttr<CUDAHostAttr>() || Method->hasAttr<CUDADeviceAttr>()) |
| 845 | return; |
| 846 | FunctionDecl *CurFn = dyn_cast<FunctionDecl>(CurContext); |
| 847 | if (!CurFn) |
| 848 | return; |
| 849 | CUDAFunctionTarget Target = IdentifyCUDATarget(CurFn); |
| 850 | if (Target == CFT_Global || Target == CFT_Device) { |
| 851 | Method->addAttr(CUDADeviceAttr::CreateImplicit(Context)); |
| 852 | } else if (Target == CFT_HostDevice) { |
| 853 | Method->addAttr(CUDADeviceAttr::CreateImplicit(Context)); |
| 854 | Method->addAttr(CUDAHostAttr::CreateImplicit(Context)); |
| 855 | } |
Justin Lebar | 7ca116c | 2016-09-30 17:14:53 +0000 | [diff] [blame] | 856 | } |
Artem Belevich | 13e9b4d | 2016-12-07 19:27:16 +0000 | [diff] [blame^] | 857 | |
| 858 | void Sema::checkCUDATargetOverload(FunctionDecl *NewFD, |
| 859 | LookupResult &Previous) { |
| 860 | assert(getLangOpts().CUDA && "Should only be called during CUDA compilation"); |
| 861 | CUDAFunctionTarget NewTarget = IdentifyCUDATarget(NewFD); |
| 862 | for (NamedDecl *OldND : Previous) { |
| 863 | FunctionDecl *OldFD = OldND->getAsFunction(); |
| 864 | if (!OldFD) |
| 865 | continue; |
| 866 | |
| 867 | CUDAFunctionTarget OldTarget = IdentifyCUDATarget(OldFD); |
| 868 | // Don't allow HD and global functions to overload other functions with the |
| 869 | // same signature. We allow overloading based on CUDA attributes so that |
| 870 | // functions can have different implementations on the host and device, but |
| 871 | // HD/global functions "exist" in some sense on both the host and device, so |
| 872 | // should have the same implementation on both sides. |
| 873 | if (NewTarget != OldTarget && |
| 874 | ((NewTarget == CFT_HostDevice) || (OldTarget == CFT_HostDevice) || |
| 875 | (NewTarget == CFT_Global) || (OldTarget == CFT_Global)) && |
| 876 | !IsOverload(NewFD, OldFD, /* UseMemberUsingDeclRules = */ false, |
| 877 | /* ConsiderCudaAttrs = */ false)) { |
| 878 | Diag(NewFD->getLocation(), diag::err_cuda_ovl_target) |
| 879 | << NewTarget << NewFD->getDeclName() << OldTarget << OldFD; |
| 880 | Diag(OldFD->getLocation(), diag::note_previous_declaration); |
| 881 | NewFD->setInvalidDecl(); |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | } |