@@ -5204,25 +5204,55 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
52045204static void handleDeviceKernelAttr (Sema &S, Decl *D, const ParsedAttr &AL) {
52055205 const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
52065206 bool IsFunctionTemplate = FD && FD->getDescribedFunctionTemplate ();
5207- if (S.getLangOpts ().SYCLIsDevice ) {
5207+ llvm::Triple Triple = S.getASTContext ().getTargetInfo ().getTriple ();
5208+ const LangOptions &LangOpts = S.getLangOpts ();
5209+
5210+ if (LangOpts.SYCLIsDevice ) {
52085211 if (!IsFunctionTemplate) {
52095212 S.Diag (AL.getLoc (), diag::warn_attribute_wrong_decl_type_str)
52105213 << AL << AL.isRegularKeywordAttribute () << " function templates" ;
5214+ AL.setInvalid ();
5215+ return ;
52115216 } else {
52125217 S.SYCL ().handleKernelAttr (D, AL);
52135218 }
52145219 } else if (DeviceKernelAttr::isSYCLSpelling (AL)) {
52155220 S.Diag (AL.getLoc (), diag::warn_attribute_ignored) << AL;
5216- } else if (S.getASTContext ().getTargetInfo ().getTriple ().isNVPTX ()) {
5221+ AL.setInvalid ();
5222+
5223+ return ;
5224+ } else if (Triple.isNVPTX ()) {
52175225 handleGlobalAttr (S, D, AL);
52185226 } else {
52195227 // OpenCL C++ will throw a more specific error.
5220- if (!S. getLangOpts () .OpenCLCPlusPlus && (!FD || IsFunctionTemplate)) {
5228+ if (!LangOpts .OpenCLCPlusPlus && (!FD || IsFunctionTemplate)) {
52215229 S.Diag (AL.getLoc (), diag::err_attribute_wrong_decl_type_str)
52225230 << AL << AL.isRegularKeywordAttribute () << " functions" ;
5231+ AL.setInvalid ();
5232+ return ;
52235233 }
52245234 handleSimpleAttribute<DeviceKernelAttr>(S, D, AL);
52255235 }
5236+ // TODO: isGPU() should probably return true for SPIR.
5237+ bool TargetDeviceEnvironment = Triple.isGPU () || Triple.isSPIR () ||
5238+ LangOpts.isTargetDevice () || LangOpts.OpenCL ;
5239+ bool IsAMDGPUMismatch =
5240+ DeviceKernelAttr::isAMDGPUSpelling (AL) && !Triple.isAMDGPU ();
5241+ bool IsNVPTXMismatch =
5242+ DeviceKernelAttr::isNVPTXSpelling (AL) && !Triple.isNVPTX ();
5243+ if (IsAMDGPUMismatch || IsNVPTXMismatch || !TargetDeviceEnvironment) {
5244+ // While both are just different spellings of the same underlying
5245+ // attribute, it makes more sense to the user if amdgpu_kernel can only
5246+ // be used on AMDGPU and the equivalent for NVPTX, so warn and ignore
5247+ // the attribute if there's a mismatch.
5248+ // Also warn if this is not an environment where a device kernel makes
5249+ // sense.
5250+ S.Diag (AL.getLoc (), diag::warn_cconv_unsupported)
5251+ << AL << (int )Sema::CallingConventionIgnoredReason::ForThisTarget;
5252+ AL.setInvalid ();
5253+ return ;
5254+ }
5255+
52265256 // Make sure we validate the CC with the target
52275257 // and warn/error if necessary.
52285258 handleCallConvAttr (S, D, AL);
0 commit comments