clang  14.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 
23 using namespace clang;
24 using namespace CodeGen;
25 
27 
29  const VarDecl &D) {
31 }
32 
34  assert(T->isOpenCLSpecificType() &&
35  "Not an OpenCL specific type!");
36 
37  llvm::LLVMContext& Ctx = CGM.getLLVMContext();
38  uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
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 llvm::PointerType::get( \
47  llvm::StructType::create(Ctx, "opencl." #ImgType "_" #Suffix "_t"), \
48  AddrSpc);
49 #include "clang/Basic/OpenCLImageTypes.def"
50  case BuiltinType::OCLSampler:
51  return getSamplerType(T);
52  case BuiltinType::OCLEvent:
53  return llvm::PointerType::get(
54  llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc);
55  case BuiltinType::OCLClkEvent:
56  return llvm::PointerType::get(
57  llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc);
58  case BuiltinType::OCLQueue:
59  return llvm::PointerType::get(
60  llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc);
61  case BuiltinType::OCLReserveID:
62  return llvm::PointerType::get(
63  llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc);
64 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
65  case BuiltinType::Id: \
66  return llvm::PointerType::get( \
67  llvm::StructType::create(Ctx, "opencl." #ExtType), AddrSpc);
68 #include "clang/Basic/OpenCLExtensionTypes.def"
69  }
70 }
71 
72 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
73  if (T->isReadOnly())
74  return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
75  else
76  return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
77 }
78 
79 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
80  llvm::Type *&PipeTy) {
81  if (!PipeTy)
82  PipeTy = llvm::PointerType::get(llvm::StructType::create(
83  CGM.getLLVMContext(), Name),
86  return PipeTy;
87 }
88 
89 llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) {
90  if (!SamplerTy)
91  SamplerTy = llvm::PointerType::get(llvm::StructType::create(
92  CGM.getLLVMContext(), "opencl.sampler_t"),
95  return SamplerTy;
96 }
97 
99  const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
100  // The type of the last (implicit) argument to be passed.
101  llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
102  unsigned TypeSize = CGM.getContext()
104  .getQuantity();
105  return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
106 }
107 
109  const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
110  // The type of the last (implicit) argument to be passed.
111  llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
112  unsigned TypeSize = CGM.getContext()
114  .getQuantity();
115  return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
116 }
117 
119  assert(CGM.getLangOpts().OpenCL);
120  return llvm::IntegerType::getInt8PtrTy(
123 }
124 
125 // Get the block literal from an expression derived from the block expression.
126 // OpenCL v2.0 s6.12.5:
127 // Block variable declarations are implicitly qualified with const. Therefore
128 // all block variables must be initialized at declaration time and may not be
129 // reassigned.
130 static const BlockExpr *getBlockExpr(const Expr *E) {
131  const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop.
132  while(!isa<BlockExpr>(E) && E != Prev) {
133  Prev = E;
134  E = E->IgnoreCasts();
135  if (auto DR = dyn_cast<DeclRefExpr>(E)) {
136  E = cast<VarDecl>(DR->getDecl())->getInit();
137  }
138  }
139  return cast<BlockExpr>(E);
140 }
141 
142 /// Record emitted llvm invoke function and llvm block literal for the
143 /// corresponding block expression.
145  llvm::Function *InvokeF,
146  llvm::Value *Block) {
147  assert(EnqueuedBlockMap.find(E) == EnqueuedBlockMap.end() &&
148  "Block expression emitted twice");
149  assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
150  assert(Block->getType()->isPointerTy() && "Invalid block literal type");
151  EnqueuedBlockMap[E].InvokeFunc = InvokeF;
152  EnqueuedBlockMap[E].BlockArg = Block;
153  EnqueuedBlockMap[E].Kernel = nullptr;
154 }
155 
156 llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) {
157  return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc;
158 }
159 
162  CGF.EmitScalarExpr(E);
163 
164  // The block literal may be assigned to a const variable. Chasing down
165  // to get the block literal.
166  const BlockExpr *Block = getBlockExpr(E);
167 
168  assert(EnqueuedBlockMap.find(Block) != EnqueuedBlockMap.end() &&
169  "Block expression not emitted");
170 
171  // Do not emit the block wrapper again if it has been emitted.
173  return EnqueuedBlockMap[Block];
174  }
175 
177  CGF, EnqueuedBlockMap[Block].InvokeFunc,
178  EnqueuedBlockMap[Block].BlockArg->stripPointerCasts());
179 
180  // The common part of the post-processing of the kernel goes here.
181  F->addFnAttr(llvm::Attribute::NoUnwind);
182  F->setCallingConv(
184  EnqueuedBlockMap[Block].Kernel = F;
185  return EnqueuedBlockMap[Block];
186 }
clang::InternalLinkage
@ InternalLinkage
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:31
clang::ASTContext::getTypeSizeInChars
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
Definition: ASTContext.cpp:2452
clang::PipeType::isReadOnly
bool isReadOnly() const
Definition: Type.h:6303
clang::Type::isOpenCLSpecificType
bool isOpenCLSpecificType() const
Definition: Type.h:6916
clang::CodeGen::CGOpenCLRuntime::EnqueuedBlockMap
llvm::DenseMap< const Expr *, EnqueuedBlockInfo > EnqueuedBlockMap
Maps block expression to block information.
Definition: CGOpenCLRuntime.h:49
clang::CodeGen::CGOpenCLRuntime::getSamplerType
llvm::PointerType * getSamplerType(const Type *T)
Definition: CGOpenCLRuntime.cpp:89
CodeGenFunction.h
clang::CodeGen::TargetCodeGenInfo::createEnqueuedBlockKernel
virtual llvm::Function * createEnqueuedBlockKernel(CodeGenFunction &CGF, llvm::Function *BlockInvokeFunc, llvm::Value *BlockLiteral) const
Create an OpenCL kernel for an enqueued block.
Definition: TargetInfo.cpp:11292
clang::PipeType::getElementType
QualType getElementType() const
Definition: Type.h:6284
clang::CodeGen::CGOpenCLRuntime::emitOpenCLEnqueuedBlock
EnqueuedBlockInfo emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E)
Definition: CGOpenCLRuntime.cpp:161
clang::CodeGen::CGOpenCLRuntime::getPipeElemSize
virtual llvm::Value * getPipeElemSize(const Expr *PipeArg)
Definition: CGOpenCLRuntime.cpp:98
clang::CodeGen::CodeGenModule::getContext
ASTContext & getContext() const
Definition: CodeGenModule.h:702
clang::PipeType
PipeType - OpenCL20.
Definition: Type.h:6273
TargetInfo.h
clang::CodeGen::CodeGenModule::getLangOpts
const LangOptions & getLangOpts() const
Definition: CodeGenModule.h:703
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
clang::CC_OpenCLKernel
@ CC_OpenCLKernel
Definition: Specifiers.h:268
clang::CodeGen::CGOpenCLRuntime::PipeWOTy
llvm::Type * PipeWOTy
Definition: CGOpenCLRuntime.h:39
clang::ASTContext::getTypeAlignInChars
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
Definition: ASTContext.cpp:2461
clang::CodeGen::CGOpenCLRuntime::recordBlockInfo
void recordBlockInfo(const BlockExpr *E, llvm::Function *InvokeF, llvm::Value *Block)
Record invoke function and block literal emitted during normal codegen for a block expression.
Definition: CGOpenCLRuntime.cpp:144
clang::CodeGen::CGOpenCLRuntime::PipeROTy
llvm::Type * PipeROTy
Definition: CGOpenCLRuntime.h:38
clang::CodeGen::CGOpenCLRuntime::SamplerTy
llvm::PointerType * SamplerTy
Definition: CGOpenCLRuntime.h:40
clang::CodeGen::CGOpenCLRuntime::getPipeElemAlign
virtual llvm::Value * getPipeElemAlign(const Expr *PipeArg)
Definition: CGOpenCLRuntime.cpp:108
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::KernelReferenceKind::Kernel
@ Kernel
clang::CodeGen::CodeGenTypes::ClangCallConvToLLVMCallConv
unsigned ClangCallConvToLLVMCallConv(CallingConv CC)
Convert clang calling convention to LLVM callilng convention.
Definition: CGCall.cpp:47
clang::CodeGen::CGOpenCLRuntime::EmitWorkGroupLocalVarDecl
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...
Definition: CGOpenCLRuntime.cpp:28
getKind
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:998
clang::ASTContext::getTargetAddressSpace
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.h:2712
clang::Type::castAs
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7227
clang::serialized_diags::create
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
Definition: SerializedDiagnosticPrinter.cpp:302
clang::CodeGen::CodeGenFunction::getTargetHooks
const TargetCodeGenInfo & getTargetHooks() const
Definition: CodeGenFunction.h:1991
clang::CodeGen::CGOpenCLRuntime::getInvokeFunction
llvm::Function * getInvokeFunction(const Expr *E)
Definition: CGOpenCLRuntime.cpp:156
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:235
clang::ASTContext::getOpenCLTypeAddrSpace
LangAS getOpenCLTypeAddrSpace(const Type *T) const
Get address space for OpenCL type.
Definition: ASTContext.cpp:6827
clang::BlockExpr
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5965
clang::CodeGen::CGOpenCLRuntime::EnqueuedBlockInfo
Structure for enqueued block information.
Definition: CGOpenCLRuntime.h:43
Value
Value
Definition: UninitializedValues.cpp:102
clang::CodeGen::CodeGenFunction::getTypes
CodeGenTypes & getTypes() const
Definition: CodeGenFunction.h:1946
getBlockExpr
static const BlockExpr * getBlockExpr(const Expr *E)
Definition: CGOpenCLRuntime.cpp:130
clang::CodeGen::CodeGenFunction::EmitScalarExpr
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
Definition: CGExprScalar.cpp:4833
clang::CodeGen::CGOpenCLRuntime::~CGOpenCLRuntime
virtual ~CGOpenCLRuntime()
Definition: CGOpenCLRuntime.cpp:26
clang::CodeGen::CGOpenCLRuntime::convertOpenCLSpecificType
virtual llvm::Type * convertOpenCLSpecificType(const Type *T)
Definition: CGOpenCLRuntime.cpp:33
clang::CodeGen::CGOpenCLRuntime::getPipeType
virtual llvm::Type * getPipeType(const PipeType *T, StringRef Name, llvm::Type *&PipeTy)
Definition: CGOpenCLRuntime.cpp:79
clang
Definition: CalledOnceCheck.h:17
CGOpenCLRuntime.h
clang::DeclaratorContext::Block
@ Block
clang::Expr::getType
QualType getType() const
Definition: Expr.h:141
clang::CodeGen::CodeGenFunction::EmitStaticVarDecl
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:398
clang::Expr::IgnoreCasts
Expr * IgnoreCasts() LLVM_READONLY
Skip past any casts which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:2912
clang::CodeGen::CGOpenCLRuntime::getGenericVoidPointerType
llvm::PointerType * getGenericVoidPointerType()
Definition: CGOpenCLRuntime.cpp:118
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::LangAS::opencl_generic
@ opencl_generic
clang::CodeGen::CodeGenModule::getLLVMContext
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenModule.h:720
clang::CodeGen::CGOpenCLRuntime::CGM
CodeGenModule & CGM
Definition: CGOpenCLRuntime.h:37
clang::CharUnits::getQuantity
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
ConstantInitBuilder.h