From 32b994bca66641cdac8586f25315daf349921ebc Mon Sep 17 00:00:00 2001 From: Justas Janickas Date: Wed, 15 Sep 2021 11:53:43 +0100 Subject: [PATCH] [OpenCL] Defines helper function for OpenCL default address space Helper function `getDefaultOpenCLPointeeAddrSpace()` introduced to `ASTContext` class. It returns default OpenCL address space depending on language version and enabled features. If generic address space is supported, the helper function returns value `LangAS::opencl_generic`. Otherwise, value `LangAS::opencl_private` is returned. Code refactoring changes performed in several suitable places. Differential Revision: https://reviews.llvm.org/D109874 --- clang/include/clang/AST/ASTContext.h | 6 ++++++ clang/lib/AST/Expr.cpp | 7 ++----- clang/lib/Sema/Sema.cpp | 2 +- clang/lib/Sema/SemaTemplateDeduction.cpp | 5 +++-- clang/lib/Sema/SemaType.cpp | 4 +--- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index b8f7c3aae260..f1b057c8003f 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -1362,6 +1362,12 @@ public: /// Get address space for OpenCL type. LangAS getOpenCLTypeAddrSpace(const Type *T) const; + /// Returns default address space based on OpenCL version and enabled features + inline LangAS getDefaultOpenCLPointeeAddrSpace() { + return LangOpts.OpenCLGenericAddressSpace ? LangAS::opencl_generic + : LangAS::opencl_private; + } + void setcudaConfigureCallDecl(FunctionDecl *FD) { cudaConfigureCallDecl = FD; } diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 31733932361f..9fde21c7ee9e 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -3779,11 +3779,8 @@ Expr::isNullPointerConstant(ASTContext &Ctx, // has non-default address space it is not treated as nullptr. // (__generic void*)0 in OpenCL 2.0 should not be treated as nullptr // since it cannot be assigned to a pointer to constant address space. - if ((Ctx.getLangOpts().OpenCLVersion >= 200 && - Pointee.getAddressSpace() == LangAS::opencl_generic) || - (Ctx.getLangOpts().OpenCL && - Ctx.getLangOpts().OpenCLVersion < 200 && - Pointee.getAddressSpace() == LangAS::opencl_private)) + if (Ctx.getLangOpts().OpenCL && + Pointee.getAddressSpace() == Ctx.getDefaultOpenCLPointeeAddrSpace()) Qs.removeAddressSpace(); if (Pointee->isVoidType() && Qs.empty() && // to void* diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 0b936d60fc5e..c997c70d17ce 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -1430,7 +1430,7 @@ NamedDecl *Sema::getCurFunctionOrMethodDecl() { LangAS Sema::getDefaultCXXMethodAddrSpace() const { if (getLangOpts().OpenCL) - return LangAS::opencl_generic; + return getASTContext().getDefaultOpenCLPointeeAddrSpace(); return LangAS::Default; } diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp index 826abbd8c362..060f42bf9d64 100644 --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -3894,8 +3894,9 @@ static bool AdjustFunctionParmAndArgTypesForDeduction( // "lvalue reference to A" is used in place of A for type deduction. if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && Arg->isLValue()) { - if (S.getLangOpts().OpenCL && !ArgType.hasAddressSpace()) - ArgType = S.Context.getAddrSpaceQualType(ArgType, LangAS::opencl_generic); + if (S.getLangOpts().OpenCL && !ArgType.hasAddressSpace()) + ArgType = S.Context.getAddrSpaceQualType( + ArgType, S.Context.getDefaultOpenCLPointeeAddrSpace()); ArgType = S.Context.getLValueReferenceType(ArgType); } } else { diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index fcc2cf055be9..d2d54281c63e 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -2092,9 +2092,7 @@ static QualType deduceOpenCLPointeeAddrSpace(Sema &S, QualType PointeeType) { !PointeeType->isSamplerT() && !PointeeType.hasAddressSpace()) PointeeType = S.getASTContext().getAddrSpaceQualType( - PointeeType, S.getLangOpts().OpenCLGenericAddressSpace - ? LangAS::opencl_generic - : LangAS::opencl_private); + PointeeType, S.getASTContext().getDefaultOpenCLPointeeAddrSpace()); return PointeeType; }