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