Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 1 | //===----- CGOpenCLRuntime.cpp - Interface to OpenCL Runtimes -------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 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" |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 17 | #include "TargetInfo.h" |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 18 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/GlobalValue.h" |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 21 | #include <assert.h> |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 26 | CGOpenCLRuntime::~CGOpenCLRuntime() {} |
Guy Benyei | 11169dd | 2012-12-18 14:30:41 +0000 | [diff] [blame] | 27 | |
| 28 | void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, |
| 29 | const VarDecl &D) { |
| 30 | return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); |
| 31 | } |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 32 | |
| 33 | llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) { |
| 34 | assert(T->isOpenCLSpecificType() && |
| 35 | "Not an OpenCL specific type!"); |
| 36 | |
Pekka Jaaskelainen | 3587b32 | 2014-01-09 13:37:30 +0000 | [diff] [blame] | 37 | llvm::LLVMContext& Ctx = CGM.getLLVMContext(); |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 38 | uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace( |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 39 | CGM.getContext().getOpenCLTypeAddrSpace(T)); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 40 | switch (cast<BuiltinType>(T)->getKind()) { |
Alexey Bader | 465c189 | 2016-09-23 14:20:00 +0000 | [diff] [blame] | 41 | default: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 42 | llvm_unreachable("Unexpected opencl builtin type!"); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 43 | return nullptr; |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 44 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 45 | case BuiltinType::Id: \ |
| 46 | return llvm::PointerType::get( \ |
| 47 | llvm::StructType::create(Ctx, "opencl." #ImgType "_" #Suffix "_t"), \ |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 48 | AddrSpc); |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 49 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 50 | case BuiltinType::OCLSampler: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 51 | return getSamplerType(T); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 52 | case BuiltinType::OCLEvent: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 53 | return llvm::PointerType::get( |
| 54 | llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 55 | case BuiltinType::OCLClkEvent: |
| 56 | return llvm::PointerType::get( |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 57 | llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 58 | case BuiltinType::OCLQueue: |
| 59 | return llvm::PointerType::get( |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 60 | llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 61 | case BuiltinType::OCLReserveID: |
| 62 | return llvm::PointerType::get( |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 63 | llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc); |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 64 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 65 | case BuiltinType::Id: \ |
| 66 | return llvm::PointerType::get( \ |
| 67 | llvm::StructType::create(Ctx, "opencl." #ExtType), AddrSpc); |
| 68 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 71 | |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 72 | llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) { |
Sven van Haastregt | 4700faa | 2018-04-27 10:37:04 +0000 | [diff] [blame] | 73 | if (T->isReadOnly()) |
| 74 | return getPipeType(T, "opencl.pipe_ro_t", PipeROTy); |
| 75 | else |
| 76 | return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy); |
| 77 | } |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 78 | |
Sven van Haastregt | 4700faa | 2018-04-27 10:37:04 +0000 | [diff] [blame] | 79 | llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name, |
| 80 | llvm::Type *&PipeTy) { |
| 81 | if (!PipeTy) |
| 82 | PipeTy = llvm::PointerType::get(llvm::StructType::create( |
| 83 | CGM.getLLVMContext(), Name), |
| 84 | CGM.getContext().getTargetAddressSpace( |
| 85 | CGM.getContext().getOpenCLTypeAddrSpace(T))); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 86 | return PipeTy; |
| 87 | } |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 88 | |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 89 | llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) { |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 90 | if (!SamplerTy) |
| 91 | SamplerTy = llvm::PointerType::get(llvm::StructType::create( |
| 92 | CGM.getLLVMContext(), "opencl.sampler_t"), |
| 93 | CGM.getContext().getTargetAddressSpace( |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 94 | CGM.getContext().getOpenCLTypeAddrSpace(T))); |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 95 | return SamplerTy; |
| 96 | } |
Alexey Bader | 465c189 | 2016-09-23 14:20:00 +0000 | [diff] [blame] | 97 | |
| 98 | llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) { |
Simon Pilgrim | 5936717 | 2020-01-08 16:41:41 +0000 | [diff] [blame] | 99 | const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); |
Alexey Bader | 465c189 | 2016-09-23 14:20:00 +0000 | [diff] [blame] | 100 | // The type of the last (implicit) argument to be passed. |
| 101 | llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext()); |
| 102 | unsigned TypeSize = CGM.getContext() |
| 103 | .getTypeSizeInChars(PipeTy->getElementType()) |
| 104 | .getQuantity(); |
| 105 | return llvm::ConstantInt::get(Int32Ty, TypeSize, false); |
| 106 | } |
| 107 | |
| 108 | llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) { |
Simon Pilgrim | 5936717 | 2020-01-08 16:41:41 +0000 | [diff] [blame] | 109 | const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); |
Alexey Bader | 465c189 | 2016-09-23 14:20:00 +0000 | [diff] [blame] | 110 | // The type of the last (implicit) argument to be passed. |
| 111 | llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext()); |
| 112 | unsigned TypeSize = CGM.getContext() |
| 113 | .getTypeAlignInChars(PipeTy->getElementType()) |
| 114 | .getQuantity(); |
| 115 | return llvm::ConstantInt::get(Int32Ty, TypeSize, false); |
| 116 | } |
Yaxun Liu | 10712d9 | 2017-10-04 20:32:17 +0000 | [diff] [blame] | 117 | |
| 118 | llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() { |
| 119 | assert(CGM.getLangOpts().OpenCL); |
| 120 | return llvm::IntegerType::getInt8PtrTy( |
| 121 | CGM.getLLVMContext(), |
| 122 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic)); |
| 123 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 124 | |
Andrew Savonichev | 43fceb2 | 2019-02-21 11:02:10 +0000 | [diff] [blame] | 125 | // Get the block literal from an expression derived from the block expression. |
| 126 | // OpenCL v2.0 s6.12.5: |
| 127 | // Block variable declarations are implicitly qualified with const. Therefore |
| 128 | // all block variables must be initialized at declaration time and may not be |
| 129 | // reassigned. |
| 130 | static const BlockExpr *getBlockExpr(const Expr *E) { |
| 131 | const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop. |
| 132 | while(!isa<BlockExpr>(E) && E != Prev) { |
| 133 | Prev = E; |
| 134 | E = E->IgnoreCasts(); |
| 135 | if (auto DR = dyn_cast<DeclRefExpr>(E)) { |
| 136 | E = cast<VarDecl>(DR->getDecl())->getInit(); |
| 137 | } |
| 138 | } |
| 139 | return cast<BlockExpr>(E); |
| 140 | } |
| 141 | |
Yaxun Liu | fa13d01 | 2018-02-15 16:39:19 +0000 | [diff] [blame] | 142 | /// Record emitted llvm invoke function and llvm block literal for the |
| 143 | /// corresponding block expression. |
| 144 | void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E, |
| 145 | llvm::Function *InvokeF, |
| 146 | llvm::Value *Block) { |
| 147 | assert(EnqueuedBlockMap.find(E) == EnqueuedBlockMap.end() && |
| 148 | "Block expression emitted twice"); |
| 149 | assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function"); |
| 150 | assert(Block->getType()->isPointerTy() && "Invalid block literal type"); |
| 151 | EnqueuedBlockMap[E].InvokeFunc = InvokeF; |
| 152 | EnqueuedBlockMap[E].BlockArg = Block; |
| 153 | EnqueuedBlockMap[E].Kernel = nullptr; |
| 154 | } |
| 155 | |
Andrew Savonichev | 43fceb2 | 2019-02-21 11:02:10 +0000 | [diff] [blame] | 156 | llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) { |
| 157 | return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc; |
| 158 | } |
| 159 | |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 160 | CGOpenCLRuntime::EnqueuedBlockInfo |
| 161 | CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) { |
Yaxun Liu | fa13d01 | 2018-02-15 16:39:19 +0000 | [diff] [blame] | 162 | CGF.EmitScalarExpr(E); |
| 163 | |
Sven van Haastregt | da3b632 | 2018-10-02 13:02:24 +0000 | [diff] [blame] | 164 | // The block literal may be assigned to a const variable. Chasing down |
| 165 | // to get the block literal. |
Andrew Savonichev | 43fceb2 | 2019-02-21 11:02:10 +0000 | [diff] [blame] | 166 | const BlockExpr *Block = getBlockExpr(E); |
Sven van Haastregt | da3b632 | 2018-10-02 13:02:24 +0000 | [diff] [blame] | 167 | |
Yaxun Liu | fa13d01 | 2018-02-15 16:39:19 +0000 | [diff] [blame] | 168 | assert(EnqueuedBlockMap.find(Block) != EnqueuedBlockMap.end() && |
| 169 | "Block expression not emitted"); |
| 170 | |
| 171 | // Do not emit the block wrapper again if it has been emitted. |
| 172 | if (EnqueuedBlockMap[Block].Kernel) { |
| 173 | return EnqueuedBlockMap[Block]; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 176 | auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel( |
Yaxun Liu | fa13d01 | 2018-02-15 16:39:19 +0000 | [diff] [blame] | 177 | CGF, EnqueuedBlockMap[Block].InvokeFunc, |
| 178 | EnqueuedBlockMap[Block].BlockArg->stripPointerCasts()); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 179 | |
| 180 | // The common part of the post-processing of the kernel goes here. |
| 181 | F->addFnAttr(llvm::Attribute::NoUnwind); |
| 182 | F->setCallingConv( |
| 183 | CGF.getTypes().ClangCallConvToLLVMCallConv(CallingConv::CC_OpenCLKernel)); |
Yaxun Liu | fa13d01 | 2018-02-15 16:39:19 +0000 | [diff] [blame] | 184 | EnqueuedBlockMap[Block].Kernel = F; |
| 185 | return EnqueuedBlockMap[Block]; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 186 | } |