1 //===----- CGOpenCLRuntime.cpp - Interface to OpenCL Runtimes -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This provides an abstract class for OpenCL code generation.  Concrete
11 // subclasses of this implement code generation for specific OpenCL
12 // runtime libraries.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "CGOpenCLRuntime.h"
17 #include "CodeGenFunction.h"
18 #include "TargetInfo.h"
19 #include "clang/CodeGen/ConstantInitBuilder.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include <assert.h>
23 
24 using namespace clang;
25 using namespace CodeGen;
26 
27 CGOpenCLRuntime::~CGOpenCLRuntime() {}
28 
29 void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF,
30                                                 const VarDecl &D) {
31   return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
32 }
33 
34 llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
35   assert(T->isOpenCLSpecificType() &&
36          "Not an OpenCL specific type!");
37 
38   llvm::LLVMContext& Ctx = CGM.getLLVMContext();
39   uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
40       CGM.getContext().getOpenCLTypeAddrSpace(T));
41   switch (cast<BuiltinType>(T)->getKind()) {
42   default:
43     llvm_unreachable("Unexpected opencl builtin type!");
44     return nullptr;
45 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
46   case BuiltinType::Id: \
47     return llvm::PointerType::get( \
48         llvm::StructType::create(Ctx, "opencl." #ImgType "_" #Suffix "_t"), \
49         AddrSpc);
50 #include "clang/Basic/OpenCLImageTypes.def"
51   case BuiltinType::OCLSampler:
52     return getSamplerType(T);
53   case BuiltinType::OCLEvent:
54     return llvm::PointerType::get(
55         llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc);
56   case BuiltinType::OCLClkEvent:
57     return llvm::PointerType::get(
58         llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc);
59   case BuiltinType::OCLQueue:
60     return llvm::PointerType::get(
61         llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc);
62   case BuiltinType::OCLReserveID:
63     return llvm::PointerType::get(
64         llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc);
65   }
66 }
67 
68 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
69   if (T->isReadOnly())
70     return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
71   else
72     return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
73 }
74 
75 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
76                                          llvm::Type *&PipeTy) {
77   if (!PipeTy)
78     PipeTy = llvm::PointerType::get(llvm::StructType::create(
79       CGM.getLLVMContext(), Name),
80       CGM.getContext().getTargetAddressSpace(
81           CGM.getContext().getOpenCLTypeAddrSpace(T)));
82   return PipeTy;
83 }
84 
85 llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) {
86   if (!SamplerTy)
87     SamplerTy = llvm::PointerType::get(llvm::StructType::create(
88       CGM.getLLVMContext(), "opencl.sampler_t"),
89       CGM.getContext().getTargetAddressSpace(
90           CGM.getContext().getOpenCLTypeAddrSpace(T)));
91   return SamplerTy;
92 }
93 
94 llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
95   const PipeType *PipeTy = PipeArg->getType()->getAs<PipeType>();
96   // The type of the last (implicit) argument to be passed.
97   llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
98   unsigned TypeSize = CGM.getContext()
99                           .getTypeSizeInChars(PipeTy->getElementType())
100                           .getQuantity();
101   return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
102 }
103 
104 llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) {
105   const PipeType *PipeTy = PipeArg->getType()->getAs<PipeType>();
106   // The type of the last (implicit) argument to be passed.
107   llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
108   unsigned TypeSize = CGM.getContext()
109                           .getTypeAlignInChars(PipeTy->getElementType())
110                           .getQuantity();
111   return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
112 }
113 
114 llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() {
115   assert(CGM.getLangOpts().OpenCL);
116   return llvm::IntegerType::getInt8PtrTy(
117       CGM.getLLVMContext(),
118       CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
119 }
120 
121 /// Record emitted llvm invoke function and llvm block literal for the
122 /// corresponding block expression.
123 void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E,
124                                       llvm::Function *InvokeF,
125                                       llvm::Value *Block) {
126   assert(EnqueuedBlockMap.find(E) == EnqueuedBlockMap.end() &&
127          "Block expression emitted twice");
128   assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
129   assert(Block->getType()->isPointerTy() && "Invalid block literal type");
130   EnqueuedBlockMap[E].InvokeFunc = InvokeF;
131   EnqueuedBlockMap[E].BlockArg = Block;
132   EnqueuedBlockMap[E].Kernel = nullptr;
133 }
134 
135 CGOpenCLRuntime::EnqueuedBlockInfo
136 CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) {
137   CGF.EmitScalarExpr(E);
138 
139   // The block literal may be assigned to a const variable. Chasing down
140   // to get the block literal.
141   if (auto DR = dyn_cast<DeclRefExpr>(E)) {
142     E = cast<VarDecl>(DR->getDecl())->getInit();
143   }
144   E = E->IgnoreImplicit();
145   if (auto Cast = dyn_cast<CastExpr>(E)) {
146     E = Cast->getSubExpr();
147   }
148   auto *Block = cast<BlockExpr>(E);
149 
150   assert(EnqueuedBlockMap.find(Block) != EnqueuedBlockMap.end() &&
151          "Block expression not emitted");
152 
153   // Do not emit the block wrapper again if it has been emitted.
154   if (EnqueuedBlockMap[Block].Kernel) {
155     return EnqueuedBlockMap[Block];
156   }
157 
158   auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel(
159       CGF, EnqueuedBlockMap[Block].InvokeFunc,
160       EnqueuedBlockMap[Block].BlockArg->stripPointerCasts());
161 
162   // The common part of the post-processing of the kernel goes here.
163   F->addFnAttr(llvm::Attribute::NoUnwind);
164   F->setCallingConv(
165       CGF.getTypes().ClangCallConvToLLVMCallConv(CallingConv::CC_OpenCLKernel));
166   EnqueuedBlockMap[Block].Kernel = F;
167   return EnqueuedBlockMap[Block];
168 }
169