Skip to content

Commit 080a9d4

Browse files
committed
[clang] Simplify device kernel attributes
Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 39beeb8 commit 080a9d4

36 files changed

+213
-136
lines changed

clang/include/clang/AST/GlobalDecl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class GlobalDecl {
164164
}
165165

166166
static KernelReferenceKind getDefaultKernelReference(const FunctionDecl *D) {
167-
return (D->hasAttr<OpenCLKernelAttr>() || D->getLangOpts().CUDAIsDevice)
167+
return (D->hasAttr<DeviceKernelAttr>() || D->getLangOpts().CUDAIsDevice)
168168
? KernelReferenceKind::Kernel
169169
: KernelReferenceKind::Stub;
170170
}

clang/include/clang/Basic/Attr.td

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ def FunctionPointer : SubsetSubject<DeclBase,
190190
"functions pointers">;
191191

192192
def OpenCLKernelFunction
193-
: SubsetSubject<Function, [{S->hasAttr<OpenCLKernelAttr>()}],
194-
"kernel functions">;
193+
: SubsetSubject<Function, [{S->getASTContext().getLangOpts().OpenCL &&
194+
S->hasAttr<DeviceKernelAttr>()}],
195+
"kernel functions">;
195196

196197
// HasFunctionProto is a more strict version of FunctionLike, so it should
197198
// never be specified in a Subjects list along with FunctionLike (due to the
@@ -1498,12 +1499,6 @@ def CUDAGridConstant : InheritableAttr {
14981499
let Documentation = [CUDAGridConstantAttrDocs];
14991500
}
15001501

