annotate clang/lib/CodeGen/CGOpenCLRuntime.cpp @ 252:1f2b6ac9f198 llvm-original

LLVM16-1
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 18 Aug 2023 09:04:13 +0900 (2023-08-18)
parents c4bab56944e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===----- CGOpenCLRuntime.cpp - Interface to OpenCL Runtimes -------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This provides an abstract class for OpenCL code generation. Concrete
anatofuz
parents:
diff changeset
10 // subclasses of this implement code generation for specific OpenCL
anatofuz
parents:
diff changeset
11 // runtime libraries.
anatofuz
parents:
diff changeset
12 //
anatofuz
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
14
anatofuz
parents:
diff changeset
15 #include "CGOpenCLRuntime.h"
anatofuz
parents:
diff changeset
16 #include "CodeGenFunction.h"
anatofuz
parents:
diff changeset
17 #include "TargetInfo.h"
anatofuz
parents:
diff changeset
18 #include "clang/CodeGen/ConstantInitBuilder.h"
anatofuz
parents:
diff changeset
19 #include "llvm/IR/DerivedTypes.h"
anatofuz
parents:
diff changeset
20 #include "llvm/IR/GlobalValue.h"
anatofuz
parents:
diff changeset
21 #include <assert.h>
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 using namespace clang;
anatofuz
parents:
diff changeset
24 using namespace CodeGen;
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 CGOpenCLRuntime::~CGOpenCLRuntime() {}
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF,
anatofuz
parents:
diff changeset
29 const VarDecl &D) {
anatofuz
parents:
diff changeset
30 return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
anatofuz
parents:
diff changeset
31 }
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
34 assert(T->isOpenCLSpecificType() && "Not an OpenCL specific type!");
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
35
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
36 // Check if the target has a specific translation for this type first.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
37 if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T))
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
38 return TransTy;
150
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 switch (cast<BuiltinType>(T)->getKind()) {
anatofuz
parents:
diff changeset
41 default:
anatofuz
parents:
diff changeset
42 llvm_unreachable("Unexpected opencl builtin type!");
anatofuz
parents:
diff changeset
43 return nullptr;
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
44 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
45 case BuiltinType::Id: \
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
46 return getPointerType(T, "opencl." #ImgType "_" #Suffix "_t");
150
anatofuz
parents:
diff changeset
47 #include "clang/Basic/OpenCLImageTypes.def"
anatofuz
parents:
diff changeset
48 case BuiltinType::OCLSampler:
anatofuz
parents:
diff changeset
49 return getSamplerType(T);
anatofuz
parents:
diff changeset
50 case BuiltinType::OCLEvent:
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
51 return getPointerType(T, "opencl.event_t");
150
anatofuz
parents:
diff changeset
52 case BuiltinType::OCLClkEvent:
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
53 return getPointerType(T, "opencl.clk_event_t");
150
anatofuz
parents:
diff changeset
54 case BuiltinType::OCLQueue:
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
55 return getPointerType(T, "opencl.queue_t");
150
anatofuz
parents:
diff changeset
56 case BuiltinType::OCLReserveID:
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
57 return getPointerType(T, "opencl.reserve_id_t");
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
58 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
59 case BuiltinType::Id: \
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
60 return getPointerType(T, "opencl." #ExtType);
150
anatofuz
parents:
diff changeset
61 #include "clang/Basic/OpenCLExtensionTypes.def"
anatofuz
parents:
diff changeset
62 }
anatofuz
parents:
diff changeset
63 }
anatofuz
parents:
diff changeset
64
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
65 llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T,
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
66 StringRef Name) {
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
67 auto I = CachedTys.find(Name);
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
68 if (I != CachedTys.end())
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
69 return I->second;
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
70
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
71 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
72 uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
73 CGM.getContext().getOpenCLTypeAddrSpace(T));
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
74 auto *PTy =
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
75 llvm::PointerType::get(llvm::StructType::create(Ctx, Name), AddrSpc);
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
76 CachedTys[Name] = PTy;
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
77 return PTy;
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
78 }
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
79
150
anatofuz
parents:
diff changeset
80 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
81 if (llvm::Type *PipeTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T))
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
82 return PipeTy;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
83
150
anatofuz
parents:
diff changeset
84 if (T->isReadOnly())
anatofuz
parents:
diff changeset
85 return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
anatofuz
parents:
diff changeset
86 else
anatofuz
parents:
diff changeset
87 return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
anatofuz
parents:
diff changeset
88 }
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
anatofuz
parents:
diff changeset
91 llvm::Type *&PipeTy) {
anatofuz
parents:
diff changeset
92 if (!PipeTy)
anatofuz
parents:
diff changeset
93 PipeTy = llvm::PointerType::get(llvm::StructType::create(
anatofuz
parents:
diff changeset
94 CGM.getLLVMContext(), Name),
anatofuz
parents:
diff changeset
95 CGM.getContext().getTargetAddressSpace(
anatofuz
parents:
diff changeset
96 CGM.getContext().getOpenCLTypeAddrSpace(T)));
anatofuz
parents:
diff changeset
97 return PipeTy;
anatofuz
parents:
diff changeset
98 }
anatofuz
parents:
diff changeset
99
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
100 llvm::Type *CGOpenCLRuntime::getSamplerType(const Type *T) {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
101 if (SamplerTy)
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
102 return SamplerTy;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
103
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
104 if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType(
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
105 CGM, CGM.getContext().OCLSamplerTy.getTypePtr()))
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
106 SamplerTy = TransTy;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
107 else
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
108 SamplerTy = llvm::PointerType::get(
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
109 llvm::StructType::create(CGM.getLLVMContext(), "opencl.sampler_t"),
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
110 CGM.getContext().getTargetAddressSpace(
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
111 CGM.getContext().getOpenCLTypeAddrSpace(T)));
150
anatofuz
parents:
diff changeset
112 return SamplerTy;
anatofuz
parents:
diff changeset
113 }
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
anatofuz
parents:
diff changeset
116 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
anatofuz
parents:
diff changeset
117 // The type of the last (implicit) argument to be passed.
anatofuz
parents:
diff changeset
118 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
anatofuz
parents:
diff changeset
119 unsigned TypeSize = CGM.getContext()
anatofuz
parents:
diff changeset
120 .getTypeSizeInChars(PipeTy->getElementType())
anatofuz
parents:
diff changeset
121 .getQuantity();
anatofuz
parents:
diff changeset
122 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
anatofuz
parents:
diff changeset
123 }
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) {
anatofuz
parents:
diff changeset
126 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
anatofuz
parents:
diff changeset
127 // The type of the last (implicit) argument to be passed.
anatofuz
parents:
diff changeset
128 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
anatofuz
parents:
diff changeset
129 unsigned TypeSize = CGM.getContext()
anatofuz
parents:
diff changeset
130 .getTypeAlignInChars(PipeTy->getElementType())
anatofuz
parents:
diff changeset
131 .getQuantity();
anatofuz
parents:
diff changeset
132 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() {
anatofuz
parents:
diff changeset
136 assert(CGM.getLangOpts().OpenCL);
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
137 return llvm::PointerType::get(
150
anatofuz
parents:
diff changeset
138 CGM.getLLVMContext(),
anatofuz
parents:
diff changeset
139 CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
anatofuz
parents:
diff changeset
140 }
anatofuz
parents:
diff changeset
141
anatofuz
parents:
diff changeset
142 // Get the block literal from an expression derived from the block expression.
anatofuz
parents:
diff changeset
143 // OpenCL v2.0 s6.12.5:
anatofuz
parents:
diff changeset
144 // Block variable declarations are implicitly qualified with const. Therefore
anatofuz
parents:
diff changeset
145 // all block variables must be initialized at declaration time and may not be
anatofuz
parents:
diff changeset
146 // reassigned.
anatofuz
parents:
diff changeset
147 static const BlockExpr *getBlockExpr(const Expr *E) {
anatofuz
parents:
diff changeset
148 const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop.
anatofuz
parents:
diff changeset
149 while(!isa<BlockExpr>(E) && E != Prev) {
anatofuz
parents:
diff changeset
150 Prev = E;
anatofuz
parents:
diff changeset
151 E = E->IgnoreCasts();
anatofuz
parents:
diff changeset
152 if (auto DR = dyn_cast<DeclRefExpr>(E)) {
anatofuz
parents:
diff changeset
153 E = cast<VarDecl>(DR->getDecl())->getInit();
anatofuz
parents:
diff changeset
154 }
anatofuz
parents:
diff changeset
155 }
anatofuz
parents:
diff changeset
156 return cast<BlockExpr>(E);
anatofuz
parents:
diff changeset
157 }
anatofuz
parents:
diff changeset
158
anatofuz
parents:
diff changeset
159 /// Record emitted llvm invoke function and llvm block literal for the
anatofuz
parents:
diff changeset
160 /// corresponding block expression.
anatofuz
parents:
diff changeset
161 void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E,
anatofuz
parents:
diff changeset
162 llvm::Function *InvokeF,
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
163 llvm::Value *Block, llvm::Type *BlockTy) {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
164 assert(!EnqueuedBlockMap.contains(E) && "Block expression emitted twice");
150
anatofuz
parents:
diff changeset
165 assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
anatofuz
parents:
diff changeset
166 assert(Block->getType()->isPointerTy() && "Invalid block literal type");
anatofuz
parents:
diff changeset
167 EnqueuedBlockMap[E].InvokeFunc = InvokeF;
anatofuz
parents:
diff changeset
168 EnqueuedBlockMap[E].BlockArg = Block;
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
169 EnqueuedBlockMap[E].BlockTy = BlockTy;
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
170 EnqueuedBlockMap[E].KernelHandle = nullptr;
150
anatofuz
parents:
diff changeset
171 }
anatofuz
parents:
diff changeset
172
anatofuz
parents:
diff changeset
173 llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) {
anatofuz
parents:
diff changeset
174 return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc;
anatofuz
parents:
diff changeset
175 }
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 CGOpenCLRuntime::EnqueuedBlockInfo
anatofuz
parents:
diff changeset
178 CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) {
anatofuz
parents:
diff changeset
179 CGF.EmitScalarExpr(E);
anatofuz
parents:
diff changeset
180
anatofuz
parents:
diff changeset
181 // The block literal may be assigned to a const variable. Chasing down
anatofuz
parents:
diff changeset
182 // to get the block literal.
anatofuz
parents:
diff changeset
183 const BlockExpr *Block = getBlockExpr(E);
anatofuz
parents:
diff changeset
184
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
185 assert(EnqueuedBlockMap.contains(Block) && "Block expression not emitted");
150
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 // Do not emit the block wrapper again if it has been emitted.
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
188 if (EnqueuedBlockMap[Block].KernelHandle) {
150
anatofuz
parents:
diff changeset
189 return EnqueuedBlockMap[Block];
anatofuz
parents:
diff changeset
190 }
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel(
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
193 CGF, EnqueuedBlockMap[Block].InvokeFunc, EnqueuedBlockMap[Block].BlockTy);
150
anatofuz
parents:
diff changeset
194
anatofuz
parents:
diff changeset
195 // The common part of the post-processing of the kernel goes here.
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
196 EnqueuedBlockMap[Block].KernelHandle = F;
150
anatofuz
parents:
diff changeset
197 return EnqueuedBlockMap[Block];
anatofuz
parents:
diff changeset
198 }