blob: dbe375294d1795a4d3c69213eeee463e6a53ef60 [file] [log] [blame]
Guy Benyei11169dd2012-12-18 14:30:41 +00001//===----- CGOpenCLRuntime.cpp - Interface to OpenCL Runtimes -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Guy Benyei11169dd2012-12-18 14:30:41 +00006//
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"
Yaxun Liu37ceede2016-07-20 19:21:11 +000017#include "TargetInfo.h"
Yaxun Liuc2a87a02017-10-14 12:23:50 +000018#include "clang/CodeGen/ConstantInitBuilder.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000019#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/GlobalValue.h"
Guy Benyeid8a08ea2012-12-18 14:38:23 +000021#include <assert.h>
Guy Benyei11169dd2012-12-18 14:30:41 +000022
23using namespace clang;
24using namespace CodeGen;
25
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000026CGOpenCLRuntime::~CGOpenCLRuntime() {}
Guy Benyei11169dd2012-12-18 14:30:41 +000027
28void CGOpenCLRuntime::EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF,
29 const VarDecl &D) {
30 return CGF.EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
31}
Guy Benyeid8a08ea2012-12-18 14:38:23 +000032
33llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
34 assert(T->isOpenCLSpecificType() &&
35 "Not an OpenCL specific type!");
36
Pekka Jaaskelainen3587b322014-01-09 13:37:30 +000037 llvm::LLVMContext& Ctx = CGM.getLLVMContext();
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000038 uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
Sven van Haastregt3bb7eaf2017-12-06 10:11:28 +000039 CGM.getContext().getOpenCLTypeAddrSpace(T));
Guy Benyeid8a08ea2012-12-18 14:38:23 +000040 switch (cast<BuiltinType>(T)->getKind()) {
Alexey Bader465c1892016-09-23 14:20:00 +000041 default:
Guy Benyeid8a08ea2012-12-18 14:38:23 +000042 llvm_unreachable("Unexpected opencl builtin type!");
Craig Topper8a13c412014-05-21 05:09:00 +000043 return nullptr;
Alexey Bader954ba212016-04-08 13:40:33 +000044#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"), \
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000048 AddrSpc);
Alexey Baderb62f1442016-04-13 08:33:41 +000049#include "clang/Basic/OpenCLImageTypes.def"
Guy Benyei61054192013-02-07 10:55:47 +000050 case BuiltinType::OCLSampler:
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000051 return getSamplerType(T);
Guy Benyei1b4fb3e2013-01-20 12:31:11 +000052 case BuiltinType::OCLEvent:
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000053 return llvm::PointerType::get(
54 llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc);
Alexey Bader9c8453f2015-09-15 11:18:52 +000055 case BuiltinType::OCLClkEvent:
56 return llvm::PointerType::get(
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000057 llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc);
Alexey Bader9c8453f2015-09-15 11:18:52 +000058 case BuiltinType::OCLQueue:
59 return llvm::PointerType::get(
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000060 llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc);
Alexey Bader9c8453f2015-09-15 11:18:52 +000061 case BuiltinType::OCLReserveID:
62 return llvm::PointerType::get(
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000063 llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc);
Andrew Savonichev3fee3512018-11-08 11:25:41 +000064#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"
Guy Benyeid8a08ea2012-12-18 14:38:23 +000069 }
70}
Xiuli Pan9c14e282016-01-09 12:53:17 +000071
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000072llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
Sven van Haastregt4700faa2018-04-27 10:37:04 +000073 if (T->isReadOnly())
74 return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);
75 else
76 return getPipeType(T, "opencl.pipe_wo_t", PipeWOTy);
77}
Xiuli Pan9c14e282016-01-09 12:53:17 +000078
Sven van Haastregt4700faa2018-04-27 10:37:04 +000079llvm::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),
84 CGM.getContext().getTargetAddressSpace(
85 CGM.getContext().getOpenCLTypeAddrSpace(T)));
Xiuli Pan9c14e282016-01-09 12:53:17 +000086 return PipeTy;
87}
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +000088
Sven van Haastregtefb4d4c2017-08-15 09:38:18 +000089llvm::PointerType *CGOpenCLRuntime::getSamplerType(const Type *T) {
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +000090 if (!SamplerTy)
91 SamplerTy = llvm::PointerType::get(llvm::StructType::create(
92 CGM.getLLVMContext(), "opencl.sampler_t"),
93 CGM.getContext().getTargetAddressSpace(
Sven van Haastregt3bb7eaf2017-12-06 10:11:28 +000094 CGM.getContext().getOpenCLTypeAddrSpace(T)));
Yaxun Liu0bc4b2d2016-07-28 19:26:30 +000095 return SamplerTy;
96}
Alexey Bader465c1892016-09-23 14:20:00 +000097
98llvm::Value *CGOpenCLRuntime::getPipeElemSize(const Expr *PipeArg) {
Simon Pilgrim59367172020-01-08 16:41:41 +000099 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
Alexey Bader465c1892016-09-23 14:20:00 +0000100 // The type of the last (implicit) argument to be passed.
101 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
102 unsigned TypeSize = CGM.getContext()
103 .getTypeSizeInChars(PipeTy->getElementType())
104 .getQuantity();
105 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
106}
107
108llvm::Value *CGOpenCLRuntime::getPipeElemAlign(const Expr *PipeArg) {
Simon Pilgrim59367172020-01-08 16:41:41 +0000109 const PipeType *PipeTy = PipeArg->getType()->castAs<PipeType>();
Alexey Bader465c1892016-09-23 14:20:00 +0000110 // The type of the last (implicit) argument to be passed.
111 llvm::Type *Int32Ty = llvm::IntegerType::getInt32Ty(CGM.getLLVMContext());
112 unsigned TypeSize = CGM.getContext()
113 .getTypeAlignInChars(PipeTy->getElementType())
114 .getQuantity();
115 return llvm::ConstantInt::get(Int32Ty, TypeSize, false);
116}
Yaxun Liu10712d92017-10-04 20:32:17 +0000117
118llvm::PointerType *CGOpenCLRuntime::getGenericVoidPointerType() {
119 assert(CGM.getLangOpts().OpenCL);
120 return llvm::IntegerType::getInt8PtrTy(
121 CGM.getLLVMContext(),
122 CGM.getContext().getTargetAddressSpace(LangAS::opencl_generic));
123}
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000124
Andrew Savonichev43fceb22019-02-21 11:02:10 +0000125// 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.
130static 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
Yaxun Liufa13d012018-02-15 16:39:19 +0000142/// Record emitted llvm invoke function and llvm block literal for the
143/// corresponding block expression.
144void CGOpenCLRuntime::recordBlockInfo(const BlockExpr *E,
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
Andrew Savonichev43fceb22019-02-21 11:02:10 +0000156llvm::Function *CGOpenCLRuntime::getInvokeFunction(const Expr *E) {
157 return EnqueuedBlockMap[getBlockExpr(E)].InvokeFunc;
158}
159
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000160CGOpenCLRuntime::EnqueuedBlockInfo
161CGOpenCLRuntime::emitOpenCLEnqueuedBlock(CodeGenFunction &CGF, const Expr *E) {
Yaxun Liufa13d012018-02-15 16:39:19 +0000162 CGF.EmitScalarExpr(E);
163
Sven van Haastregtda3b6322018-10-02 13:02:24 +0000164 // The block literal may be assigned to a const variable. Chasing down
165 // to get the block literal.
Andrew Savonichev43fceb22019-02-21 11:02:10 +0000166 const BlockExpr *Block = getBlockExpr(E);
Sven van Haastregtda3b6322018-10-02 13:02:24 +0000167
Yaxun Liufa13d012018-02-15 16:39:19 +0000168 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.
172 if (EnqueuedBlockMap[Block].Kernel) {
173 return EnqueuedBlockMap[Block];
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000174 }
175
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000176 auto *F = CGF.getTargetHooks().createEnqueuedBlockKernel(
Yaxun Liufa13d012018-02-15 16:39:19 +0000177 CGF, EnqueuedBlockMap[Block].InvokeFunc,
178 EnqueuedBlockMap[Block].BlockArg->stripPointerCasts());
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000179
180 // The common part of the post-processing of the kernel goes here.
181 F->addFnAttr(llvm::Attribute::NoUnwind);
182 F->setCallingConv(
183 CGF.getTypes().ClangCallConvToLLVMCallConv(CallingConv::CC_OpenCLKernel));
Yaxun Liufa13d012018-02-15 16:39:19 +0000184 EnqueuedBlockMap[Block].Kernel = F;
185 return EnqueuedBlockMap[Block];
Yaxun Liuc2a87a02017-10-14 12:23:50 +0000186}