Skip to content
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

[SYCL] Add global_device and global_host address spaces #1704

Merged
merged 12 commits into from
Jun 5, 2020
10 changes: 9 additions & 1 deletion clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@ class Qualifiers {
// Otherwise in OpenCLC v2.0 s6.5.5: every address space except
// for __constant can be used as __generic.
(A == LangAS::opencl_generic && B != LangAS::opencl_constant) ||
// We also define global_device and global_host address spaces,
// to distinguish global pointers allocated on host to pointers
MrSidims marked this conversation as resolved.
Show resolved Hide resolved
// allocated on device, which are a subset of __global.
// FIXME: add a reference to spec when ready
(A == LangAS::opencl_global && (B == LangAS::opencl_global_device ||
B == LangAS::opencl_global_host)) ||
// Consider pointer size address spaces to be equivalent to default.
((isPtrSizeAddressSpace(A) || A == LangAS::Default) &&
(isPtrSizeAddressSpace(B) || B == LangAS::Default));
Expand All @@ -493,7 +499,9 @@ class Qualifiers {
(!hasAddressSpace() &&
(other.getAddressSpace() == LangAS::opencl_private ||
other.getAddressSpace() == LangAS::opencl_local ||
other.getAddressSpace() == LangAS::opencl_global));
other.getAddressSpace() == LangAS::opencl_global ||
MrSidims marked this conversation as resolved.
Show resolved Hide resolved
other.getAddressSpace() == LangAS::opencl_global_device ||
other.getAddressSpace() == LangAS::opencl_global_host));
}

/// Determines if these qualifiers compatibly include another set.
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/AddressSpaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ enum class LangAS : unsigned {
opencl_constant,
opencl_private,
opencl_generic,
opencl_global_device,
opencl_global_host,

// CUDA specific address spaces.
cuda_device,
Expand Down
10 changes: 10 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,16 @@ def OpenCLGlobalAddressSpace : TypeAttr {
let Documentation = [OpenCLAddressSpaceGlobalDocs];
}

def OpenCLGlobalDeviceAddressSpace : TypeAttr {
let Spellings = [Clang<"opencl_global_device">];
let Documentation = [OpenCLGlobalAddressSpacesDocs];
}

def OpenCLGlobalHostAddressSpace : TypeAttr {
let Spellings = [Clang<"opencl_global_host">];
let Documentation = [OpenCLGlobalAddressSpacesDocs];
}

def OpenCLLocalAddressSpace : TypeAttr {
let Spellings = [Keyword<"__local">, Keyword<"local">, Clang<"opencl_local">];
let Documentation = [OpenCLAddressSpaceLocalDocs];
Expand Down
11 changes: 11 additions & 0 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,17 @@ scope) variables and static local variable as well.
}];
}

def OpenCLGlobalAddressSpacesDocs : Documentation {
let Category = DocOpenCLAddressSpaces;
let Heading = "[[clang::opencl_global_device]], [[clang::opencl_global_host]]";
let Content = [{
The (global_device) and (global_host) address space attributes specify that an
object is allocated in global memory on the device/host. It helps distinguishing
USM pointers that access device memory and accessors that access global memory
from those that access host memory.
}];
}

