blob: f77983a15f4cbe961aad94f628cbde1c25fbfe51 [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 Bataev36bf0112015-03-10 05:15:26 +000016#include "CGCleanup.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000017#include "clang/AST/Decl.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000018#include "clang/AST/StmtOpenMP.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000019#include "llvm/ADT/ArrayRef.h"
Alexey Bataevd74d0602014-10-13 06:02:40 +000020#include "llvm/IR/CallSite.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000021#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/GlobalValue.h"
23#include "llvm/IR/Value.h"
24#include "llvm/Support/raw_ostream.h"
Alexey Bataev23b69422014-06-18 07:08:49 +000025#include <cassert>
Alexey Bataev9959db52014-05-06 10:08:46 +000026
27using namespace clang;
28using namespace CodeGen;
29
Benjamin Kramerc52193f2014-10-10 13:57:57 +000030namespace {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000031/// \brief Base class for handling code generation inside OpenMP regions.
Alexey Bataev18095712014-10-10 12:19:54 +000032class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
33public:
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000034 CGOpenMPRegionInfo(const OMPExecutableDirective &D, const CapturedStmt &CS)
35 : CGCapturedStmtInfo(CS, CR_OpenMP), Directive(D) {}
Alexey Bataev18095712014-10-10 12:19:54 +000036
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000037 CGOpenMPRegionInfo(const OMPExecutableDirective &D)
38 : CGCapturedStmtInfo(CR_OpenMP), Directive(D) {}
39
40 /// \brief Get a variable or parameter for storing global thread id
Alexey Bataev18095712014-10-10 12:19:54 +000041 /// inside OpenMP construct.
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000042 virtual const VarDecl *getThreadIDVariable() const = 0;
Alexey Bataev18095712014-10-10 12:19:54 +000043
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000044 /// \brief Get an LValue for the current ThreadID variable.
Alexey Bataev62b63b12015-03-10 07:28:44 +000045 /// \return LValue for thread id variable. This LValue always has type int32*.
46 virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
Alexey Bataev18095712014-10-10 12:19:54 +000047
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000048 /// \brief Emit the captured statement body.
49 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) override;
50
Alexey Bataev18095712014-10-10 12:19:54 +000051 static bool classof(const CGCapturedStmtInfo *Info) {
52 return Info->getKind() == CR_OpenMP;
53 }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000054protected:
55 /// \brief OpenMP executable directive associated with the region.
56 const OMPExecutableDirective &Directive;
57};
Alexey Bataev18095712014-10-10 12:19:54 +000058
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000059/// \brief API for captured statement code generation in OpenMP constructs.
60class CGOpenMPOutlinedRegionInfo : public CGOpenMPRegionInfo {
61public:
62 CGOpenMPOutlinedRegionInfo(const OMPExecutableDirective &D,
63 const CapturedStmt &CS, const VarDecl *ThreadIDVar)
64 : CGOpenMPRegionInfo(D, CS), ThreadIDVar(ThreadIDVar) {
65 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
66 }
67 /// \brief Get a variable or parameter for storing global thread id
68 /// inside OpenMP construct.
69 virtual const VarDecl *getThreadIDVariable() const override {
70 return ThreadIDVar;
71 }
Alexey Bataev18095712014-10-10 12:19:54 +000072 /// \brief Get the name of the capture helper.
Benjamin Kramerc52193f2014-10-10 13:57:57 +000073 StringRef getHelperName() const override { return ".omp_outlined."; }
Alexey Bataev18095712014-10-10 12:19:54 +000074
75private:
76 /// \brief A variable or parameter storing global thread id for OpenMP
77 /// constructs.
78 const VarDecl *ThreadIDVar;
Alexey Bataev8cbe0a62015-02-26 10:27:34 +000079};
80
Alexey Bataev62b63b12015-03-10 07:28:44 +000081/// \brief API for captured statement code generation in OpenMP constructs.
82class CGOpenMPTaskOutlinedRegionInfo : public CGOpenMPRegionInfo {
83public:
84 CGOpenMPTaskOutlinedRegionInfo(const OMPExecutableDirective &D,
85 const CapturedStmt &CS,
86 const VarDecl *ThreadIDVar,
87 const VarDecl *PartIDVar)
88 : CGOpenMPRegionInfo(D, CS), ThreadIDVar(ThreadIDVar),
89 PartIDVar(PartIDVar) {
90 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
91 }
92 /// \brief Get a variable or parameter for storing global thread id
93 /// inside OpenMP construct.
94 virtual const VarDecl *getThreadIDVariable() const override {
95 return ThreadIDVar;
96 }
97
98 /// \brief Get an LValue for the current ThreadID variable.
99 virtual LValue getThreadIDVariableLValue(CodeGenFunction &CGF) override;
100
101 /// \brief Emit the captured statement body.
102 virtual void EmitBody(CodeGenFunction &CGF, const Stmt *S) override;
103
104 /// \brief Get the name of the capture helper.
105 StringRef getHelperName() const override { return ".omp_outlined."; }
106
107private:
108 /// \brief A variable or parameter storing global thread id for OpenMP
109 /// constructs.
110 const VarDecl *ThreadIDVar;
111 /// \brief A variable or parameter storing part id for OpenMP tasking
112 /// constructs.
113 const VarDecl *PartIDVar;
114};
115
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000116/// \brief API for inlined captured statement code generation in OpenMP
117/// constructs.
118class CGOpenMPInlinedRegionInfo : public CGOpenMPRegionInfo {
119public:
120 CGOpenMPInlinedRegionInfo(const OMPExecutableDirective &D,
121 CodeGenFunction::CGCapturedStmtInfo *OldCSI)
122 : CGOpenMPRegionInfo(D), OldCSI(OldCSI),
123 OuterRegionInfo(dyn_cast_or_null<CGOpenMPRegionInfo>(OldCSI)) {}
124 // \brief Retrieve the value of the context parameter.
125 virtual llvm::Value *getContextValue() const override {
126 if (OuterRegionInfo)
127 return OuterRegionInfo->getContextValue();
128 llvm_unreachable("No context value for inlined OpenMP region");
129 }
130 /// \brief Lookup the captured field decl for a variable.
131 virtual const FieldDecl *lookup(const VarDecl *VD) const override {
132 if (OuterRegionInfo)
133 return OuterRegionInfo->lookup(VD);
134 llvm_unreachable("Trying to reference VarDecl that is neither local nor "
135 "captured in outer OpenMP region");
136 }
137 virtual FieldDecl *getThisFieldDecl() const override {
138 if (OuterRegionInfo)
139 return OuterRegionInfo->getThisFieldDecl();
140 return nullptr;
141 }
142 /// \brief Get a variable or parameter for storing global thread id
143 /// inside OpenMP construct.
144 virtual const VarDecl *getThreadIDVariable() const override {
145 if (OuterRegionInfo)
146 return OuterRegionInfo->getThreadIDVariable();
147 return nullptr;
148 }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000149
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000150 /// \brief Get the name of the capture helper.
151 virtual StringRef getHelperName() const override {
152 llvm_unreachable("No helper name for inlined OpenMP construct");
153 }
154
155 CodeGenFunction::CGCapturedStmtInfo *getOldCSI() const { return OldCSI; }
156
157private:
158 /// \brief CodeGen info about outer OpenMP region.
159 CodeGenFunction::CGCapturedStmtInfo *OldCSI;
160 CGOpenMPRegionInfo *OuterRegionInfo;
Alexey Bataev18095712014-10-10 12:19:54 +0000161};
Benjamin Kramerc52193f2014-10-10 13:57:57 +0000162} // namespace
Alexey Bataev18095712014-10-10 12:19:54 +0000163
164LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
165 return CGF.MakeNaturalAlignAddrLValue(
Alexey Bataev62b63b12015-03-10 07:28:44 +0000166 CGF.Builder.CreateAlignedLoad(
167 CGF.GetAddrOfLocalVar(getThreadIDVariable()),
168 CGF.PointerAlignInBytes),
169 getThreadIDVariable()
170 ->getType()
171 ->castAs<PointerType>()
172 ->getPointeeType());
Alexey Bataev18095712014-10-10 12:19:54 +0000173}
174
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000175void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, const Stmt *S) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000176 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
Alexey Bataev03b340a2014-10-21 03:16:40 +0000177 CGF.EmitOMPPrivateClause(Directive, PrivateScope);
Alexey Bataev435ad7b2014-10-10 09:48:26 +0000178 CGF.EmitOMPFirstprivateClause(Directive, PrivateScope);
Alexey Bataev8f7c1b02014-12-05 04:09:23 +0000179 if (PrivateScope.Privatize())
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000180 // Emit implicit barrier to synchronize threads and avoid data races.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000181 CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, Directive.getLocStart(),
182 /*IsExplicit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000183 CGCapturedStmtInfo::EmitBody(CGF, S);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000184}
185
Alexey Bataev62b63b12015-03-10 07:28:44 +0000186LValue CGOpenMPTaskOutlinedRegionInfo::getThreadIDVariableLValue(
187 CodeGenFunction &CGF) {
188 return CGF.MakeNaturalAlignAddrLValue(
189 CGF.GetAddrOfLocalVar(getThreadIDVariable()),
190 getThreadIDVariable()->getType());
191}
192
193void CGOpenMPTaskOutlinedRegionInfo::EmitBody(CodeGenFunction &CGF,
194 const Stmt *S) {
195 if (PartIDVar) {
196 // TODO: emit code for untied tasks.
197 }
198 CGCapturedStmtInfo::EmitBody(CGF, S);
199}
200
Alexey Bataev9959db52014-05-06 10:08:46 +0000201CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
Alexey Bataev62b63b12015-03-10 07:28:44 +0000202 : CGM(CGM), DefaultOpenMPPSource(nullptr), KmpRoutineEntryPtrTy(nullptr) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000203 IdentTy = llvm::StructType::create(
204 "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
205 CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
Alexander Musmanfdfa8552014-09-11 08:10:57 +0000206 CGM.Int8PtrTy /* psource */, nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +0000207 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
Alexey Bataev23b69422014-06-18 07:08:49 +0000208 llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
209 llvm::PointerType::getUnqual(CGM.Int32Ty)};
Alexey Bataev9959db52014-05-06 10:08:46 +0000210 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000211 KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
Alexey Bataev9959db52014-05-06 10:08:46 +0000212}
213
214llvm::Value *
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000215CGOpenMPRuntime::emitOutlinedFunction(const OMPExecutableDirective &D,
216 const VarDecl *ThreadIDVar) {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000217 assert(ThreadIDVar->getType()->isPointerType() &&
218 "thread id variable must be of type kmp_int32 *");
Alexey Bataev18095712014-10-10 12:19:54 +0000219 const CapturedStmt *CS = cast<CapturedStmt>(D.getAssociatedStmt());
220 CodeGenFunction CGF(CGM, true);
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000221 CGOpenMPOutlinedRegionInfo CGInfo(D, *CS, ThreadIDVar);
Alexey Bataev18095712014-10-10 12:19:54 +0000222 CGF.CapturedStmtInfo = &CGInfo;
223 return CGF.GenerateCapturedStmtFunction(*CS);
224}
225
226llvm::Value *
Alexey Bataev62b63b12015-03-10 07:28:44 +0000227CGOpenMPRuntime::emitTaskOutlinedFunction(const OMPExecutableDirective &D,
228 const VarDecl *ThreadIDVar,
229 const VarDecl *PartIDVar) {
230 assert(!ThreadIDVar->getType()->isPointerType() &&
231 "thread id variable must be of type kmp_int32 for tasks");
232 auto *CS = cast<CapturedStmt>(D.getAssociatedStmt());
233 CodeGenFunction CGF(CGM, true);
234 CGOpenMPTaskOutlinedRegionInfo CGInfo(D, *CS, ThreadIDVar, PartIDVar);
235 CGF.CapturedStmtInfo = &CGInfo;
236 return CGF.GenerateCapturedStmtFunction(*CS);
237}
238
239llvm::Value *
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000240CGOpenMPRuntime::getOrCreateDefaultLocation(OpenMPLocationFlags Flags) {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000241 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000242 if (!Entry) {
243 if (!DefaultOpenMPPSource) {
244 // Initialize default location for psource field of ident_t structure of
245 // all ident_t objects. Format is ";file;function;line;column;;".
246 // Taken from
247 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
248 DefaultOpenMPPSource =
249 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;");
250 DefaultOpenMPPSource =
251 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
252 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000253 auto DefaultOpenMPLocation = new llvm::GlobalVariable(
254 CGM.getModule(), IdentTy, /*isConstant*/ true,
255 llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +0000256 DefaultOpenMPLocation->setUnnamedAddr(true);
Alexey Bataev9959db52014-05-06 10:08:46 +0000257
258 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true);
Alexey Bataev23b69422014-06-18 07:08:49 +0000259 llvm::Constant *Values[] = {Zero,
260 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
261 Zero, Zero, DefaultOpenMPPSource};
Alexey Bataev9959db52014-05-06 10:08:46 +0000262 llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values);
263 DefaultOpenMPLocation->setInitializer(Init);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000264 OpenMPDefaultLocMap[Flags] = DefaultOpenMPLocation;
Alexey Bataev9959db52014-05-06 10:08:46 +0000265 return DefaultOpenMPLocation;
266 }
267 return Entry;
268}
269
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000270llvm::Value *CGOpenMPRuntime::emitUpdateLocation(CodeGenFunction &CGF,
271 SourceLocation Loc,
272 OpenMPLocationFlags Flags) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000273 // If no debug info is generated - return global default location.
274 if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo ||
275 Loc.isInvalid())
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000276 return getOrCreateDefaultLocation(Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000277
278 assert(CGF.CurFn && "No function in current CodeGenFunction.");
279
Alexey Bataev9959db52014-05-06 10:08:46 +0000280 llvm::Value *LocValue = nullptr;
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000281 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
282 if (I != OpenMPLocThreadIDMap.end())
Alexey Bataev18095712014-10-10 12:19:54 +0000283 LocValue = I->second.DebugLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +0000284 // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if
285 // GetOpenMPThreadID was called before this routine.
286 if (LocValue == nullptr) {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000287 // Generate "ident_t .kmpc_loc.addr;"
288 llvm::AllocaInst *AI = CGF.CreateTempAlloca(IdentTy, ".kmpc_loc.addr");
Alexey Bataev9959db52014-05-06 10:08:46 +0000289 AI->setAlignment(CGM.getDataLayout().getPrefTypeAlignment(IdentTy));
Alexey Bataev18095712014-10-10 12:19:54 +0000290 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
291 Elem.second.DebugLoc = AI;
Alexey Bataev9959db52014-05-06 10:08:46 +0000292 LocValue = AI;
293
294 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
295 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000296 CGF.Builder.CreateMemCpy(LocValue, getOrCreateDefaultLocation(Flags),
Alexey Bataev9959db52014-05-06 10:08:46 +0000297 llvm::ConstantExpr::getSizeOf(IdentTy),
298 CGM.PointerAlignInBytes);
299 }
300
301 // char **psource = &.kmpc_loc_<flags>.addr.psource;
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000302 auto *PSource =
Alexey Bataev9959db52014-05-06 10:08:46 +0000303 CGF.Builder.CreateConstInBoundsGEP2_32(LocValue, 0, IdentField_PSource);
304
Alexey Bataevf002aca2014-05-30 05:48:40 +0000305 auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
306 if (OMPDebugLoc == nullptr) {
307 SmallString<128> Buffer2;
308 llvm::raw_svector_ostream OS2(Buffer2);
309 // Build debug location
310 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
311 OS2 << ";" << PLoc.getFilename() << ";";
312 if (const FunctionDecl *FD =
313 dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
314 OS2 << FD->getQualifiedNameAsString();
315 }
316 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
317 OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
318 OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +0000319 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000320 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
Alexey Bataevf002aca2014-05-30 05:48:40 +0000321 CGF.Builder.CreateStore(OMPDebugLoc, PSource);
322
Alexey Bataev9959db52014-05-06 10:08:46 +0000323 return LocValue;
324}
325
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000326llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
327 SourceLocation Loc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000328 assert(CGF.CurFn && "No function in current CodeGenFunction.");
329
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000330 llvm::Value *ThreadID = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000331 // Check whether we've already cached a load of the thread id in this
332 // function.
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000333 auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
Alexey Bataev18095712014-10-10 12:19:54 +0000334 if (I != OpenMPLocThreadIDMap.end()) {
335 ThreadID = I->second.ThreadID;
Alexey Bataev03b340a2014-10-21 03:16:40 +0000336 if (ThreadID != nullptr)
337 return ThreadID;
338 }
339 if (auto OMPRegionInfo =
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000340 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
Alexey Bataev62b63b12015-03-10 07:28:44 +0000341 if (OMPRegionInfo->getThreadIDVariable()) {
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000342 // Check if this an outlined function with thread id passed as argument.
343 auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000344 ThreadID = CGF.EmitLoadOfLValue(LVal, Loc).getScalarVal();
345 // If value loaded in entry block, cache it and use it everywhere in
346 // function.
347 if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
348 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
349 Elem.second.ThreadID = ThreadID;
350 }
351 return ThreadID;
Alexey Bataevd6c57552014-07-25 07:55:17 +0000352 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000353 }
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000354
355 // This is not an outlined function region - need to call __kmpc_int32
356 // kmpc_global_thread_num(ident_t *loc).
357 // Generate thread id value and cache this value for use across the
358 // function.
359 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
360 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
361 ThreadID =
362 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
363 emitUpdateLocation(CGF, Loc));
364 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
365 Elem.second.ThreadID = ThreadID;
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000366 return ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +0000367}
368
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000369void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000370 assert(CGF.CurFn && "No function in current CodeGenFunction.");
Alexey Bataev03b340a2014-10-21 03:16:40 +0000371 if (OpenMPLocThreadIDMap.count(CGF.CurFn))
372 OpenMPLocThreadIDMap.erase(CGF.CurFn);
Alexey Bataev9959db52014-05-06 10:08:46 +0000373}
374
375llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
376 return llvm::PointerType::getUnqual(IdentTy);
377}
378
379llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
380 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
381}
382
383llvm::Constant *
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000384CGOpenMPRuntime::createRuntimeFunction(OpenMPRTLFunction Function) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000385 llvm::Constant *RTLFn = nullptr;
386 switch (Function) {
387 case OMPRTL__kmpc_fork_call: {
388 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
389 // microtask, ...);
Alexey Bataev23b69422014-06-18 07:08:49 +0000390 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
391 getKmpc_MicroPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000392 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +0000393 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
Alexey Bataev9959db52014-05-06 10:08:46 +0000394 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
395 break;
396 }
397 case OMPRTL__kmpc_global_thread_num: {
398 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
Alexey Bataev23b69422014-06-18 07:08:49 +0000399 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000400 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +0000401 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
Alexey Bataev9959db52014-05-06 10:08:46 +0000402 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
403 break;
404 }
Alexey Bataev97720002014-11-11 04:05:39 +0000405 case OMPRTL__kmpc_threadprivate_cached: {
406 // Build void *__kmpc_threadprivate_cached(ident_t *loc,
407 // kmp_int32 global_tid, void *data, size_t size, void ***cache);
408 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
409 CGM.VoidPtrTy, CGM.SizeTy,
410 CGM.VoidPtrTy->getPointerTo()->getPointerTo()};
411 llvm::FunctionType *FnTy =
412 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false);
413 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached");
414 break;
415 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000416 case OMPRTL__kmpc_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000417 // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
418 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000419 llvm::Type *TypeParams[] = {
420 getIdentTyPointerTy(), CGM.Int32Ty,
421 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
422 llvm::FunctionType *FnTy =
423 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
424 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
425 break;
426 }
Alexey Bataev97720002014-11-11 04:05:39 +0000427 case OMPRTL__kmpc_threadprivate_register: {
428 // Build void __kmpc_threadprivate_register(ident_t *, void *data,
429 // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
430 // typedef void *(*kmpc_ctor)(void *);
431 auto KmpcCtorTy =
432 llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
433 /*isVarArg*/ false)->getPointerTo();
434 // typedef void *(*kmpc_cctor)(void *, void *);
435 llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
436 auto KmpcCopyCtorTy =
437 llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs,
438 /*isVarArg*/ false)->getPointerTo();
439 // typedef void (*kmpc_dtor)(void *);
440 auto KmpcDtorTy =
441 llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false)
442 ->getPointerTo();
443 llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy,
444 KmpcCopyCtorTy, KmpcDtorTy};
445 auto FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs,
446 /*isVarArg*/ false);
447 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register");
448 break;
449 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000450 case OMPRTL__kmpc_end_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000451 // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
452 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000453 llvm::Type *TypeParams[] = {
454 getIdentTyPointerTy(), CGM.Int32Ty,
455 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
456 llvm::FunctionType *FnTy =
457 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
458 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
459 break;
460 }
Alexey Bataev8f7c1b02014-12-05 04:09:23 +0000461 case OMPRTL__kmpc_cancel_barrier: {
462 // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
463 // global_tid);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000464 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
465 llvm::FunctionType *FnTy =
Alexey Bataev8f7c1b02014-12-05 04:09:23 +0000466 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
467 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier");
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000468 break;
469 }
Alexander Musmanc6388682014-12-15 07:07:06 +0000470 // Build __kmpc_for_static_init*(
471 // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
472 // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
473 // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
474 // kmp_int[32|64] incr, kmp_int[32|64] chunk);
475 case OMPRTL__kmpc_for_static_init_4: {
476 auto ITy = CGM.Int32Ty;
477 auto PtrTy = llvm::PointerType::getUnqual(ITy);
478 llvm::Type *TypeParams[] = {
479 getIdentTyPointerTy(), // loc
480 CGM.Int32Ty, // tid
481 CGM.Int32Ty, // schedtype
482 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
483 PtrTy, // p_lower
484 PtrTy, // p_upper
485 PtrTy, // p_stride
486 ITy, // incr
487 ITy // chunk
488 };
489 llvm::FunctionType *FnTy =
490 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
491 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_4");
492 break;
493 }
494 case OMPRTL__kmpc_for_static_init_4u: {
495 auto ITy = CGM.Int32Ty;
496 auto PtrTy = llvm::PointerType::getUnqual(ITy);
497 llvm::Type *TypeParams[] = {
498 getIdentTyPointerTy(), // loc
499 CGM.Int32Ty, // tid
500 CGM.Int32Ty, // schedtype
501 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
502 PtrTy, // p_lower
503 PtrTy, // p_upper
504 PtrTy, // p_stride
505 ITy, // incr
506 ITy // chunk
507 };
508 llvm::FunctionType *FnTy =
509 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
510 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_4u");
511 break;
512 }
513 case OMPRTL__kmpc_for_static_init_8: {
514 auto ITy = CGM.Int64Ty;
515 auto PtrTy = llvm::PointerType::getUnqual(ITy);
516 llvm::Type *TypeParams[] = {
517 getIdentTyPointerTy(), // loc
518 CGM.Int32Ty, // tid
519 CGM.Int32Ty, // schedtype
520 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
521 PtrTy, // p_lower
522 PtrTy, // p_upper
523 PtrTy, // p_stride
524 ITy, // incr
525 ITy // chunk
526 };
527 llvm::FunctionType *FnTy =
528 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
529 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_8");
530 break;
531 }
532 case OMPRTL__kmpc_for_static_init_8u: {
533 auto ITy = CGM.Int64Ty;
534 auto PtrTy = llvm::PointerType::getUnqual(ITy);
535 llvm::Type *TypeParams[] = {
536 getIdentTyPointerTy(), // loc
537 CGM.Int32Ty, // tid
538 CGM.Int32Ty, // schedtype
539 llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
540 PtrTy, // p_lower
541 PtrTy, // p_upper
542 PtrTy, // p_stride
543 ITy, // incr
544 ITy // chunk
545 };
546 llvm::FunctionType *FnTy =
547 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
548 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_8u");
549 break;
550 }
551 case OMPRTL__kmpc_for_static_fini: {
552 // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
553 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
554 llvm::FunctionType *FnTy =
555 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
556 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini");
557 break;
558 }
Alexey Bataevb2059782014-10-13 08:23:51 +0000559 case OMPRTL__kmpc_push_num_threads: {
560 // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
561 // kmp_int32 num_threads)
562 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
563 CGM.Int32Ty};
564 llvm::FunctionType *FnTy =
565 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
566 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads");
567 break;
568 }
Alexey Bataevd74d0602014-10-13 06:02:40 +0000569 case OMPRTL__kmpc_serialized_parallel: {
570 // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
571 // global_tid);
572 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
573 llvm::FunctionType *FnTy =
574 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
575 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
576 break;
577 }
578 case OMPRTL__kmpc_end_serialized_parallel: {
579 // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
580 // global_tid);
581 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
582 llvm::FunctionType *FnTy =
583 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
584 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
585 break;
586 }
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000587 case OMPRTL__kmpc_flush: {
Alexey Bataevd76df6d2015-02-24 12:55:09 +0000588 // Build void __kmpc_flush(ident_t *loc);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000589 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
590 llvm::FunctionType *FnTy =
Alexey Bataevd76df6d2015-02-24 12:55:09 +0000591 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
Alexey Bataevcc37cc12014-11-20 04:34:54 +0000592 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush");
593 break;
594 }
Alexey Bataev8d690652014-12-04 07:23:53 +0000595 case OMPRTL__kmpc_master: {
596 // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid);
597 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
598 llvm::FunctionType *FnTy =
599 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
600 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master");
601 break;
602 }
603 case OMPRTL__kmpc_end_master: {
604 // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid);
605 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
606 llvm::FunctionType *FnTy =
607 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
608 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master");
609 break;
610 }
Alexey Bataev9f797f32015-02-05 05:57:51 +0000611 case OMPRTL__kmpc_omp_taskyield: {
612 // Build kmp_int32 __kmpc_omp_taskyield(ident_t *, kmp_int32 global_tid,
613 // int end_part);
614 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.IntTy};
615 llvm::FunctionType *FnTy =
616 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
617 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_taskyield");
618 break;
619 }
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000620 case OMPRTL__kmpc_single: {
621 // Build kmp_int32 __kmpc_single(ident_t *loc, kmp_int32 global_tid);
622 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
623 llvm::FunctionType *FnTy =
624 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
625 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_single");
626 break;
627 }
628 case OMPRTL__kmpc_end_single: {
629 // Build void __kmpc_end_single(ident_t *loc, kmp_int32 global_tid);
630 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
631 llvm::FunctionType *FnTy =
632 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
633 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_single");
634 break;
635 }
Alexey Bataev62b63b12015-03-10 07:28:44 +0000636 case OMPRTL__kmpc_omp_task_alloc: {
637 // Build kmp_task_t *__kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
638 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
639 // kmp_routine_entry_t *task_entry);
640 assert(KmpRoutineEntryPtrTy != nullptr &&
641 "Type kmp_routine_entry_t must be created.");
642 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, CGM.Int32Ty,
643 CGM.SizeTy, CGM.SizeTy, KmpRoutineEntryPtrTy};
644 // Return void * and then cast to particular kmp_task_t type.
645 llvm::FunctionType *FnTy =
646 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
647 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task_alloc");
648 break;
649 }
650 case OMPRTL__kmpc_omp_task: {
651 // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
652 // *new_task);
653 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
654 CGM.VoidPtrTy};
655 llvm::FunctionType *FnTy =
656 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
657 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_omp_task");
658 break;
659 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000660 }
661 return RTLFn;
662}
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000663
Alexey Bataev97720002014-11-11 04:05:39 +0000664llvm::Constant *
665CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
666 // Lookup the entry, lazily creating it if necessary.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000667 return getOrCreateInternalVariable(CGM.Int8PtrPtrTy,
Alexey Bataev97720002014-11-11 04:05:39 +0000668 Twine(CGM.getMangledName(VD)) + ".cache.");
669}
670
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000671llvm::Value *CGOpenMPRuntime::getAddrOfThreadPrivate(CodeGenFunction &CGF,
672 const VarDecl *VD,
673 llvm::Value *VDAddr,
674 SourceLocation Loc) {
Alexey Bataev97720002014-11-11 04:05:39 +0000675 auto VarTy = VDAddr->getType()->getPointerElementType();
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000676 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataev97720002014-11-11 04:05:39 +0000677 CGF.Builder.CreatePointerCast(VDAddr, CGM.Int8PtrTy),
678 CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)),
679 getOrCreateThreadPrivateCache(VD)};
680 return CGF.EmitRuntimeCall(
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000681 createRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args);
Alexey Bataev97720002014-11-11 04:05:39 +0000682}
683
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000684void CGOpenMPRuntime::emitThreadPrivateVarInit(
Alexey Bataev97720002014-11-11 04:05:39 +0000685 CodeGenFunction &CGF, llvm::Value *VDAddr, llvm::Value *Ctor,
686 llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) {
687 // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime
688 // library.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000689 auto OMPLoc = emitUpdateLocation(CGF, Loc);
690 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
Alexey Bataev97720002014-11-11 04:05:39 +0000691 OMPLoc);
692 // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor)
693 // to register constructor/destructor for variable.
694 llvm::Value *Args[] = {OMPLoc,
695 CGF.Builder.CreatePointerCast(VDAddr, CGM.VoidPtrTy),
696 Ctor, CopyCtor, Dtor};
Alexey Bataev1e4b7132014-12-03 12:11:24 +0000697 CGF.EmitRuntimeCall(
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000698 createRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args);
Alexey Bataev97720002014-11-11 04:05:39 +0000699}
700
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000701llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
Alexey Bataev97720002014-11-11 04:05:39 +0000702 const VarDecl *VD, llvm::Value *VDAddr, SourceLocation Loc,
703 bool PerformInit, CodeGenFunction *CGF) {
704 VD = VD->getDefinition(CGM.getContext());
705 if (VD && ThreadPrivateWithDefinition.count(VD) == 0) {
706 ThreadPrivateWithDefinition.insert(VD);
707 QualType ASTTy = VD->getType();
708
709 llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr;
710 auto Init = VD->getAnyInitializer();
711 if (CGM.getLangOpts().CPlusPlus && PerformInit) {
712 // Generate function that re-emits the declaration's initializer into the
713 // threadprivate copy of the variable VD
714 CodeGenFunction CtorCGF(CGM);
715 FunctionArgList Args;
716 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
717 /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
718 Args.push_back(&Dst);
719
720 auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
721 CGM.getContext().VoidPtrTy, Args, FunctionType::ExtInfo(),
722 /*isVariadic=*/false);
723 auto FTy = CGM.getTypes().GetFunctionType(FI);
724 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
725 FTy, ".__kmpc_global_ctor_.", Loc);
726 CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
727 Args, SourceLocation());
728 auto ArgVal = CtorCGF.EmitLoadOfScalar(
729 CtorCGF.GetAddrOfLocalVar(&Dst),
730 /*Volatile=*/false, CGM.PointerAlignInBytes,
731 CGM.getContext().VoidPtrTy, Dst.getLocation());
732 auto Arg = CtorCGF.Builder.CreatePointerCast(
733 ArgVal,
734 CtorCGF.ConvertTypeForMem(CGM.getContext().getPointerType(ASTTy)));
735 CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
736 /*IsInitializer=*/true);
737 ArgVal = CtorCGF.EmitLoadOfScalar(
738 CtorCGF.GetAddrOfLocalVar(&Dst),
739 /*Volatile=*/false, CGM.PointerAlignInBytes,
740 CGM.getContext().VoidPtrTy, Dst.getLocation());
741 CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue);
742 CtorCGF.FinishFunction();
743 Ctor = Fn;
744 }
745 if (VD->getType().isDestructedType() != QualType::DK_none) {
746 // Generate function that emits destructor call for the threadprivate copy
747 // of the variable VD
748 CodeGenFunction DtorCGF(CGM);
749 FunctionArgList Args;
750 ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
751 /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
752 Args.push_back(&Dst);
753
754 auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
755 CGM.getContext().VoidTy, Args, FunctionType::ExtInfo(),
756 /*isVariadic=*/false);
757 auto FTy = CGM.getTypes().GetFunctionType(FI);
758 auto Fn = CGM.CreateGlobalInitOrDestructFunction(
759 FTy, ".__kmpc_global_dtor_.", Loc);
760 DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
761 SourceLocation());
762 auto ArgVal = DtorCGF.EmitLoadOfScalar(
763 DtorCGF.GetAddrOfLocalVar(&Dst),
764 /*Volatile=*/false, CGM.PointerAlignInBytes,
765 CGM.getContext().VoidPtrTy, Dst.getLocation());
766 DtorCGF.emitDestroy(ArgVal, ASTTy,
767 DtorCGF.getDestroyer(ASTTy.isDestructedType()),
768 DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
769 DtorCGF.FinishFunction();
770 Dtor = Fn;
771 }
772 // Do not emit init function if it is not required.
773 if (!Ctor && !Dtor)
774 return nullptr;
775
776 llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
777 auto CopyCtorTy =
778 llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs,
779 /*isVarArg=*/false)->getPointerTo();
780 // Copying constructor for the threadprivate variable.
781 // Must be NULL - reserved by runtime, but currently it requires that this
782 // parameter is always NULL. Otherwise it fires assertion.
783 CopyCtor = llvm::Constant::getNullValue(CopyCtorTy);
784 if (Ctor == nullptr) {
785 auto CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
786 /*isVarArg=*/false)->getPointerTo();
787 Ctor = llvm::Constant::getNullValue(CtorTy);
788 }
789 if (Dtor == nullptr) {
790 auto DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy,
791 /*isVarArg=*/false)->getPointerTo();
792 Dtor = llvm::Constant::getNullValue(DtorTy);
793 }
794 if (!CGF) {
795 auto InitFunctionTy =
796 llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
797 auto InitFunction = CGM.CreateGlobalInitOrDestructFunction(
798 InitFunctionTy, ".__omp_threadprivate_init_.");
799 CodeGenFunction InitCGF(CGM);
800 FunctionArgList ArgList;
801 InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
802 CGM.getTypes().arrangeNullaryFunction(), ArgList,
803 Loc);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000804 emitThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000805 InitCGF.FinishFunction();
806 return InitFunction;
807 }
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000808 emitThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
Alexey Bataev97720002014-11-11 04:05:39 +0000809 }
810 return nullptr;
811}
812
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000813void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
814 llvm::Value *OutlinedFn,
815 llvm::Value *CapturedStruct) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000816 // Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/)
817 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000818 emitUpdateLocation(CGF, Loc),
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000819 CGF.Builder.getInt32(1), // Number of arguments after 'microtask' argument
820 // (there is only one additional argument - 'context')
821 CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy()),
822 CGF.EmitCastToVoidPtr(CapturedStruct)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000823 auto RTLFn = createRuntimeFunction(OMPRTL__kmpc_fork_call);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000824 CGF.EmitRuntimeCall(RTLFn, Args);
825}
826
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000827void CGOpenMPRuntime::emitSerialCall(CodeGenFunction &CGF, SourceLocation Loc,
828 llvm::Value *OutlinedFn,
829 llvm::Value *CapturedStruct) {
830 auto ThreadID = getThreadID(CGF, Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000831 // Build calls:
832 // __kmpc_serialized_parallel(&Loc, GTid);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000833 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), ThreadID};
834 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_serialized_parallel),
835 Args);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000836
837 // OutlinedFn(&GTid, &zero, CapturedStruct);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000838 auto ThreadIDAddr = emitThreadIDAddress(CGF, Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000839 auto Int32Ty =
840 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
841 auto ZeroAddr = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".zero.addr");
842 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
843 llvm::Value *OutlinedFnArgs[] = {ThreadIDAddr, ZeroAddr, CapturedStruct};
844 CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
845
846 // __kmpc_end_serialized_parallel(&Loc, GTid);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000847 llvm::Value *EndArgs[] = {emitUpdateLocation(CGF, Loc), ThreadID};
848 CGF.EmitRuntimeCall(
849 createRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel), EndArgs);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000850}
851
NAKAMURA Takumi59c74b222014-10-27 08:08:18 +0000852// If we're inside an (outlined) parallel region, use the region info's
Alexey Bataevd74d0602014-10-13 06:02:40 +0000853// thread-ID variable (it is passed in a first argument of the outlined function
854// as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
855// regular serial code region, get thread ID by calling kmp_int32
856// kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
857// return the address of that temp.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000858llvm::Value *CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF,
Alexey Bataevd74d0602014-10-13 06:02:40 +0000859 SourceLocation Loc) {
860 if (auto OMPRegionInfo =
861 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000862 if (OMPRegionInfo->getThreadIDVariable())
Alexey Bataev62b63b12015-03-10 07:28:44 +0000863 return OMPRegionInfo->getThreadIDVariableLValue(CGF).getAddress();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +0000864
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000865 auto ThreadID = getThreadID(CGF, Loc);
Alexey Bataevd74d0602014-10-13 06:02:40 +0000866 auto Int32Ty =
867 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
868 auto ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
869 CGF.EmitStoreOfScalar(ThreadID,
870 CGF.MakeNaturalAlignAddrLValue(ThreadIDTemp, Int32Ty));
871
872 return ThreadIDTemp;
873}
874
Alexey Bataev97720002014-11-11 04:05:39 +0000875llvm::Constant *
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000876CGOpenMPRuntime::getOrCreateInternalVariable(llvm::Type *Ty,
Alexey Bataev97720002014-11-11 04:05:39 +0000877 const llvm::Twine &Name) {
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000878 SmallString<256> Buffer;
879 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev97720002014-11-11 04:05:39 +0000880 Out << Name;
881 auto RuntimeName = Out.str();
David Blaikie13156b62014-11-19 03:06:06 +0000882 auto &Elem = *InternalVars.insert(std::make_pair(RuntimeName, nullptr)).first;
883 if (Elem.second) {
884 assert(Elem.second->getType()->getPointerElementType() == Ty &&
Alexey Bataev97720002014-11-11 04:05:39 +0000885 "OMP internal variable has different type than requested");
David Blaikie13156b62014-11-19 03:06:06 +0000886 return &*Elem.second;
Alexey Bataev97720002014-11-11 04:05:39 +0000887 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000888
David Blaikie13156b62014-11-19 03:06:06 +0000889 return Elem.second = new llvm::GlobalVariable(
890 CGM.getModule(), Ty, /*IsConstant*/ false,
891 llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
892 Elem.first());
Alexey Bataev97720002014-11-11 04:05:39 +0000893}
894
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000895llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
Alexey Bataev97720002014-11-11 04:05:39 +0000896 llvm::Twine Name(".gomp_critical_user_", CriticalName);
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000897 return getOrCreateInternalVariable(KmpCriticalNameTy, Name.concat(".var"));
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000898}
899
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000900void CGOpenMPRuntime::emitCriticalRegion(
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000901 CodeGenFunction &CGF, StringRef CriticalName,
902 const std::function<void()> &CriticalOpGen, SourceLocation Loc) {
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000903 auto RegionLock = getCriticalRegionLock(CriticalName);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000904 // __kmpc_critical(ident_t *, gtid, Lock);
905 // CriticalOpGen();
906 // __kmpc_end_critical(ident_t *, gtid, Lock);
907 // Prepare arguments and build a call to __kmpc_critical
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000908 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
909 RegionLock};
910 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_critical), Args);
Alexey Bataev75ddfab2014-12-01 11:32:38 +0000911 CriticalOpGen();
912 // Build a call to __kmpc_end_critical
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000913 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_end_critical), Args);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000914}
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000915
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000916static void emitIfStmt(CodeGenFunction &CGF, llvm::Value *IfCond,
917 const std::function<void()> &BodyOpGen) {
Alexey Bataev8d690652014-12-04 07:23:53 +0000918 llvm::Value *CallBool = CGF.EmitScalarConversion(
919 IfCond,
920 CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true),
921 CGF.getContext().BoolTy);
922
923 auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
924 auto *ContBlock = CGF.createBasicBlock("omp_if.end");
925 // Generate the branch (If-stmt)
926 CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
927 CGF.EmitBlock(ThenBlock);
928 BodyOpGen();
929 // Emit the rest of bblocks/branches
930 CGF.EmitBranch(ContBlock);
931 CGF.EmitBlock(ContBlock, true);
932}
933
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000934void CGOpenMPRuntime::emitMasterRegion(CodeGenFunction &CGF,
935 const std::function<void()> &MasterOpGen,
936 SourceLocation Loc) {
Alexey Bataev8d690652014-12-04 07:23:53 +0000937 // if(__kmpc_master(ident_t *, gtid)) {
938 // MasterOpGen();
939 // __kmpc_end_master(ident_t *, gtid);
940 // }
941 // Prepare arguments and build a call to __kmpc_master
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000942 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
943 auto *IsMaster =
944 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_master), Args);
945 emitIfStmt(CGF, IsMaster, [&]() -> void {
Alexey Bataev8d690652014-12-04 07:23:53 +0000946 MasterOpGen();
947 // Build a call to __kmpc_end_master.
948 // OpenMP [1.2.2 OpenMP Language Terminology]
949 // For C/C++, an executable statement, possibly compound, with a single
950 // entry at the top and a single exit at the bottom, or an OpenMP construct.
951 // * Access to the structured block must not be the result of a branch.
952 // * The point of exit cannot be a branch out of the structured block.
953 // * The point of entry must not be a call to setjmp().
954 // * longjmp() and throw() must not violate the entry/exit criteria.
955 // * An expression statement, iteration statement, selection statement, or
956 // try block is considered to be a structured block if the corresponding
957 // compound statement obtained by enclosing it in { and } would be a
958 // structured block.
959 // It is analyzed in Sema, so we can just call __kmpc_end_master() on
960 // fallthrough rather than pushing a normal cleanup for it.
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000961 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_end_master), Args);
Alexey Bataev8d690652014-12-04 07:23:53 +0000962 });
963}
964
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000965void CGOpenMPRuntime::emitTaskyieldCall(CodeGenFunction &CGF,
966 SourceLocation Loc) {
Alexey Bataev9f797f32015-02-05 05:57:51 +0000967 // Build call __kmpc_omp_taskyield(loc, thread_id, 0);
968 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000969 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataev9f797f32015-02-05 05:57:51 +0000970 llvm::ConstantInt::get(CGM.IntTy, /*V=*/0, /*isSigned=*/true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000971 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_taskyield), Args);
Alexey Bataev9f797f32015-02-05 05:57:51 +0000972}
973
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000974void CGOpenMPRuntime::emitSingleRegion(CodeGenFunction &CGF,
975 const std::function<void()> &SingleOpGen,
976 SourceLocation Loc) {
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000977 // if(__kmpc_single(ident_t *, gtid)) {
978 // SingleOpGen();
979 // __kmpc_end_single(ident_t *, gtid);
980 // }
981 // Prepare arguments and build a call to __kmpc_single
Alexey Bataev3eff5f42015-02-25 08:32:46 +0000982 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc)};
983 auto *IsSingle =
984 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_single), Args);
985 emitIfStmt(CGF, IsSingle, [&]() -> void {
Alexey Bataev6956e2e2015-02-05 06:35:41 +0000986 SingleOpGen();
987 // Build a call to __kmpc_end_single.
988 // OpenMP [1.2.2 OpenMP Language Terminology]
989 // For C/C++, an executable statement, possibly compound, with a single
990 // entry at the top and a single exit at the bottom, or an OpenMP construct.
991 // * Access to the structured block must not be the result of a branch.
992 // * The point of exit cannot be a branch out of the structured block.
993 // * The point of entry must not be a call to setjmp().
994 // * longjmp() and throw() must not violate the entry/exit criteria.
995 // * An expression statement, iteration statement, selection statement, or
996 // try block is considered to be a structured block if the corresponding
997 // compound statement obtained by enclosing it in { and } would be a
998 // structured block.
999 // It is analyzed in Sema, so we can just call __kmpc_end_single() on
1000 // fallthrough rather than pushing a normal cleanup for it.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001001 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_end_single), Args);
Alexey Bataev6956e2e2015-02-05 06:35:41 +00001002 });
1003}
1004
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001005void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
1006 bool IsExplicit) {
Alexey Bataev8f7c1b02014-12-05 04:09:23 +00001007 // Build call __kmpc_cancel_barrier(loc, thread_id);
1008 auto Flags = static_cast<OpenMPLocationFlags>(
1009 OMP_IDENT_KMPC |
1010 (IsExplicit ? OMP_IDENT_BARRIER_EXPL : OMP_IDENT_BARRIER_IMPL));
1011 // Build call __kmpc_cancel_barrier(loc, thread_id);
1012 // Replace __kmpc_barrier() function by __kmpc_cancel_barrier() because this
1013 // one provides the same functionality and adds initial support for
1014 // cancellation constructs introduced in OpenMP 4.0. __kmpc_cancel_barrier()
1015 // is provided default by the runtime library so it safe to make such
1016 // replacement.
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001017 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, Flags),
1018 getThreadID(CGF, Loc)};
1019 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_cancel_barrier), Args);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00001020}
1021
Alexander Musmanc6388682014-12-15 07:07:06 +00001022/// \brief Schedule types for 'omp for' loops (these enumerators are taken from
1023/// the enum sched_type in kmp.h).
1024enum OpenMPSchedType {
1025 /// \brief Lower bound for default (unordered) versions.
1026 OMP_sch_lower = 32,
1027 OMP_sch_static_chunked = 33,
1028 OMP_sch_static = 34,
1029 OMP_sch_dynamic_chunked = 35,
1030 OMP_sch_guided_chunked = 36,
1031 OMP_sch_runtime = 37,
1032 OMP_sch_auto = 38,
1033 /// \brief Lower bound for 'ordered' versions.
1034 OMP_ord_lower = 64,
1035 /// \brief Lower bound for 'nomerge' versions.
1036 OMP_nm_lower = 160,
1037};
1038
1039/// \brief Map the OpenMP loop schedule to the runtime enumeration.
1040static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind,
1041 bool Chunked) {
1042 switch (ScheduleKind) {
1043 case OMPC_SCHEDULE_static:
1044 return Chunked ? OMP_sch_static_chunked : OMP_sch_static;
1045 case OMPC_SCHEDULE_dynamic:
1046 return OMP_sch_dynamic_chunked;
1047 case OMPC_SCHEDULE_guided:
1048 return OMP_sch_guided_chunked;
1049 case OMPC_SCHEDULE_auto:
1050 return OMP_sch_auto;
1051 case OMPC_SCHEDULE_runtime:
1052 return OMP_sch_runtime;
1053 case OMPC_SCHEDULE_unknown:
1054 assert(!Chunked && "chunk was specified but schedule kind not known");
1055 return OMP_sch_static;
1056 }
1057 llvm_unreachable("Unexpected runtime schedule");
1058}
1059
1060bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
1061 bool Chunked) const {
1062 auto Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
1063 return Schedule == OMP_sch_static;
1064}
1065
Alexander Musmandf7a8e22015-01-22 08:49:35 +00001066bool CGOpenMPRuntime::isDynamic(OpenMPScheduleClauseKind ScheduleKind) const {
1067 auto Schedule = getRuntimeSchedule(ScheduleKind, /* Chunked */ false);
1068 assert(Schedule != OMP_sch_static_chunked && "cannot be chunked here");
1069 return Schedule != OMP_sch_static;
1070}
1071
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001072void CGOpenMPRuntime::emitForInit(CodeGenFunction &CGF, SourceLocation Loc,
1073 OpenMPScheduleClauseKind ScheduleKind,
1074 unsigned IVSize, bool IVSigned,
1075 llvm::Value *IL, llvm::Value *LB,
1076 llvm::Value *UB, llvm::Value *ST,
1077 llvm::Value *Chunk) {
Alexander Musmanc6388682014-12-15 07:07:06 +00001078 OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunk != nullptr);
1079 // Call __kmpc_for_static_init(
1080 // ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
1081 // kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
1082 // kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
1083 // kmp_int[32|64] incr, kmp_int[32|64] chunk);
1084 // TODO: Implement dynamic schedule.
1085
1086 // If the Chunk was not specified in the clause - use default value 1.
1087 if (Chunk == nullptr)
1088 Chunk = CGF.Builder.getIntN(IVSize, /*C*/ 1);
1089
1090 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001091 emitUpdateLocation(CGF, Loc, OMP_IDENT_KMPC), getThreadID(CGF, Loc),
Alexander Musmanc6388682014-12-15 07:07:06 +00001092 CGF.Builder.getInt32(Schedule), // Schedule type
1093 IL, // &isLastIter
1094 LB, // &LB
1095 UB, // &UB
1096 ST, // &Stride
1097 CGF.Builder.getIntN(IVSize, 1), // Incr
1098 Chunk // Chunk
1099 };
1100 assert((IVSize == 32 || IVSize == 64) &&
1101 "Index size is not compatible with the omp runtime");
1102 auto F = IVSize == 32 ? (IVSigned ? OMPRTL__kmpc_for_static_init_4
1103 : OMPRTL__kmpc_for_static_init_4u)
1104 : (IVSigned ? OMPRTL__kmpc_for_static_init_8
1105 : OMPRTL__kmpc_for_static_init_8u);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001106 CGF.EmitRuntimeCall(createRuntimeFunction(F), Args);
Alexander Musmanc6388682014-12-15 07:07:06 +00001107}
1108
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001109void CGOpenMPRuntime::emitForFinish(CodeGenFunction &CGF, SourceLocation Loc,
1110 OpenMPScheduleClauseKind ScheduleKind) {
Alexander Musmanc6388682014-12-15 07:07:06 +00001111 assert((ScheduleKind == OMPC_SCHEDULE_static ||
1112 ScheduleKind == OMPC_SCHEDULE_unknown) &&
1113 "Non-static schedule kinds are not yet implemented");
1114 // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid);
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001115 llvm::Value *Args[] = {emitUpdateLocation(CGF, Loc, OMP_IDENT_KMPC),
1116 getThreadID(CGF, Loc)};
1117 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_for_static_fini),
1118 Args);
Alexander Musmanc6388682014-12-15 07:07:06 +00001119}
1120
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001121void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
1122 llvm::Value *NumThreads,
1123 SourceLocation Loc) {
Alexey Bataevb2059782014-10-13 08:23:51 +00001124 // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
1125 llvm::Value *Args[] = {
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001126 emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
Alexey Bataevb2059782014-10-13 08:23:51 +00001127 CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001128 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_push_num_threads),
1129 Args);
Alexey Bataevb2059782014-10-13 08:23:51 +00001130}
1131
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001132void CGOpenMPRuntime::emitFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
1133 SourceLocation Loc) {
Alexey Bataevd76df6d2015-02-24 12:55:09 +00001134 // Build call void __kmpc_flush(ident_t *loc)
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001135 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_flush),
1136 emitUpdateLocation(CGF, Loc));
Alexey Bataevcc37cc12014-11-20 04:34:54 +00001137}
Alexey Bataev3eff5f42015-02-25 08:32:46 +00001138
Alexey Bataev62b63b12015-03-10 07:28:44 +00001139namespace {
1140/// \brief Indexes of fields for type kmp_task_t.
1141enum KmpTaskTFields {
1142 /// \brief List of shared variables.
1143 KmpTaskTShareds,
1144 /// \brief Task routine.
1145 KmpTaskTRoutine,
1146 /// \brief Partition id for the untied tasks.
1147 KmpTaskTPartId,
1148 /// \brief Function with call of destructors for private variables.
1149 KmpTaskTDestructors,
1150};
1151} // namespace
1152
1153void CGOpenMPRuntime::emitKmpRoutineEntryT(QualType KmpInt32Ty) {
1154 if (!KmpRoutineEntryPtrTy) {
1155 // Build typedef kmp_int32 (* kmp_routine_entry_t)(kmp_int32, void *); type.
1156 auto &C = CGM.getContext();
1157 QualType KmpRoutineEntryTyArgs[] = {KmpInt32Ty, C.VoidPtrTy};
1158 FunctionProtoType::ExtProtoInfo EPI;
1159 KmpRoutineEntryPtrQTy = C.getPointerType(
1160 C.getFunctionType(KmpInt32Ty, KmpRoutineEntryTyArgs, EPI));
1161 KmpRoutineEntryPtrTy = CGM.getTypes().ConvertType(KmpRoutineEntryPtrQTy);
1162 }
1163}
1164
1165static void addFieldToRecordDecl(ASTContext &C, DeclContext *DC,
1166 QualType FieldTy) {
1167 auto *Field = FieldDecl::Create(
1168 C, DC, SourceLocation(), SourceLocation(), /*Id=*/nullptr, FieldTy,
1169 C.getTrivialTypeSourceInfo(FieldTy, SourceLocation()),
1170 /*BW=*/nullptr, /*Mutable=*/false, /*InitStyle=*/ICIS_NoInit);
1171 Field->setAccess(AS_public);
1172 DC->addDecl(Field);
1173}
1174
1175static QualType createKmpTaskTRecordDecl(CodeGenModule &CGM,
1176 QualType KmpInt32Ty,
1177 QualType KmpRoutineEntryPointerQTy) {
1178 auto &C = CGM.getContext();
1179 // Build struct kmp_task_t {
1180 // void * shareds;
1181 // kmp_routine_entry_t routine;
1182 // kmp_int32 part_id;
1183 // kmp_routine_entry_t destructors;
1184 // /* private vars */
1185 // };
1186 auto *RD = C.buildImplicitRecord("kmp_task_t");
1187 RD->startDefinition();
1188 addFieldToRecordDecl(C, RD, C.VoidPtrTy);
1189 addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy);
1190 addFieldToRecordDecl(C, RD, KmpInt32Ty);
1191 addFieldToRecordDecl(C, RD, KmpRoutineEntryPointerQTy);
1192 // TODO: add private fields.
1193 RD->completeDefinition();
1194 return C.getRecordType(RD);
1195}
1196
1197/// \brief Emit a proxy function which accepts kmp_task_t as the second
1198/// argument.
1199/// \code
1200/// kmp_int32 .omp_task_entry.(kmp_int32 gtid, kmp_task_t *tt) {
1201/// TaskFunction(gtid, tt->part_id, tt->shareds);
1202/// return 0;
1203/// }
1204/// \endcode
1205static llvm::Value *
1206emitProxyTaskFunction(CodeGenModule &CGM, SourceLocation Loc,
1207 QualType KmpInt32Ty, QualType KmpTaskTPtrQTy,
1208 QualType SharedsPtrTy, llvm::Value *TaskFunction) {
1209 auto &C = CGM.getContext();
1210 FunctionArgList Args;
1211 ImplicitParamDecl GtidArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, KmpInt32Ty);
1212 ImplicitParamDecl TaskTypeArg(C, /*DC=*/nullptr, Loc,
1213 /*Id=*/nullptr, KmpTaskTPtrQTy);
1214 Args.push_back(&GtidArg);
1215 Args.push_back(&TaskTypeArg);
1216 FunctionType::ExtInfo Info;
1217 auto &TaskEntryFnInfo =
1218 CGM.getTypes().arrangeFreeFunctionDeclaration(KmpInt32Ty, Args, Info,
1219 /*isVariadic=*/false);
1220 auto *TaskEntryTy = CGM.getTypes().GetFunctionType(TaskEntryFnInfo);
1221 auto *TaskEntry =
1222 llvm::Function::Create(TaskEntryTy, llvm::GlobalValue::InternalLinkage,
1223 ".omp_task_entry.", &CGM.getModule());
1224 CGM.SetLLVMFunctionAttributes(/*D=*/nullptr, TaskEntryFnInfo, TaskEntry);
1225 CodeGenFunction CGF(CGM);
1226 CGF.disableDebugInfo();
1227 CGF.StartFunction(GlobalDecl(), KmpInt32Ty, TaskEntry, TaskEntryFnInfo, Args);
1228
1229 // TaskFunction(gtid, tt->part_id, tt->shareds);
1230 auto *GtidParam = CGF.EmitLoadOfScalar(
1231 CGF.GetAddrOfLocalVar(&GtidArg), /*Volatile=*/false,
1232 C.getTypeAlignInChars(KmpInt32Ty).getQuantity(), KmpInt32Ty, Loc);
1233 auto TaskTypeArgAddr = CGF.EmitLoadOfScalar(
1234 CGF.GetAddrOfLocalVar(&TaskTypeArg), /*Volatile=*/false,
1235 CGM.PointerAlignInBytes, KmpTaskTPtrQTy, Loc);
1236 auto *PartidPtr = CGF.Builder.CreateStructGEP(TaskTypeArgAddr,
1237 /*Idx=*/KmpTaskTPartId);
1238 auto *PartidParam = CGF.EmitLoadOfScalar(
1239 PartidPtr, /*Volatile=*/false,
1240 C.getTypeAlignInChars(KmpInt32Ty).getQuantity(), KmpInt32Ty, Loc);
1241 auto *SharedsPtr = CGF.Builder.CreateStructGEP(TaskTypeArgAddr,
1242 /*Idx=*/KmpTaskTShareds);
1243 auto *SharedsParam =
1244 CGF.EmitLoadOfScalar(SharedsPtr, /*Volatile=*/false,
1245 CGM.PointerAlignInBytes, C.VoidPtrTy, Loc);
1246 llvm::Value *CallArgs[] = {
1247 GtidParam, PartidParam,
1248 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
1249 SharedsParam, CGF.ConvertTypeForMem(SharedsPtrTy))};
1250 CGF.EmitCallOrInvoke(TaskFunction, CallArgs);
1251 CGF.EmitStoreThroughLValue(
1252 RValue::get(CGF.Builder.getInt32(/*C=*/0)),
1253 CGF.MakeNaturalAlignAddrLValue(CGF.ReturnValue, KmpInt32Ty));
1254 CGF.FinishFunction();
1255 return TaskEntry;
1256}
1257
1258void CGOpenMPRuntime::emitTaskCall(
1259 CodeGenFunction &CGF, SourceLocation Loc, bool Tied,
1260 llvm::PointerIntPair<llvm::Value *, 1, bool> Final,
1261 llvm::Value *TaskFunction, QualType SharedsTy, llvm::Value *Shareds) {
1262 auto &C = CGM.getContext();
1263 auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1264 // Build type kmp_routine_entry_t (if not built yet).
1265 emitKmpRoutineEntryT(KmpInt32Ty);
1266 // Build particular struct kmp_task_t for the given task.
1267 auto KmpTaskQTy =
1268 createKmpTaskTRecordDecl(CGM, KmpInt32Ty, KmpRoutineEntryPtrQTy);
1269 QualType KmpTaskTPtrQTy = C.getPointerType(KmpTaskQTy);
1270 auto KmpTaskTPtrTy = CGF.ConvertType(KmpTaskQTy)->getPointerTo();
1271 auto KmpTaskTySize = CGM.getSize(C.getTypeSizeInChars(KmpTaskQTy));
1272 QualType SharedsPtrTy = C.getPointerType(SharedsTy);
1273
1274 // Build a proxy function kmp_int32 .omp_task_entry.(kmp_int32 gtid,
1275 // kmp_task_t *tt);
1276 auto *TaskEntry = emitProxyTaskFunction(CGM, Loc, KmpInt32Ty, KmpTaskTPtrQTy,
1277 SharedsPtrTy, TaskFunction);
1278
1279 // Build call kmp_task_t * __kmpc_omp_task_alloc(ident_t *, kmp_int32 gtid,
1280 // kmp_int32 flags, size_t sizeof_kmp_task_t, size_t sizeof_shareds,
1281 // kmp_routine_entry_t *task_entry);
1282 // Task flags. Format is taken from
1283 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h,
1284 // description of kmp_tasking_flags struct.
1285 const unsigned TiedFlag = 0x1;
1286 const unsigned FinalFlag = 0x2;
1287 unsigned Flags = Tied ? TiedFlag : 0;
1288 auto *TaskFlags =
1289 Final.getPointer()
1290 ? CGF.Builder.CreateSelect(Final.getPointer(),
1291 CGF.Builder.getInt32(FinalFlag),
1292 CGF.Builder.getInt32(/*C=*/0))
1293 : CGF.Builder.getInt32(Final.getInt() ? FinalFlag : 0);
1294 TaskFlags = CGF.Builder.CreateOr(TaskFlags, CGF.Builder.getInt32(Flags));
1295 auto SharedsSize = C.getTypeSizeInChars(SharedsTy);
1296 llvm::Value *AllocArgs[] = {emitUpdateLocation(CGF, Loc),
1297 getThreadID(CGF, Loc), TaskFlags, KmpTaskTySize,
1298 CGM.getSize(SharedsSize),
1299 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
1300 TaskEntry, KmpRoutineEntryPtrTy)};
1301 auto *NewTask = CGF.EmitRuntimeCall(
1302 createRuntimeFunction(OMPRTL__kmpc_omp_task_alloc), AllocArgs);
1303 auto *NewTaskNewTaskTTy =
1304 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(NewTask, KmpTaskTPtrTy);
1305 // Fill the data in the resulting kmp_task_t record.
1306 // Copy shareds if there are any.
1307 if (!SharedsTy->getAsStructureType()->getDecl()->field_empty())
1308 CGF.EmitAggregateCopy(
1309 CGF.EmitLoadOfScalar(
1310 CGF.Builder.CreateStructGEP(NewTaskNewTaskTTy,
1311 /*Idx=*/KmpTaskTShareds),
1312 /*Volatile=*/false, CGM.PointerAlignInBytes, SharedsPtrTy, Loc),
1313 Shareds, SharedsTy);
1314 // TODO: generate function with destructors for privates.
1315 // Provide pointer to function with destructors for privates.
1316 CGF.Builder.CreateAlignedStore(
1317 llvm::ConstantPointerNull::get(
1318 cast<llvm::PointerType>(KmpRoutineEntryPtrTy)),
1319 CGF.Builder.CreateStructGEP(NewTaskNewTaskTTy,
1320 /*Idx=*/KmpTaskTDestructors),
1321 CGM.PointerAlignInBytes);
1322
1323 // NOTE: routine and part_id fields are intialized by __kmpc_omp_task_alloc()
1324 // libcall.
1325 // Build kmp_int32 __kmpc_omp_task(ident_t *, kmp_int32 gtid, kmp_task_t
1326 // *new_task);
1327 llvm::Value *TaskArgs[] = {emitUpdateLocation(CGF, Loc),
1328 getThreadID(CGF, Loc), NewTask};
1329 // TODO: add check for untied tasks.
1330 CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_omp_task), TaskArgs);
1331}
1332
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001333InlinedOpenMPRegionRAII::InlinedOpenMPRegionRAII(
1334 CodeGenFunction &CGF, const OMPExecutableDirective &D)
1335 : CGF(CGF) {
1336 CGF.CapturedStmtInfo = new CGOpenMPInlinedRegionInfo(D, CGF.CapturedStmtInfo);
Alexey Bataev36bf0112015-03-10 05:15:26 +00001337 // 1.2.2 OpenMP Language Terminology
1338 // Structured block - An executable statement with a single entry at the
1339 // top and a single exit at the bottom.
1340 // The point of exit cannot be a branch out of the structured block.
1341 // longjmp() and throw() must not violate the entry/exit criteria.
1342 CGF.EHStack.pushTerminate();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001343}
1344
1345InlinedOpenMPRegionRAII::~InlinedOpenMPRegionRAII() {
Alexey Bataev36bf0112015-03-10 05:15:26 +00001346 CGF.EHStack.popTerminate();
Alexey Bataev8cbe0a62015-02-26 10:27:34 +00001347 auto *OldCSI =
1348 cast<CGOpenMPInlinedRegionInfo>(CGF.CapturedStmtInfo)->getOldCSI();
1349 delete CGF.CapturedStmtInfo;
1350 CGF.CapturedStmtInfo = OldCSI;
1351}
1352