10b57cec5SDimitry Andric //===----- CGOpenCLRuntime.cpp - Interface to OpenCL Runtimes -------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This provides an abstract class for OpenCL code generation. Concrete 100b57cec5SDimitry Andric // subclasses of this implement code generation for specific OpenCL 110b57cec5SDimitry Andric // runtime libraries. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "CGOpenCLRuntime.h" 160b57cec5SDimitry Andric #include "CodeGenFunction.h" 170b57cec5SDimitry Andric #include "TargetInfo.h" 180b57cec5SDimitry Andric #include "clang/CodeGen/ConstantInitBuilder.h" 190b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 200b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 210b57cec5SDimitry Andric #include <assert.h> 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric using namespace clang; 240b57cec5SDimitry Andric using namespace CodeGen; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric CGOpenCLRuntime::~CGOpenCLRuntime() {} 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, 290b57cec5SDimitry Andric const VarDecl &D) { 300b57cec5SDimitry Andric return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); 310b57cec5SDimitry Andric } 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) { 340b57cec5SDimitry Andric assert(T->isOpenCLSpecificType() && 350b57cec5SDimitry Andric "Not an OpenCL specific type!"); 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric llvm::LLVMContext& Ctx = CGM.getLLVMContext(); 380b57cec5SDimitry Andric uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace( 390b57cec5SDimitry Andric CGM.getContext().getOpenCLTypeAddrSpace(T)); 400b57cec5SDimitry Andric switch (cast<BuiltinType>(T)->getKind()) { 410b57cec5SDimitry Andric default: 420b57cec5SDimitry Andric llvm_unreachable("Unexpected opencl builtin type!"); 430b57cec5SDimitry Andric return nullptr; 440b57cec5SDimitry Andric #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 450b57cec5SDimitry Andric case BuiltinType::Id: \ 460b57cec5SDimitry Andric return llvm::PointerType::get( \ 470b57cec5SDimitry Andric llvm::StructType::create(Ctx, "opencl." #ImgType "_" #Suffix "_t"), \ 480b57cec5SDimitry Andric AddrSpc); 490b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def" 500b57cec5SDimitry Andric case BuiltinType::OCLSampler: 510b57cec5SDimitry Andric return getSamplerType(T); 520b57cec5SDimitry Andric case BuiltinType::OCLEvent: 530b57cec5SDimitry Andric return llvm::PointerType::get( 540b57cec5SDimitry Andric llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc); 550b57cec5SDimitry Andric case BuiltinType::OCLClkEvent: 560b57cec5SDimitry Andric return llvm::PointerType::get( 570b57cec5SDimitry Andric llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc); 580b57cec5SDimitry Andric case BuiltinType::OCLQueue: 590b57cec5SDimitry Andric return llvm::PointerType::get( 600b57cec5SDimitry Andric llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc); 610b57cec5SDimitry Andric case BuiltinType::OCLReserveID: 620b57cec5SDimitry Andric return llvm::PointerType::get( 630b57cec5SDimitry Andric llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc); 640b57cec5SDimitry Andric #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 650b57cec5SDimitry Andric case BuiltinType::Id: \ 660b57cec5SDimitry Andric return llvm::PointerType::get( \ 670b57cec5SDimitry Andric llvm::StructType::create(Ctx, "opencl." #ExtType), AddrSpc); 680b57cec5SDimitry Andric #include "clang/Basic/OpenCLExtensionTypes.def" 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) { 730b57cec5SDimitry Andric if (T->isReadOnly()) 740b57cec5SDimitry Andric return getPipeType(T, "opencl.pipe_ro_t", PipeROTy); 750b57cec5SDimitry Andric else 760b57cec5SDimitry Andric return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy); 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name, 800b57cec5SDimitry Andric llvm::Type *&PipeTy) { 810b57cec5SDimitry Andric if (!PipeTy) 820b57cec5SDimitry Andric PipeTy = llvm::PointerType::get(llvm::StructType::create( 830b57cec5SDimitry Andric CGM.getLLVMContext(), Name), 840b57cec5SDimitry Andric CGM.getContext().getTargetAddressSpace( 850b57cec5SDimitry Andric CGM.getContext().getOpenCLTypeAddrSpace(T))); 860b57cec5SDimitry Andric return PipeTy; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) { 900b57cec5SDimitry Andric if (!SamplerTy) 910b57cec5SDimitry Andric SamplerTy = llvm::PointerType::get(llvm::StructType::create( 920b57cec5SDimitry Andric CGM.getLLVMContext(), "opencl.sampler_t"), 930b57cec5SDimitry Andric CGM.getContext().getTargetAddressSpace( 940b57cec5SDimitry Andric CGM.getContext().getOpenCLTypeAddrSpace(T))); 950b57cec5SDimitry Andric return SamplerTy; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) { 99*480093f4SDimitry Andric const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); 1000b57cec5SDimitry Andric // The type of the last (implicit) argument to be passed. 1010b57cec5SDimitry Andric llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext()); 1020b57cec5SDimitry Andric unsigned TypeSize = CGM.getContext() 1030b57cec5SDimitry Andric .getTypeSizeInChars(PipeTy->getElementType()) 1040b57cec5SDimitry Andric .getQuantity(); 1050b57cec5SDimitry Andric return llvm::ConstantInt::get(Int32Ty, TypeSize, false); 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) { 109*480093f4SDimitry Andric const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); 1100b57cec5SDimitry Andric // The type of the last (implicit) argument to be passed. 1110b57cec5SDimitry Andric llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext()); 1120b57cec5SDimitry Andric unsigned TypeSize = CGM.getContext() 1130b57cec5SDimitry Andric .getTypeAlignInChars(PipeTy->getElementType()) 1140b57cec5SDimitry Andric .getQuantity(); 1150b57cec5SDimitry Andric return llvm::ConstantInt::get(Int32Ty, TypeSize, false); 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() { 1190b57cec5SDimitry Andric assert(CGM.getLangOpts().OpenCL); 1200b57cec5SDimitry Andric return llvm::IntegerType::getInt8PtrTy( 1210b57cec5SDimitry Andric CGM.getLLVMContext(), 1220b57cec5SDimitry Andric CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic)); 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric // Get the block literal from an expression derived from the block expression. 1260b57cec5SDimitry Andric // OpenCL v2.0 s6.12.5: 1270b57cec5SDimitry Andric // Block variable declarations are implicitly qualified with const. Therefore 1280b57cec5SDimitry Andric // all block variables must be initialized at declaration time and may not be 1290b57cec5SDimitry Andric // reassigned. 1300b57cec5SDimitry Andric static const BlockExpr *getBlockExpr(const Expr *E) { 1310b57cec5SDimitry Andric const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop. 1320b57cec5SDimitry Andric while(!isa<BlockExpr>(E) && E != Prev) { 1330b57cec5SDimitry Andric Prev = E; 1340b57cec5SDimitry Andric E = E->IgnoreCasts(); 1350b57cec5SDimitry Andric if (auto DR = dyn_cast<DeclRefExpr>(E)) { 1360b57cec5SDimitry Andric E = cast<VarDecl>(DR->getDecl())->getInit(); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric return cast<BlockExpr>(E); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric /// Record emitted llvm invoke function and llvm block literal for the 1430b57cec5SDimitry Andric /// corresponding block expression. 1440b57cec5SDimitry Andric void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E, 1450b57cec5SDimitry Andric llvm::Function *InvokeF, 1460b57cec5SDimitry Andric llvm::Value *Block) { 1470b57cec5SDimitry Andric assert(EnqueuedBlockMap.find(E) == EnqueuedBlockMap.end() && 1480b57cec5SDimitry Andric "Block expression emitted twice"); 1490b57cec5SDimitry Andric assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function"); 1500b57cec5SDimitry Andric assert(Block->getType()->isPointerTy() && "Invalid block literal type"); 1510b57cec5SDimitry Andric EnqueuedBlockMap[E].InvokeFunc = InvokeF; 1520b57cec5SDimitry Andric EnqueuedBlockMap[E].BlockArg = Block; 1530b57cec5SDimitry Andric EnqueuedBlockMap[E].Kernel = nullptr; 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) { 1570b57cec5SDimitry Andric return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc; 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric CGOpenCLRuntime::EnqueuedBlockInfo 1610b57cec5SDimitry Andric CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) { 1620b57cec5SDimitry Andric CGF.EmitScalarExpr(E); 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // The block literal may be assigned to a const variable. Chasing down 1650b57cec5SDimitry Andric // to get the block literal. 1660b57cec5SDimitry Andric const BlockExpr *Block = getBlockExpr(E); 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric assert(EnqueuedBlockMap.find(Block) != EnqueuedBlockMap.end() && 1690b57cec5SDimitry Andric "Block expression not emitted"); 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric // Do not emit the block wrapper again if it has been emitted. 1720b57cec5SDimitry Andric if (EnqueuedBlockMap[Block].Kernel) { 1730b57cec5SDimitry Andric return EnqueuedBlockMap[Block]; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel( 1770b57cec5SDimitry Andric CGF, EnqueuedBlockMap[Block].InvokeFunc, 1780b57cec5SDimitry Andric EnqueuedBlockMap[Block].BlockArg->stripPointerCasts()); 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // The common part of the post-processing of the kernel goes here. 1810b57cec5SDimitry Andric F->addFnAttr(llvm::Attribute::NoUnwind); 1820b57cec5SDimitry Andric F->setCallingConv( 1830b57cec5SDimitry Andric CGF.getTypes().ClangCallConvToLLVMCallConv(CallingConv::CC_OpenCLKernel)); 1840b57cec5SDimitry Andric EnqueuedBlockMap[Block].Kernel = F; 1850b57cec5SDimitry Andric return EnqueuedBlockMap[Block]; 1860b57cec5SDimitry Andric } 187