Skip to content

Commit be91732

Browse files
committed
[clang] Add spir_kernel attribute
Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 7dad8b9 commit be91732

File tree

14 files changed

+61
-2
lines changed

14 files changed

+61
-2
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ def TargetRISCV : TargetArch<["riscv32", "riscv64"]>;
472472
def TargetX86 : TargetArch<["x86"]>;
473473
def TargetX86_64 : TargetArch<["x86_64"]>;
474474
def TargetAnyX86 : TargetArch<["x86", "x86_64"]>;
475+
def TargetSPIR : TargetArch<["spir", "spir64"]>;
475476
def TargetSPIRV : TargetArch<["spirv", "spirv32", "spirv64"]>;
477+
def TargetAnySPIR : TargetArch<!listconcat(TargetSPIR.Arches, TargetSPIRV.Arches)>;
476478
def TargetWebAssembly : TargetArch<["wasm32", "wasm64"]>;
477479
def TargetNVPTX : TargetArch<["nvptx", "nvptx64"]>;
478480
def TargetWindows : TargetSpec {
@@ -1504,6 +1506,12 @@ def NVPTXKernel : InheritableAttr, TargetSpecificAttr<TargetNVPTX> {
15041506
let Documentation = [Undocumented];
15051507
}
15061508

1509+
def SPIRKernel : InheritableAttr, TargetSpecificAttr<TargetAnySPIR> {
1510+
let Spellings = [Clang<"spir_kernel">];
1511+
let Subjects = SubjectList<[Function]>;
1512+
let Documentation = [Undocumented];
1513+
}
1514+
15071515
def HIPManaged : InheritableAttr {
15081516
let Spellings = [GNU<"managed">, Declspec<"__managed__">];
15091517
let Subjects = SubjectList<[Var]>;

clang/include/clang/Basic/Specifiers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ 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_SpirKernel, // __attribute__((spir_kernel))
292293
CC_OpenCLKernel, // inferred for OpenCL kernels
293294
CC_Swift, // __attribute__((swiftcall))
294295
CC_SwiftAsync, // __attribute__((swiftasynccall))

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3532,6 +3532,7 @@ StringRef CXXNameMangler::getCallingConvQualifierName(CallingConv CC) {
35323532
case CC_AMDGPUKernelCall:
35333533
case CC_IntelOclBicc:
35343534
case CC_SpirFunction:
3535+
case CC_SpirKernel:
35353536
case CC_OpenCLKernel:
35363537
case CC_PreserveMost:
35373538
case CC_PreserveAll:

clang/lib/AST/Type.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,6 +3623,8 @@ StringRef FunctionType::getNameForCallConv(CallingConv CC) {
36233623
return "intel_ocl_bicc";
36243624
case CC_SpirFunction:
36253625
return "spir_function";
3626+
case CC_SpirKernel:
3627+
return "spir_kernel";
36263628
case CC_OpenCLKernel:
36273629
return "opencl_kernel";
36283630
case CC_Swift:

clang/lib/AST/TypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,9 @@ void TypePrinter::printFunctionAfter(const FunctionType::ExtInfo &Info,
11151115
case CC_OpenCLKernel:
11161116
// Do nothing. These CCs are not available as attributes.
11171117
break;
1118+
case CC_SpirKernel:
1119+
OS << " __attribute__((spir_kernel))";
1120+
break;
11181121
case CC_Swift:
11191122
OS << " __attribute__((swiftcall))";
11201123
break;

clang/lib/Basic/Targets/SPIR.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ 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
195-
: CCCR_Warning;
194+
return (CC == CC_SpirKernel || CC == CC_SpirFunction ||
195+
CC == CC_OpenCLKernel)
196+
? CCCR_OK
197+
: CCCR_Warning;
196198
}
197199

198200
CallingConv getDefaultCallingConv() const override {

clang/lib/CodeGen/CGCall.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ unsigned CodeGenTypes::ClangCallConvToLLVMCallConv(CallingConv CC) {
8484
return llvm::CallingConv::AMDGPU_KERNEL;
8585
case CC_SpirFunction:
8686
return llvm::CallingConv::SPIR_FUNC;
87+
case CC_SpirKernel:
88+
return llvm::CallingConv::SPIR_KERNEL;
8789
case CC_OpenCLKernel:
8890
return CGM.getTargetCodeGenInfo().getOpenCLKernelCallingConv();
8991
case CC_PreserveMost:

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,8 @@ static unsigned getDwarfCC(CallingConv CC) {
15891589
return llvm::dwarf::DW_CC_LLVM_IntelOclBicc;
15901590
case CC_SpirFunction:
15911591
return llvm::dwarf::DW_CC_LLVM_SpirFunction;
1592+
case CC_SpirKernel:
1593+
return llvm::dwarf::DW_CC_LLVM_SpirKernel;
15921594
case CC_OpenCLKernel:
15931595
case CC_AMDGPUKernelCall:
15941596
return llvm::dwarf::DW_CC_LLVM_OpenCLKernel;

clang/lib/CodeGen/Targets/SPIR.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
6060
llvm::Type *ElementType, llvm::LLVMContext &Ctx) const;
6161
void
6262
setOCLKernelStubCallingConvention(const FunctionType *&FT) const override;
63+
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
64+
CodeGen::CodeGenModule &M) const override;
6365
};
6466
class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
6567
public:
@@ -238,6 +240,20 @@ void CommonSPIRTargetCodeGenInfo::setOCLKernelStubCallingConvention(
238240
FT, FT->getExtInfo().withCallingConv(CC_SpirFunction));
239241
}
240242

243+
void CommonSPIRTargetCodeGenInfo::setTargetAttributes(
244+
const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
245+
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
246+
if (!FD)
247+
return;
248+
249+
llvm::Function *F = cast<llvm::Function>(GV);
250+
251+
// Attach kernel metadata directly if compiling for SPIR.
252+
if (FD->hasAttr<SPIRKernelAttr>()) {
253+
F->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
254+
}
255+
}
256+
241257
LangAS
242258
SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
243259
const VarDecl *D) const {
@@ -262,6 +278,7 @@ SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
262278

263279
void SPIRVTargetCodeGenInfo::setTargetAttributes(
264280
const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
281+
CommonSPIRTargetCodeGenInfo::setTargetAttributes(D, GV, M);
265282
if (!M.getLangOpts().HIP ||
266283
M.getTarget().getTriple().getVendor() != llvm::Triple::AMD)
267284
return;

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5503,6 +5503,9 @@ bool Sema::CheckCallingConvAttr(const ParsedAttr &Attrs, CallingConv &CC,
55035503
llvm::Log2_64(ABIVLen) - 5);
55045504
break;
55055505
}
5506+
case ParsedAttr::AT_SPIRKernel:
5507+
CC = CC_SpirKernel;
5508+
break;
55065509
default: llvm_unreachable("unexpected attribute kind");
55075510
}
55085511

@@ -7152,6 +7155,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
71527155
case ParsedAttr::AT_CUDALaunchBounds:
71537156
handleLaunchBoundsAttr(S, D, AL);
71547157
break;
7158+
case ParsedAttr::AT_SPIRKernel:
7159+
handleSimpleAttribute<SPIRKernelAttr>(S, D, AL);
7160+
break;
71557161
case ParsedAttr::AT_Restrict:
71567162
handleRestrictAttr(S, D, AL);
71577163
break;

clang/test/Misc/pragma-attribute-supported-attributes-list.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
// CHECK-NEXT: ReturnTypestate (SubjectMatchRule_function, SubjectMatchRule_variable_is_parameter)
182182
// CHECK-NEXT: ReturnsNonNull (SubjectMatchRule_objc_method, SubjectMatchRule_function)
183183
// CHECK-NEXT: ReturnsTwice (SubjectMatchRule_function)
184+
// CHECK-NEXT: SPIRKernel (SubjectMatchRule_function)
184185
// CHECK-NEXT: SYCLKernelEntryPoint (SubjectMatchRule_function)
185186
// CHECK-NEXT: SYCLSpecialClass (SubjectMatchRule_record)
186187
// CHECK-NEXT: ScopedLockable (SubjectMatchRule_record)

clang/test/Misc/spir-kernel-attr.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -triple spir -emit-llvm %s -o - | FileCheck %s
2+
// RUN: %clang_cc1 -triple spir64 -emit-llvm %s -o - | FileCheck %s
3+
// RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -emit-llvm %s -o - | FileCheck %s
4+
// RUN: %clang_cc1 -triple spirv32 -emit-llvm %s -o - | FileCheck %s
5+
// RUN: %clang_cc1 -triple spirv64 -emit-llvm %s -o - | FileCheck %s
6+
__attribute__((spir_kernel)) void foo(void) {}
7+
8+
[[clang::spir_kernel]] void bar(void) {}
9+
10+
// CHECK-COUNT-2: spir_kernel

llvm/include/llvm/BinaryFormat/Dwarf.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ HANDLE_DW_CC(0xcd, LLVM_PreserveNone)
11271127
HANDLE_DW_CC(0xce, LLVM_RISCVVectorCall)
11281128
HANDLE_DW_CC(0xcf, LLVM_SwiftTail)
11291129
HANDLE_DW_CC(0xd0, LLVM_RISCVVLSCall)
1130+
HANDLE_DW_CC(0xd1, LLVM_SpirKernel)
11301131
// From GCC source code (include/dwarf2.h): This DW_CC_ value is not currently
11311132
// generated by any toolchain. It is used internally to GDB to indicate OpenCL
11321133
// C functions that have been compiled with the IBM XL C for OpenCL compiler and

llvm/include/llvm/DebugInfo/DWARF/DWARFTypePrinter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,9 @@ void DWARFTypePrinter<DieType>::appendSubroutineNameAfter(
741741
// instantiated with function types with these calling conventions won't
742742
// have distinct names - so we'd need to fix that too)
743743
break;
744+
case dwarf::CallingConvention::DW_CC_LLVM_SpirKernel:
745+
OS << " __attribute((spir_kernel))";
746+
break;
744747
case dwarf::CallingConvention::DW_CC_LLVM_Swift:
745748
// SwiftAsync missing
746749
OS << " __attribute__((swiftcall))";

0 commit comments

Comments
 (0)