Skip to content

Commit f1a7138

Browse files
committed
[SYCL] Emit OpenCL/SPIR metadata and calling convention for SYCL
device code. Signed-off-by: Vladimir Lazarev <vladimir.lazarev@intel.com>
1 parent 1e60aa6 commit f1a7138

File tree

7 files changed

+96
-3
lines changed

7 files changed

+96
-3
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9933,7 +9933,7 @@ void ASTContext::forEachMultiversionedFunctionVersion(
99339933
CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic,
99349934
bool IsCXXMethod) const {
99359935
// Pass through to the C++ ABI object
9936-
if (IsCXXMethod)
9936+
if (IsCXXMethod && !LangOpts.SYCL)
99379937
return ABI->getDefaultMethodCallConv(IsVariadic);
99389938

99399939
switch (LangOpts.getDefaultCallingConv()) {

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
945945
if (CGM.getCodeGenOpts().ProfileSampleAccurate)
946946
Fn->addFnAttr("profile-sample-accurate");
947947

948-
if (getLangOpts().OpenCL) {
948+
if (getLangOpts().OpenCL || getLangOpts().SYCL) {
949949
// Add metadata for a kernel function.
950950
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
951951
EmitOpenCLKernelMetadata(FD, Fn);

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,23 @@ void CodeGenModule::Release() {
552552
}
553553
}
554554

555+
// Emit SYCL specific module metadata: OpenCL/SPIR version.
556+
if (LangOpts.SYCL) {
557+
llvm::Metadata *OCLVerElts[] = {
558+
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1)),
559+
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2))};
560+
llvm::NamedMDNode *OCLVerMD =
561+
TheModule.getOrInsertNamedMetadata("opencl.ocl.version");
562+
llvm::LLVMContext &Ctx = TheModule.getContext();
563+
OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
564+
llvm::Metadata *SPIRVerElts[] = {
565+
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1)),
566+
llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 2))};
567+
llvm::NamedMDNode *SPIRVerMD =
568+
TheModule.getOrInsertNamedMetadata("opencl.spir.version");
569+
SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
570+
}
571+
555572
if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
556573
assert(PLevel < 3 && "Invalid PIC Level");
557574
getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ FunctionDecl *CreateSYCLKernelFunction(ASTContext &Context, StringRef Name,
2525
ArrayRef<DeclaratorDecl *> ArgDecls) {
2626

2727
DeclContext *DC = Context.getTranslationUnitDecl();
28-
FunctionProtoType::ExtProtoInfo Info;
28+
FunctionProtoType::ExtProtoInfo Info(CC_OpenCLKernel);
2929
QualType RetTy = Context.VoidTy;
3030
QualType FuncTy = Context.getFunctionType(RetTy, ArgTys, Info);
3131
DeclarationName DN = DeclarationName(&Context.Idents.get(Name));
@@ -161,6 +161,7 @@ void BuildArgTys(ASTContext &Context,
161161
if (TemplateDecl) {
162162
QualType PointeeType = TemplateDecl->getTemplateArgs()[0].getAsType();
163163
Qualifiers Quals = PointeeType.getQualifiers();
164+
// TODO: get address space from accessor template parameter.
164165
Quals.setAddressSpace(LangAS::opencl_global);
165166
PointeeType =
166167
Context.getQualifiedType(PointeeType.getUnqualifiedType(), Quals);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang -cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -S -I /sycl_include_path -I /opencl_include_path -I /usr/include/c++/4.8.5 -I /usr/include/c++/4.8.5/x86_64-redhat-linux -I /usr/include/c++/4.8.5/backward -I /include -I /usr/include -fcxx-exceptions -fexceptions -emit-llvm -x c++ %s -o - | FileCheck %s
2+
3+
// CHECK: define {{.*}}spir_kernel void @kernel_function() {{[^{]+}} !kernel_arg_addr_space ![[MD:[0-9]+]] !kernel_arg_access_qual ![[MD]] !kernel_arg_type ![[MD]] !kernel_arg_base_type ![[MD]] !kernel_arg_type_qual ![[MD]] {
4+
// CHECK: ![[MD]] = !{}
5+
// XFAIL: *
6+
7+
#include <CL/sycl.hpp>
8+
9+
10+
using namespace cl::sycl;
11+
12+
int main() {
13+
14+
queue myQueue;
15+
16+
myQueue.submit([&](handler &cgh) {
17+
18+
cgh.single_task<class kernel_function>([=]() {
19+
});
20+
});
21+
22+
myQueue.wait();
23+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang -cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -S -I /sycl_include_path -I /opencl_include_path -I /usr/include/c++/4.8.5 -I /usr/include/c++/4.8.5/x86_64-redhat-linux -I /usr/include/c++/4.8.5/backward -I /include -I /usr/include -fcxx-exceptions -fexceptions -emit-llvm -x c++ %s -o - | FileCheck %s
2+
3+
// XFAIL: *
4+
5+
#include <CL/sycl.hpp>
6+
7+
8+
using namespace cl::sycl;
9+
10+
int main() {
11+
12+
queue myQueue;
13+
14+
myQueue.submit([&](handler &cgh) {
15+
16+
// CHECK: define spir_kernel void @kernel_function()
17+
18+
// CHECK: call spir_func void @"_ZZZ4mainENK3$_0clERN2cl4sycl7handlerEENKUlvE_clEv"(%class.anon* %0)
19+
20+
// CHECK: define internal spir_func void @"_ZZZ4mainENK3$_0clERN2cl4sycl7handlerEENKUlvE_clEv"(%class.anon* %this)
21+
22+
cgh.single_task<class kernel_function>([=]() {
23+
});
24+
});
25+
26+
myQueue.wait();
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang -cc1 -triple spir64-unknown-linux-sycldevice -std=c++11 -fsycl-is-device -S -I /sycl_include_path -I /opencl_include_path -I /usr/include/c++/4.8.5 -I /usr/include/c++/4.8.5/x86_64-redhat-linux -I /usr/include/c++/4.8.5/backward -I /include -I /usr/include -fcxx-exceptions -fexceptions -emit-llvm -x c++ %s -o - | FileCheck %s
2+
3+
// XFAIL: *
4+
5+
#include <CL/sycl.hpp>
6+
7+
using namespace cl::sycl;
8+
9+
int main() {
10+
11+
queue myQueue;
12+
13+
myQueue.submit([&](handler &cgh) {
14+
15+
cgh.single_task<class kernel_function>([=]() {
16+
});
17+
});
18+
19+
myQueue.wait();
20+
}
21+
22+
23+
// CHECK: !opencl.ocl.version = !{[[VER:![0-9]+]]}
24+
// CHECK: !opencl.spir.version = !{[[VER]]}
25+
// CHECK: [[VER]] = !{i32 1, i32 2}

0 commit comments

Comments
 (0)