| 1 | //===----- CGOpenCLRuntime.cpp - Interface to OpenCL Runtimes -------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This provides an abstract class for OpenCL code generation. Concrete |
| 10 | // subclasses of this implement code generation for specific OpenCL |
| 11 | // runtime libraries. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CGOpenCLRuntime.h" |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "TargetInfo.h" |
| 18 | #include "clang/CodeGen/ConstantInitBuilder.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/GlobalValue.h" |
| 21 | #include <assert.h> |
| 22 | |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | CGOpenCLRuntime::~CGOpenCLRuntime() {} |
| 27 | |
| 28 | void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, |
| 29 | const VarDecl &D) { |
| 30 | return CGF.EmitStaticVarDecl(D, Linkage: llvm::GlobalValue::InternalLinkage); |
| 31 | } |
| 32 | |
| 33 | llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) { |
| 34 | assert(T->isOpenCLSpecificType() && "Not an OpenCL specific type!" ); |
| 35 | |
| 36 | // Check if the target has a specific translation for this type first. |
| 37 | if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T)) |
| 38 | return TransTy; |
| 39 | |
| 40 | if (T->isSamplerT()) |
| 41 | return getSamplerType(T); |
| 42 | |
| 43 | return getPointerType(T); |
| 44 | } |
| 45 | |
| 46 | llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T) { |
| 47 | uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace( |
| 48 | AS: CGM.getContext().getOpenCLTypeAddrSpace(T)); |
| 49 | return llvm::PointerType::get(C&: CGM.getLLVMContext(), AddressSpace: AddrSpc); |
| 50 | } |
| 51 | |
| 52 | llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) { |
| 53 | if (llvm::Type *PipeTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T)) |
| 54 | return PipeTy; |
| 55 | |
| 56 | if (T->isReadOnly()) |
| 57 | return getPipeType(T, Name: "opencl.pipe_ro_t" , PipeTy&: PipeROTy); |
| 58 | else |
| 59 | return getPipeType(T, Name: "opencl.pipe_wo_t" , PipeTy&: PipeWOTy); |
| 60 | } |
| 61 | |
| 62 | llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name, |
| 63 | llvm::Type *&PipeTy) { |
| 64 | if (!PipeTy) |
| 65 | PipeTy = getPointerType(T); |
| 66 | return PipeTy; |
| 67 | } |
| 68 | |
| 69 | llvm::Type *CGOpenCLRuntime::getSamplerType(const Type *T) { |
| 70 | if (SamplerTy) |
| 71 | return SamplerTy; |
| 72 | |
| 73 | if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType( |
| 74 | CGM, T: CGM.getContext().OCLSamplerTy.getTypePtr())) |
| 75 | SamplerTy = TransTy; |
| 76 | else |
| 77 | SamplerTy = getPointerType(T); |
| 78 | return SamplerTy; |
| 79 | } |
| 80 | |
| 81 | llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) { |
| 82 | const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); |
| 83 | // The type of the last (implicit) argument to be passed. |
| 84 | llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(C&: CGM.getLLVMContext()); |
| 85 | unsigned TypeSize = CGM.getContext() |
| 86 | .getTypeSizeInChars(T: PipeTy->getElementType()) |
| 87 | .getQuantity(); |
| 88 | return llvm::ConstantInt::get(Ty: Int32Ty, V: TypeSize, IsSigned: false); |
| 89 | } |
| 90 | |
| 91 | llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) { |
| 92 | const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); |
| 93 | // The type of the last (implicit) argument to be passed. |
| 94 | llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(C&: CGM.getLLVMContext()); |
| 95 | unsigned TypeSize = CGM.getContext() |
| 96 | .getTypeAlignInChars(T: PipeTy->getElementType()) |
| 97 | .getQuantity(); |
| 98 | return llvm::ConstantInt::get(Ty: Int32Ty, V: TypeSize, IsSigned: false); |
| 99 | } |
| 100 | |
| 101 | llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() { |
| 102 | assert(CGM.getLangOpts().OpenCL); |
| 103 | return llvm::PointerType::get( |
| 104 | C&: CGM.getLLVMContext(), |
| 105 | AddressSpace: CGM.getContext().getTargetAddressSpace(AS: LangAS::opencl_generic)); |
| 106 | } |
| 107 | |
| 108 | // Get the block literal from an expression derived from the block expression. |
| 109 | // OpenCL v2.0 s6.12.5: |
| 110 | // Block variable declarations are implicitly qualified with const. Therefore |
| 111 | // all block variables must be initialized at declaration time and may not be |
| 112 | // reassigned. |
| 113 | static const BlockExpr *getBlockExpr(const Expr *E) { |
| 114 | const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop. |
| 115 | while(!isa<BlockExpr>(Val: E) && E != Prev) { |
| 116 | Prev = E; |
| 117 | E = E->IgnoreCasts(); |
| 118 | if (auto DR = dyn_cast<DeclRefExpr>(Val: E)) { |
| 119 | E = cast<VarDecl>(Val: DR->getDecl())->getInit(); |
| 120 | } |
| 121 | } |
| 122 | return cast<BlockExpr>(Val: E); |
| 123 | } |
| 124 | |
| 125 | /// Record emitted llvm invoke function and llvm block literal for the |
| 126 | /// corresponding block expression. |
| 127 | void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E, |
| 128 | llvm::Function *InvokeF, |
| 129 | llvm::Value *Block, llvm::Type *BlockTy) { |
| 130 | assert(!EnqueuedBlockMap.contains(E) && "Block expression emitted twice" ); |
| 131 | assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function" ); |
| 132 | assert(Block->getType()->isPointerTy() && "Invalid block literal type" ); |
| 133 | EnqueuedBlockInfo &BlockInfo = EnqueuedBlockMap[E]; |
| 134 | BlockInfo.InvokeFunc = InvokeF; |
| 135 | BlockInfo.BlockArg = Block; |
| 136 | BlockInfo.BlockTy = BlockTy; |
| 137 | BlockInfo.KernelHandle = nullptr; |
| 138 | } |
| 139 | |
| 140 | llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) { |
| 141 | return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc; |
| 142 | } |
| 143 | |
| 144 | CGOpenCLRuntime::EnqueuedBlockInfo |
| 145 | CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) { |
| 146 | CGF.EmitScalarExpr(E); |
| 147 | |
| 148 | // The block literal may be assigned to a const variable. Chasing down |
| 149 | // to get the block literal. |
| 150 | const BlockExpr *Block = getBlockExpr(E); |
| 151 | |
| 152 | auto It = EnqueuedBlockMap.find(Val: Block); |
| 153 | assert(It != EnqueuedBlockMap.end() && "Block expression not emitted" ); |
| 154 | EnqueuedBlockInfo &BlockInfo = It->second; |
| 155 | |
| 156 | // Do not emit the block wrapper again if it has been emitted. |
| 157 | if (BlockInfo.KernelHandle) { |
| 158 | return BlockInfo; |
| 159 | } |
| 160 | |
| 161 | auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel( |
| 162 | CGF, BlockInvokeFunc: BlockInfo.InvokeFunc, BlockTy: BlockInfo.BlockTy); |
| 163 | |
| 164 | // The common part of the post-processing of the kernel goes here. |
| 165 | BlockInfo.KernelHandle = F; |
| 166 | return BlockInfo; |
| 167 | } |
| 168 | |