Skip to content

[SYCL] Refactor address space attributes to align with llorg. #3634

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 9 commits into from
May 14, 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
18 changes: 9 additions & 9 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,22 +486,22 @@ class Qualifiers {
// allocated on device, which are a subset of __global.
(A == LangAS::opencl_global && (B == LangAS::opencl_global_device ||
B == LangAS::opencl_global_host)) ||
(A == LangAS::sycl_global && (B == LangAS::sycl_global_device ||
B == LangAS::sycl_global_host)) ||
// Consider pointer size address spaces to be equivalent to default.
((isPtrSizeAddressSpace(A) || A == LangAS::Default) &&
(isPtrSizeAddressSpace(B) || B == LangAS::Default));
(isPtrSizeAddressSpace(B) || B == LangAS::Default)) ||
// Default is a superset of SYCL address spaces.
(A == LangAS::Default &&
(B == LangAS::sycl_private || B == LangAS::sycl_local ||
B == LangAS::sycl_global || B == LangAS::sycl_global_device ||
B == LangAS::sycl_global_host));
}

/// Returns true if the address space in these qualifiers is equal to or
/// a superset of the address space in the argument qualifiers.
bool isAddressSpaceSupersetOf(Qualifiers other) const {
return isAddressSpaceSupersetOf(getAddressSpace(),
other.getAddressSpace()) ||
(!hasAddressSpace() &&
(other.getAddressSpace() == LangAS::opencl_private ||
other.getAddressSpace() == LangAS::opencl_local ||
other.getAddressSpace() == LangAS::opencl_global ||
other.getAddressSpace() == LangAS::opencl_global_device ||
other.getAddressSpace() == LangAS::opencl_global_host));
return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace());
}

