From 4333cfba6839672c6bebb510f2b42e847a5fa7c5 Mon Sep 17 00:00:00 2001 From: camc Date: Thu, 2 Oct 2025 15:15:35 +0000 Subject: [PATCH 1/3] [clang] Fix device_kernel attribute crash on unsupported targets when not using AMDGPU spelling --- clang/lib/Sema/SemaDeclAttr.cpp | 6 +++++- clang/lib/Sema/SemaType.cpp | 2 +- clang/test/Sema/callingconv.c | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index a8dfa4d7df2d5..979ca03e9867e 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -5204,7 +5204,11 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { static void handleDeviceKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { const auto *FD = dyn_cast_or_null(D); bool IsFunctionTemplate = FD && FD->getDescribedFunctionTemplate(); - if (S.getLangOpts().SYCLIsDevice) { + if (!S.getLangOpts().OpenCL && !S.getLangOpts().CUDA && !DeviceKernelAttr::isAMDGPUSpelling(AL)) { + // This is already diagnosed for AMDGPU in getCCForDeclaratorChunk + S.Diag(AL.getLoc(), diag::warn_cconv_unsupported) + << AL << (int)Sema::CallingConventionIgnoredReason::ForThisTarget; + } else if (S.getLangOpts().SYCLIsDevice) { if (!IsFunctionTemplate) { S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str) << AL << AL.isRegularKeywordAttribute() << "function templates"; diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index bee613aa5f1c5..aaa92b238376c 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -3780,7 +3780,7 @@ static CallingConv getCCForDeclaratorChunk( } } } - if (!S.getLangOpts().isSYCL()) { + if (S.getLangOpts().OpenCL || S.getLangOpts().CUDA) { for (const ParsedAttr &AL : D.getDeclSpec().getAttributes()) { if (AL.getKind() == ParsedAttr::AT_DeviceKernel) { CC = CC_DeviceKernel; diff --git a/clang/test/Sema/callingconv.c b/clang/test/Sema/callingconv.c index f0b8b80a32974..2ea8757526666 100644 --- a/clang/test/Sema/callingconv.c +++ b/clang/test/Sema/callingconv.c @@ -55,6 +55,10 @@ int __attribute__((aarch64_vector_pcs)) aavpcs(void); // expected-warning {{'aar int __attribute__((aarch64_sve_pcs)) aasvepcs(void); // expected-warning {{'aarch64_sve_pcs' calling convention is not supported for this target}} int __attribute__((amdgpu_kernel)) amdgpu_kernel(void); // expected-warning {{'amdgpu_kernel' calling convention is not supported for this target}} +int __attribute__((device_kernel)) device_kernel(void) { // expected-warning {{'device_kernel' calling convention is not supported for this target}} +} +int __attribute__((sycl_kernel)) sycl_kernel(void) { // expected-warning {{'sycl_kernel' calling convention is not supported for this target}} +} // PR6361 void ctest3(); From 624482b6bbbf0d90294f641bb14b9b5b3b038cae Mon Sep 17 00:00:00 2001 From: camc Date: Thu, 2 Oct 2025 15:27:31 +0000 Subject: [PATCH 2/3] add release note --- clang/docs/ReleaseNotes.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index e8deae50e4cb0..69f7341c20372 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -397,6 +397,8 @@ Bug Fixes to Attribute Support - Using ``[[gnu::cleanup(some_func)]]`` where some_func is annotated with ``[[gnu::error("some error")]]`` now correctly triggers an error. (#GH146520) - Fix a crash when the function name is empty in the `swift_name` attribute. (#GH157075) +- Fix a crash and missing diagnostic when using some device_kernel attributes in + unsupported targets. (#GH161077) Bug Fixes to C++ Support ^^^^^^^^^^^^^^^^^^^^^^^^ From e4803bc8791cc067df29021e3da5e21414502bc8 Mon Sep 17 00:00:00 2001 From: c Date: Thu, 2 Oct 2025 15:30:42 +0000 Subject: [PATCH 3/3] formatting --- clang/lib/Sema/SemaDeclAttr.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 979ca03e9867e..6ed7d4d23d918 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -5204,7 +5204,8 @@ static void handleCallConvAttr(Sema &S, Decl *D, const ParsedAttr &AL) { static void handleDeviceKernelAttr(Sema &S, Decl *D, const ParsedAttr &AL) { const auto *FD = dyn_cast_or_null(D); bool IsFunctionTemplate = FD && FD->getDescribedFunctionTemplate(); - if (!S.getLangOpts().OpenCL && !S.getLangOpts().CUDA && !DeviceKernelAttr::isAMDGPUSpelling(AL)) { + if (!S.getLangOpts().OpenCL && !S.getLangOpts().CUDA && + !DeviceKernelAttr::isAMDGPUSpelling(AL)) { // This is already diagnosed for AMDGPU in getCCForDeclaratorChunk S.Diag(AL.getLoc(), diag::warn_cconv_unsupported) << AL << (int)Sema::CallingConventionIgnoredReason::ForThisTarget;