/src/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 | 0 | CGOpenCLRuntime::~CGOpenCLRuntime() {} |
27 | | |
28 | | void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, |
29 | 0 | const VarDecl &D) { |
30 | 0 | return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage); |
31 | 0 | } |
32 | | |
33 | 0 | llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) { |
34 | 0 | assert(T->isOpenCLSpecificType() && "Not an OpenCL specific type!"); |
35 | | |
36 | | // Check if the target has a specific translation for this type first. |
37 | 0 | if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T)) |
38 | 0 | return TransTy; |
39 | | |
40 | 0 | if (T->isSamplerT()) |
41 | 0 | return getSamplerType(T); |
42 | | |
43 | 0 | return getPointerType(T); |
44 | 0 | } |
45 | | |
46 | 0 | llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T) { |
47 | 0 | uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace( |
48 | 0 | CGM.getContext().getOpenCLTypeAddrSpace(T)); |
49 | 0 | return llvm::PointerType::get(CGM.getLLVMContext(), AddrSpc); |
50 | 0 | } |
51 | | |
52 | 0 | llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) { |
53 | 0 | if (llvm::Type *PipeTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T)) |
54 | 0 | return PipeTy; |
55 | | |
56 | 0 | if (T->isReadOnly()) |
57 | 0 | return getPipeType(T, "opencl.pipe_ro_t", PipeROTy); |
58 | 0 | else |
59 | 0 | return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy); |
60 | 0 | } |
61 | | |
62 | | llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name, |
63 | 0 | llvm::Type *&PipeTy) { |
64 | 0 | if (!PipeTy) |
65 | 0 | PipeTy = getPointerType(T); |
66 | 0 | return PipeTy; |
67 | 0 | } |
68 | | |
69 | 0 | llvm::Type *CGOpenCLRuntime::getSamplerType(const Type *T) { |
70 | 0 | if (SamplerTy) |
71 | 0 | return SamplerTy; |
72 | | |
73 | 0 | if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType( |
74 | 0 | CGM, CGM.getContext().OCLSamplerTy.getTypePtr())) |
75 | 0 | SamplerTy = TransTy; |
76 | 0 | else |
77 | 0 | SamplerTy = getPointerType(T); |
78 | 0 | return SamplerTy; |
79 | 0 | } |
80 | | |
81 | 0 | llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) { |
82 | 0 | const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); |
83 | | // The type of the last (implicit) argument to be passed. |
84 | 0 | llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext()); |
85 | 0 | unsigned TypeSize = CGM.getContext() |
86 | 0 | .getTypeSizeInChars(PipeTy->getElementType()) |
87 | 0 | .getQuantity(); |
88 | 0 | return llvm::ConstantInt::get(Int32Ty, TypeSize, false); |
89 | 0 | } |
90 | | |
91 | 0 | llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) { |
92 | 0 | const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>(); |
93 | | // The type of the last (implicit) argument to be passed. |
94 | 0 | llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext()); |
95 | 0 | unsigned TypeSize = CGM.getContext() |
96 | 0 | .getTypeAlignInChars(PipeTy->getElementType()) |
97 | 0 | .getQuantity(); |
98 | 0 | return llvm::ConstantInt::get(Int32Ty, TypeSize, false); |
99 | 0 | } |
100 | | |
101 | 0 | llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() { |
102 | 0 | assert(CGM.getLangOpts().OpenCL); |
103 | 0 | return llvm::PointerType::get( |
104 | 0 | CGM.getLLVMContext(), |
105 | 0 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic)); |
106 | 0 | } |
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 | 0 | static const BlockExpr *getBlockExpr(const Expr *E) { |
114 | 0 | const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop. |
115 | 0 | while(!isa<BlockExpr>(E) && E != Prev) { |
116 | 0 | Prev = E; |
117 | 0 | E = E->IgnoreCasts(); |
118 | 0 | if (auto DR = dyn_cast<DeclRefExpr>(E)) { |
119 | 0 | E = cast<VarDecl>(DR->getDecl())->getInit(); |
120 | 0 | } |
121 | 0 | } |
122 | 0 | return cast<BlockExpr>(E); |
123 | 0 | } |
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 | 0 | llvm::Value *Block, llvm::Type *BlockTy) { |
130 | 0 | assert(!EnqueuedBlockMap.contains(E) && "Block expression emitted twice"); |
131 | 0 | assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function"); |
132 | 0 | assert(Block->getType()->isPointerTy() && "Invalid block literal type"); |
133 | 0 | EnqueuedBlockMap[E].InvokeFunc = InvokeF; |
134 | 0 | EnqueuedBlockMap[E].BlockArg = Block; |
135 | 0 | EnqueuedBlockMap[E].BlockTy = BlockTy; |
136 | 0 | EnqueuedBlockMap[E].KernelHandle = nullptr; |
137 | 0 | } |
138 | | |
139 | 0 | llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) { |
140 | 0 | return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc; |
141 | 0 | } |
142 | | |
143 | | CGOpenCLRuntime::EnqueuedBlockInfo |
144 | 0 | CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) { |
145 | 0 | CGF.EmitScalarExpr(E); |
146 | | |
147 | | // The block literal may be assigned to a const variable. Chasing down |
148 | | // to get the block literal. |
149 | 0 | const BlockExpr *Block = getBlockExpr(E); |
150 | |
|
151 | 0 | assert(EnqueuedBlockMap.contains(Block) && "Block expression not emitted"); |
152 | | |
153 | | // Do not emit the block wrapper again if it has been emitted. |
154 | 0 | if (EnqueuedBlockMap[Block].KernelHandle) { |
155 | 0 | return EnqueuedBlockMap[Block]; |
156 | 0 | } |
157 | | |
158 | 0 | auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel( |
159 | 0 | CGF, EnqueuedBlockMap[Block].InvokeFunc, EnqueuedBlockMap[Block].BlockTy); |
160 | | |
161 | | // The common part of the post-processing of the kernel goes here. |
162 | 0 | EnqueuedBlockMap[Block].KernelHandle = F; |
163 | 0 | return EnqueuedBlockMap[Block]; |
164 | 0 | } |