Skip to content

Commit 870b840

Browse files
authored
[SYCL] Refactor address space attributes to align with llorg. (#3634)
This PR contains two patches: 1. [SYCL] Implement SYCL address space attributes handling - cherry-pick from llorg of llvm/llvm-project@7818906. 2. Adjust DPC++ to address space handling changes from llorg - changes on top of llorg commit to keep DPC++ working. Today use re-use OpenCL attributes in SYCL mode, so second patch updates these uses with SYCL attributes (not all of them though). Second patch includes content of https://reviews.llvm.org/D100396 + additional changes: - Enable `opencl_constant` address space in SYCL mode. - SYCL mode reuses a lot of OpenCL code, which rely on OpenCL language address spaces. Switching to new SYCL address spaces breaks re-use "as is". I updated uses of `opencl_local` address space, but there are a lot of uses for `opencl_global` (e.g. special types like images, pipes, etc. are annotated with OpenCL language address space). We will have to introduce SYCL address spaces in less invasive way possible. - clang/lib/Sema/SPIRVBuiltins.td updated to use SYCL address spaces. - Reverted a whitespace few changes and one LIT test (clang/test/SemaOpenCLCXX/address-space-lambda.clcpp) to match llorg source code. - Reverted 761b2ea, which was a workaround for custom SYCL mangling scheme for NVPTX target.
1 parent ad46b64 commit 870b840

31 files changed

+526
-3800
lines changed

clang/include/clang/AST/Type.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,22 +486,22 @@ class Qualifiers {
486486
// allocated on device, which are a subset of __global.
487487
(A == LangAS::opencl_global && (B == LangAS::opencl_global_device ||
488488
B == LangAS::opencl_global_host)) ||
489+
(A == LangAS::sycl_global && (B == LangAS::sycl_global_device ||
490+
B == LangAS::sycl_global_host)) ||
489491
// Consider pointer size address spaces to be equivalent to default.
490492
((isPtrSizeAddressSpace(A) || A == LangAS::Default) &&
491-
(isPtrSizeAddressSpace(B) || B == LangAS::Default));
493+
(isPtrSizeAddressSpace(B) || B == LangAS::Default)) ||
494+
// Default is a superset of SYCL address spaces.
495+
(A == LangAS::Default &&
496+
(B == LangAS::sycl_private || B == LangAS::sycl_local ||
497+
B == LangAS::sycl_global || B == LangAS::sycl_global_device ||
498+
B == LangAS::sycl_global_host));
492499
}
493500

494501
/// Returns true if the address space in these qualifiers is equal to or
495502
/// a superset of the address space in the argument qualifiers.
496503
bool isAddressSpaceSupersetOf(Qualifiers other) const {
497-
return isAddressSpaceSupersetOf(getAddressSpace(),
498-
other.getAddressSpace()) ||
499-
(!hasAddressSpace() &&
500-
(other.getAddressSpace() == LangAS::opencl_private ||
501-
other.getAddressSpace() == LangAS::opencl_local ||
502-
other.getAddressSpace() == LangAS::opencl_global ||
503-
other.getAddressSpace() == LangAS::opencl_global_device ||
504-
other.getAddressSpace() == LangAS::opencl_global_host));
504+
return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace());
505505
}
506506

507507
/// Determines if these qualifiers compatibly include another set.

