Index: include/clang/Basic/Builtins.h =================================================================== --- include/clang/Basic/Builtins.h +++ include/clang/Basic/Builtins.h @@ -36,6 +36,7 @@ CXX_LANG = 0x4, // builtin for cplusplus only. OBJC_LANG = 0x8, // builtin for objective-c and objective-c++ MS_LANG = 0x10, // builtin requires MS mode. + OCLC_LANG = 0x20,// builtin for OpenCL C only. ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages. ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode. ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG // builtin requires MS mode. Index: include/clang/Basic/Builtins.def =================================================================== --- include/clang/Basic/Builtins.def +++ include/clang/Basic/Builtins.def @@ -1252,6 +1252,32 @@ BUILTIN(__builtin_nontemporal_store, "v.", "t") BUILTIN(__builtin_nontemporal_load, "v.", "t") +// OpenCL v2.0 s6.13.16, s9.17.3.5 -- Pipe functions. +// We need the generic prototype, since the packet type could be anything. +LANGBUILTIN(read_pipe, "i.", "tn", OCLC_LANG) +LANGBUILTIN(write_pipe, "i.", "tn", OCLC_LANG) + +LANGBUILTIN(reserve_read_pipe, "i.", "tn", OCLC_LANG) +LANGBUILTIN(reserve_write_pipe, "i.", "tn", OCLC_LANG) + +LANGBUILTIN(commit_write_pipe, "v.", "tn", OCLC_LANG) +LANGBUILTIN(commit_read_pipe, "v.", "tn", OCLC_LANG) + +LANGBUILTIN(sub_group_reserve_read_pipe, "i.", "tn", OCLC_LANG) +LANGBUILTIN(sub_group_reserve_write_pipe, "i.", "tn", OCLC_LANG) + +LANGBUILTIN(sub_group_commit_read_pipe, "v.", "tn", OCLC_LANG) +LANGBUILTIN(sub_group_commit_write_pipe, "v.", "tn", OCLC_LANG) + +LANGBUILTIN(work_group_reserve_read_pipe, "i.", "tn", OCLC_LANG) +LANGBUILTIN(work_group_reserve_write_pipe, "i.", "tn", OCLC_LANG) + +LANGBUILTIN(work_group_commit_read_pipe, "v.", "tn", OCLC_LANG) +LANGBUILTIN(work_group_commit_write_pipe, "v.", "tn", OCLC_LANG) + +LANGBUILTIN(get_pipe_num_packets, "Ui.", "tn", OCLC_LANG) +LANGBUILTIN(get_pipe_max_packets, "Ui.", "tn", OCLC_LANG) + #undef BUILTIN #undef LIBBUILTIN #undef LANGBUILTIN Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -7654,6 +7654,16 @@ def err_opencl_implicit_vector_conversion : Error< "implicit conversions between vector types (%0 and %1) are not permitted">; +// OpenCL v2.0 s6.13.6 -- Builtin Pipe Functions +def err_opencl_builtin_pipe_first_arg : Error< + "first argument to %0 must be a pipe type">; +def err_opencl_builtin_pipe_arg_num : Error< + "invalid number of arguments to function: %0">; +def err_opencl_builtin_pipe_invalid_arg : Error< + "invalid argument type to function %0 (expecting: %1)">; +def err_opencl_builtin_pipe_invalid_access_modifier : Error< + "invalid pipe access modifier (expecting %0)">; + // OpenCL Section 6.8.g def err_opencl_unknown_type_specifier : Error< "OpenCL does not support the '%0' %select{type qualifier|storage class specifier}1">; Index: lib/Basic/Builtins.cpp =================================================================== --- lib/Basic/Builtins.cpp +++ lib/Basic/Builtins.cpp @@ -59,7 +59,8 @@ bool MSModeUnsupported = !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG); bool ObjCUnsupported = !LangOpts.ObjC1 && BuiltinInfo.Langs == OBJC_LANG; - return !BuiltinsUnsupported && !MathBuiltinsUnsupported && + bool OclCUnsupported = !LangOpts.OpenCL && BuiltinInfo.Langs == OCLC_LANG; + return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported && !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported; } Index: lib/CodeGen/CGBuiltin.cpp =================================================================== --- lib/CodeGen/CGBuiltin.cpp +++ lib/CodeGen/CGBuiltin.cpp @@ -14,6 +14,7 @@ #include "CodeGenFunction.h" #include "CGCXXABI.h" #include "CGObjCRuntime.h" +#include "CGOpenCLRuntime.h" #include "CodeGenModule.h" #include "TargetInfo.h" #include "clang/AST/ASTContext.h" @@ -1963,6 +1964,158 @@ return RValue::get(llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy)); break; } + + case Builtin::BIread_pipe: + case Builtin::BIwrite_pipe: { + llvm::LLVMContext &Ctxt = getLLVMContext(); + Value *Arg0 = EmitScalarExpr(E->getArg(0)), + *Arg1 = EmitScalarExpr(E->getArg(1)); + Value *PacketSize = CGOpenCLRuntime(CGM).getPipeElemSize(E->getArg(0)); + + // Type of the packet parameter. + llvm::Type *I8PTy = llvm::PointerType::get(llvm::Type::getInt8Ty(Ctxt), 4U); + + // Testing which overloaded version we should generate the call for. + if (2U == E->getNumArgs()) { + // The name is mangled with hard-coded, since we would like to avoid the + // AST rewrite. + const char *Name = + (BuiltinID == Builtin::BIread_pipe) + ? "_Z9read_pipePU3AS110ocl_pipe_tPU3AS4vi" + : "_Z10write_pipePU3AS110ocl_pipe_tPU3AS4vi"; + // Re-Creating the function type for this call, since the original type + // is variadic, we make them to be generic type. + llvm::Type *ArgTys[] = {Arg0->getType(), I8PTy, Int32Ty}; + llvm::FunctionType *FTy = llvm::FunctionType::get( + Int32Ty, llvm::ArrayRef(ArgTys), false); + Value *BCast = Builder.CreatePointerCast(Arg1, I8PTy); + return RValue::get(Builder.CreateCall( + CGM.CreateRuntimeFunction(FTy, Name), {Arg0, BCast, PacketSize})); + } else { + assert(4 == E->getNumArgs() && + "Illegal number of parameters to pipe function"); + const char *Name = + (BuiltinID == Builtin::BIread_pipe) + ? "_Z9read_pipePU3AS110ocl_pipe_t16ocl_reserve_id_tjPU3AS4vi" + : "_Z10write_pipePU3AS110ocl_pipe_t16ocl_reserve_id_tjPU3AS4vi"; + + llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty, I8PTy, + Int32Ty}; + Value *Arg2 = EmitScalarExpr(E->getArg(2)), + *Arg3 = EmitScalarExpr(E->getArg(3)); + llvm::FunctionType *FTy = llvm::FunctionType::get( + Int32Ty, llvm::ArrayRef(ArgTys), false); + Value *BCast = Builder.CreatePointerCast(Arg3, I8PTy); + // We know the third argument is an integer type (Verified by Sema), but + // we may need to cast it i32. + if (Arg2->getType() != Int32Ty) + Arg2 = Builder.CreateZExtOrTrunc(Arg2, Int32Ty); + return RValue::get( + Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name), + {Arg0, Arg1, Arg2, BCast, PacketSize})); + } + } + case Builtin::BIreserve_read_pipe: + case Builtin::BIreserve_write_pipe: + case Builtin::BIwork_group_reserve_read_pipe: + case Builtin::BIwork_group_reserve_write_pipe: + case Builtin::BIsub_group_reserve_read_pipe: + case Builtin::BIsub_group_reserve_write_pipe: { + // Composing the mangled name for the function. + const char *Name; + if (BuiltinID == Builtin::BIreserve_read_pipe) + Name = "_Z17reserve_read_pipePU3AS110ocl_pipe_tji"; + else if (BuiltinID == Builtin::BIreserve_write_pipe) + Name = "_Z18reserve_write_pipePU3AS110ocl_pipe_tji"; + else if (BuiltinID == Builtin::BIwork_group_reserve_read_pipe) + Name = "_Z28work_group_reserve_read_pipePU3AS110ocl_pipe_tji"; + else if (BuiltinID == Builtin::BIwork_group_reserve_write_pipe) + Name = "_Z29work_group_reserve_write_pipePU3AS110ocl_pipe_tji"; + else if (BuiltinID == Builtin::BIsub_group_reserve_read_pipe) + Name = "_Z27sub_group_reserve_read_pipePU3AS110ocl_pipe_tji"; + else + Name = "_Z28sub_group_reserve_write_pipePU3AS110ocl_pipe_tji"; + + CGOpenCLRuntime CGOCL(CGM); + + //Value *PacketSize = CGOCL.getPipeElemSize(E->getArg(0)); + Value *PacketSize = CGOpenCLRuntime(CGM).getPipeElemSize(E->getArg(0)); + Value *Arg0 = EmitScalarExpr(E->getArg(0)), + *Arg1 = EmitScalarExpr(E->getArg(1)); + llvm::Type *ReservedIDTy = ConvertType(getContext().OCLReserveIDTy); + + // Building the generic function prototype. + llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty, Int32Ty}; + llvm::FunctionType *FTy = llvm::FunctionType::get( + ReservedIDTy, llvm::ArrayRef(ArgTys), false); + // We know the third argument is an integer type (Verified by Sema, but + // we may need to cast it. + if (Arg1->getType() != Int32Ty) + Arg1 = Builder.CreateZExtOrTrunc(Arg1, Int32Ty); + return RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name), + {Arg0, Arg1, PacketSize})); + } + case Builtin::BIcommit_read_pipe: + case Builtin::BIcommit_write_pipe: + case Builtin::BIwork_group_commit_read_pipe: + case Builtin::BIwork_group_commit_write_pipe: + case Builtin::BIsub_group_commit_read_pipe: + case Builtin::BIsub_group_commit_write_pipe: { + const char *Name; + if (BuiltinID == Builtin::BIcommit_read_pipe) + Name = "_Z16commit_read_pipePU3AS110ocl_pipe_t16ocl_reserve_id_ti"; + else if (BuiltinID == Builtin::BIcommit_write_pipe) + Name = "_Z17commit_write_pipePU3AS110ocl_pipe_t16ocl_reserve_id_ti"; + else if (BuiltinID == Builtin::BIwork_group_commit_read_pipe) + Name = "_Z27work_group_commit_read_pipePU3AS110ocl_pipe_t16ocl_reserve_" + "id_ti"; + else if (BuiltinID == Builtin::BIwork_group_commit_write_pipe) + Name = "_Z28work_group_commit_write_pipePU3AS110ocl_pipe_t16ocl_reserve_" + "id_ti"; + else if (BuiltinID == Builtin::BIsub_group_commit_read_pipe) + Name = + "_Z26sub_group_commit_read_pipePU3AS110ocl_pipe_t16ocl_reserve_id_ti"; + else + Name = "_Z27sub_group_commit_write_pipePU3AS110ocl_pipe_t16ocl_reserve_" + "id_ti"; + + CGOpenCLRuntime CGOCL(CGM); + + //Value *PacketSize = CGOCL.getPipeElemSize(E->getArg(0)); + Value *PacketSize = CGOpenCLRuntime(CGM).getPipeElemSize(E->getArg(0)); + Value *Arg0 = EmitScalarExpr(E->getArg(0)), + *Arg1 = EmitScalarExpr(E->getArg(1)); + + // Building the generic function prototype. + llvm::Type *ArgTys[] = {Arg0->getType(), Arg1->getType(), Int32Ty}; + llvm::FunctionType *FTy = + llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), + llvm::ArrayRef(ArgTys), false); + + return RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name), + {Arg0, Arg1, PacketSize})); + } + case Builtin::BIget_pipe_num_packets: + case Builtin::BIget_pipe_max_packets: { + const char *Name; + if (BuiltinID == Builtin::BIget_pipe_num_packets) + Name = "_Z20get_pipe_num_packetsPU3AS110ocl_pipe_ti"; + else + Name = "_Z20get_pipe_max_packetsPU3AS110ocl_pipe_ti"; + SmallString<64> SS; + + // Building the generic function prototype. + CGOpenCLRuntime CGOCL(CGM); + //Value *PacketSize = CGOCL.getPipeElemSize(E->getArg(0)); + Value *PacketSize = CGOpenCLRuntime(CGM).getPipeElemSize(E->getArg(0)); + Value *Arg0 = EmitScalarExpr(E->getArg(0)); + llvm::Type *ArgTys[] = {Arg0->getType(), Int32Ty}; + llvm::FunctionType *FTy = llvm::FunctionType::get( + Int32Ty, llvm::ArrayRef(ArgTys), false); + + return RValue::get(Builder.CreateCall(CGM.CreateRuntimeFunction(FTy, Name), + {Arg0, PacketSize})); + } } // If this is an alias for a lib function (e.g. __builtin_sin), emit Index: lib/CodeGen/CGOpenCLRuntime.h =================================================================== --- lib/CodeGen/CGOpenCLRuntime.h +++ lib/CodeGen/CGOpenCLRuntime.h @@ -47,6 +47,10 @@ virtual llvm::Type *convertOpenCLSpecificType(const Type *T); virtual llvm::Type *getPipeType(); + + // \brief Returnes a value which indicates the size in bytes of the pipe + // element. + virtual llvm::Value *getPipeElemSize(const Expr *PipeArg); }; } Index: lib/CodeGen/CGOpenCLRuntime.cpp =================================================================== --- lib/CodeGen/CGOpenCLRuntime.cpp +++ lib/CodeGen/CGOpenCLRuntime.cpp @@ -110,3 +110,14 @@ return PipeTy; } + +llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) { + const PipeType *PipeTy = PipeArg->getType()->getAs(); + // The type of the last (implicit) argument to be passed. + llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext()); + unsigned TypeSizeInBits = + CGM.getContext().getTypeSize(PipeTy->getElementType()); + return llvm::ConstantInt::get(Int32Ty, + TypeSizeInBits / 8, // Size in bytes. + false); +} Index: lib/Sema/SemaChecking.cpp =================================================================== --- lib/Sema/SemaChecking.cpp +++ lib/Sema/SemaChecking.cpp @@ -258,6 +258,191 @@ return false; } +/// Returns readable name for a call. +static StringRef getFunctionName(CallExpr *Call) { + return cast(Call->getCalleeDecl())->getName(); +} + +/// Returns OpenCL access qual. +static OpenCLImageAccessAttr *getOpenCLImageAcces(const Decl *D) { + if (D->hasAttr()) + return D->getAttr(); + return nullptr; +} + +/// Returns true if pipe element type is different from the pointer. +static bool checkOpenCLPipeArg(Sema &S, CallExpr *Call) { + const Expr *Arg0 = Call->getArg(0); + // First argument type should always be pipe. + if (!Arg0->getType()->isPipeType()) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_first_arg) + << getFunctionName(Call) << Arg0->getSourceRange(); + return true; + } + OpenCLImageAccessAttr *AccessQual = + getOpenCLImageAcces(cast(Arg0)->getDecl()); + // Validates the access modifier is compatible with the call. + // From OpenCL C Specification 6.13.16 the access qualifiers for + // pipe should only be read_only and write_only, and assumed to + // be read_only if no qualifier is specified. + bool isValid = true; + // TODO: For all pipe built-in read is for read_only? + bool ReadOnly = getFunctionName(Call).find("read") != StringRef::npos; + if (ReadOnly) + isValid = AccessQual == nullptr || AccessQual->isReadOnly(); + else + isValid = AccessQual != nullptr && AccessQual->isWriteOnly(); + if (!isValid) { + const char *AM = ReadOnly ? "read_only" : "write_only"; + S.Diag(Arg0->getLocStart(), + diag::err_opencl_builtin_pipe_invalid_access_modifier) + << AM << Arg0->getSourceRange(); + return true; + } + + return false; +} + +/// Returns true if pipe element type is different from the pointer. +static bool checkOpenCLPipePacketType(Sema &S, CallExpr *Call, unsigned Idx) { + const Expr *Arg0 = Call->getArg(0); + const Expr *ArgIdx = Call->getArg(Idx); + const PipeType *PipeTy = cast(Arg0->getType()); + const Type *EltTy = PipeTy->getElementType().getTypePtr(); + const PointerType *ArgTy = + dyn_cast(ArgIdx->getType().getTypePtr()); + // The Idx argument should be a pointer and the type of the pointer and + // the type of pipe element should also be the same. + if (!ArgTy || EltTy != ArgTy->getPointeeType().getTypePtr()) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) + << getFunctionName(Call) + << S.Context.getPointerType(PipeTy->getElementType()) + << ArgIdx->getSourceRange(); + return true; + } + return false; +} + +// \brief Performs semantic analysis for the read/write_pipe call. +// \param S Reference to the semantic analyzer. +// \param Call A pointer to the builtin call. +// \return True if a semantic error has been found, false otherwise. +static bool SemaBuiltinRWPipe(Sema &S, CallExpr *Call) { + + if (checkOpenCLPipeArg(S, Call)) + return true; + + // Two kinds of read/write pipe + // From OpenCL C Specification 6.13.16.2 the built-in read/write + // functions have following forms. + switch (Call->getNumArgs()) { + case 2: { + // The call with 2 arguments should be + // read/write_pipe(pipe T, T*) + // check packet type T + if (checkOpenCLPipePacketType(S, Call, 1)) + return true; + } break; + + case 4: { + // The call with 4 arguments should be + // read/write_pipe(pipe T, reserve_id_t, uint, T*) + // check reserve_id_t + if (!Call->getArg(1)->getType()->isReserveIDT()) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) + << getFunctionName(Call) << S.Context.OCLReserveIDTy + << Call->getArg(1)->getSourceRange(); + return true; + } + + // check the index + const Expr *Arg2 = Call->getArg(2); + if (!Arg2->getType()->isIntegerType() && + !Arg2->getType()->isUnsignedIntegerType()) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) + << getFunctionName(Call) << S.Context.UnsignedIntTy + << Arg2->getSourceRange(); + return true; + } + + // check packet type T + if (checkOpenCLPipePacketType(S, Call, 3)) + return true; + } break; + default: + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_arg_num) + << getFunctionName(Call) << Call->getSourceRange(); + return true; + } + + return false; +} + +// \brief Performs a semantic analysis on the {work_group_/sub_group_ +// /_}reserve_{read/write}_pipe +// \param S Reference to the semantic analyzer. +// \param Call The call to the builtin function to be analyzed. +// \return True if a semantic error was found, false otherwise. +static bool SemaBuiltinReserveRWPipe(Sema &S, CallExpr *Call) { + if (checkArgCount(S, Call, 2)) + return true; + + if (checkOpenCLPipeArg(S, Call)) + return true; + + // check the reverse size + if (!Call->getArg(1)->getType()->isIntegerType() && + !Call->getArg(1)->getType()->isUnsignedIntegerType()) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) + << getFunctionName(Call) << S.Context.UnsignedIntTy + << Call->getArg(1)->getSourceRange(); + return true; + } + + return false; +} + +// \brief Performs a semantic analysis on {work_group_/sub_group_ +// /_}commit_{read/write}_pipe +// \param S Reference to the semantic analyzer. +// \param Call The call to the builtin function to be analyzed. +// \return True if a semantic error was found, false otherwise. +static bool SemaBuiltinCommitRWPipe(Sema &S, CallExpr *Call) { + if (checkArgCount(S, Call, 2)) + return true; + + if (checkOpenCLPipeArg(S, Call)) + return true; + + // check reserve_id_t + if (!Call->getArg(1)->getType()->isReserveIDT()) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_invalid_arg) + << getFunctionName(Call) << S.Context.OCLReserveIDTy + << Call->getArg(1)->getSourceRange(); + return true; + } + + return false; +} + +// \brief Performs a semantic analysis on the call to built-in Pipe +// Query Functions. +// \param S Reference to the semantic analyzer. +// \param Call The call to the builtin function to be analyzed. +// \return True if a semantic error was found, false otherwise. +static bool SemaBuiltinPipePacketes(Sema &S, CallExpr *Call) { + if (checkArgCount(S, Call, 1)) + return true; + + if (!Call->getArg(0)->getType()->isPipeType()) { + S.Diag(Call->getLocStart(), diag::err_opencl_builtin_pipe_first_arg) + << getFunctionName(Call) << Call->getArg(0)->getSourceRange(); + return true; + } + + return false; +} + ExprResult Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID, CallExpr *TheCall) { @@ -563,6 +748,38 @@ TheCall->setType(Context.VoidPtrTy); break; + case Builtin::BIread_pipe: + case Builtin::BIwrite_pipe: + // Since those two functions are declared with var args, therefore we need + // a semantic check for the argument. + if (SemaBuiltinRWPipe(*this, TheCall)) + return ExprError(); + break; + case Builtin::BIreserve_read_pipe: + case Builtin::BIreserve_write_pipe: + case Builtin::BIwork_group_reserve_read_pipe: + case Builtin::BIwork_group_reserve_write_pipe: + case Builtin::BIsub_group_reserve_read_pipe: + case Builtin::BIsub_group_reserve_write_pipe: + if (SemaBuiltinReserveRWPipe(*this, TheCall)) + return ExprError(); + // We need to override the return type of the reserve pipe functions. + TheCall->setType(Context.OCLReserveIDTy); + break; + case Builtin::BIcommit_read_pipe: + case Builtin::BIcommit_write_pipe: + case Builtin::BIwork_group_commit_read_pipe: + case Builtin::BIwork_group_commit_write_pipe: + case Builtin::BIsub_group_commit_read_pipe: + case Builtin::BIsub_group_commit_write_pipe: + if (SemaBuiltinCommitRWPipe(*this, TheCall)) + return ExprError(); + break; + case Builtin::BIget_pipe_num_packets: + case Builtin::BIget_pipe_max_packets: + if (SemaBuiltinPipePacketes(*this, TheCall)) + return ExprError(); + break; }