Skip to content

Commit c278543

Browse files
committed
[SYCL] Implement SYCL address-space rules.
Signed-off-by: Vladimir Lazarev <[email protected]>
1 parent effac35 commit c278543

21 files changed

+337
-26
lines changed

clang/include/clang/AST/Type.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,13 @@ class Qualifiers {
473473
// Otherwise in OpenCLC v2.0 s6.5.5: every address space except
474474
// for __constant can be used as __generic.
475475
(getAddressSpace() == LangAS::opencl_generic &&
476-
other.getAddressSpace() != LangAS::opencl_constant);
476+
other.getAddressSpace() != LangAS::opencl_constant) ||
477+
(!hasAddressSpace() &&
478+
(other.getAddressSpace() == LangAS::sycl_private ||
479+
other.getAddressSpace() == LangAS::sycl_local ||
480+
other.getAddressSpace() == LangAS::sycl_global ||
481+
other.getAddressSpace() == LangAS::sycl_constant ||
482+
other.getAddressSpace() == LangAS::sycl_generic));
477483
}
478484

479485
/// 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
@@ -43,6 +43,13 @@ enum class LangAS : unsigned {
4343
cuda_constant,
4444
cuda_shared,
4545

46+
sycl_global,
47+
sycl_local,
48+
sycl_constant,
49+
sycl_private,
50+
// Likely never used, but useful in the future to reserve the spot in the
51+
// enum.
52+
sycl_generic,
4653
// This denotes the count of language-specific address spaces and also
4754
// the offset added to the target-specific address spaces, which are usually
4855
// specified by address space attributes __attribute__(address_space(n))).

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9482,4 +9482,7 @@ def err_builtin_launder_invalid_arg : Error<
94829482
"%select{non-pointer|function pointer|void pointer}0 argument to "
94839483
"'__builtin_launder' is not allowed">;
94849484

9485+
def err_sycl_attribute_address_space_invalid : Error<
9486+
"address space is outside the valid range of values">;
9487+
94859488
} // end of sema component.

clang/include/clang/Basic/TokenKinds.def

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ PUNCTUATOR(caretcaret, "^^")
248248
// MSVC <= v18.
249249
// KEYOPENCLC - This is a keyword in OpenCL C
250250
// KEYOPENCLCXX - This is a keyword in OpenCL C++
251+
// KEYSYCL - This is a keyword in SYCL C++
251252
// KEYNOOPENCL - This is a keyword that is not supported in OpenCL C
252253
// nor in OpenCL C++.
253254
// KEYALTIVEC - This is a keyword in AltiVec
@@ -530,10 +531,10 @@ KEYWORD(__unaligned , KEYMS)
530531
KEYWORD(__super , KEYMS)
531532