clang/include/clang/Basic/AddressSpaces.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ enum class LangAS : unsigned {
4444
cuda_constant,
4545
cuda_shared,
4646

47+
// SYCL specific address spaces.
48+
sycl_global,
49+
sycl_global_device,
50+
sycl_global_host,
51+
sycl_local,
52+
sycl_private,
53+
4754
// Pointer size and extension address spaces.
4855
ptr32_sptr,
4956
ptr32_uptr,

clang/include/clang/Sema/ParsedAttr.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,8 @@ class ParsedAttr final
632632
/// a Spelling enumeration, the value UINT_MAX is returned.
633633
unsigned getSemanticSpelling() const;
634634

635-
/// If this is an OpenCL addr space attribute returns its representation
636-
/// in LangAS, otherwise returns default addr space.
635+
/// If this is an OpenCL address space attribute, returns its representation
636+
/// in LangAS, otherwise returns default address space.
637637
LangAS asOpenCLLangAS() const {
638638
switch (getParsedKind()) {
639639
case ParsedAttr::AT_OpenCLConstantAddressSpace:
@@ -655,6 +655,30 @@ class ParsedAttr final
655655
}
656656
}
657657

658+
/// If this is an OpenCL address space attribute, returns its SYCL
659+
/// representation in LangAS, otherwise returns default address space.
660+
LangAS asSYCLLangAS() const {
661+
switch (getKind()) {
662+
// FIXME: there are uses of `opencl_constant` attribute in SYCL mode.
663+
// See https://github.com/intel/llvm/issues/3062 for more details.
664+
case ParsedAttr::AT_OpenCLConstantAddressSpace:
665+
return LangAS::opencl_constant;
666+
case ParsedAttr::AT_OpenCLGlobalAddressSpace:
667+
return LangAS::sycl_global;
668+
case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace:
669+
return LangAS::sycl_global_device;
670+
case ParsedAttr::AT_OpenCLGlobalHostAddressSpace:
671+
return LangAS::sycl_global_host;
672+
case ParsedAttr::AT_OpenCLLocalAddressSpace:
673+
return LangAS::sycl_local;
674+
case ParsedAttr::AT_OpenCLPrivateAddressSpace:
675+
return LangAS::sycl_private;
676+
case ParsedAttr::AT_OpenCLGenericAddressSpace:
677+
default:
678+
return LangAS::Default;
679+
}
680+
}
681+
658682
AttributeCommonInfo::Kind getKind() const {
659683
return AttributeCommonInfo::Kind(Info.AttrKind);
660684
}

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13295,7 +13295,7 @@ class Sema final {
1329513295
bool isSYCLEsimdPrivateGlobal(VarDecl *VDecl) {
1329613296
return getLangOpts().SYCLIsDevice && VDecl->hasAttr<SYCLSimdAttr>() &&
1329713297
VDecl->hasGlobalStorage() &&
13298-
(VDecl->getType().getAddressSpace() == LangAS::opencl_private);
13298+
(VDecl->getType().getAddressSpace() == LangAS::sycl_private);
1329913299
}
1330013300
};
1330113301

clang/lib/AST/ASTContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,11 @@ static const LangASMap *getAddressSpaceMap(const TargetInfo &T,
931931
7, // cuda_device
932932
8, // cuda_constant
933933
9, // cuda_shared
934+
1, // sycl_global
935+
5, // sycl_global_device
936+
6, // sycl_global_host
937+
3, // sycl_local
938+
0, // sycl_private
934939
10, // ptr32_sptr
935940
11, // ptr32_uptr
936941
12 // ptr64

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2501,7 +2501,8 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSp
25012501
if (Context.getASTContext().addressSpaceMapManglingFor(AS)) {
25022502
// <target-addrspace> ::= "AS" <address-space-number>
25032503
unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
2504-
if (TargetAS != 0 || (Context.getASTContext().getLangOpts().SYCLIsDevice))
2504+
if (TargetAS != 0 ||
2505+
Context.getASTContext().getTargetAddressSpace(LangAS::Default) != 0)
25052506
ASString = "AS" + llvm::utostr(TargetAS);
25062507
} else {
25072508
switch (AS) {
@@ -2530,6 +2531,22 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSp
25302531
case LangAS::opencl_generic:
25312532
ASString = "CLgeneric";
25322533
break;
2534+
// <SYCL-addrspace> ::= "SY" [ "global" | "local" | "private" ]
2535+
case LangAS::sycl_global:
2536+
ASString = "SYglobal";
2537+
break;
2538+
case LangAS::sycl_global_device:
2539+
ASString = "SYglobaldevice";
2540+
break;
2541+
case LangAS::sycl_global_host:
2542+
ASString = "SYglobalhost";
2543+
break;
2544+
case LangAS::sycl_local:
2545+
ASString = "SYlocal";
2546+
break;
2547+
case LangAS::sycl_private:
2548+
ASString = "SYprivate";
2549+
break;
25332550
// <CUDA-addrspace> ::= "CU" [ "device" | "constant" | "shared" ]
25342551
case LangAS::cuda_device:
25352552
ASString = "CUdevice";

clang/lib/AST/TypePrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,18 +2128,23 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
21282128
case LangAS::Default:
21292129
return "";
21302130
case LangAS::opencl_global:
2131+
case LangAS::sycl_global:
21312132
return "__global";
21322133
case LangAS::opencl_local:
2134+
case LangAS::sycl_local:
21332135
return "__local";
21342136
case LangAS::opencl_private:
2137+
case LangAS::sycl_private:
21352138
return "__private";
21362139
case LangAS::opencl_constant:
21372140
return "__constant";
21382141
case LangAS::opencl_generic:
21392142
return "__generic";
21402143
case LangAS::opencl_global_device:
2144+
case LangAS::sycl_global_device:
21412145
return "__global_device";
21422146
case LangAS::opencl_global_host:
2147+
case LangAS::sycl_global_host:
21432148
return "__global_host";
21442149
case LangAS::cuda_device:
21452150
return "__device__";

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
5151
Global, // cuda_device
5252
Constant, // cuda_constant
5353
Local, // cuda_shared
54+
Global, // sycl_global
55+
Global, // sycl_global_device
56+
Global, // sycl_global_host
57+
Local, // sycl_local
58+
Private, // sycl_private
5459
Generic, // ptr32_sptr
5560
Generic, // ptr32_uptr
5661
Generic // ptr64
@@ -68,9 +73,16 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
6873
Global, // cuda_device
6974
Constant, // cuda_constant
7075
Local, // cuda_shared
76+
// SYCL address space values for this map are dummy
77+
Generic, // sycl_global
78+
Generic, // sycl_global_device
79+
Generic, // sycl_global_host
80+
Generic, // sycl_local
81+
Generic, // sycl_private
7182
Generic, // ptr32_sptr
7283
Generic, // ptr32_uptr
7384
Generic // ptr64
85+
7486
};
7587
} // namespace targets
7688
} // namespace clang

clang/lib/Basic/Targets/NVPTX.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ static const unsigned NVPTXAddrSpaceMap[] = {
3535
1, // cuda_device
3636
4, // cuda_constant
3737
3, // cuda_shared
38+
1, // sycl_global
39+
1, // sycl_global_device
40+
1, // sycl_global_host
41+
3, // sycl_local
42+
0, // sycl_private
3843
0, // ptr32_sptr
3944
0, // ptr32_uptr
4045
0 // ptr64
@@ -46,8 +51,8 @@ static const int NVPTXDWARFAddrSpaceMap[] = {
4651
-1, // Default, opencl_private or opencl_generic - not defined
4752
5, // opencl_global
4853
-1,
49-
8, // opencl_local or cuda_shared
50-
4, // opencl_constant or cuda_constant
54+
8, // opencl_local or cuda_shared
55+
4, // opencl_constant or cuda_constant
5156
};
5257

5358
class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {

clang/lib/Basic/Targets/SPIR.h

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
namespace clang {
2424
namespace targets {
2525

26-
static const unsigned SPIRAddrSpaceMap[] = {
26+
static const unsigned SPIRDefIsPrivMap[] = {
2727
0, // Default
2828
1, // opencl_global
2929
3, // opencl_local
@@ -35,23 +35,38 @@ static const unsigned SPIRAddrSpaceMap[] = {
3535
0, // cuda_device
3636
0, // cuda_constant
3737
0, // cuda_shared
38+
// SYCL address space values for this map are dummy
39+
0, // sycl_global
40+
0, // sycl_global_device
41+
0, // sycl_global_host
42+
0, // sycl_local
43+
0, // sycl_private
3844
0, // ptr32_sptr
3945
0, // ptr32_uptr
4046
0 // ptr64
4147
};
4248

43-
static const unsigned SYCLAddrSpaceMap[] = {
49+
static const unsigned SPIRDefIsGenMap[] = {
4450
4, // Default
51+
// OpenCL address space values for this map are dummy and they can't be used
52+
// FIXME: reset opencl_global entry to 0. Currently CodeGen libary uses
53+
// opencl_global in SYCL language mode, but we should switch to using
54+
// sycl_global instead.
4555
1, // opencl_global
46-
3, // opencl_local
56+
0, // opencl_local
4757
2, // opencl_constant
4858
0, // opencl_private
49-
4, // opencl_generic
50-
5, // opencl_global_device
51-
6, // opencl_global_host
59+
0, // opencl_generic
60+
0, // opencl_global_device
61+
0, // opencl_global_host
5262
0, // cuda_device
5363
0, // cuda_constant
5464
0, // cuda_shared
65+
1, // sycl_global
66+
5, // sycl_global_device
67+
6, // sycl_global_host
68+
3, // sycl_local
69+
0, // sycl_private
5570
0, // ptr32_sptr
5671
0, // ptr32_uptr
5772
0 // ptr64
@@ -64,9 +79,7 @@ class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {
6479
TLSSupported = false;
6580
VLASupported = false;
6681
LongWidth = LongAlign = 64;
67-
AddrSpaceMap = (Triple.getEnvironment() == llvm::Triple::SYCLDevice)
68-
? &SYCLAddrSpaceMap
69-
: &SPIRAddrSpaceMap;
82+
AddrSpaceMap = &SPIRDefIsPrivMap;
7083
UseAddrSpaceMapMangling = true;
7184
HasLegalHalfType = true;
7285
HasFloat16 = true;
@@ -114,14 +127,20 @@ class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {
114127
return CC_SpirFunction;
115128
}
116129

117-
llvm::Optional<LangAS> getConstantAddressSpace() const override {
118-
// If we assign "opencl_constant" address space the following code becomes
119-
// illegal, because it can't be cast to any other address space:
120-
//
121-
// const char *getLiteral() {
122-
// return "AB";
123-
// }
124-
return LangAS::opencl_global;
130+
void setAddressSpaceMap(bool DefaultIsGeneric) {
131+
AddrSpaceMap = DefaultIsGeneric ? &SPIRDefIsGenMap : &SPIRDefIsPrivMap;
132+
}
133+
134+
void adjust(LangOptions &Opts) override {
135+
TargetInfo::adjust(Opts);
136+
// NOTE: SYCL specification considers unannotated pointers and references
137+
// to be pointing to the generic address space. See section 5.9.3 of
138+
// SYCL 2020 specification.
139+
// Currently, there is no way of representing SYCL's default address space
140+
// language semantics along with the semantics of embedded C's default
141+
// address space in the same address space map. Hence the map needs to be
142+
// reset to allow mapping to the desired value of 'Default' entry for SYCL.
143+
setAddressSpaceMap(/*DefaultIsGeneric=*/Opts.SYCLIsDevice);
125144
}
126145

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

135154
bool hasInt128Type() const override { return false; }
136155
};
156+
137157
class LLVM_LIBRARY_VISIBILITY SPIR32TargetInfo : public SPIRTargetInfo {
138158
public:
139159
SPIR32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)

clang/lib/Basic/Targets/TCE.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
4242
0, // cuda_device
4343
0, // cuda_constant
4444
0, // cuda_shared
45+
0, // sycl_global
46+
0, // sycl_global_device
47+
0, // sycl_global_host
48+
0, // sycl_local
49+
0, // sycl_private
4550
0, // ptr32_sptr
4651
0, // ptr32_uptr
4752
0, // ptr64

clang/lib/Basic/Targets/X86.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ static const unsigned X86AddrSpaceMap[] = {
3535
0, // cuda_device
3636
0, // cuda_constant
3737
0, // cuda_shared
38+
0, // sycl_global
39+
0, // sycl_global_device
40+
0, // sycl_global_host
41+
0, // sycl_local
42+
0, // sycl_private
3843
270, // ptr32_sptr
3944
271, // ptr32_uptr
4045
272 // ptr64

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,11 @@ llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl(
258258
LangAS AS = GetGlobalVarAddressSpace(&D);
259259
unsigned TargetAS = getContext().getTargetAddressSpace(AS);
260260

261-
// OpenCL variables in local address space and CUDA shared
261+
// OpenCL/SYCL variables in local address space and CUDA shared
262262
// variables cannot have an initializer.
263263
llvm::Constant *Init = nullptr;
264264
if (Ty.getAddressSpace() == LangAS::opencl_local ||
265+
Ty.getAddressSpace() == LangAS::sycl_local ||
265266
D.hasAttr<CUDASharedAttr>() || D.hasAttr<LoaderUninitializedAttr>())
266267
Init = llvm::UndefValue::get(LTy);
267268
else
@@ -1149,7 +1150,7 @@ Address CodeGenModule::createUnnamedGlobalFrom(const VarDecl &D,
11491150
bool isConstant = true;
11501151
llvm::GlobalVariable *InsertBefore = nullptr;
11511152
unsigned AS =
1152-
getContext().getTargetAddressSpace(getStringLiteralAddressSpace());
1153+
getContext().getTargetAddressSpace(GetGlobalConstantAddressSpace());
11531154
std::string Name;
11541155
if (D.hasGlobalStorage())
11551156
Name = getMangledName(&D).str() + ".const";

0 commit comments

Comments
 (0)