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, llvm::GlobalValue::InternalLinkage);
31 }
32 
33 llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
34   assert(T->isOpenCLSpecificType() &&
35          "Not an OpenCL specific type!");
36 
37   switch (cast<BuiltinType>(T)->getKind()) {
38   default:
39     llvm_unreachable("Unexpected opencl builtin type!");
40     return nullptr;
41 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix)                   \
42   case BuiltinType::Id:                                                        \
43     return getPointerType(T, "opencl." #ImgType "_" #Suffix "_t");
44 #include "clang/Basic/OpenCLImageTypes.def"
45   case BuiltinType::OCLSampler:
46     return getSamplerType(T);
47   case BuiltinType::OCLEvent:
48     return getPointerType(T, "opencl.event_t");
49   case BuiltinType::OCLClkEvent:
50     return getPointerType(T, "opencl.clk_event_t");
51   case BuiltinType::OCLQueue:
52     return getPointerType(T, "opencl.queue_t");
53   case BuiltinType::OCLReserveID:
54     return getPointerType(T, "opencl.reserve_id_t");
55 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext)                                      \
56   case BuiltinType::Id:                                                        \
57     return getPointerType(T, "opencl." #ExtType);
58 #include "clang/Basic/OpenCLExtensionTypes.def"
59   }
60 }
61 
62 llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T,
63                                                    StringRef Name) {
64   auto I = CachedTys.find(Name);
65   if (I != CachedTys.end())
66     return I->second;
67 
68   llvm::LLVMContext &Ctx = CGM.getLLVMContext();
69   uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
70       CGM.getContext().getOpenCLTypeAddrSpace(T));
71   auto *PTy =
72       llvm::PointerType::get(llvm::StructType::create(Ctx, Name), AddrSpc);
73   CachedTys[Name] = PTy;
74   return PTy;
75 }
76 
77 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
78   if (T->isReadOnly())
79     return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
80   else
81     return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
82 }
83 
84 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
85                                          llvm::Type *&PipeTy) {
86   if (!PipeTy)
87     PipeTy = llvm::PointerType::get(llvm::StructType::create(
88       CGM.getLLVMContext(), Name),
89       CGM.getContext().getTargetAddressSpace(
90           CGM.getContext().getOpenCLTypeAddrSpace(T)));
91   return PipeTy;
92 }
93 
94 llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) {
95   if (!SamplerTy)
96     SamplerTy = llvm::PointerType::get(llvm::StructType::create(
97       CGM.getLLVMContext(), "opencl.sampler_t"),
98       CGM.getContext().getTargetAddressSpace(
99           CGM.getContext().getOpenCLTypeAddrSpace(T)));
100   return SamplerTy;
101 }
102 
103 llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
104   const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
105   // The type of the last (implicit) argument to be passed.
106   llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
107   unsigned TypeSize = CGM.getContext()
108                           .getTypeSizeInChars(PipeTy->getElementType())
109                           .getQuantity();
110   return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
111 }
112 
113 llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) {
114   const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
115   // The type of the last (implicit) argument to be passed.
116   llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
117   unsigned TypeSize = CGM.getContext()
118                           .getTypeAlignInChars(PipeTy->getElementType())
119                           .getQuantity();
120   return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
121 }
122 
123 llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() {
124   assert(CGM.getLangOpts().OpenCL);
125   return llvm::IntegerType::getInt8PtrTy(
126       CGM.getLLVMContext(),
127       CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
128 }
129 
130 // Get the block literal from an expression derived from the block expression.
131 // OpenCL v2.0 s6.12.5:
132 // Block variable declarations are implicitly qualified with const. Therefore
133 // all block variables must be initialized at declaration time and may not be
134 // reassigned.
135 static const BlockExpr *getBlockExpr(const Expr *E) {
136   const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop.
137   while(!isa<BlockExpr>(E) && E != Prev) {
138     Prev = E;
139     E = E->IgnoreCasts();
140     if (auto DR = dyn_cast<DeclRefExpr>(E)) {
141       E = cast<VarDecl>(DR->getDecl())->getInit();
142     }
143   }
144   return cast<BlockExpr>(E);
145 }
146 
147 /// Record emitted llvm invoke function and llvm block literal for the
148 /// corresponding block expression.
149 void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E,
150                                       llvm::Function *InvokeF,
151                                       llvm::Value *Block, llvm::Type *BlockTy) {
152   assert(EnqueuedBlockMap.find(E) == EnqueuedBlockMap.end() &&
153          "Block expression emitted twice");
154   assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
155   assert(Block->getType()->isPointerTy() && "Invalid block literal type");
156   EnqueuedBlockMap[E].InvokeFunc = InvokeF;
157   EnqueuedBlockMap[E].BlockArg = Block;
158   EnqueuedBlockMap[E].BlockTy = BlockTy;
159   EnqueuedBlockMap[E].Kernel = nullptr;
160 }
161 
162 llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) {
163   return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc;
164 }
165 
166 CGOpenCLRuntime::EnqueuedBlockInfo
167 CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) {
168   CGF.EmitScalarExpr(E);
169 
170   // The block literal may be assigned to a const variable. Chasing down
171   // to get the block literal.
172   const BlockExpr *Block = getBlockExpr(E);
173 
174   assert(EnqueuedBlockMap.find(Block) != EnqueuedBlockMap.end() &&
175          "Block expression not emitted");
176 
177   // Do not emit the block wrapper again if it has been emitted.
178   if (EnqueuedBlockMap[Block].Kernel) {
179     return EnqueuedBlockMap[Block];
180   }
181 
182   auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel(
183       CGF, EnqueuedBlockMap[Block].InvokeFunc, EnqueuedBlockMap[Block].BlockTy);
184 
185   // The common part of the post-processing of the kernel goes here.
186   F->addFnAttr(llvm::Attribute::NoUnwind);
187   F->setCallingConv(
188       CGF.getTypes().ClangCallConvToLLVMCallConv(CallingConv::CC_OpenCLKernel));
189   EnqueuedBlockMap[Block].Kernel = F;
190   return EnqueuedBlockMap[Block];
191 }
192