532533
// OpenCL address space qualifiers
533-
KEYWORD(__global , KEYOPENCLC | KEYOPENCLCXX)
534-
KEYWORD(__local , KEYOPENCLC | KEYOPENCLCXX)
535-
KEYWORD(__constant , KEYOPENCLC | KEYOPENCLCXX)
536-
KEYWORD(__private , KEYOPENCLC | KEYOPENCLCXX)
534+
KEYWORD(__global , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
535+
KEYWORD(__local , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
536+
KEYWORD(__constant , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
537+
KEYWORD(__private , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
537538
KEYWORD(__generic , KEYOPENCLC | KEYOPENCLCXX)
538539
ALIAS("global", __global , KEYOPENCLC)
539540
ALIAS("local", __local , KEYOPENCLC)

clang/lib/AST/ASTContext.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,12 @@ static const LangASMap *getAddressSpaceMap(const TargetInfo &T,
760760
4, // opencl_generic
761761
5, // cuda_device
762762
6, // cuda_constant
763-
7 // cuda_shared
763+
7, // cuda_shared
764+
1, // sycl_global
765+
3, // sycl_local
766+
2, // sycl_constant
767+
5, // sycl_private
768+
4, // sycl_generic
764769
};
765770
return &FakeAddrSpaceMap;
766771
} else {

clang/lib/AST/ItaniumMangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2224,7 +2224,7 @@ void CXXNameMangler::mangleQualifiers(Qualifiers Quals, const DependentAddressSp
22242224
if (Context.getASTContext().addressSpaceMapManglingFor(AS)) {
22252225
// <target-addrspace> ::= "AS" <address-space-number>
22262226
unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
2227-
if (TargetAS != 0)
2227+
if (TargetAS != 0 || (Context.getASTContext().getLangOpts().SYCL))
22282228
ASString = "AS" + llvm::utostr(TargetAS);
22292229
} else {
22302230
switch (AS) {

clang/lib/AST/TypePrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,15 +1747,21 @@ void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
17471747
addSpace = true;
17481748
switch (addrspace) {
17491749
case LangAS::opencl_global:
1750+
case LangAS::sycl_global:
17501751
OS << "__global";
17511752
break;
17521753
case LangAS::opencl_local:
1754+
case LangAS::sycl_local:
17531755
OS << "__local";
17541756
break;
17551757
case LangAS::opencl_private:
17561758
break;
1759+
case LangAS::sycl_private:
1760+
OS << "__private";
1761+
break;
17571762
case LangAS::opencl_constant:
17581763
case LangAS::cuda_constant:
1764+
case LangAS::sycl_constant:
17591765
OS << "__constant";
17601766
break;
17611767
case LangAS::opencl_generic:

clang/lib/Basic/IdentifierTable.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ namespace {
100100
KEYMODULES = 0x100000,
101101
KEYCXX2A = 0x200000,
102102
KEYOPENCLCXX = 0x400000,
103-
KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A,
104-
KEYALL = (0xffffff & ~KEYNOMS18 &
103+
KEYSYCL = 0x1000000,
104+
KEYALLCXX = KEYSYCL | KEYCXX | KEYCXX11 | KEYCXX2A,
105+
KEYALL = (0x1ffffff & ~KEYNOMS18 &
105106
~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
106107
};
107108

@@ -136,6 +137,7 @@ static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
136137
if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLC))
137138
return KS_Enabled;
138139
if (LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLCXX)) return KS_Enabled;
140+
if (LangOpts.SYCL && (Flags & KEYSYCL)) return KS_Enabled;
139141
if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
140142
if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
141143
// We treat bridge casts as objective-C keywords so we can warn on them

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
4646
Generic, // opencl_generic
4747
Global, // cuda_device
4848
Constant, // cuda_constant
49-
Local // cuda_shared
49+
Local, // cuda_shared
50+
Global, // sycl_global
51+
Local, // sycl_local
52+
Constant, // sycl_constant
53+
Private, // sycl_private
54+
Generic, // sycl_generic
5055
};
5156

5257
const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
@@ -58,7 +63,12 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
5863
Generic, // opencl_generic
5964
Global, // cuda_device
6065
Constant, // cuda_constant
61-
Local // cuda_shared
66+
Local, // cuda_shared
67+
Global, // sycl_global
68+
Local, // sycl_local
69+
Constant, // sycl_constant
70+
Private, // sycl_private
71+
Generic, // sycl_generic
6272
};
6373
} // namespace targets
6474
} // namespace clang

clang/lib/Basic/Targets/NVPTX.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ static const unsigned NVPTXAddrSpaceMap[] = {
3434
1, // cuda_device
3535
4, // cuda_constant
3636
3, // cuda_shared
37+
1, // sycl_global
38+
3, // sycl_local
39+
4, // sycl_constant
40+
5, // sycl_private
41+
// FIXME: generic has to be added to the target
42+
0, // sycl_generic
3743
};
3844

3945
class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {

clang/lib/Basic/Targets/SPIR.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ static const unsigned SPIRAddrSpaceMap[] = {
3131
4, // opencl_generic
3232
0, // cuda_device
3333
0, // cuda_constant
34-
0 // cuda_shared
34+
0, // cuda_shared
35+
1, // sycl_global
36+
3, // sycl_local
37+
2, // sycl_constant
38+
5, // sycl_private
39+
4, // sycl_generic
3540
};
3641

3742
class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {

clang/lib/Basic/Targets/TCE.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
4040
0, // opencl_generic
4141
0, // cuda_device
4242
0, // cuda_constant
43-
0 // cuda_shared
43+
0, // cuda_shared
44+
3, // sycl_global
45+
4, // sycl_local
46+
5, // sycl_constant
47+
5, // sycl_private
48+
// FIXME: generic has to be added to the target
49+
0, // sycl_generic
4450
};
4551

4652
class LLVM_LIBRARY_VISIBILITY TCETargetInfo : public TargetInfo {

clang/lib/CodeGen/CGCall.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,6 +4043,17 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
40434043
V->getType()->isIntegerTy())
40444044
V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
40454045

4046+
if (FirstIRArg < IRFuncTy->getNumParams()) {
4047+
const auto *LHSPtrTy =
4048+
dyn_cast_or_null<llvm::PointerType>(V->getType());
4049+
const auto *RHSPtrTy = dyn_cast_or_null<llvm::PointerType>(
4050+
IRFuncTy->getParamType(FirstIRArg));
4051+
if (LHSPtrTy && RHSPtrTy &&
4052+
LHSPtrTy->getAddressSpace() != RHSPtrTy->getAddressSpace())
4053+
V = Builder.CreateAddrSpaceCast(V,
4054+
IRFuncTy->getParamType(FirstIRArg));
4055+
}
4056+
40464057
// If the argument doesn't match, perform a bitcast to coerce it. This
40474058
// can happen due to trivial type mismatches.
40484059
if (FirstIRArg < IRFuncTy->getNumParams() &&

clang/lib/Sema/SemaType.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5785,14 +5785,35 @@ QualType Sema::BuildAddressSpaceAttr(QualType &T, Expr *AddrSpace,
57855785
llvm::APSInt max(addrSpace.getBitWidth());
57865786
max =
57875787
Qualifiers::MaxAddressSpace - (unsigned)LangAS::FirstTargetAddressSpace;
5788+
57885789
if (addrSpace > max) {
57895790
Diag(AttrLoc, diag::err_attribute_address_space_too_high)
57905791
<< (unsigned)max.getZExtValue() << AddrSpace->getSourceRange();
57915792
return QualType();
57925793
}
57935794

5794-
LangAS ASIdx =
5795-
getLangASFromTargetAS(static_cast<unsigned>(addrSpace.getZExtValue()));
5795+
if (LangOpts.SYCL && (addrSpace == 4 || addrSpace > 5)) {
5796+
Diag(AttrLoc, diag::err_sycl_attribute_address_space_invalid)
5797+
<< AddrSpace->getSourceRange();
5798+
return QualType();
5799+
}
5800+
5801+
LangAS ASIdx = getLangASFromTargetAS(
5802+
static_cast<unsigned>(addrSpace.getZExtValue()));
5803+
5804+
if (LangOpts.SYCL) {
5805+
ASIdx =
5806+
[](unsigned AS) {
5807+
switch (AS) {
5808+
case 5: case 0: return LangAS::sycl_private;
5809+
case 1: return LangAS::sycl_global;
5810+
case 2: return LangAS::sycl_constant;
5811+
case 3: return LangAS::sycl_local;
5812+
case 4: default: llvm_unreachable("Invalid SYCL AS");
5813+
}
5814+
}(static_cast<unsigned>(ASIdx) -
5815+
static_cast<unsigned>(LangAS::FirstTargetAddressSpace));
5816+
}
57965817

57975818
// If this type is already address space qualified with a different
57985819
// address space, reject it.
@@ -5886,15 +5907,23 @@ static void HandleAddressSpaceTypeAttribute(QualType &Type,
58865907
// The keyword-based type attributes imply which address space to use.
58875908
switch (Attr.getKind()) {
58885909
case ParsedAttr::AT_OpenCLGlobalAddressSpace:
5889-
ASIdx = LangAS::opencl_global; break;
5910+
ASIdx =
5911+
S.getLangOpts().SYCL ? LangAS::sycl_global : LangAS::opencl_global;
5912+
break;
58905913
case ParsedAttr::AT_OpenCLLocalAddressSpace:
5891-
ASIdx = LangAS::opencl_local; break;
5914+
ASIdx = S.getLangOpts().SYCL ? LangAS::sycl_local : LangAS::opencl_local;
5915+
break;
58925916
case ParsedAttr::AT_OpenCLConstantAddressSpace:
5893-
ASIdx = LangAS::opencl_constant; break;
5917+
ASIdx = S.getLangOpts().SYCL ? LangAS::sycl_constant
5918+
: LangAS::opencl_constant;
5919+
break;
58945920
case ParsedAttr::AT_OpenCLGenericAddressSpace:
5895-
ASIdx = LangAS::opencl_generic; break;
5921+
ASIdx = LangAS::opencl_generic;
5922+
break;
58965923
case ParsedAttr::AT_OpenCLPrivateAddressSpace:
5897-
ASIdx = LangAS::opencl_private; break;
5924+
ASIdx =
5925+
S.getLangOpts().SYCL ? LangAS::sycl_private : LangAS::opencl_private;
5926+
break;
58985927
default:
58995928
llvm_unreachable("Invalid address space");
59005929
}

0 commit comments

Comments
 (0)