Skip to content

[SYCL] Fix builtins address space type #4275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions clang/include/clang/Basic/AddressSpaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@ inline bool isPtrSizeAddressSpace(LangAS AS) {
AS == LangAS::ptr64);
}

inline LangAS asSYCLLangAS(LangAS AS) {
switch (AS) {
case LangAS::opencl_global:
return LangAS::sycl_global;
case LangAS::opencl_global_device:
return LangAS::sycl_global_device;
case LangAS::opencl_global_host:
return LangAS::sycl_global_host;
case LangAS::opencl_local:
return LangAS::sycl_local;
case LangAS::opencl_private:
return LangAS::sycl_private;
default:
return AS;
}
}

inline LangAS asOpenCLLangAS(LangAS AS) {
switch (AS) {
case LangAS::sycl_global:
return LangAS::opencl_global;
case LangAS::sycl_global_device:
return LangAS::opencl_global_device;
case LangAS::sycl_global_host:
return LangAS::opencl_global_host;
case LangAS::sycl_local:
return LangAS::opencl_local;
case LangAS::sycl_private:
return LangAS::opencl_private;
default:
return AS;
}
}

} // namespace clang

#endif // LLVM_CLANG_BASIC_ADDRESSSPACES_H
15 changes: 15 additions & 0 deletions clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,21 @@ void MicrosoftCXXNameMangler::mangleAddressSpaceType(QualType T,
case LangAS::cuda_device:
Extra.mangleSourceName("_ASCUdevice");
break;
case LangAS::sycl_global:
Extra.mangleSourceName("_ASSYglobal");
break;
case LangAS::sycl_global_device:
Extra.mangleSourceName("_ASSYdevice");
break;
case LangAS::sycl_global_host:
Extra.mangleSourceName("_ASSYhost");
break;
case LangAS::sycl_local:
Extra.mangleSourceName("_ASSYlocal");
break;
case LangAS::sycl_private:
Extra.mangleSourceName("_ASSYprivate");
break;
case LangAS::cuda_constant:
Extra.mangleSourceName("_ASCUconstant");
break;
Expand Down
18 changes: 18 additions & 0 deletions clang/test/CodeGenOpenCL/spirv-builtins-addr-space.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %clang_cc1 %s -x cl -fdeclare-spirv-builtins -fsyntax-only -emit-llvm -o - -O0 | FileCheck %s
//
// Check that SPIR-V builtins are declared with OpenCL address spaces rather
// than SYCL address spaces when using them with OpenCL. OpenCL address spaces
// are mangled with the CL prefix and SYCL address spaces are mangled with the
// SY prefix.

// CHECK: __spirv_ocl_modf{{.*}}CLglobal
void modf_global(float a, global float *ptr) { __spirv_ocl_modf(a, ptr); }

// CHECK: __spirv_ocl_modf{{.*}}CLlocal
void modf_local(float a, local float *ptr) { __spirv_ocl_modf(a, ptr); }

// CHECK: __spirv_ocl_modf{{.*}}CLprivate
void modf_private(float a) {
float *ptr;
__spirv_ocl_modf(a, ptr);
}
29 changes: 29 additions & 0 deletions clang/test/CodeGenSYCL/spirv-builtins-addr-space.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %clang_cc1 %s -fsycl-is-device -fdeclare-spirv-builtins -fsyntax-only -emit-llvm -o - -O0 | FileCheck %s
//
// Check that SPIR-V builtins are declared with SYCL address spaces rather
// than OpenCL address spaces when using them with SYCL. OpenCL address spaces
// are mangled with the CL prefix and SYCL address spaces are mangled with the
// SY prefix.
//
// The opencl_global, opencl_local, and opencl_private attributes get turned
// into sycl_global, sycl_local and sycl_private address spaces by clang.

#include "Inputs/sycl.hpp"

// CHECK: __spirv_ocl_modf{{.*}}SYglobal
void modf_global(float a) {
__attribute__((opencl_global)) float *ptr = nullptr;
sycl::kernel_single_task<class fake_kernel>([=]() { __spirv_ocl_modf(a, ptr); });
}

// CHECK: __spirv_ocl_modf{{.*}}SYlocal
void modf_local(float a) {
__attribute__((opencl_local)) float *ptr = nullptr;
sycl::kernel_single_task<class fake_kernel>([=]() { __spirv_ocl_modf(a, ptr); });
}

// CHECK: __spirv_ocl_modf{{.*}}SYprivate
void modf_private(float a) {
__attribute__((opencl_private)) float *ptr = nullptr;
sycl::kernel_single_task<class fake_kernel>([=]() { __spirv_ocl_modf(a, ptr); });
}
4 changes: 3 additions & 1 deletion clang/utils/TableGen/ClangProgModelBuiltinEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,9 @@ static QualType getOpenCLTypedefType(Sema &S, llvm::StringRef Name);
// [const|volatile] pointers, so this is ok to do it as a last step.
if (Ty.IsPointer != 0) {
for (unsigned Index = 0; Index < QT.size(); Index++) {
QT[Index] = Context.getAddrSpaceQualType(QT[Index], Ty.AS);
QT[Index] = Context.getAddrSpaceQualType(
QT[Index], S.getLangOpts().SYCLIsDevice ? asSYCLLangAS(Ty.AS)
: asOpenCLLangAS(Ty.AS));
QT[Index] = Context.getPointerType(QT[Index]);
}
}
Expand Down