blob: ecc844f8646d11aa994afa007c08c5ddcf702ee2 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a class for OpenMP runtime code generation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGOpenMPRuntime.h"
15#include "CodeGenFunction.h"
Alexey Bataev18095712014-10-10 12:19:54 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000017#include "clang/AST/Decl.h"
18#include "llvm/ADT/ArrayRef.h"
Alexey Bataevd74d0602014-10-13 06:02:40 +000019#include "llvm/IR/CallSite.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000020#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/GlobalValue.h"
22#include "llvm/IR/Value.h"
23#include "llvm/Support/raw_ostream.h"
Alexey Bataev23b69422014-06-18 07:08:49 +000024#include <cassert>
Alexey Bataev9959db52014-05-06 10:08:46 +000025
26using namespace clang;
27using namespace CodeGen;
28
Benjamin Kramerc52193f2014-10-10 13:57:57 +000029namespace {
Alexey Bataev18095712014-10-10 12:19:54 +000030/// \brief API for captured statement code generation in OpenMP constructs.
31class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
32public:
33 CGOpenMPRegionInfo(const OMPExecutableDirective &D, const CapturedStmt &CS,
34 const VarDecl *ThreadIDVar)
35 : CGCapturedStmtInfo(CS, CR_OpenMP), ThreadIDVar(ThreadIDVar),
36 Directive(D) {
37 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
38 }
39
Alexey Bataev18095712014-10-10 12:19:54 +000040 /// \brief Gets a variable or parameter for storing global thread id
41 /// inside OpenMP construct.
42 const VarDecl *getThreadIDVariable() const { return ThreadIDVar; }
43
44 /// \brief Gets an LValue for the current ThreadID variable.
45 LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
46
47 static bool classof(const CGCapturedStmtInfo *Info) {
48 return Info->getKind() == CR_OpenMP;
49 }
50
51 /// \brief Emit the captured statement body.
Benjamin Kramerc52193f2014-10-10 13:57:57 +000052 void EmitBody(CodeGenFunction &CGF, Stmt *S) override;
Alexey Bataev18095712014-10-10 12:19:54 +000053
54 /// \brief Get the name of the capture helper.
Benjamin Kramerc52193f2014-10-10 13:57:57 +000055 StringRef getHelperName() const override { return ".omp_outlined."; }
Alexey Bataev18095712014-10-10 12:19:54 +000056
57private:
58 /// \brief A variable or parameter storing global thread id for OpenMP
59 /// constructs.
60 const VarDecl *ThreadIDVar;
61 /// \brief OpenMP executable directive associated with the region.
62 const OMPExecutableDirective &Directive;
63};
Benjamin Kramerc52193f2014-10-10 13:57:57 +000064} // namespace
Alexey Bataev18095712014-10-10 12:19:54 +000065
66LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
67 return CGF.MakeNaturalAlignAddrLValue(
68 CGF.GetAddrOfLocalVar(ThreadIDVar),
69 CGF.getContext().getPointerType(ThreadIDVar->getType()));
70}
71
Alexey Bataev4a5bb772014-10-08 14:01:46 +000072void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, Stmt *S) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +000073 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
Alexey Bataev03b340a2014-10-21 03:16:40 +000074 CGF.EmitOMPPrivateClause(Directive, PrivateScope);
Alexey Bataev435ad7b2014-10-10 09:48:26 +000075 CGF.EmitOMPFirstprivateClause(Directive, PrivateScope);
76 if (PrivateScope.Privatize()) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +000077 // Emit implicit barrier to synchronize threads and avoid data races.
78 auto Flags = static_cast<CGOpenMPRuntime::OpenMPLocationFlags>(
79 CGOpenMPRuntime::OMP_IDENT_KMPC |
80 CGOpenMPRuntime::OMP_IDENT_BARRIER_IMPL);
81 CGF.CGM.getOpenMPRuntime().EmitOMPBarrierCall(CGF, Directive.getLocStart(),
82 Flags);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000083 }
84 CGCapturedStmtInfo::EmitBody(CGF, S);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000085}
86
Alexey Bataev9959db52014-05-06 10:08:46 +000087CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
88 : CGM(CGM), DefaultOpenMPPSource(nullptr) {
89 IdentTy = llvm::StructType::create(
90 "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
91 CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
Alexander Musmanfdfa8552014-09-11 08:10:57 +000092 CGM.Int8PtrTy /* psource */, nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +000093 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
Alexey Bataev23b69422014-06-18 07:08:49 +000094 llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
95 llvm::PointerType::getUnqual(CGM.Int32Ty)};
Alexey Bataev9959db52014-05-06 10:08:46 +000096 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000097 KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
Alexey Bataev9959db52014-05-06 10:08:46 +000098}
99
100llvm::Value *
Alexey Bataev18095712014-10-10 12:19:54 +0000101CGOpenMPRuntime::EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
102 const VarDecl *ThreadIDVar) {
103 const CapturedStmt *CS = cast<CapturedStmt>(D.getAssociatedStmt());
104 CodeGenFunction CGF(CGM, true);
105 CGOpenMPRegionInfo CGInfo(D, *CS, ThreadIDVar);
106 CGF.CapturedStmtInfo = &CGInfo;
107 return CGF.GenerateCapturedStmtFunction(*CS);
108}
109
110llvm::Value *
Alexey Bataev9959db52014-05-06 10:08:46 +0000111CGOpenMPRuntime::GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags) {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000112 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000113 if (!Entry) {
114 if (!DefaultOpenMPPSource) {
115 // Initialize default location for psource field of ident_t structure of
116 // all ident_t objects. Format is ";file;function;line;column;;".
117 // Taken from
118 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
119 DefaultOpenMPPSource =
120 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;");
121 DefaultOpenMPPSource =
122 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
123 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000124 auto DefaultOpenMPLocation = new llvm::GlobalVariable(
125 CGM.getModule(), IdentTy, /*isConstant*/ true,
126 llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +0000127 DefaultOpenMPLocation->setUnnamedAddr(true);
Alexey Bataev9959db52014-05-06 10:08:46 +0000128
129 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true);
Alexey Bataev23b69422014-06-18 07:08:49 +0000130 llvm::Constant *Values[] = {Zero,
131 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
132 Zero, Zero, DefaultOpenMPPSource};
Alexey Bataev9959db52014-05-06 10:08:46 +0000133 llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values);
134 DefaultOpenMPLocation->setInitializer(Init);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000135 OpenMPDefaultLocMap[Flags] = DefaultOpenMPLocation;
Alexey Bataev9959db52014-05-06 10:08:46 +0000136 return DefaultOpenMPLocation;
137 }
138 return Entry;
139}
140
141llvm::Value *CGOpenMPRuntime::EmitOpenMPUpdateLocation(
142 CodeGenFunction &CGF, SourceLocation Loc, OpenMPLocationFlags Flags) {
143 // If no debug info is generated - return global default location.
144 if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo ||
145 Loc.isInvalid())
146 return GetOrCreateDefaultOpenMPLocation(Flags);
147
148 assert(CGF.CurFn && "No function in current CodeGenFunction.");
149
Alexey Bataev9959db52014-05-06 10:08:46 +0000150 llvm::Value *LocValue = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000151 OpenMPLocThreadIDMapTy::iterator I = OpenMPLocThreadIDMap.find(CGF.CurFn);
152 if (I != OpenMPLocThreadIDMap.end()) {
153 LocValue = I->second.DebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +0000154 } else {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000155 // Generate "ident_t .kmpc_loc.addr;"
156 llvm::AllocaInst *AI = CGF.CreateTempAlloca(IdentTy, ".kmpc_loc.addr");
Alexey Bataev9959db52014-05-06 10:08:46 +0000157 AI->setAlignment(CGM.getDataLayout().getPrefTypeAlignment(IdentTy));
Alexey Bataev18095712014-10-10 12:19:54 +0000158 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
159 Elem.second.DebugLoc = AI;
Alexey Bataev9959db52014-05-06 10:08:46 +0000160 LocValue = AI;
161
162 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
163 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
164 CGF.Builder.CreateMemCpy(LocValue, GetOrCreateDefaultOpenMPLocation(Flags),
165 llvm::ConstantExpr::getSizeOf(IdentTy),
166 CGM.PointerAlignInBytes);
167 }
168
169 // char **psource = &.kmpc_loc_<flags>.addr.psource;
170 llvm::Value *PSource =
171 CGF.Builder.CreateConstInBoundsGEP2_32(LocValue, 0, IdentField_PSource);
172
Alexey Bataevf002aca2014-05-30 05:48:40 +0000173 auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
174 if (OMPDebugLoc == nullptr) {
175 SmallString<128> Buffer2;
176 llvm::raw_svector_ostream OS2(Buffer2);
177 // Build debug location
178 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
179 OS2 << ";" << PLoc.getFilename() << ";";
180 if (const FunctionDecl *FD =
181 dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
182 OS2 << FD->getQualifiedNameAsString();
183 }
184 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
185 OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
186 OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +0000187 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000188 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
Alexey Bataevf002aca2014-05-30 05:48:40 +0000189 CGF.Builder.CreateStore(OMPDebugLoc, PSource);
190
Alexey Bataev9959db52014-05-06 10:08:46 +0000191 return LocValue;
192}
193
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000194llvm::Value *CGOpenMPRuntime::GetOpenMPThreadID(CodeGenFunction &CGF,
195 SourceLocation Loc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000196 assert(CGF.CurFn && "No function in current CodeGenFunction.");
197
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000198 llvm::Value *ThreadID = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000199 // Check whether we've already cached a load of the thread id in this
200 // function.
201 OpenMPLocThreadIDMapTy::iterator I = OpenMPLocThreadIDMap.find(CGF.CurFn);
202 if (I != OpenMPLocThreadIDMap.end()) {
203 ThreadID = I->second.ThreadID;
Alexey Bataev03b340a2014-10-21 03:16:40 +0000204 if (ThreadID != nullptr)
205 return ThreadID;
206 }
207 if (auto OMPRegionInfo =
Alexey Bataev18095712014-10-10 12:19:54 +0000208 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
209 // Check if this an outlined function with thread id passed as argument.
210 auto ThreadIDVar = OMPRegionInfo->getThreadIDVariable();
211 auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
212 auto RVal = CGF.EmitLoadOfLValue(LVal, Loc);
213 LVal = CGF.MakeNaturalAlignAddrLValue(RVal.getScalarVal(),
214 ThreadIDVar->getType());
215 ThreadID = CGF.EmitLoadOfLValue(LVal, Loc).getScalarVal();
216 // If value loaded in entry block, cache it and use it everywhere in
217 // function.
218 if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
219 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
220 Elem.second.ThreadID = ThreadID;
Alexey Bataevd6c57552014-07-25 07:55:17 +0000221 }
Alexey Bataev18095712014-10-10 12:19:54 +0000222 } else {
223 // This is not an outlined function region - need to call __kmpc_int32
224 // kmpc_global_thread_num(ident_t *loc).
225 // Generate thread id value and cache this value for use across the
226 // function.
227 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
228 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
229 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc)};
230 ThreadID = CGF.EmitRuntimeCall(
231 CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num), Args);
232 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
233 Elem.second.ThreadID = ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +0000234 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000235 return ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +0000236}
237
238void CGOpenMPRuntime::FunctionFinished(CodeGenFunction &CGF) {
239 assert(CGF.CurFn && "No function in current CodeGenFunction.");
Alexey Bataev03b340a2014-10-21 03:16:40 +0000240 if (OpenMPLocThreadIDMap.count(CGF.CurFn))
241 OpenMPLocThreadIDMap.erase(CGF.CurFn);
Alexey Bataev9959db52014-05-06 10:08:46 +0000242}
243
244llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
245 return llvm::PointerType::getUnqual(IdentTy);
246}
247
248llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
249 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
250}
251
252llvm::Constant *
253CGOpenMPRuntime::CreateRuntimeFunction(OpenMPRTLFunction Function) {
254 llvm::Constant *RTLFn = nullptr;
255 switch (Function) {
256 case OMPRTL__kmpc_fork_call: {
257 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
258 // microtask, ...);
Alexey Bataev23b69422014-06-18 07:08:49 +0000259 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
260 getKmpc_MicroPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000261 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +0000262 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
Alexey Bataev9959db52014-05-06 10:08:46 +0000263 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
264 break;
265 }
266 case OMPRTL__kmpc_global_thread_num: {
267 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
Alexey Bataev23b69422014-06-18 07:08:49 +0000268 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000269 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +0000270 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
Alexey Bataev9959db52014-05-06 10:08:46 +0000271 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
272 break;
273 }
Alexey Bataev97720002014-11-11 04:05:39 +0000274 case OMPRTL__kmpc_threadprivate_cached: {
275 // Build void *__kmpc_threadprivate_cached(ident_t *loc,
276 // kmp_int32 global_tid, void *data, size_t size, void ***cache);
277 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
278 CGM.VoidPtrTy, CGM.SizeTy,
279 CGM.VoidPtrTy->getPointerTo()->getPointerTo()};
280 llvm::FunctionType *FnTy =
281 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false);
282 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached");
283 break;
284 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000285 case OMPRTL__kmpc_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000286 // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
287 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000288 llvm::Type *TypeParams[] = {
289 getIdentTyPointerTy(), CGM.Int32Ty,
290 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
291 llvm::FunctionType *FnTy =
292 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
293 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
294 break;
295 }
Alexey Bataev97720002014-11-11 04:05:39 +0000296 case OMPRTL__kmpc_threadprivate_register: {
297 // Build void __kmpc_threadprivate_register(ident_t *, void *data,
298 // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
299 // typedef void *(*kmpc_ctor)(void *);
300 auto KmpcCtorTy =
301 llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
302 /*isVarArg*/ false)->getPointerTo();
303 // typedef void *(*kmpc_cctor)(void *, void *);
304 llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
305 auto KmpcCopyCtorTy =
306 llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs,
307 /*isVarArg*/ false)->getPointerTo();
308 // typedef void (*kmpc_dtor)(void *);
309 auto KmpcDtorTy =
310 llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false)
311 ->getPointerTo();
312 llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy,
313 KmpcCopyCtorTy, KmpcDtorTy};
314 auto FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs,
315 /*isVarArg*/ false);
316 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register");
317 break;
318 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000319 case OMPRTL__kmpc_end_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000320 // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
321 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000322 llvm::Type *TypeParams[] = {
323 getIdentTyPointerTy(), CGM.Int32Ty,
324 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
325 llvm::FunctionType *FnTy =
326 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
327 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
328 break;
329 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000330 case OMPRTL__kmpc_barrier: {
331 // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
332 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
333 llvm::FunctionType *FnTy =
334 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
335 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier");
336 break;
337 }
Alexey Bataevb2059782014-10-13 08:23:51 +0000338 case OMPRTL__kmpc_push_num_threads: {
339 // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
340 // kmp_int32 num_threads)
341 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
342 CGM.Int32Ty};
343 llvm::FunctionType *FnTy =
344 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
345 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads");
346 break;
347 }
Alexey Bataevd74d0602014-10-13 06:02:40 +0000348 case OMPRTL__kmpc_serialized_parallel: {
349 // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
350 // global_tid);
351 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
352 llvm::FunctionType *FnTy =
353 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
354 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
355 break;
356 }
357 case OMPRTL__kmpc_end_serialized_parallel: {
358 // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
359 // global_tid);
360 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
361 llvm::FunctionType *FnTy =
362 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
363 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
364 break;
365 }
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000366 case OMPRTL__kmpc_flush: {
367 // Build void __kmpc_flush(ident_t *loc, ...);
368 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
369 llvm::FunctionType *FnTy =
370 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
371 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush");
372 break;
373 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000374 }
375 return RTLFn;
376}
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000377
Alexey Bataev97720002014-11-11 04:05:39 +0000378llvm::Constant *
379CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
380 // Lookup the entry, lazily creating it if necessary.
381 return GetOrCreateInternalVariable(CGM.Int8PtrPtrTy,
382 Twine(CGM.getMangledName(VD)) + ".cache.");
383}
384
385llvm::Value *CGOpenMPRuntime::getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
386 const VarDecl *VD,
387 llvm::Value *VDAddr,
388 SourceLocation Loc) {
389 auto VarTy = VDAddr->getType()->getPointerElementType();
390 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
391 GetOpenMPThreadID(CGF, Loc),
392 CGF.Builder.CreatePointerCast(VDAddr, CGM.Int8PtrTy),
393 CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)),
394 getOrCreateThreadPrivateCache(VD)};
395 return CGF.EmitRuntimeCall(
396 CreateRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args);
397}
398
399void CGOpenMPRuntime::EmitOMPThreadPrivateVarInit(
400 CodeGenFunction &CGF, llvm::Value *VDAddr, llvm::Value *Ctor,
401 llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) {
402 // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime
403 // library.
404 auto OMPLoc = EmitOpenMPUpdateLocation(CGF, Loc);
405 CGF.EmitRuntimeCall(CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num),
406 OMPLoc);
407 // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor)
408 // to register constructor/destructor for variable.
409 llvm::Value *Args[] = {OMPLoc,
410 CGF.Builder.CreatePointerCast(VDAddr, CGM.VoidPtrTy),
411 Ctor, CopyCtor, Dtor};
412 CGF.EmitRuntimeCall(CreateRuntimeFunction(
413 CGOpenMPRuntime::OMPRTL__kmpc_threadprivate_register),
414 Args);
415}
416
417llvm::Function *CGOpenMPRuntime::EmitOMPThreadPrivateVarDefinition(
418 const VarDecl *VD, llvm::Value *VDAddr, SourceLocation Loc,
419 bool PerformInit, CodeGenFunction *CGF) {
420 VD = VD->getDefinition(CGM.getContext());
421 if (VD && ThreadPrivateWithDefinition.count(VD) == 0) {
422 ThreadPrivateWithDefinition.insert(VD);
423 QualType ASTTy = VD->getType();
424
425 llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr;
426 auto Init = VD->getAnyInitializer();
427 if (CGM.getLangOpts().CPlusPlus && PerformInit) {
428 // Generate function that re-emits the declaration's initializer into the
429 // threadprivate copy of the variable VD
430 CodeGenFunction CtorCGF(CGM);
431 FunctionArgList Args;
432 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
433 /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
434 Args.push_back(&Dst);
435
436 auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
437 CGM.getContext().VoidPtrTy, Args, FunctionType::ExtInfo(),
438 /*isVariadic=*/false);
439 auto FTy = CGM.getTypes().GetFunctionType(FI);
440 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
441 FTy, ".__kmpc_global_ctor_.", Loc);
442 CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
443 Args, SourceLocation());
444 auto ArgVal = CtorCGF.EmitLoadOfScalar(
445 CtorCGF.GetAddrOfLocalVar(&Dst),
446 /*Volatile=*/false, CGM.PointerAlignInBytes,
447 CGM.getContext().VoidPtrTy, Dst.getLocation());
448 auto Arg = CtorCGF.Builder.CreatePointerCast(
449 ArgVal,
450 CtorCGF.ConvertTypeForMem(CGM.getContext().getPointerType(ASTTy)));
451 CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
452 /*IsInitializer=*/true);
453 ArgVal = CtorCGF.EmitLoadOfScalar(
454 CtorCGF.GetAddrOfLocalVar(&Dst),
455 /*Volatile=*/false, CGM.PointerAlignInBytes,
456 CGM.getContext().VoidPtrTy, Dst.getLocation());
457 CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue);
458 CtorCGF.FinishFunction();
459 Ctor = Fn;
460 }
461 if (VD->getType().isDestructedType() != QualType::DK_none) {
462 // Generate function that emits destructor call for the threadprivate copy
463 // of the variable VD
464 CodeGenFunction DtorCGF(CGM);
465 FunctionArgList Args;
466 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
467 /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
468 Args.push_back(&Dst);
469
470 auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
471 CGM.getContext().VoidTy, Args, FunctionType::ExtInfo(),
472 /*isVariadic=*/false);
473 auto FTy = CGM.getTypes().GetFunctionType(FI);
474 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
475 FTy, ".__kmpc_global_dtor_.", Loc);
476 DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
477 SourceLocation());
478 auto ArgVal = DtorCGF.EmitLoadOfScalar(
479 DtorCGF.GetAddrOfLocalVar(&Dst),
480 /*Volatile=*/false, CGM.PointerAlignInBytes,
481 CGM.getContext().VoidPtrTy, Dst.getLocation());
482 DtorCGF.emitDestroy(ArgVal, ASTTy,
483 DtorCGF.getDestroyer(ASTTy.isDestructedType()),
484 DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
485 DtorCGF.FinishFunction();
486 Dtor = Fn;
487 }
488 // Do not emit init function if it is not required.
489 if (!Ctor && !Dtor)
490 return nullptr;
491
492 llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
493 auto CopyCtorTy =
494 llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs,
495 /*isVarArg=*/false)->getPointerTo();
496 // Copying constructor for the threadprivate variable.
497 // Must be NULL - reserved by runtime, but currently it requires that this
498 // parameter is always NULL. Otherwise it fires assertion.
499 CopyCtor = llvm::Constant::getNullValue(CopyCtorTy);
500 if (Ctor == nullptr) {
501 auto CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
502 /*isVarArg=*/false)->getPointerTo();
503 Ctor = llvm::Constant::getNullValue(CtorTy);
504 }
505 if (Dtor == nullptr) {
506 auto DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy,
507 /*isVarArg=*/false)->getPointerTo();
508 Dtor = llvm::Constant::getNullValue(DtorTy);
509 }
510 if (!CGF) {
511 auto InitFunctionTy =
512 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
513 auto InitFunction = CGM.CreateGlobalInitOrDestructFunction(
514 InitFunctionTy, ".__omp_threadprivate_init_.");
515 CodeGenFunction InitCGF(CGM);
516 FunctionArgList ArgList;
517 InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
518 CGM.getTypes().arrangeNullaryFunction(), ArgList,
519 Loc);
520 EmitOMPThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
521 InitCGF.FinishFunction();
522 return InitFunction;
523 }
524 EmitOMPThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
525 }
526 return nullptr;
527}
528
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000529void CGOpenMPRuntime::EmitOMPParallelCall(CodeGenFunction &CGF,
530 SourceLocation Loc,
531 llvm::Value *OutlinedFn,
532 llvm::Value *CapturedStruct) {
533 // Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/)
534 llvm::Value *Args[] = {
535 EmitOpenMPUpdateLocation(CGF, Loc),
536 CGF.Builder.getInt32(1), // Number of arguments after 'microtask' argument
537 // (there is only one additional argument - 'context')
538 CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy()),
539 CGF.EmitCastToVoidPtr(CapturedStruct)};
540 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_fork_call);
541 CGF.EmitRuntimeCall(RTLFn, Args);
542}
543
Alexey Bataevd74d0602014-10-13 06:02:40 +0000544void CGOpenMPRuntime::EmitOMPSerialCall(CodeGenFunction &CGF,
545 SourceLocation Loc,
546 llvm::Value *OutlinedFn,
547 llvm::Value *CapturedStruct) {
548 auto ThreadID = GetOpenMPThreadID(CGF, Loc);
549 // Build calls:
550 // __kmpc_serialized_parallel(&Loc, GTid);
551 llvm::Value *SerArgs[] = {EmitOpenMPUpdateLocation(CGF, Loc), ThreadID};
552 auto RTLFn =
553 CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_serialized_parallel);
554 CGF.EmitRuntimeCall(RTLFn, SerArgs);
555
556 // OutlinedFn(&GTid, &zero, CapturedStruct);
557 auto ThreadIDAddr = EmitThreadIDAddress(CGF, Loc);
558 auto Int32Ty =
559 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
560 auto ZeroAddr = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".zero.addr");
561 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
562 llvm::Value *OutlinedFnArgs[] = {ThreadIDAddr, ZeroAddr, CapturedStruct};
563 CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
564
565 // __kmpc_end_serialized_parallel(&Loc, GTid);
566 llvm::Value *EndSerArgs[] = {EmitOpenMPUpdateLocation(CGF, Loc), ThreadID};
567 RTLFn = CreateRuntimeFunction(
568 CGOpenMPRuntime::OMPRTL__kmpc_end_serialized_parallel);
569 CGF.EmitRuntimeCall(RTLFn, EndSerArgs);
570}
571
NAKAMURA Takumi59c74b222014-10-27 08:08:18 +0000572// If we're inside an (outlined) parallel region, use the region info's
Alexey Bataevd74d0602014-10-13 06:02:40 +0000573// thread-ID variable (it is passed in a first argument of the outlined function
574// as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
575// regular serial code region, get thread ID by calling kmp_int32
576// kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
577// return the address of that temp.
578llvm::Value *CGOpenMPRuntime::EmitThreadIDAddress(CodeGenFunction &CGF,
579 SourceLocation Loc) {
580 if (auto OMPRegionInfo =
581 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
582 return CGF.EmitLoadOfLValue(OMPRegionInfo->getThreadIDVariableLValue(CGF),
583 SourceLocation()).getScalarVal();
584 auto ThreadID = GetOpenMPThreadID(CGF, Loc);
585 auto Int32Ty =
586 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
587 auto ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
588 CGF.EmitStoreOfScalar(ThreadID,
589 CGF.MakeNaturalAlignAddrLValue(ThreadIDTemp, Int32Ty));
590
591 return ThreadIDTemp;
592}
593
Alexey Bataev97720002014-11-11 04:05:39 +0000594llvm::Constant *
595CGOpenMPRuntime::GetOrCreateInternalVariable(llvm::Type *Ty,
596 const llvm::Twine &Name) {
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000597 SmallString<256> Buffer;
598 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev97720002014-11-11 04:05:39 +0000599 Out << Name;
600 auto RuntimeName = Out.str();
David Blaikie13156b62014-11-19 03:06:06 +0000601 auto &Elem = *InternalVars.insert(std::make_pair(RuntimeName, nullptr)).first;
602 if (Elem.second) {
603 assert(Elem.second->getType()->getPointerElementType() == Ty &&
Alexey Bataev97720002014-11-11 04:05:39 +0000604 "OMP internal variable has different type than requested");
David Blaikie13156b62014-11-19 03:06:06 +0000605 return &*Elem.second;
Alexey Bataev97720002014-11-11 04:05:39 +0000606 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000607
David Blaikie13156b62014-11-19 03:06:06 +0000608 return Elem.second = new llvm::GlobalVariable(
609 CGM.getModule(), Ty, /*IsConstant*/ false,
610 llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
611 Elem.first());
Alexey Bataev97720002014-11-11 04:05:39 +0000612}
613
614llvm::Value *CGOpenMPRuntime::GetCriticalRegionLock(StringRef CriticalName) {
615 llvm::Twine Name(".gomp_critical_user_", CriticalName);
616 return GetOrCreateInternalVariable(KmpCriticalNameTy, Name.concat(".var"));
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000617}
618
619void CGOpenMPRuntime::EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
620 llvm::Value *RegionLock,
621 SourceLocation Loc) {
622 // Prepare other arguments and build a call to __kmpc_critical
623 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000624 GetOpenMPThreadID(CGF, Loc), RegionLock};
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000625 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_critical);
626 CGF.EmitRuntimeCall(RTLFn, Args);
627}
628
629void CGOpenMPRuntime::EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
630 llvm::Value *RegionLock,
631 SourceLocation Loc) {
Alexey Bataevf9472182014-09-22 12:32:31 +0000632 // Prepare other arguments and build a call to __kmpc_end_critical
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000633 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000634 GetOpenMPThreadID(CGF, Loc), RegionLock};
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000635 auto RTLFn =
636 CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_end_critical);
637 CGF.EmitRuntimeCall(RTLFn, Args);
638}
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000639
640void CGOpenMPRuntime::EmitOMPBarrierCall(CodeGenFunction &CGF,
641 SourceLocation Loc,
642 OpenMPLocationFlags Flags) {
643 // Build call __kmpc_barrier(loc, thread_id)
644 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc, Flags),
645 GetOpenMPThreadID(CGF, Loc)};
646 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_barrier);
647 CGF.EmitRuntimeCall(RTLFn, Args);
648}
649
Alexey Bataevb2059782014-10-13 08:23:51 +0000650void CGOpenMPRuntime::EmitOMPNumThreadsClause(CodeGenFunction &CGF,
651 llvm::Value *NumThreads,
652 SourceLocation Loc) {
653 // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
654 llvm::Value *Args[] = {
655 EmitOpenMPUpdateLocation(CGF, Loc), GetOpenMPThreadID(CGF, Loc),
656 CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
657 llvm::Constant *RTLFn = CGF.CGM.getOpenMPRuntime().CreateRuntimeFunction(
658 CGOpenMPRuntime::OMPRTL__kmpc_push_num_threads);
659 CGF.EmitRuntimeCall(RTLFn, Args);
660}
661
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000662void CGOpenMPRuntime::EmitOMPFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
663 SourceLocation Loc) {
664 // Build call void __kmpc_flush(ident_t *loc, ...)
665 // FIXME: List of variables is ignored by libiomp5 runtime, no need to
666 // generate it, just request full memory fence.
667 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
668 llvm::ConstantInt::get(CGM.Int32Ty, 0)};
669 auto *RTLFn = CGF.CGM.getOpenMPRuntime().CreateRuntimeFunction(
670 CGOpenMPRuntime::OMPRTL__kmpc_flush);
671 CGF.EmitRuntimeCall(RTLFn, Args);
672}