clang 17.0.0git
CGOpenCLRuntime.cpp
Go to the documentation of this file.
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"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/GlobalValue.h"
21#include <assert.h>
22
23using namespace clang;
24using namespace CodeGen;
25
27
29 const VarDecl &D) {
30 return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
31}
32
34 assert(T->isOpenCLSpecificType() && "Not an OpenCL specific type!");
35
36 // Check if the target has a specific translation for this type first.
37 if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T))
38 return TransTy;
39
40 switch (cast<BuiltinType>(T)->getKind()) {
41 default:
42 llvm_unreachable("Unexpected opencl builtin type!");
43 return nullptr;
44#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
45 case BuiltinType::Id: \
46 return getPointerType(T, "opencl." #ImgType "_" #Suffix "_t");
47#include "clang/Basic/OpenCLImageTypes.def"
48 case BuiltinType::OCLSampler:
49 return getSamplerType(T);
50 case BuiltinType::OCLEvent:
51 return getPointerType(T, "opencl.event_t");
52 case BuiltinType::OCLClkEvent:
53 return getPointerType(T, "opencl.clk_event_t");
54 case BuiltinType::OCLQueue:
55 return getPointerType(T, "opencl.queue_t");
56 case BuiltinType::OCLReserveID:
57 return getPointerType(T, "opencl.reserve_id_t");
58#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
59 case BuiltinType::Id: \
60 return getPointerType(T, "opencl." #ExtType);
61#include "clang/Basic/OpenCLExtensionTypes.def"
62 }
63}
64
65llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T,
66 StringRef Name) {
67 auto I = CachedTys.find(Name);
68 if (I != CachedTys.end())
69 return I->second;
70
71 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
72 uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
74 auto *PTy =
75 llvm::PointerType::get(llvm::StructType::create(Ctx, Name), AddrSpc);
76 CachedTys[Name] = PTy;
77 return PTy;
78}
79
81 if (llvm::Type *PipeTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T))
82 return PipeTy;
83
84 if (T->isReadOnly())
85 return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
86 else
87 return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
88}
89
90llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
91 llvm::Type *&PipeTy) {
92 if (!PipeTy)
93 PipeTy = llvm::PointerType::get(llvm::StructType::create(
94 CGM.getLLVMContext(), Name),
97 return PipeTy;
98}
99
101 if (SamplerTy)
102 return SamplerTy;
103
104 if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType(
106 SamplerTy = TransTy;
107 else
108 SamplerTy = llvm::PointerType::get(
109 llvm::StructType::create(CGM.getLLVMContext(), "opencl.sampler_t"),
112 return SamplerTy;
113}
114
115llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
116 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
117 // The type of the last (implicit) argument to be passed.
118 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
119 unsigned TypeSize = CGM.getContext()
121 .getQuantity();
122 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
123}
124
125llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) {
126 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
127 // The type of the last (implicit) argument to be passed.
128 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
129 unsigned TypeSize = CGM.getContext()
131 .getQuantity();
132 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
133}
134
136 assert(CGM.getLangOpts().OpenCL);
137 return llvm::IntegerType::getInt8PtrTy(
140}
141
142// Get the block literal from an expression derived from the block expression.
143// OpenCL v2.0 s6.12.5:
144// Block variable declarations are implicitly qualified with const. Therefore
145// all block variables must be initialized at declaration time and may not be
146// reassigned.
147static const BlockExpr *getBlockExpr(const Expr *E) {
148 const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop.
149 while(!isa<BlockExpr>(E) && E != Prev) {
150 Prev = E;
151 E = E->IgnoreCasts();
152 if (auto DR = dyn_cast<DeclRefExpr>(E)) {
153 E = cast<VarDecl>(DR->getDecl())->getInit();
154 }
155 }
156 return cast<BlockExpr>(E);
157}
158
159/// Record emitted llvm invoke function and llvm block literal for the
160/// corresponding block expression.
162 llvm::Function *InvokeF,
163 llvm::Value *Block, llvm::Type *BlockTy) {
164 assert(!EnqueuedBlockMap.contains(E) && "Block expression emitted twice");
165 assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
166 assert(Block->getType()->isPointerTy() && "Invalid block literal type");
167 EnqueuedBlockMap[E].InvokeFunc = InvokeF;
168 EnqueuedBlockMap[E].BlockArg = Block;
169 EnqueuedBlockMap[E].BlockTy = BlockTy;
170 EnqueuedBlockMap[E].KernelHandle = nullptr;
171}
172
173llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) {
174 return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc;
175}
176
179 CGF.EmitScalarExpr(E);
180
181 // The block literal may be assigned to a const variable. Chasing down
182 // to get the block literal.
183 const BlockExpr *Block = getBlockExpr(E);
184
185 assert(EnqueuedBlockMap.contains(Block) && "Block expression not emitted");
186
187 // Do not emit the block wrapper again if it has been emitted.
188 if (EnqueuedBlockMap[Block].KernelHandle) {
189 return EnqueuedBlockMap[Block];
190 }
191
193 CGF, EnqueuedBlockMap[Block].InvokeFunc, EnqueuedBlockMap[Block].BlockTy);
194
195 // The common part of the post-processing of the kernel goes here.
196 EnqueuedBlockMap[Block].KernelHandle = F;
197 return EnqueuedBlockMap[Block];
198}
static const BlockExpr * getBlockExpr(const Expr *E)
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:1048
LangAS getOpenCLTypeAddrSpace(const Type *T) const
Get address space for OpenCL type.
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType OCLSamplerTy
Definition: ASTContext.h:1113
unsigned getTargetAddressSpace(LangAS AS) const
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6025
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Definition: CanonicalType.h:83
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
virtual llvm::Type * getPipeType(const PipeType *T, StringRef Name, llvm::Type *&PipeTy)
EnqueuedBlockInfo emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E)
llvm::PointerType * getPointerType(const Type *T, StringRef Name)
virtual llvm::Value * getPipeElemAlign(const Expr *PipeArg)
llvm::Function * getInvokeFunction(const Expr *E)
void recordBlockInfo(const BlockExpr *E, llvm::Function *InvokeF, llvm::Value *Block, llvm::Type *BlockTy)
Record invoke function and block literal emitted during normal codegen for a block expression.
llvm::PointerType * getGenericVoidPointerType()
llvm::DenseMap< const Expr *, EnqueuedBlockInfo > EnqueuedBlockMap
Maps block expression to block information.
virtual llvm::Type * convertOpenCLSpecificType(const Type *T)
llvm::StringMap< llvm::PointerType * > CachedTys
virtual llvm::Value * getPipeElemSize(const Expr *PipeArg)
virtual void EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, const VarDecl &D)
Emit the IR required for a work-group-local variable declaration, and add an entry to CGF's LocalDecl...
llvm::Type * getSamplerType(const Type *T)
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
const TargetCodeGenInfo & getTargetHooks() const
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
const LangOptions & getLangOpts() const
ASTContext & getContext() const
const TargetCodeGenInfo & getTargetCodeGenInfo()
llvm::LLVMContext & getLLVMContext()
virtual llvm::Type * getOpenCLType(CodeGenModule &CGM, const Type *T) const
Return an LLVM type that corresponds to an OpenCL type.
Definition: TargetInfo.h:398
virtual llvm::Value * createEnqueuedBlockKernel(CodeGenFunction &CGF, llvm::Function *BlockInvokeFunc, llvm::Type *BlockTy) const
Create an OpenCL kernel for an enqueued block.
This represents one expression.
Definition: Expr.h:110
Expr * IgnoreCasts() LLVM_READONLY
Skip past any casts which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3039
QualType getType() const
Definition: Expr.h:142
PipeType - OpenCL20.
Definition: Type.h:6512
QualType getElementType() const
Definition: Type.h:6523
bool isReadOnly() const
Definition: Type.h:6542
The base class of the type hierarchy.
Definition: Type.h:1568
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7497
bool isOpenCLSpecificType() const
Definition: Type.h:7165
Represents a variable declaration or definition.
Definition: Decl.h:913
Structure for enqueued block information.