def OpenCLAddressSpaceLocalDocs : Documentation {
let Category = DocOpenCLAddressSpaces;
let Heading = "__local, local, [[clang::opencl_local]]";
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Sema/ParsedAttr.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,10 @@ class ParsedAttr final
return LangAS::opencl_constant;
case ParsedAttr::AT_OpenCLGlobalAddressSpace:
return LangAS::opencl_global;
case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace:
return LangAS::opencl_global_device;
case ParsedAttr::AT_OpenCLGlobalHostAddressSpace:
return LangAS::opencl_global_host;
case ParsedAttr::AT_OpenCLLocalAddressSpace:
return LangAS::opencl_local;
case ParsedAttr::AT_OpenCLPrivateAddressSpace:
Expand Down
26 changes: 14 additions & 12 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,18 +919,20 @@ static const LangASMap *getAddressSpaceMap(const TargetInfo &T,
// The fake address space map must have a distinct entry for each
// language-specific address space.
static const unsigned FakeAddrSpaceMap[] = {
0, // Default
1, // opencl_global
3, // opencl_local
2, // opencl_constant
0, // opencl_private
4, // opencl_generic
5, // cuda_device
6, // cuda_constant
7, // cuda_shared
8, // ptr32_sptr
9, // ptr32_uptr
10 // ptr64
0, // Default
1, // opencl_global
3, // opencl_local
2, // opencl_constant
0, // opencl_private
4, // opencl_generic
11, // opencl_global_device
12, // opencl_global_host
5, // cuda_device
6, // cuda_constant
7, // cuda_shared
8, // ptr32_sptr
9, // ptr32_uptr
10 // ptr64
};
return &FakeAddrSpaceMap;
} else {
Expand Down
41 changes: 32 additions & 9 deletions clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2387,16 +2387,39 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSp
switch (AS) {
default: llvm_unreachable("Not a language specific address space");
// <OpenCL-addrspace> ::= "CL" [ "global" | "local" | "constant" |
// "private"| "generic" ]
case LangAS::opencl_global: ASString = "CLglobal"; break;
case LangAS::opencl_local: ASString = "CLlocal"; break;
case LangAS::opencl_constant: ASString = "CLconstant"; break;
case LangAS::opencl_private: ASString = "CLprivate"; break;
case LangAS::opencl_generic: ASString = "CLgeneric"; break;
// "private"| "generic" | "global_device" |
// "global_host" ]
case LangAS::opencl_global:
ASString = "CLglobal";
break;
case LangAS::opencl_global_device:
ASString = "CLDevice";
break;
case LangAS::opencl_global_host:
ASString = "CLHost";
break;
case LangAS::opencl_local:
ASString = "CLlocal";
break;
case LangAS::opencl_constant:
ASString = "CLconstant";
break;
case LangAS::opencl_private:
ASString = "CLprivate";
break;
case LangAS::opencl_generic:
ASString = "CLgeneric";
break;
// <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ]
case LangAS::cuda_device: ASString = "CUdevice"; break;
case LangAS::cuda_constant: ASString = "CUconstant"; break;
case LangAS::cuda_shared: ASString = "CUshared"; break;
case LangAS::cuda_device:
ASString = "CUdevice";
break;
case LangAS::cuda_constant:
ASString = "CUconstant";
break;
case LangAS::cuda_shared:
ASString = "CUshared";
break;
// <ptrsize-addrspace> ::= [ "ptr32_sptr" | "ptr32_uptr" | "ptr64" ]
case LangAS::ptr32_sptr:
ASString = "ptr32_sptr";
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,12 @@ void MicrosoftCXXNameMangler::mangleAddressSpaceType(QualType T,
case LangAS::opencl_global:
Extra.mangleSourceName("_ASCLglobal");
break;
case LangAS::opencl_global_device:
Extra.mangleSourceName("_ASCLDevice");
break;
case LangAS::opencl_global_host:
Extra.mangleSourceName("_ASCLHost");
break;
case LangAS::opencl_local:
Extra.mangleSourceName("_ASCLlocal");
break;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,8 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,

case attr::OpenCLPrivateAddressSpace:
case attr::OpenCLGlobalAddressSpace:
case attr::OpenCLGlobalDeviceAddressSpace:
case attr::OpenCLGlobalHostAddressSpace:
case attr::OpenCLLocalAddressSpace:
case attr::OpenCLConstantAddressSpace:
case attr::OpenCLGenericAddressSpace:
Expand Down Expand Up @@ -1880,6 +1882,10 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
return "__constant";
case LangAS::opencl_generic:
return "__generic";
case LangAS::opencl_global_device:
return "__global_device";
case LangAS::opencl_global_host:
return "__global_host";
case LangAS::cuda_device:
return "__device__";
case LangAS::cuda_constant:
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Basic/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
Constant, // opencl_constant
Private, // opencl_private
Generic, // opencl_generic
Global, // opencl_global_device
Global, // opencl_global_host
Global, // cuda_device
Constant, // cuda_constant
Local, // cuda_shared
Expand All @@ -60,6 +62,8 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
Constant, // opencl_constant
Private, // opencl_private
Generic, // opencl_generic
Global, // opencl_global_device
Global, // opencl_global_host
Global, // cuda_device
Constant, // cuda_constant
Local, // cuda_shared
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/NVPTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static const unsigned NVPTXAddrSpaceMap[] = {
0, // opencl_private
// FIXME: generic has to be added to the target
0, // opencl_generic
1, // opencl_global_device
1, // opencl_global_host
1, // cuda_device
4, // cuda_constant
3, // cuda_shared
Expand Down
52 changes: 28 additions & 24 deletions clang/lib/Basic/Targets/SPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,37 @@ namespace clang {
namespace targets {

static const unsigned SPIRAddrSpaceMap[] = {
0, // Default
1, // opencl_global
3, // opencl_local
2, // opencl_constant
0, // opencl_private
4, // opencl_generic
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // ptr32_sptr
0, // ptr32_uptr
0 // ptr64
0, // Default
1, // opencl_global
3, // opencl_local
2, // opencl_constant
0, // opencl_private
4, // opencl_generic
11, // opencl_global_device
12, // opencl_global_host
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // ptr32_sptr
0, // ptr32_uptr
0 // ptr64
};

static const unsigned SYCLAddrSpaceMap[] = {
4, // Default
1, // opencl_global
3, // opencl_local
2, // opencl_constant
0, // opencl_private
4, // opencl_generic
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // ptr32_sptr
0, // ptr32_uptr
0 // ptr64
4, // Default
1, // opencl_global
3, // opencl_local
2, // opencl_constant
0, // opencl_private
4, // opencl_generic
11, // opencl_global_device
12, // opencl_global_host
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // ptr32_sptr
0, // ptr32_uptr
0 // ptr64
};

class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/TCE.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
4, // opencl_local
5, // opencl_constant
0, // opencl_private
1, // opencl_global_device
1, // opencl_global_host
// FIXME: generic has to be added to the target
0, // opencl_generic
0, // cuda_device
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Basic/Targets/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ static const unsigned X86AddrSpaceMap[] = {
0, // opencl_constant
0, // opencl_private
0, // opencl_generic
0, // opencl_global_device
0, // opencl_global_host
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
Expand Down
18 changes: 14 additions & 4 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1360,10 +1360,18 @@ static void removeImageAccessQualifier(std::string& TyName) {
// (basically all single AS CPUs).
static unsigned ArgInfoAddressSpace(LangAS AS) {
switch (AS) {
case LangAS::opencl_global: return 1;
case LangAS::opencl_constant: return 2;
case LangAS::opencl_local: return 3;
case LangAS::opencl_generic: return 4; // Not in SPIR 2.0 specs.
case LangAS::opencl_global:
return 1;
case LangAS::opencl_constant:
return 2;
case LangAS::opencl_local:
return 3;
case LangAS::opencl_generic:
return 4; // Not in SPIR 2.0 specs.
case LangAS::opencl_global_device:
return 11;
case LangAS::opencl_global_host:
return 12;
default:
return 0; // Assume private.
}
Expand Down Expand Up @@ -3858,6 +3866,8 @@ LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
if (LangOpts.OpenCL) {
AddrSpace = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
assert(AddrSpace == LangAS::opencl_global ||
AddrSpace == LangAS::opencl_global_device ||
AddrSpace == LangAS::opencl_global_host ||
AddrSpace == LangAS::opencl_constant ||
AddrSpace == LangAS::opencl_local ||
AddrSpace >= LangAS::FirstTargetAddressSpace);
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7967,6 +7967,8 @@ static bool isAddressSpaceKind(const ParsedAttr &attr) {
return attrKind == ParsedAttr::AT_AddressSpace ||
attrKind == ParsedAttr::AT_OpenCLPrivateAddressSpace ||
attrKind == ParsedAttr::AT_OpenCLGlobalAddressSpace ||
attrKind == ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace ||
attrKind == ParsedAttr::AT_OpenCLGlobalHostAddressSpace ||
attrKind == ParsedAttr::AT_OpenCLLocalAddressSpace ||
attrKind == ParsedAttr::AT_OpenCLConstantAddressSpace ||
attrKind == ParsedAttr::AT_OpenCLGenericAddressSpace;
Expand Down Expand Up @@ -8048,6 +8050,8 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type,
break;
case ParsedAttr::AT_OpenCLPrivateAddressSpace:
case ParsedAttr::AT_OpenCLGlobalAddressSpace:
case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace:
case ParsedAttr::AT_OpenCLGlobalHostAddressSpace:
case ParsedAttr::AT_OpenCLLocalAddressSpace:
case ParsedAttr::AT_OpenCLConstantAddressSpace:
case ParsedAttr::AT_OpenCLGenericAddressSpace:
Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/language_address_space_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ void langas() {
// CHECK: VarDecl {{.*}} z_global '__global int *'
[[clang::opencl_global]] int *z_global;

// CHECK: VarDecl {{.*}} x_global_device '__global_device int *'
__attribute__((opencl_global_device)) int *x_global_device;

// CHECK: VarDecl {{.*}} z_global_device '__global_device int *'
[[clang::opencl_global_device]] int *z_global_device;

// CHECK: VarDecl {{.*}} x_global_host '__global_host int *'
__attribute__((opencl_global_host)) int *x_global_host;

// CHECK: VarDecl {{.*}} z_global_host '__global_host int *'
[[clang::opencl_global_host]] int *z_global_host;

// CHECK: VarDecl {{.*}} x_local '__local int *'
__attribute__((opencl_local)) int *x_local;

Expand Down
12 changes: 12 additions & 0 deletions clang/test/CodeGenCXX/mangle-address-space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ void ocl_f0(char __private *p) { }

struct ocl_OpaqueType;
typedef ocl_OpaqueType __global * ocl_OpaqueTypePtr;
typedef ocl_OpaqueType __attribute__((opencl_global_host)) * ocl_OpaqueTypePtrH;
typedef ocl_OpaqueType
__attribute__((opencl_global_device)) *
ocl_OpaqueTypePtrD;

// CHECKOCL-LABEL: define {{.*}}void @_Z6ocl_f0PU8CLglobal14ocl_OpaqueType
// WINOCL-LABEL: define {{.*}}void @"?ocl_f0@@YAXPEAU?$_ASCLglobal@$$CAUocl_OpaqueType@@@__clang@@@Z"
Expand All @@ -61,4 +65,12 @@ __constant float *ocl_f1(char __generic const *p) { return 0;}
// CHECKOCL-LABEL: define {{.*}}float* @_Z6ocl_f2PU9CLgenericKc
// WINOCL-LABEL: define {{.*}}float* @"?ocl_f2@@YAPEAU?$_ASCLgeneric@$$CAM@__clang@@QEAU?$_ASCLgeneric@$$CBD@2@@Z"
__generic float *ocl_f2(__generic char const * const p) { return 0;}

// CHECKOCL-LABEL: define {{.*}}void @_Z6ocl_f3PU6CLHost14ocl_OpaqueType
// WINOCL-LABEL: define {{.*}}void @"?ocl_f3@@YAXPEAU?$_ASCLHost@$$CAUocl_OpaqueType@@@__clang@@@Z"
void ocl_f3(ocl_OpaqueTypePtrH) {}

// CHECKOCL-LABEL: define {{.*}}void @_Z6ocl_f4PU8CLDevice14ocl_OpaqueType
// WINOCL-LABEL: define {{.*}}void @"?ocl_f4@@YAXPEAU?$_ASCLDevice@$$CAUocl_OpaqueType@@@__clang@@@Z"
void ocl_f4(ocl_OpaqueTypePtrD) {}
#endif
Loading