From f6b1d84e4a5439fab95d55a54e1993e4d1dfe92c Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Mon, 30 Jun 2025 05:47:55 -0700 Subject: [PATCH 1/6] [SYCL][CodeGen] Directly disable kernel stub for SYCL (NFC) The way 1ce380335dc2a56f499afb44cba7b033c4118945 and bdd5232e9b03cd912044a486dfb88ff9e5d8744e disabled the kernel stub for SYCL is weird, returning from `TargetCodeGenInfo::setOCLKernelStubCallingConvention()` in SYCL mode. Basically we were actually emitting code for the stub would but we customized it to not change its calling convention. Instead, add `getLangOpts().OpenCL` checks to `CodeGenFunction::GenerateCode` (to emit the kernel contents directly into the kernel) and to `CodeGenModule::EmitGlobal` (to disable the deferred emission of the stub). With this change, we should never come across `KernelReferenceKind::Stub` in SYCL mode. --- clang/lib/CodeGen/CodeGenFunction.cpp | 3 ++- clang/lib/CodeGen/CodeGenModule.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 30748d694cd09..2b7a59f9cee88 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1932,7 +1932,8 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, // Implicit copy-assignment gets the same special treatment as implicit // copy-constructors. emitImplicitAssignmentOperatorBody(Args); - } else if (FD->hasAttr() && + } else if (getLangOpts().OpenCL && + FD->hasAttr() && GD.getKernelReferenceKind() == KernelReferenceKind::Kernel) { CallArgList CallArgs; for (unsigned i = 0; i < Args.size(); ++i) { diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 9478b836e1fd0..43d3d7872e1d4 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -4341,7 +4341,8 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) { // Ignore declarations, they will be emitted on their first use. if (const auto *FD = dyn_cast(Global)) { - if (FD->hasAttr() && FD->doesThisDeclarationHaveABody()) + if (getLangOpts().OpenCL && FD->hasAttr() && + FD->doesThisDeclarationHaveABody()) addDeferredDeclToEmit(GlobalDecl(FD, KernelReferenceKind::Stub)); // Update deferred annotations with the latest declaration if the function From 7ef59ed0da23639df3c0439e58c948ba914d2474 Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Mon, 30 Jun 2025 06:01:48 -0700 Subject: [PATCH 2/6] Revert "[SYCL] Disable kernel stub for now (#28629)" This reverts commit 1ce380335dc2a56f499afb44cba7b033c4118945. --- clang/lib/CodeGen/Targets/SPIR.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/clang/lib/CodeGen/Targets/SPIR.cpp b/clang/lib/CodeGen/Targets/SPIR.cpp index f5333eb9bb840..4803d2803117f 100644 --- a/clang/lib/CodeGen/Targets/SPIR.cpp +++ b/clang/lib/CodeGen/Targets/SPIR.cpp @@ -394,9 +394,6 @@ void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention( void CommonSPIRTargetCodeGenInfo::setOCLKernelStubCallingConvention( const FunctionType *&FT) const { - // Disable kernel stub for sycl - if (getABIInfo().getContext().getLangOpts().isSYCL()) - return; FT = getABIInfo().getContext().adjustFunctionType( FT, FT->getExtInfo().withCallingConv(CC_SpirFunction)); } From d90e22a45fbe4f4db1382cb29f984ef60cf1a7dc Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Mon, 30 Jun 2025 06:05:31 -0700 Subject: [PATCH 3/6] Revert "[SYCL] Disable OCL kernel stub for NativeCPU as well (#29074)" This reverts commit 2d3018c3ac6bb5dbc0b4250c7262033080856248. --- clang/lib/CodeGen/TargetInfo.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp index f9b4a5dbbe20c..7d176e421ac4e 100644 --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -119,10 +119,6 @@ unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { void TargetCodeGenInfo::setOCLKernelStubCallingConvention( const FunctionType *&FT) const { - - if (getABIInfo().getContext().getLangOpts().SYCLIsNativeCPU) - return; - FT = getABIInfo().getContext().adjustFunctionType( FT, FT->getExtInfo().withCallingConv(CC_C)); } From 0e85cfa9c313db9a5d8ef7f83a1de7e592f24297 Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Mon, 30 Jun 2025 06:05:53 -0700 Subject: [PATCH 4/6] [SYCL][AMDGPU] Revert WA in AMDGPU TCGI setOCLKernelStubCallingConvention --- clang/lib/CodeGen/Targets/AMDGPU.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/clang/lib/CodeGen/Targets/AMDGPU.cpp b/clang/lib/CodeGen/Targets/AMDGPU.cpp index 7275bbfd78478..452b2e6858673 100644 --- a/clang/lib/CodeGen/Targets/AMDGPU.cpp +++ b/clang/lib/CodeGen/Targets/AMDGPU.cpp @@ -305,8 +305,6 @@ class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const override; unsigned getOpenCLKernelCallingConv() const override; - void - setOCLKernelStubCallingConvention(const FunctionType *&FT) const override; llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, llvm::PointerType *T, QualType QT) const override; @@ -437,14 +435,6 @@ unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { return llvm::CallingConv::AMDGPU_KERNEL; } -void AMDGPUTargetCodeGenInfo::setOCLKernelStubCallingConvention( - const FunctionType *&FT) const { - bool IsSYCL = getABIInfo().getContext().getLangOpts().isSYCL(); - FT = getABIInfo().getContext().adjustFunctionType( - FT, - FT->getExtInfo().withCallingConv(!IsSYCL ? CC_C : CC_AMDGPUKernelCall)); -} - // Currently LLVM assumes null pointers always have value 0, // which results in incorrectly transformed IR. Therefore, instead of // emitting null pointers in private and local address spaces, a null From 2c9358a2e71f0a8410e848c38437062e01b189ba Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Mon, 30 Jun 2025 06:17:34 -0700 Subject: [PATCH 5/6] Apply clang-format --- clang/lib/CodeGen/CodeGenFunction.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 2b7a59f9cee88..d01c1dc6e0ab9 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1932,8 +1932,7 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, // Implicit copy-assignment gets the same special treatment as implicit // copy-constructors. emitImplicitAssignmentOperatorBody(Args); - } else if (getLangOpts().OpenCL && - FD->hasAttr() && + } else if (getLangOpts().OpenCL && FD->hasAttr() && GD.getKernelReferenceKind() == KernelReferenceKind::Kernel) { CallArgList CallArgs; for (unsigned i = 0; i < Args.size(); ++i) { From 41f4d2f6dea557e5069c8a35cddd7d56d0600e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Gergely?= Date: Mon, 30 Jun 2025 15:51:32 +0200 Subject: [PATCH 6/6] Update clang/lib/CodeGen/CodeGenModule.cpp --- clang/lib/CodeGen/CodeGenModule.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 43d3d7872e1d4..b2b1b72454f80 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -4341,7 +4341,7 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) { // Ignore declarations, they will be emitted on their first use. if (const auto *FD = dyn_cast(Global)) { - if (getLangOpts().OpenCL && FD->hasAttr() && + if (LangOpts.OpenCL && FD->hasAttr() && FD->doesThisDeclarationHaveABody()) addDeferredDeclToEmit(GlobalDecl(FD, KernelReferenceKind::Stub));