Skip to content

Commit

Permalink
[SYCL] Implement SYCL address-space rules.
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Lazarev <vladimir.lazarev@intel.com>
  • Loading branch information
vladimirlaz committed Jan 22, 2019
1 parent effac35 commit c278543
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 26 deletions.
8 changes: 7 additions & 1 deletion clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,13 @@ class Qualifiers {
// Otherwise in OpenCLC v2.0 s6.5.5: every address space except
// for __constant can be used as __generic.
(getAddressSpace() == LangAS::opencl_generic &&
other.getAddressSpace() != LangAS::opencl_constant);
other.getAddressSpace() != LangAS::opencl_constant) ||
(!hasAddressSpace() &&
(other.getAddressSpace() == LangAS::sycl_private ||
other.getAddressSpace() == LangAS::sycl_local ||
other.getAddressSpace() == LangAS::sycl_global ||
other.getAddressSpace() == LangAS::sycl_constant ||
other.getAddressSpace() == LangAS::sycl_generic));
}

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

sycl_global,
sycl_local,
sycl_constant,
sycl_private,
// Likely never used, but useful in the future to reserve the spot in the
// enum.
sycl_generic,
// This denotes the count of language-specific address spaces and also
// the offset added to the target-specific address spaces, which are usually
// specified by address space attributes __attribute__(address_space(n))).
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -9482,4 +9482,7 @@ def err_builtin_launder_invalid_arg : Error<
"%select{non-pointer|function pointer|void pointer}0 argument to "
"'__builtin_launder' is not allowed">;

def err_sycl_attribute_address_space_invalid : Error<
"address space is outside the valid range of values">;

} // end of sema component.
9 changes: 5 additions & 4 deletions clang/include/clang/Basic/TokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ PUNCTUATOR(caretcaret, "^^")
// MSVC <= v18.
// KEYOPENCLC - This is a keyword in OpenCL C
// KEYOPENCLCXX - This is a keyword in OpenCL C++
// KEYSYCL - This is a keyword in SYCL C++
// KEYNOOPENCL - This is a keyword that is not supported in OpenCL C
// nor in OpenCL C++.
// KEYALTIVEC - This is a keyword in AltiVec
Expand Down Expand Up @@ -530,10 +531,10 @@ KEYWORD(__unaligned , KEYMS)
KEYWORD(__super , KEYMS)

// OpenCL address space qualifiers
KEYWORD(__global , KEYOPENCLC | KEYOPENCLCXX)
KEYWORD(__local , KEYOPENCLC | KEYOPENCLCXX)
KEYWORD(__constant , KEYOPENCLC | KEYOPENCLCXX)
KEYWORD(__private , KEYOPENCLC | KEYOPENCLCXX)
KEYWORD(__global , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
KEYWORD(__local , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
KEYWORD(__constant , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
KEYWORD(__private , KEYOPENCLC | KEYOPENCLCXX | KEYSYCL)
KEYWORD(__generic , KEYOPENCLC | KEYOPENCLCXX)
ALIAS("global", __global , KEYOPENCLC)
ALIAS("local", __local , KEYOPENCLC)
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,12 @@ static const LangASMap *getAddressSpaceMap(const TargetInfo &T,
4, // opencl_generic
5, // cuda_device
6, // cuda_constant
7 // cuda_shared
7, // cuda_shared
1, // sycl_global
3, // sycl_local
2, // sycl_constant
5, // sycl_private
4, // sycl_generic
};
return &FakeAddrSpaceMap;
} else {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,7 @@ 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)
if (TargetAS != 0 || (Context.getASTContext().getLangOpts().SYCL))
ASString = "AS" + llvm::utostr(TargetAS);
} else {
switch (AS) {
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 @@ -1747,15 +1747,21 @@ void Qualifiers::print(raw_ostream &OS, const PrintingPolicy& Policy,
addSpace = true;
switch (addrspace) {
case LangAS::opencl_global:
case LangAS::sycl_global:
OS << "__global";
break;
case LangAS::opencl_local:
case LangAS::sycl_local:
OS << "__local";
break;
case LangAS::opencl_private:
break;
case LangAS::sycl_private:
OS << "__private";
break;
case LangAS::opencl_constant:
case LangAS::cuda_constant:
case LangAS::sycl_constant:
OS << "__constant";
break;
case LangAS::opencl_generic:
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Basic/IdentifierTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ namespace {
KEYMODULES = 0x100000,
KEYCXX2A = 0x200000,
KEYOPENCLCXX = 0x400000,
KEYALLCXX = KEYCXX | KEYCXX11 | KEYCXX2A,
KEYALL = (0xffffff & ~KEYNOMS18 &
KEYSYCL = 0x1000000,
KEYALLCXX = KEYSYCL | KEYCXX | KEYCXX11 | KEYCXX2A,
KEYALL = (0x1ffffff & ~KEYNOMS18 &
~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
};

Expand Down Expand Up @@ -136,6 +137,7 @@ static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLC))
return KS_Enabled;
if (LangOpts.OpenCLCPlusPlus && (Flags & KEYOPENCLCXX)) return KS_Enabled;
if (LangOpts.SYCL && (Flags & KEYSYCL)) return KS_Enabled;
if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
// We treat bridge casts as objective-C keywords so we can warn on them
Expand Down
14 changes: 12 additions & 2 deletions clang/lib/Basic/Targets/AMDGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
Generic, // opencl_generic
Global, // cuda_device
Constant, // cuda_constant
Local // cuda_shared
Local, // cuda_shared
Global, // sycl_global
Local, // sycl_local
Constant, // sycl_constant
Private, // sycl_private
Generic, // sycl_generic
};

const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
Expand All @@ -58,7 +63,12 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
Generic, // opencl_generic
Global, // cuda_device
Constant, // cuda_constant
Local // cuda_shared
Local, // cuda_shared
Global, // sycl_global
Local, // sycl_local
Constant, // sycl_constant
Private, // sycl_private
Generic, // sycl_generic
};
} // namespace targets
} // namespace clang
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Basic/Targets/NVPTX.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ static const unsigned NVPTXAddrSpaceMap[] = {
1, // cuda_device
4, // cuda_constant
3, // cuda_shared
1, // sycl_global
3, // sycl_local
4, // sycl_constant
5, // sycl_private
// FIXME: generic has to be added to the target
0, // sycl_generic
};

class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Basic/Targets/SPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ static const unsigned SPIRAddrSpaceMap[] = {
4, // opencl_generic
0, // cuda_device
0, // cuda_constant
0 // cuda_shared
0, // cuda_shared
1, // sycl_global
3, // sycl_local
2, // sycl_constant
5, // sycl_private
4, // sycl_generic
};

class LLVM_LIBRARY_VISIBILITY SPIRTargetInfo : public TargetInfo {
Expand Down
8 changes: 7 additions & 1 deletion clang/lib/Basic/Targets/TCE.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
0, // opencl_generic
0, // cuda_device
0, // cuda_constant
0 // cuda_shared
0, // cuda_shared
3, // sycl_global
4, // sycl_local
5, // sycl_constant
5, // sycl_private
// FIXME: generic has to be added to the target
0, // sycl_generic
};

class LLVM_LIBRARY_VISIBILITY TCETargetInfo : public TargetInfo {
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,17 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
V->getType()->isIntegerTy())
V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());

