clang 20.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 if (T->isSamplerT())
41 return getSamplerType(T);
42
43 return getPointerType(T);
44}
45
46llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T) {
47 uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
49 return llvm::PointerType::get(CGM.getLLVMContext(), AddrSpc);
50}
51
53 if (llvm::Type *PipeTy = CGM.getTargetCodeGenInfo().getOpenCLType(CGM, T))
54 return PipeTy;
55
56 if (T->isReadOnly())
57 return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
58 else
59 return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
60}
61
62llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
63 llvm::Type *&PipeTy) {
64 if (!PipeTy)
65 PipeTy = getPointerType(T);
66 return PipeTy;
67}
68
70 if (SamplerTy)
71 return SamplerTy;
72
73 if (llvm::Type *TransTy = CGM.getTargetCodeGenInfo().getOpenCLType(
75 SamplerTy = TransTy;
76 else
78 return SamplerTy;
79}
80
81llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
82 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
83 // The type of the last (implicit) argument to be passed.
84 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
85 unsigned TypeSize = CGM.getContext()
87 .getQuantity();
88 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
89}
90
91llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) {
92 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
93 // The type of the last (implicit) argument to be passed.
94 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
95 unsigned TypeSize = CGM.getContext()
97 .getQuantity();
98 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
99}
100
102 assert(CGM.getLangOpts().OpenCL);
103 return llvm::PointerType::get(
106}
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.
113static const BlockExpr *getBlockExpr(const Expr *E) {
114 const Expr *Prev = nullptr; // to make sure we do not stuck in infinite loop.
115 while(!isa<BlockExpr>(E) && E != Prev) {
116 Prev = E;
117 E = E->IgnoreCasts();
118 if (auto DR = dyn_cast<DeclRefExpr>(E)) {
119 E = cast<VarDecl>(DR->getDecl())->getInit();
120 }
121 }
122 return cast<BlockExpr>(E);
123}
124
125/// Record emitted llvm invoke function and llvm block literal for the
126/// corresponding block expression.
128 llvm::Function *InvokeF,
129 llvm::Value *Block, llvm::Type *BlockTy) {
130 assert(!EnqueuedBlockMap.contains(E) && "Block expression emitted twice");
131 assert(isa<llvm::Function>(InvokeF) && "Invalid invoke function");
132 assert(Block->getType()->isPointerTy() && "Invalid block literal type");
133 EnqueuedBlockMap[E].InvokeFunc = InvokeF;
134 EnqueuedBlockMap[E].BlockArg = Block;
135 EnqueuedBlockMap[E].BlockTy = BlockTy;
136 EnqueuedBlockMap[E].KernelHandle = nullptr;
137}
138
140 return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc;
141}
142
145 CGF.EmitScalarExpr(E);
146
147 // The block literal may be assigned to a const variable. Chasing down
148 // to get the block literal.
149 const BlockExpr *Block = getBlockExpr(E);
150
151 assert(EnqueuedBlockMap.contains(Block) && "Block expression not emitted");
152
153 // Do not emit the block wrapper again if it has been emitted.
154 if (EnqueuedBlockMap[Block].KernelHandle) {
155 return EnqueuedBlockMap[Block];
156 }
157
159 CGF, EnqueuedBlockMap[Block].InvokeFunc, EnqueuedBlockMap[Block].BlockTy);
160
161 // The common part of the post-processing of the kernel goes here.
162 EnqueuedBlockMap[Block].KernelHandle = F;
163 return EnqueuedBlockMap[Block];
164}
static const BlockExpr * getBlockExpr(const Expr *E)
const Decl * D
Expr * E
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:1156
unsigned getTargetAddressSpace(LangAS AS) const
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6365
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)
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)
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:417
virtual llvm::Value * createEnqueuedBlockKernel(CodeGenFunction &CGF, llvm::Function *BlockInvokeFunc, llvm::Type *BlockTy) const
Create an OpenCL kernel for an enqueued block.
Definition: TargetInfo.cpp:178
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:3054
QualType getType() const
Definition: Expr.h:142
PipeType - OpenCL20.
Definition: Type.h:7599
QualType getElementType() const
Definition: Type.h:7610
The base class of the type hierarchy.
Definition: Type.h:1829
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8607
bool isOpenCLSpecificType() const
Definition: Type.h:8266
bool isSamplerT() const
Definition: Type.h:8210
Represents a variable declaration or definition.
Definition: Decl.h:879
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Structure for enqueued block information.