/// Determines if these qualifiers compatibly include another set.
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/AddressSpaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ enum class LangAS : unsigned {
cuda_constant,
cuda_shared,

// SYCL specific address spaces.
sycl_global,
sycl_global_device,
sycl_global_host,
sycl_local,
sycl_private,

// Pointer size and extension address spaces.
ptr32_sptr,
ptr32_uptr,
Expand Down
28 changes: 26 additions & 2 deletions clang/include/clang/Sema/ParsedAttr.h
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ class ParsedAttr final
/// a Spelling enumeration, the value UINT_MAX is returned.
unsigned getSemanticSpelling() const;

/// If this is an OpenCL addr space attribute returns its representation
/// in LangAS, otherwise returns default addr space.
/// If this is an OpenCL address space attribute, returns its representation
/// in LangAS, otherwise returns default address space.
LangAS asOpenCLLangAS() const {
switch (getParsedKind()) {
case ParsedAttr::AT_OpenCLConstantAddressSpace:
Expand All @@ -655,6 +655,30 @@ class ParsedAttr final
}
}

/// If this is an OpenCL address space attribute, returns its SYCL
/// representation in LangAS, otherwise returns default address space.
LangAS asSYCLLangAS() const {
switch (getKind()) {
// FIXME: there are uses of `opencl_constant` attribute in SYCL mode.
// See https://github.com/intel/llvm/issues/3062 for more details.
case ParsedAttr::AT_OpenCLConstantAddressSpace:
return LangAS::opencl_constant;
case ParsedAttr::AT_OpenCLGlobalAddressSpace:
return LangAS::sycl_global;
case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace:
return LangAS::sycl_global_device;
case ParsedAttr::AT_OpenCLGlobalHostAddressSpace:
return LangAS::sycl_global_host;
case ParsedAttr::AT_OpenCLLocalAddressSpace:
return LangAS::sycl_local;
case ParsedAttr::AT_OpenCLPrivateAddressSpace:
return LangAS::sycl_private;
case ParsedAttr::AT_OpenCLGenericAddressSpace:
default:
return LangAS::Default;
}
}

AttributeCommonInfo::Kind getKind() const {
return AttributeCommonInfo::Kind(Info.AttrKind);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -13295,7 +13295,7 @@ class Sema final {
bool isSYCLEsimdPrivateGlobal(VarDecl *VDecl) {
return getLangOpts().SYCLIsDevice && VDecl->hasAttr<SYCLSimdAttr>() &&
VDecl->hasGlobalStorage() &&
(VDecl->getType().getAddressSpace() == LangAS::opencl_private);
(VDecl->getType().getAddressSpace() == LangAS::sycl_private);
}
};

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,11 @@ static const LangASMap *getAddressSpaceMap(const TargetInfo &T,
7, // cuda_device
8, // cuda_constant
9, // cuda_shared
1, // sycl_global
5, // sycl_global_device
6, // sycl_global_host
3, // sycl_local
0, // sycl_private
10, // ptr32_sptr
11, // ptr32_uptr
12 // ptr64
Expand Down
19 changes: 18 additions & 1 deletion clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,8 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSp
if (Context.getASTContext().addressSpaceMapManglingFor(AS)) {
// <target-addrspace> ::= "AS" <address-space-number>
unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
if (TargetAS != 0 || (Context.getASTContext().getLangOpts().SYCLIsDevice))
if (TargetAS != 0 ||
Context.getASTContext().getTargetAddressSpace(LangAS::Default) != 0)
ASString = "AS" + llvm::utostr(TargetAS);
} else {
switch (AS) {
Expand Down Expand Up @@ -2530,6 +2531,22 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSp
case LangAS::opencl_generic:
ASString = "CLgeneric";
break;
// <SYCL-addrspace> ::= "SY" [ "global" | "local" | "private" ]
case LangAS::sycl_global:
ASString = "SYglobal";
break;
case LangAS::sycl_global_device:
ASString = "SYglobaldevice";
break;
case LangAS::sycl_global_host:
ASString = "SYglobalhost";
break;
case LangAS::sycl_local:
ASString = "SYlocal";
break;
case LangAS::sycl_private:
ASString = "SYprivate";
break;
// <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ]
case LangAS::cuda_device:
ASString = "CUdevice";
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/TypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2128,18 +2128,23 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
case LangAS::Default:
return "";
case LangAS::opencl_global:
case LangAS::sycl_global:
return "__global";
case LangAS::opencl_local:
case LangAS::sycl_local:
return "__local";
case LangAS::opencl_private:
case LangAS::sycl_private:
return "__private";
case LangAS::opencl_constant:
return "__constant";
case LangAS::opencl_generic:
return "__generic";
case LangAS::opencl_global_device:
case LangAS::sycl_global_device:
return "__global_device";
case LangAS::opencl_global_host:
case LangAS::sycl_global_host:
return "__global_host";
case LangAS::cuda_device:
return "__device__";
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Basic/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
Global, // cuda_device
Constant, // cuda_constant
Local, // cuda_shared
Global, // sycl_global
Global, // sycl_global_device
Global, // sycl_global_host
Local, // sycl_local
Private, // sycl_private
Generic, // ptr32_sptr
Generic, // ptr32_uptr
Generic // ptr64
Expand All @@ -68,9 +73,16 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
Global, // cuda_device
Constant, // cuda_constant
Local, // cuda_shared
// SYCL address space values for this map are dummy
Generic, // sycl_global
Generic, // sycl_global_device
Generic, // sycl_global_host
Generic, // sycl_local
Generic, // sycl_private
Generic, // ptr32_sptr
Generic, // ptr32_uptr
Generic // ptr64

};
} // namespace targets
} // namespace clang
Expand Down
9 changes: 7 additions & 2 deletions clang/lib/Basic/Targets/NVPTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ static const unsigned NVPTXAddrSpaceMap[] = {
1, // cuda_device
4, // cuda_constant
3, // cuda_shared
1, // sycl_global
1, // sycl_global_device
1, // sycl_global_host
3, // sycl_local
0, // sycl_private
0, // ptr32_sptr
0, // ptr32_uptr
0 // ptr64
Expand All @@ -46,8 +51,8 @@ static const int NVPTXDWARFAddrSpaceMap[] = {
-1, // Default, opencl_private or opencl_generic - not defined
5, // opencl_global
-1,
8, // opencl_local or cuda_shared
4, // opencl_constant or cuda_constant
8, // opencl_local or cuda_shared
4, // opencl_constant or cuda_constant
};

class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
Expand Down
54 changes: 37 additions & 17 deletions clang/lib/Basic/Targets/SPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace clang {
namespace targets {

static const unsigned SPIRAddrSpaceMap[] = {
static const unsigned SPIRDefIsPrivMap[] = {
0, // Default
1, // opencl_global
3, // opencl_local
Expand All @@ -35,23 +35,38 @@ static const unsigned SPIRAddrSpaceMap[] = {
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
// SYCL address space values for this map are dummy
0, // sycl_global
0, // sycl_global_device
0, // sycl_global_host
0, // sycl_local
0, // sycl_private
0, // ptr32_sptr
0, // ptr32_uptr
0 // ptr64
};

static const unsigned SYCLAddrSpaceMap[] = {
static const unsigned SPIRDefIsGenMap[] = {
4, // Default
// OpenCL address space values for this map are dummy and they can't be used
// FIXME: reset opencl_global entry to 0. Currently CodeGen libary uses
// opencl_global in SYCL language mode, but we should switch to using
// sycl_global instead.
1, // opencl_global
3, // opencl_local
0, // opencl_local
2, // opencl_constant
0, // opencl_private
4, // opencl_generic
5, // opencl_global_device
6, // opencl_global_host
0, // opencl_generic
0, // opencl_global_device
0, // opencl_global_host
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
1, // sycl_global
5, // sycl_global_device
6, // sycl_global_host
3, // sycl_local
0, // sycl_private
0, // ptr32_sptr
0, // ptr32_uptr
0 // ptr64
Expand All @@ -64,9 +79,7 @@ class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {
TLSSupported = false;
VLASupported = false;
LongWidth = LongAlign = 64;
AddrSpaceMap = (Triple.getEnvironment() == llvm::Triple::SYCLDevice)
? &SYCLAddrSpaceMap
: &SPIRAddrSpaceMap;
AddrSpaceMap = &SPIRDefIsPrivMap;
UseAddrSpaceMapMangling = true;
HasLegalHalfType = true;
HasFloat16 = true;
Expand Down Expand Up @@ -114,14 +127,20 @@ class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {
return CC_SpirFunction;
}

llvm::Optional<LangAS> getConstantAddressSpace() const override {
// If we assign "opencl_constant" address space the following code becomes
// illegal, because it can't be cast to any other address space:
//
// const char *getLiteral() {
// return "AB";
// }
return LangAS::opencl_global;
void setAddressSpaceMap(bool DefaultIsGeneric) {
AddrSpaceMap = DefaultIsGeneric ? &SPIRDefIsGenMap : &SPIRDefIsPrivMap;
}

void adjust(LangOptions &Opts) override {
TargetInfo::adjust(Opts);
// NOTE: SYCL specification considers unannotated pointers and references
// to be pointing to the generic address space. See section 5.9.3 of
// SYCL 2020 specification.
// Currently, there is no way of representing SYCL's default address space
// language semantics along with the semantics of embedded C's default
// address space in the same address space map. Hence the map needs to be
// reset to allow mapping to the desired value of 'Default' entry for SYCL.
setAddressSpaceMap(/*DefaultIsGeneric=*/Opts.SYCLIsDevice);
}

void setSupportedOpenCLOpts() override {
Expand All @@ -134,6 +153,7 @@ class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {

bool hasInt128Type() const override { return false; }
};

class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {
public:
SPIR32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Basic/Targets/TCE.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // sycl_global
0, // sycl_global_device
0, // sycl_global_host
0, // sycl_local
0, // sycl_private
0, // ptr32_sptr
0, // ptr32_uptr
0, // ptr64
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Basic/Targets/X86.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ static const unsigned X86AddrSpaceMap[] = {
0, // cuda_device
0, // cuda_constant
0, // cuda_shared
0, // sycl_global
0, // sycl_global_device
0, // sycl_global_host
0, // sycl_local
0, // sycl_private
270, // ptr32_sptr
271, // ptr32_uptr
272 // ptr64
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CGDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl(
LangAS AS = GetGlobalVarAddressSpace(&D);
unsigned TargetAS = getContext().getTargetAddressSpace(AS);

// OpenCL variables in local address space and CUDA shared
// OpenCL/SYCL variables in local address space and CUDA shared
// variables cannot have an initializer.
llvm::Constant *Init = nullptr;
if (Ty.getAddressSpace() == LangAS::opencl_local ||
Ty.getAddressSpace() == LangAS::sycl_local ||
D.hasAttr<CUDASharedAttr>() || D.hasAttr<LoaderUninitializedAttr>())
Init = llvm::UndefValue::get(LTy);
else
Expand Down Expand Up @@ -1149,7 +1150,7 @@ Address CodeGenModule::createUnnamedGlobalFrom(const VarDecl &D,
bool isConstant = true;
llvm::GlobalVariable *InsertBefore = nullptr;
unsigned AS =
getContext().getTargetAddressSpace(getStringLiteralAddressSpace());
getContext().getTargetAddressSpace(GetGlobalConstantAddressSpace());
std::string Name;
if (D.hasGlobalStorage())
Name = getMangledName(&D).str() + ".const";
Expand Down
Loading