Skip to content

Commit 86d1d6b

Browse files
authored
[clang] Use TargetInfo to determine device kernel calling convention (#144728)
We should abstract this logic away to `TargetInfo`. See #137882 for more information. --------- Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 7aecd7e commit 86d1d6b

File tree

6 files changed

+24
-30
lines changed

6 files changed

+24
-30
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,8 @@ unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {
8383
return llvm::CallingConv::AArch64_SVE_VectorCall;
8484
case CC_SpirFunction:
8585
return llvm::CallingConv::SPIR_FUNC;
86-
case CC_DeviceKernel: {
87-
if (CGM.getLangOpts().OpenCL)
88-
return CGM.getTargetCodeGenInfo().getOpenCLKernelCallingConv();
89-
if (CGM.getTriple().isSPIROrSPIRV())
90-
return llvm::CallingConv::SPIR_KERNEL;
91-
if (CGM.getTriple().isAMDGPU())
92-
return llvm::CallingConv::AMDGPU_KERNEL;
93-
if (CGM.getTriple().isNVPTX())
94-
return llvm::CallingConv::PTX_Kernel;
95-
llvm_unreachable("Unknown kernel calling convention");
96-
}
86+
case CC_DeviceKernel:
87+
return CGM.getTargetCodeGenInfo().getDeviceKernelCallingConv();
9788
case CC_PreserveMost:
9889
return llvm::CallingConv::PreserveMost;
9990
case CC_PreserveAll:

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,21 @@ TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
103103
Opt += Lib;
104104
}
105105

106-
unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
107-
// OpenCL kernels are called via an explicit runtime API with arguments
108-
// set with clSetKernelArg(), not as normal sub-functions.
109-
// Return SPIR_KERNEL by default as the kernel calling convention to
110-
// ensure the fingerprint is fixed such way that each OpenCL argument
111-
// gets one matching argument in the produced kernel function argument
112-
// list to enable feasible implementation of clSetKernelArg() with
113-
// aggregates etc. In case we would use the default C calling conv here,
114-
// clSetKernelArg() might break depending on the target-specific
115-
// conventions; different targets might split structs passed as values
116-
// to multiple function arguments etc.
117-
return llvm::CallingConv::SPIR_KERNEL;
106+
unsigned TargetCodeGenInfo::getDeviceKernelCallingConv() const {
107+
if (getABIInfo().getContext().getLangOpts().OpenCL) {
108+
// Device kernels are called via an explicit runtime API with arguments,
109+
// such as set with clSetKernelArg() for OpenCL, not as normal
110+
// sub-functions. Return SPIR_KERNEL by default as the kernel calling
111+
// convention to ensure the fingerprint is fixed such way that each kernel
112+
// argument gets one matching argument in the produced kernel function
113+
// argument list to enable feasible implementation of clSetKernelArg() with
114+
// aggregates etc. In case we would use the default C calling conv here,
115+
// clSetKernelArg() might break depending on the target-specific
116+
// conventions; different targets might split structs passed as values
117+
// to multiple function arguments etc.
118+
return llvm::CallingConv::SPIR_KERNEL;
119+
}
120+
llvm_unreachable("Unknown kernel calling convention");
118121
}
119122

120123
void TargetCodeGenInfo::setOCLKernelStubCallingConvention(

clang/lib/CodeGen/TargetInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ class TargetCodeGenInfo {
298298
llvm::StringRef Value,
299299
llvm::SmallString<32> &Opt) const {}
300300

301-
/// Get LLVM calling convention for OpenCL kernel.
302-
virtual unsigned getOpenCLKernelCallingConv() const;
301+
/// Get LLVM calling convention for device kernels.
302+
virtual unsigned getDeviceKernelCallingConv() const;
303303

304304
/// Get target specific null pointer.
305305
/// \param T is the LLVM type of the null pointer.

clang/lib/CodeGen/Targets/AMDGPU.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
304304

305305
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
306306
CodeGen::CodeGenModule &M) const override;
307-
unsigned getOpenCLKernelCallingConv() const override;
307+
unsigned getDeviceKernelCallingConv() const override;
308308

309309
llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
310310
llvm::PointerType *T, QualType QT) const override;
@@ -431,7 +431,7 @@ void AMDGPUTargetCodeGenInfo::setTargetAttributes(
431431
F->addFnAttr("amdgpu-ieee", "false");
432432
}
433433

434-
unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
434+
unsigned AMDGPUTargetCodeGenInfo::getDeviceKernelCallingConv() const {
435435
return llvm::CallingConv::AMDGPU_KERNEL;
436436
}
437437

clang/lib/CodeGen/Targets/NVPTX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
7878
return true;
7979
}
8080

81-
unsigned getOpenCLKernelCallingConv() const override {
81+
unsigned getDeviceKernelCallingConv() const override {
8282
return llvm::CallingConv::PTX_Kernel;
8383
}
8484

clang/lib/CodeGen/Targets/SPIR.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
5151
getABIInfo().getDataLayout().getAllocaAddrSpace());
5252
}
5353

54-
unsigned getOpenCLKernelCallingConv() const override;
54+
unsigned getDeviceKernelCallingConv() const override;
5555
llvm::Type *getOpenCLType(CodeGenModule &CGM, const Type *T) const override;
5656
llvm::Type *
5757
getHLSLType(CodeGenModule &CGM, const Type *Ty,
@@ -219,7 +219,7 @@ void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
219219
}
220220
}
221221

222-
unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
222+
unsigned CommonSPIRTargetCodeGenInfo::getDeviceKernelCallingConv() const {
223223
return llvm::CallingConv::SPIR_KERNEL;
224224
}
225225

0 commit comments

Comments
 (0)