@@ -5204,25 +5204,55 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5204
5204
static void handleDeviceKernelAttr (Sema &S, Decl *D, const ParsedAttr &AL) {
5205
5205
const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
5206
5206
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 ) {
5208
5211
if (!IsFunctionTemplate) {
5209
5212
S.Diag (AL.getLoc (), diag::warn_attribute_wrong_decl_type_str)
5210
5213
<< AL << AL.isRegularKeywordAttribute () << " function templates" ;
5214
+ AL.setInvalid ();
5215
+ return ;
5211
5216
} else {
5212
5217
S.SYCL ().handleKernelAttr (D, AL);
5213
5218
}
5214
5219
} else if (DeviceKernelAttr::isSYCLSpelling (AL)) {
5215
5220
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 ()) {
5217
5225
handleGlobalAttr (S, D, AL);
5218
5226
} else {
5219
5227
// OpenCL C++ will throw a more specific error.
5220
- if (!S. getLangOpts () .OpenCLCPlusPlus && (!FD || IsFunctionTemplate)) {
5228
+ if (!LangOpts .OpenCLCPlusPlus && (!FD || IsFunctionTemplate)) {
5221
5229
S.Diag (AL.getLoc (), diag::err_attribute_wrong_decl_type_str)
5222
5230
<< AL << AL.isRegularKeywordAttribute () << " functions" ;
5231
+ AL.setInvalid ();
5232
+ return ;
5223
5233
}
5224
5234
handleSimpleAttribute<DeviceKernelAttr>(S, D, AL);
5225
5235
}
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
+
5226
5256
// Make sure we validate the CC with the target
5227
5257
// and warn/error if necessary.
5228
5258
handleCallConvAttr (S, D, AL);
0 commit comments