if (FirstIRArg < IRFuncTy->getNumParams()) {
const auto *LHSPtrTy =
dyn_cast_or_null<llvm::PointerType>(V->getType());
const auto *RHSPtrTy = dyn_cast_or_null<llvm::PointerType>(
IRFuncTy->getParamType(FirstIRArg));
if (LHSPtrTy && RHSPtrTy &&
LHSPtrTy->getAddressSpace() != RHSPtrTy->getAddressSpace())
V = Builder.CreateAddrSpaceCast(V,
IRFuncTy->getParamType(FirstIRArg));
}

// If the argument doesn't match, perform a bitcast to coerce it. This
// can happen due to trivial type mismatches.
if (FirstIRArg < IRFuncTy->getNumParams() &&
Expand Down
43 changes: 36 additions & 7 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5785,14 +5785,35 @@ QualType Sema::BuildAddressSpaceAttr(QualType &T, Expr *AddrSpace,
llvm::APSInt max(addrSpace.getBitWidth());
max =
Qualifiers::MaxAddressSpace - (unsigned)LangAS::FirstTargetAddressSpace;

if (addrSpace > max) {
Diag(AttrLoc, diag::err_attribute_address_space_too_high)
<< (unsigned)max.getZExtValue() << AddrSpace->getSourceRange();
return QualType();
}

LangAS ASIdx =
getLangASFromTargetAS(static_cast<unsigned>(addrSpace.getZExtValue()));
if (LangOpts.SYCL && (addrSpace == 4 || addrSpace > 5)) {
Diag(AttrLoc, diag::err_sycl_attribute_address_space_invalid)
<< AddrSpace->getSourceRange();
return QualType();
}

LangAS ASIdx = getLangASFromTargetAS(
static_cast<unsigned>(addrSpace.getZExtValue()));

if (LangOpts.SYCL) {
ASIdx =
[](unsigned AS) {
switch (AS) {
case 5: case 0: return LangAS::sycl_private;
case 1: return LangAS::sycl_global;
case 2: return LangAS::sycl_constant;
case 3: return LangAS::sycl_local;
case 4: default: llvm_unreachable("Invalid SYCL AS");
}
}(static_cast<unsigned>(ASIdx) -
static_cast<unsigned>(LangAS::FirstTargetAddressSpace));
}

// If this type is already address space qualified with a different
// address space, reject it.
Expand Down Expand Up @@ -5886,15 +5907,23 @@ static void HandleAddressSpaceTypeAttribute(QualType &Type,
// The keyword-based type attributes imply which address space to use.
switch (Attr.getKind()) {
case ParsedAttr::AT_OpenCLGlobalAddressSpace:
ASIdx = LangAS::opencl_global; break;
ASIdx =
S.getLangOpts().SYCL ? LangAS::sycl_global : LangAS::opencl_global;
break;
case ParsedAttr::AT_OpenCLLocalAddressSpace:
ASIdx = LangAS::opencl_local; break;
ASIdx = S.getLangOpts().SYCL ? LangAS::sycl_local : LangAS::opencl_local;
break;
case ParsedAttr::AT_OpenCLConstantAddressSpace:
ASIdx = LangAS::opencl_constant; break;
ASIdx = S.getLangOpts().SYCL ? LangAS::sycl_constant
: LangAS::opencl_constant;
break;
case ParsedAttr::AT_OpenCLGenericAddressSpace:
ASIdx = LangAS::opencl_generic; break;
ASIdx = LangAS::opencl_generic;
break;
case ParsedAttr::AT_OpenCLPrivateAddressSpace:
ASIdx = LangAS::opencl_private; break;
ASIdx =
S.getLangOpts().SYCL ? LangAS::sycl_private : LangAS::opencl_private;
break;
default:
llvm_unreachable("Invalid address space");
}
Expand Down
Loading

0 comments on commit c278543

Please sign in to comment.