Index: cfe/trunk/include/clang/Basic/Builtins.def =================================================================== --- cfe/trunk/include/clang/Basic/Builtins.def +++ cfe/trunk/include/clang/Basic/Builtins.def @@ -1305,6 +1305,11 @@ LANGBUILTIN(get_pipe_num_packets, "Ui.", "tn", OCLC_LANG) LANGBUILTIN(get_pipe_max_packets, "Ui.", "tn", OCLC_LANG) +// OpenCL v2.0 s6.13.9 - Address space qualifier functions. +LANGBUILTIN(to_global, "v*v*", "tn", OCLC_LANG) +LANGBUILTIN(to_local, "v*v*", "tn", OCLC_LANG) +LANGBUILTIN(to_private, "v*v*", "tn", OCLC_LANG) + #undef BUILTIN #undef LIBBUILTIN #undef LANGBUILTIN Index: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td @@ -7856,6 +7856,8 @@ def warn_opencl_attr_deprecated_ignored : Warning < "%0 attribute is deprecated and ignored in OpenCL version %1">, InGroup; +def err_opencl_builtin_requires_version : Error< + "%0 requires OpenCL version %1%select{| or above}2">; // OpenCL v2.0 s6.13.6 -- Builtin Pipe Functions def err_opencl_builtin_pipe_first_arg : Error< @@ -7887,6 +7889,11 @@ def err_opencl_extern_block_declaration : Error< "invalid block variable declaration - using 'extern' storage class is disallowed">; +// OpenCL v2.0 s6.13.9 - Address space qualifier functions. +def err_opencl_builtin_to_addr_arg_num : Error< + "invalid number of arguments to function: %0">; +def err_opencl_builtin_to_addr_invalid_arg : Error< + "invalid argument %0 to function: %1, expecting a generic pointer argument">; } // end of sema category let CategoryName = "OpenMP Issue" in { Index: cfe/trunk/lib/CodeGen/CGBuiltin.cpp =================================================================== --- cfe/trunk/lib/CodeGen/CGBuiltin.cpp +++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp @@ -2124,6 +2124,29 @@ Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name), {Arg0})); } + // OpenCL v2.0 s6.13.9 - Address space qualifier functions. + case Builtin::BIto_global: + case Builtin::BIto_local: + case Builtin::BIto_private: { + auto Arg0 = EmitScalarExpr(E->getArg(0)); + auto NewArgT = llvm::PointerType::get(Int8Ty, + CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic)); + auto NewRetT = llvm::PointerType::get(Int8Ty, + CGM.getContext().getTargetAddressSpace( + E->getType()->getPointeeType().getAddressSpace())); + auto FTy = llvm::FunctionType::get(NewRetT, {NewArgT}, false); + llvm::Value *NewArg; + if (Arg0->getType()->getPointerAddressSpace() != + NewArgT->getPointerAddressSpace()) + NewArg = Builder.CreateAddrSpaceCast(Arg0, NewArgT); + else + NewArg = Builder.CreateBitOrPointerCast(Arg0, NewArgT); + auto NewCall = Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, + E->getDirectCallee()->getName()), {NewArg}); + return RValue::get(Builder.CreateBitOrPointerCast(NewCall, + ConvertType(E->getType()))); + } + case Builtin::BIprintf: if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) return EmitCUDADevicePrintfCallExpr(E, ReturnValue); Index: cfe/trunk/lib/Sema/SemaChecking.cpp =================================================================== --- cfe/trunk/lib/Sema/SemaChecking.cpp +++ cfe/trunk/lib/Sema/SemaChecking.cpp @@ -455,6 +455,52 @@ return false; } +// \brief Performs semantic analysis for the to_global/local/private call. +// \param S Reference to the semantic analyzer. +// \param BuiltinID ID of the builtin function. +// \param Call A pointer to the builtin call. +// \return True if a semantic error has been found, false otherwise. +static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID, + CallExpr *Call) { + // OpenCL v2.0 s6.13.9 - Address space qualifier functions. + if (S.getLangOpts().OpenCLVersion < 200) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_requires_version) + << Call->getDirectCallee() << "2.0" << 1 << Call->getSourceRange(); + return true; + } + + if (Call->getNumArgs() != 1) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_to_addr_arg_num) + << Call->getDirectCallee() << Call->getSourceRange(); + return true; + } + + auto RT = Call->getArg(0)->getType(); + if (!RT->isPointerType() || RT->getPointeeType() + .getAddressSpace() == LangAS::opencl_constant) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_to_addr_invalid_arg) + << Call->getArg(0) << Call->getDirectCallee() << Call->getSourceRange(); + return true; + } + + RT = RT->getPointeeType(); + auto Qual = RT.getQualifiers(); + switch (BuiltinID) { + case Builtin::BIto_global: + Qual.setAddressSpace(LangAS::opencl_global); + break; + case Builtin::BIto_local: + Qual.setAddressSpace(LangAS::opencl_local); + break; + default: + Qual.removeAddressSpace(); + } + Call->setType(S.Context.getPointerType(S.Context.getQualifiedType( + RT.getUnqualifiedType(), Qual))); + + return false; +} + ExprResult Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, CallExpr *TheCall) { @@ -789,6 +835,12 @@ if (SemaBuiltinPipePackets(*this, TheCall)) return ExprError(); break; + case Builtin::BIto_global: + case Builtin::BIto_local: + case Builtin::BIto_private: + if (SemaOpenCLBuiltinToAddr(*this, BuiltinID, TheCall)) + return ExprError(); + break; } // Since the target specific builtins for each arch overlap, only check those Index: cfe/trunk/test/CodeGenOpenCL/to_addr_builtin.cl =================================================================== --- cfe/trunk/test/CodeGenOpenCL/to_addr_builtin.cl +++ cfe/trunk/test/CodeGenOpenCL/to_addr_builtin.cl @@ -0,0 +1,89 @@ +// RUN: %clang_cc1 -triple spir-unknown-unknown -emit-llvm -O0 -cl-std=CL2.0 -o - %s | FileCheck %s + +// CHECK: %[[A:.*]] = type { float, float, float } +typedef struct { + float x,y,z; +} A; +typedef private A *PA; +typedef global A *GA; + +void test(void) { + global int *glob; + local int *loc; + private int *priv; + generic int *gen; + + //CHECK: %[[ARG:.*]] = addrspacecast i32 addrspace(1)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(1)* @to_global(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(1)* %[[RET]] to i32 addrspace(1)* + glob = to_global(glob); + + //CHECK: %[[ARG:.*]] = addrspacecast i32 addrspace(3)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(1)* @to_global(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(1)* %[[RET]] to i32 addrspace(1)* + glob = to_global(loc); + + //CHECK: %[[ARG:.*]] = addrspacecast i32* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(1)* @to_global(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(1)* %[[RET]] to i32 addrspace(1)* + glob = to_global(priv); + + //CHECK: %[[ARG:.*]] = bitcast i32 addrspace(4)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(1)* @to_global(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(1)* %[[RET]] to i32 addrspace(1)* + glob = to_global(gen); + + //CHECK: %[[ARG:.*]] = addrspacecast i32 addrspace(1)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(3)* @to_local(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(3)* %[[RET]] to i32 addrspace(3)* + loc = to_local(glob); + + //CHECK: %[[ARG:.*]] = addrspacecast i32 addrspace(3)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(3)* @to_local(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(3)* %[[RET]] to i32 addrspace(3)* + loc = to_local(loc); + + //CHECK: %[[ARG:.*]] = addrspacecast i32* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(3)* @to_local(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(3)* %[[RET]] to i32 addrspace(3)* + loc = to_local(priv); + + //CHECK: %[[ARG:.*]] = bitcast i32 addrspace(4)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(3)* @to_local(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(3)* %[[RET]] to i32 addrspace(3)* + loc = to_local(gen); + + //CHECK: %[[ARG:.*]] = addrspacecast i32 addrspace(1)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8* @to_private(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8* %[[RET]] to i32* + priv = to_private(glob); + + //CHECK: %[[ARG:.*]] = addrspacecast i32 addrspace(3)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8* @to_private(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8* %[[RET]] to i32* + priv = to_private(loc); + + //CHECK: %[[ARG:.*]] = addrspacecast i32* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8* @to_private(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8* %[[RET]] to i32* + priv = to_private(priv); + + //CHECK: %[[ARG:.*]] = bitcast i32 addrspace(4)* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8* @to_private(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8* %[[RET]] to i32* + priv = to_private(gen); + + //CHECK: %[[ARG:.*]] = addrspacecast %[[A]]* %{{.*}} to i8 addrspace(4)* + //CHECK: %[[RET:.*]] = call i8 addrspace(1)* @to_global(i8 addrspace(4)* %[[ARG]]) + //CHECK: %{{.*}} = bitcast i8 addrspace(1)* %[[RET]] to %[[A]] addrspace(1)* + PA pA; + GA gA = to_global(pA); + + //CHECK-NOT: addrspacecast + //CHECK-NOT: bitcast + //CHECK: call i8 addrspace(1)* @to_global(i8 addrspace(4)* %{{.*}}) + //CHECK-NOT: addrspacecast + //CHECK-NOT: bitcast + generic void *gen_v; + global void *glob_v = to_global(gen_v); +} Index: cfe/trunk/test/SemaOpenCL/to_addr_builtin.cl =================================================================== --- cfe/trunk/test/SemaOpenCL/to_addr_builtin.cl +++ cfe/trunk/test/SemaOpenCL/to_addr_builtin.cl @@ -0,0 +1,54 @@ +// RUN: %clang_cc1 -verify -fsyntax-only %s +// RUN: %clang_cc1 -verify -fsyntax-only -cl-std=CL2.0 %s + +void test(void) { + global int *glob; + local int *loc; + constant int *con; + typedef constant int const_int_ty; + const_int_ty *con_typedef; + + glob = to_global(glob, loc); +#if __OPENCL_C_VERSION__ < CL_VERSION_2_0 + // expected-error@-2{{'to_global' requires OpenCL version 2.0 or above}} +#else + // expected-error@-4{{invalid number of arguments to function: 'to_global'}} +#endif + + int x; + glob = to_global(x); +#if __OPENCL_C_VERSION__ < CL_VERSION_2_0 + // expected-error@-2{{'to_global' requires OpenCL version 2.0 or above}} +#else + // expected-error@-4{{invalid argument x to function: 'to_global', expecting a generic pointer argument}} +#endif + + glob = to_global(con); +#if __OPENCL_C_VERSION__ < CL_VERSION_2_0 + // expected-error@-2{{'to_global' requires OpenCL version 2.0 or above}} +#else + // expected-error@-4{{invalid argument con to function: 'to_global', expecting a generic pointer argument}} +#endif + + glob = to_global(con_typedef); +#if __OPENCL_C_VERSION__ < CL_VERSION_2_0 + // expected-error@-2{{'to_global' requires OpenCL version 2.0 or above}} +#else + // expected-error@-4{{invalid argument con_typedef to function: 'to_global', expecting a generic pointer argument}} +#endif + + loc = to_global(glob); +#if __OPENCL_C_VERSION__ < CL_VERSION_2_0 + // expected-error@-2{{'to_global' requires OpenCL version 2.0 or above}} +#else + // expected-error@-4{{assigning '__global int *' to '__local int *' changes address space of pointer}} +#endif + + global char *glob_c = to_global(loc); +#if __OPENCL_C_VERSION__ < CL_VERSION_2_0 + // expected-error@-2{{'to_global' requires OpenCL version 2.0 or above}} +#else + // expected-warning@-4{{incompatible pointer types initializing '__global char *' with an expression of type '__global int *'}} +#endif + +}