1501-
def NVPTXKernel : InheritableAttr, TargetSpecificAttr<TargetNVPTX> {
1502-
let Spellings = [Clang<"nvptx_kernel">];
1503-
let Subjects = SubjectList<[Function]>;
1504-
let Documentation = [Undocumented];
1505-
}
1506-
15071502
def HIPManaged : InheritableAttr {
15081503
let Spellings = [GNU<"managed">, Declspec<"__managed__">];
15091504
let Subjects = SubjectList<[Var]>;
@@ -1538,11 +1533,44 @@ def CUDAShared : InheritableAttr {
15381533
}
15391534
def : MutualExclusions<[CUDAConstant, CUDAShared, HIPManaged]>;
15401535

1541-
def SYCLKernel : InheritableAttr {
1542-
let Spellings = [Clang<"sycl_kernel">];
1543-
let Subjects = SubjectList<[FunctionTmpl]>;
1544-
let LangOpts = [SYCLDevice];
1536+
def DeviceKernel : DeclOrTypeAttr {
1537+
let Spellings = [Clang<"device_kernel">, Clang<"sycl_kernel">,
1538+
Clang<"nvptx_kernel">, Clang<"amdgpu_kernel">,
1539+
CustomKeyword<"__kernel">, CustomKeyword<"kernel">];
1540+
let LangOpts = [];
15451541
let Documentation = [SYCLKernelDocs];
1542+
let AdditionalMembers =
1543+
[{
1544+
inline bool isAMDGPUSpelling() const {
1545+
return isAMDGPUSpelling(*this);
1546+
}
1547+
template<typename T>
1548+
static inline bool isAMDGPUSpelling(const T& Attr) {
1549+
return Attr.getAttrName()->getName() == "amdgpu_kernel";
1550+
}
1551+
inline bool isNVPTXSpelling() const {
1552+
return isNVPTXSpelling(*this);
1553+
}
1554+
template<typename T>
1555+
static inline bool isNVPTXSpelling(const T& Attr) {
1556+
return Attr.getAttrName()->getName() == "nvptx_kernel";
1557+
}
1558+
inline bool isOpenCLSpelling() const {
1559+
return isOpenCLSpelling(*this);
1560+
}
1561+
template<typename T>
1562+
static inline bool isOpenCLSpelling(const T& Attr) {
1563+
return Attr.getAttrName()->getName() == "kernel" ||
1564+
Attr.getAttrName()->getName() == "__kernel";
1565+
}
1566+
inline bool isSYCLSpelling() const {
1567+
return isSYCLSpelling(*this);
1568+
}
1569+
template<typename T>
1570+
static inline bool isSYCLSpelling(const T& Attr) {
1571+
return Attr.getAttrName()->getName() == "sycl_kernel";
1572+
}
1573+
}];
15461574
}
15471575

15481576
def SYCLKernelEntryPoint : InheritableAttr {
@@ -1608,15 +1636,6 @@ def Allocating : TypeAttr {
16081636
let Documentation = [AllocatingDocs];
16091637
}
16101638

1611-
// Similar to CUDA, OpenCL attributes do not receive a [[]] spelling because
1612-
// the specification does not expose them with one currently.
1613-
def OpenCLKernel : InheritableAttr {
1614-
let Spellings = [CustomKeyword<"__kernel">, CustomKeyword<"kernel">];
1615-
let Subjects = SubjectList<[Function], ErrorDiag>;
1616-
let Documentation = [Undocumented];
1617-
let SimpleHandler = 1;
1618-
}
1619-
16201639
def OpenCLUnrollHint : StmtAttr {
16211640
let Spellings = [GNU<"opencl_unroll_hint">];
16221641
let Subjects = SubjectList<[ForStmt, CXXForRangeStmt, WhileStmt, DoStmt],
@@ -2351,11 +2370,6 @@ def AMDGPUMaxNumWorkGroups : InheritableAttr {
23512370
let Subjects = SubjectList<[Function], ErrorDiag, "kernel functions">;
23522371
}
23532372

2354-
def AMDGPUKernelCall : DeclOrTypeAttr {
2355-
let Spellings = [Clang<"amdgpu_kernel">];
2356-
let Documentation = [Undocumented];
2357-
}
2358-
23592373
def BPFPreserveAccessIndex : InheritableAttr,
23602374
TargetSpecificAttr<TargetBPF> {
23612375
let Spellings = [Clang<"preserve_access_index">];

clang/include/clang/Basic/Specifiers.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,13 @@ namespace clang {
289289
CC_AAPCS_VFP, // __attribute__((pcs("aapcs-vfp")))
290290
CC_IntelOclBicc, // __attribute__((intel_ocl_bicc))
291291
CC_SpirFunction, // default for OpenCL functions on SPIR target
292-
CC_OpenCLKernel, // inferred for OpenCL kernels
292+
CC_DeviceKernel, // __attribute__((device_kernel))
293293
CC_Swift, // __attribute__((swiftcall))
294294
CC_SwiftAsync, // __attribute__((swiftasynccall))
295295
CC_PreserveMost, // __attribute__((preserve_most))
296296
CC_PreserveAll, // __attribute__((preserve_all))
297297
CC_AArch64VectorCall, // __attribute__((aarch64_vector_pcs))
298298
CC_AArch64SVEPCS, // __attribute__((aarch64_sve_pcs))
299-
CC_AMDGPUKernelCall, // __attribute__((amdgpu_kernel))
300299
CC_M68kRTD, // __attribute__((m68k_rtd))
301300
CC_PreserveNone, // __attribute__((preserve_none))
302301
CC_RISCVVectorCall, // __attribute__((riscv_vector_cc))
@@ -326,7 +325,7 @@ namespace clang {
326325
case CC_X86Pascal:
327326
case CC_X86VectorCall:
328327
case CC_SpirFunction:
329-
case CC_OpenCLKernel:
328+
case CC_DeviceKernel:
330329
case CC_Swift:
331330
case CC_SwiftAsync:
332331
case CC_M68kRTD:

clang/lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,7 +3541,7 @@ bool FunctionDecl::isExternC() const {
35413541
}
35423542

35433543
bool FunctionDecl::isInExternCContext() const {
3544-
if (hasAttr<OpenCLKernelAttr>())
3544+
if (hasAttr<DeviceKernelAttr>() && getASTContext().getLangOpts().OpenCL)
35453545
return true;
35463546
return getLexicalDeclContext()->isExternCContext();
35473547
}
@@ -5512,7 +5512,7 @@ FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) {
55125512
}
55135513

55145514
bool FunctionDecl::isReferenceableKernel() const {
5515-
return hasAttr<CUDAGlobalAttr>() || hasAttr<OpenCLKernelAttr>();
5515+
return hasAttr<CUDAGlobalAttr>() || hasAttr<DeviceKernelAttr>();
55165516
}
55175517

55185518
BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,8 @@ void CXXNameMangler::mangleUnqualifiedName(
15561556
FD && FD->hasAttr<CUDAGlobalAttr>() &&
15571557
GD.getKernelReferenceKind() == KernelReferenceKind::Stub;
15581558
bool IsOCLDeviceStub =
1559-
FD && FD->hasAttr<OpenCLKernelAttr>() &&
1559+
getASTContext().getLangOpts().OpenCL && FD &&
1560+
FD->hasAttr<DeviceKernelAttr>() &&
15601561
GD.getKernelReferenceKind() == KernelReferenceKind::Stub;
15611562
if (IsDeviceStub)
15621563
mangleDeviceStubName(II);
@@ -3529,10 +3530,9 @@ StringRef CXXNameMangler::getCallingConvQualifierName(CallingConv CC) {
35293530
case CC_AAPCS_VFP:
35303531
case CC_AArch64VectorCall:
35313532
case CC_AArch64SVEPCS:
3532-
case CC_AMDGPUKernelCall:
35333533
case CC_IntelOclBicc:
35343534
case CC_SpirFunction:
3535-
case CC_OpenCLKernel:
3535+
case CC_DeviceKernel:
35363536
case CC_PreserveMost:
35373537
case CC_PreserveAll:
35383538
case CC_M68kRTD:

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,8 @@ void MicrosoftCXXNameMangler::mangleUnqualifiedName(GlobalDecl GD,
11641164
->hasAttr<CUDAGlobalAttr>())) &&
11651165
GD.getKernelReferenceKind() == KernelReferenceKind::Stub;
11661166
bool IsOCLDeviceStub =
1167-
ND && isa<FunctionDecl>(ND) && ND->hasAttr<OpenCLKernelAttr>() &&
1167+
getASTContext().getLangOpts().OpenCL && ND &&
1168+
isa<FunctionDecl>(ND) && ND->hasAttr<DeviceKernelAttr>() &&
11681169
GD.getKernelReferenceKind() == KernelReferenceKind::Stub;
11691170
if (IsDeviceStub)
11701171
mangleSourceName(

clang/lib/AST/Type.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3594,14 +3594,12 @@ StringRef FunctionType::getNameForCallConv(CallingConv CC) {
35943594
return "aarch64_vector_pcs";
35953595
case CC_AArch64SVEPCS:
35963596
return "aarch64_sve_pcs";
3597-
case CC_AMDGPUKernelCall:
3598-
return "amdgpu_kernel";
35993597
case CC_IntelOclBicc:
36003598
return "intel_ocl_bicc";
36013599
case CC_SpirFunction:
36023600
return "spir_function";
3603-
case CC_OpenCLKernel:
3604-
return "opencl_kernel";
3601+
case CC_DeviceKernel:
3602+
return "device_kernel";
36053603
case CC_Swift:
36063604
return "swiftcall";
36073605
case CC_SwiftAsync:
@@ -4302,7 +4300,7 @@ bool AttributedType::isCallingConv() const {
43024300
case attr::VectorCall:
43034301
case attr::AArch64VectorPcs:
43044302
case attr::AArch64SVEPcs:
4305-
case attr::AMDGPUKernelCall:
4303+
case attr::DeviceKernel:
43064304
case attr::Pascal:
43074305
case attr::MSABI:
43084306
case attr::SysVABI:

clang/lib/AST/TypePrinter.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,8 @@ void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info,
10961096
case CC_AArch64SVEPCS:
10971097
OS << "__attribute__((aarch64_sve_pcs))";
10981098
break;
1099-
case CC_AMDGPUKernelCall:
1100-
OS << "__attribute__((amdgpu_kernel))";
1099+
case CC_DeviceKernel:
1100+
OS << "__attribute__((device_kernel))";
11011101
break;
11021102
case CC_IntelOclBicc:
11031103
OS << " __attribute__((intel_ocl_bicc))";
@@ -1112,7 +1112,6 @@ void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info,
11121112
OS << " __attribute__((regcall))";
11131113
break;
11141114
case CC_SpirFunction:
1115-
case CC_OpenCLKernel:
11161115
// Do nothing. These CCs are not available as attributes.
11171116
break;
11181117
case CC_Swift:
@@ -2065,7 +2064,9 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
20652064
}
20662065
case attr::AArch64VectorPcs: OS << "aarch64_vector_pcs"; break;
20672066
case attr::AArch64SVEPcs: OS << "aarch64_sve_pcs"; break;
2068-
case attr::AMDGPUKernelCall: OS << "amdgpu_kernel"; break;
2067+
case attr::DeviceKernel:
2068+
OS << T->getAttr()->getSpelling();
2069+
break;
20692070
case attr::IntelOclBicc: OS << "inteloclbicc"; break;
20702071
case attr::PreserveMost:
20712072
OS << "preserve_most";

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ AArch64TargetInfo::checkCallingConvention(CallingConv CC) const {
13411341
case CC_PreserveMost:
13421342
case CC_PreserveAll:
13431343
case CC_PreserveNone:
1344-
case CC_OpenCLKernel:
1344+
case CC_DeviceKernel:
13451345
case CC_AArch64VectorCall:
13461346
case CC_AArch64SVEPCS:
13471347
case CC_Win64:
@@ -1699,7 +1699,7 @@ WindowsARM64TargetInfo::checkCallingConvention(CallingConv CC) const {
16991699
case CC_X86FastCall:
17001700
return CCCR_Ignore;
17011701
case CC_C:
1702-
case CC_OpenCLKernel:
1702+
case CC_DeviceKernel:
17031703
case CC_PreserveMost:
17041704
case CC_PreserveAll:
17051705
case CC_PreserveNone:

clang/lib/Basic/Targets/AMDGPU.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
415415
default:
416416
return CCCR_Warning;
417417
case CC_C:
418-
case CC_OpenCLKernel:
419-
case CC_AMDGPUKernelCall:
418+
case CC_DeviceKernel:
420419
return CCCR_OK;
421420
}
422421
}

clang/lib/Basic/Targets/ARM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ ARMTargetInfo::checkCallingConvention(CallingConv CC) const {
14051405
case CC_AAPCS_VFP:
14061406
case CC_Swift:
14071407
case CC_SwiftAsync:
1408-
case CC_OpenCLKernel:
1408+
case CC_DeviceKernel:
14091409
return CCCR_OK;
14101410
default:
14111411
return CCCR_Warning;
@@ -1480,7 +1480,7 @@ WindowsARMTargetInfo::checkCallingConvention(CallingConv CC) const {
14801480
case CC_X86VectorCall:
14811481
return CCCR_Ignore;
14821482
case CC_C:
1483-
case CC_OpenCLKernel:
1483+
case CC_DeviceKernel:
14841484
case CC_PreserveMost:
14851485
case CC_PreserveAll:
14861486
case CC_Swift:

clang/lib/Basic/Targets/BPF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class LLVM_LIBRARY_VISIBILITY BPFTargetInfo : public TargetInfo {
9494
default:
9595
return CCCR_Warning;
9696
case CC_C:
97-
case CC_OpenCLKernel:
97+
case CC_DeviceKernel:
9898
return CCCR_OK;
9999
}
100100
}

clang/lib/Basic/Targets/Mips.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ WindowsMipsTargetInfo::checkCallingConvention(CallingConv CC) const {
337337
case CC_X86VectorCall:
338338
return CCCR_Ignore;
339339
case CC_C:
340-
case CC_OpenCLKernel:
340+
case CC_DeviceKernel:
341341
case CC_PreserveMost:
342342
case CC_PreserveAll:
343343
case CC_Swift:

clang/lib/Basic/Targets/SPIR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class LLVM_LIBRARY_VISIBILITY BaseSPIRTargetInfo : public TargetInfo {
191191
}
192192

193193
CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
194-
return (CC == CC_SpirFunction || CC == CC_OpenCLKernel) ? CCCR_OK
194+
return (CC == CC_SpirFunction || CC == CC_DeviceKernel) ? CCCR_OK
195195
: CCCR_Warning;
196196
}
197197

clang/lib/Basic/Targets/SystemZ.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class LLVM_LIBRARY_VISIBILITY SystemZTargetInfo : public TargetInfo {
244244
switch (CC) {
245245
case CC_C:
246246
case CC_Swift:
247-
case CC_OpenCLKernel:
247+
case CC_DeviceKernel:
248248
return CCCR_OK;
249249
case CC_SwiftAsync:
250250
return CCCR_Error;

clang/lib/Basic/Targets/X86.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,11 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
408408
case CC_Swift:
409409
case CC_X86Pascal:
410410
case CC_IntelOclBicc:
411-
case CC_OpenCLKernel:
412411
return CCCR_OK;
413412
case CC_SwiftAsync:
414413
return CCCR_Error;
414+
case CC_DeviceKernel:
415+
return IsOpenCL ? CCCR_OK : CCCR_Warning;
415416
default:
416417
return CCCR_Warning;
417418
}
@@ -439,7 +440,13 @@ class LLVM_LIBRARY_VISIBILITY X86TargetInfo : public TargetInfo {
439440
uint64_t getPointerAlignV(LangAS AddrSpace) const override {
440441
return getPointerWidthV(AddrSpace);
441442
}
443+
void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override {
444+
TargetInfo::adjust(Diags, Opts);
445+
IsOpenCL = Opts.OpenCL;
446+
}
442447

448+
private:
449+
bool IsOpenCL = false;
443450
};
444451

445452
// X86-32 generic target
@@ -785,8 +792,9 @@ class LLVM_LIBRARY_VISIBILITY X86_64TargetInfo : public X86TargetInfo {
785792
case CC_PreserveAll:
786793
case CC_PreserveNone:
787794
case CC_X86RegCall:
788-
case CC_OpenCLKernel:
789795
return CCCR_OK;
796+
case CC_DeviceKernel:
797+
return IsOpenCL ? CCCR_OK : CCCR_Warning;
790798
default:
791799
return CCCR_Warning;
792800
}
@@ -817,7 +825,6 @@ class LLVM_LIBRARY_VISIBILITY X86_64TargetInfo : public X86TargetInfo {
817825
return X86TargetInfo::validateGlobalRegisterVariable(RegName, RegSize,
818826
HasSizeMismatch);
819827
}
820-
821828
void setMaxAtomicWidth() override {
822829
if (hasFeature("cx16"))
823830
MaxAtomicInlineWidth = 128;
@@ -829,6 +836,14 @@ class LLVM_LIBRARY_VISIBILITY X86_64TargetInfo : public X86TargetInfo {
829836
size_t getMaxBitIntWidth() const override {
830837
return llvm::IntegerType::MAX_INT_BITS;
831838
}
839+
840+
void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override {
841+
TargetInfo::adjust(Diags, Opts);
842+
IsOpenCL = Opts.OpenCL;
843+
}
844+
845+
private:
846+
bool IsOpenCL = false;
832847
};
833848

834849
// x86-64 UEFI target
@@ -913,7 +928,7 @@ class LLVM_LIBRARY_VISIBILITY WindowsX86_64TargetInfo
913928
case CC_Swift:
914929
case CC_SwiftAsync:
915930
case CC_X86RegCall:
916-
case CC_OpenCLKernel:
931+
case CC_DeviceKernel:
917932
return CCCR_OK;
918933
default:
919934
return CCCR_Warning;

0 commit comments

Comments
 (0)