xref: /minix3/external/bsd/llvm/dist/clang/lib/CodeGen/CGOpenMPRuntime.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // This provides a class for OpenMP runtime code generation.
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
13*0a6a1f1dSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc #include "CGOpenMPRuntime.h"
15*0a6a1f1dSLionel Sambuc #include "CodeGenFunction.h"
16*0a6a1f1dSLionel Sambuc #include "clang/AST/Decl.h"
17*0a6a1f1dSLionel Sambuc #include "clang/AST/StmtOpenMP.h"
18*0a6a1f1dSLionel Sambuc #include "llvm/ADT/ArrayRef.h"
19*0a6a1f1dSLionel Sambuc #include "llvm/IR/CallSite.h"
20*0a6a1f1dSLionel Sambuc #include "llvm/IR/DerivedTypes.h"
21*0a6a1f1dSLionel Sambuc #include "llvm/IR/GlobalValue.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/IR/Value.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/Support/raw_ostream.h"
24*0a6a1f1dSLionel Sambuc #include <cassert>
25*0a6a1f1dSLionel Sambuc 
26*0a6a1f1dSLionel Sambuc using namespace clang;
27*0a6a1f1dSLionel Sambuc using namespace CodeGen;
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc namespace {
30*0a6a1f1dSLionel Sambuc /// \brief API for captured statement code generation in OpenMP constructs.
31*0a6a1f1dSLionel Sambuc class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
32*0a6a1f1dSLionel Sambuc public:
CGOpenMPRegionInfo(const OMPExecutableDirective & D,const CapturedStmt & CS,const VarDecl * ThreadIDVar)33*0a6a1f1dSLionel Sambuc   CGOpenMPRegionInfo(const OMPExecutableDirective &D, const CapturedStmt &CS,
34*0a6a1f1dSLionel Sambuc                      const VarDecl *ThreadIDVar)
35*0a6a1f1dSLionel Sambuc       : CGCapturedStmtInfo(CS, CR_OpenMP), ThreadIDVar(ThreadIDVar),
36*0a6a1f1dSLionel Sambuc         Directive(D) {
37*0a6a1f1dSLionel Sambuc     assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
38*0a6a1f1dSLionel Sambuc   }
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc   /// \brief Gets a variable or parameter for storing global thread id
41*0a6a1f1dSLionel Sambuc   /// inside OpenMP construct.
getThreadIDVariable() const42*0a6a1f1dSLionel Sambuc   const VarDecl *getThreadIDVariable() const { return ThreadIDVar; }
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc   /// \brief Gets an LValue for the current ThreadID variable.
45*0a6a1f1dSLionel Sambuc   LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
46*0a6a1f1dSLionel Sambuc 
classof(const CGCapturedStmtInfo * Info)47*0a6a1f1dSLionel Sambuc   static bool classof(const CGCapturedStmtInfo *Info) {
48*0a6a1f1dSLionel Sambuc     return Info->getKind() == CR_OpenMP;
49*0a6a1f1dSLionel Sambuc   }
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc   /// \brief Emit the captured statement body.
52*0a6a1f1dSLionel Sambuc   void EmitBody(CodeGenFunction &CGF, Stmt *S) override;
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc   /// \brief Get the name of the capture helper.
getHelperName() const55*0a6a1f1dSLionel Sambuc   StringRef getHelperName() const override { return ".omp_outlined."; }
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc private:
58*0a6a1f1dSLionel Sambuc   /// \brief A variable or parameter storing global thread id for OpenMP
59*0a6a1f1dSLionel Sambuc   /// constructs.
60*0a6a1f1dSLionel Sambuc   const VarDecl *ThreadIDVar;
61*0a6a1f1dSLionel Sambuc   /// \brief OpenMP executable directive associated with the region.
62*0a6a1f1dSLionel Sambuc   const OMPExecutableDirective &Directive;
63*0a6a1f1dSLionel Sambuc };
64*0a6a1f1dSLionel Sambuc } // namespace
65*0a6a1f1dSLionel Sambuc 
getThreadIDVariableLValue(CodeGenFunction & CGF)66*0a6a1f1dSLionel Sambuc LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
67*0a6a1f1dSLionel Sambuc   return CGF.MakeNaturalAlignAddrLValue(
68*0a6a1f1dSLionel Sambuc       CGF.GetAddrOfLocalVar(ThreadIDVar),
69*0a6a1f1dSLionel Sambuc       CGF.getContext().getPointerType(ThreadIDVar->getType()));
70*0a6a1f1dSLionel Sambuc }
71*0a6a1f1dSLionel Sambuc 
EmitBody(CodeGenFunction & CGF,Stmt * S)72*0a6a1f1dSLionel Sambuc void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, Stmt *S) {
73*0a6a1f1dSLionel Sambuc   CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
74*0a6a1f1dSLionel Sambuc   CGF.EmitOMPPrivateClause(Directive, PrivateScope);
75*0a6a1f1dSLionel Sambuc   CGF.EmitOMPFirstprivateClause(Directive, PrivateScope);
76*0a6a1f1dSLionel Sambuc   if (PrivateScope.Privatize())
77*0a6a1f1dSLionel Sambuc     // Emit implicit barrier to synchronize threads and avoid data races.
78*0a6a1f1dSLionel Sambuc     CGF.CGM.getOpenMPRuntime().EmitOMPBarrierCall(CGF, Directive.getLocStart(),
79*0a6a1f1dSLionel Sambuc                                                   /*IsExplicit=*/false);
80*0a6a1f1dSLionel Sambuc   CGCapturedStmtInfo::EmitBody(CGF, S);
81*0a6a1f1dSLionel Sambuc }
82*0a6a1f1dSLionel Sambuc 
CGOpenMPRuntime(CodeGenModule & CGM)83*0a6a1f1dSLionel Sambuc CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
84*0a6a1f1dSLionel Sambuc     : CGM(CGM), DefaultOpenMPPSource(nullptr) {
85*0a6a1f1dSLionel Sambuc   IdentTy = llvm::StructType::create(
86*0a6a1f1dSLionel Sambuc       "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
87*0a6a1f1dSLionel Sambuc       CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
88*0a6a1f1dSLionel Sambuc       CGM.Int8PtrTy /* psource */, nullptr);
89*0a6a1f1dSLionel Sambuc   // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
90*0a6a1f1dSLionel Sambuc   llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
91*0a6a1f1dSLionel Sambuc                                llvm::PointerType::getUnqual(CGM.Int32Ty)};
92*0a6a1f1dSLionel Sambuc   Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
93*0a6a1f1dSLionel Sambuc   KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
94*0a6a1f1dSLionel Sambuc }
95*0a6a1f1dSLionel Sambuc 
96*0a6a1f1dSLionel Sambuc llvm::Value *
EmitOpenMPOutlinedFunction(const OMPExecutableDirective & D,const VarDecl * ThreadIDVar)97*0a6a1f1dSLionel Sambuc CGOpenMPRuntime::EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
98*0a6a1f1dSLionel Sambuc                                             const VarDecl *ThreadIDVar) {
99*0a6a1f1dSLionel Sambuc   const CapturedStmt *CS = cast<CapturedStmt>(D.getAssociatedStmt());
100*0a6a1f1dSLionel Sambuc   CodeGenFunction CGF(CGM, true);
101*0a6a1f1dSLionel Sambuc   CGOpenMPRegionInfo CGInfo(D, *CS, ThreadIDVar);
102*0a6a1f1dSLionel Sambuc   CGF.CapturedStmtInfo = &CGInfo;
103*0a6a1f1dSLionel Sambuc   return CGF.GenerateCapturedStmtFunction(*CS);
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc llvm::Value *
GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags)107*0a6a1f1dSLionel Sambuc CGOpenMPRuntime::GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags) {
108*0a6a1f1dSLionel Sambuc   llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
109*0a6a1f1dSLionel Sambuc   if (!Entry) {
110*0a6a1f1dSLionel Sambuc     if (!DefaultOpenMPPSource) {
111*0a6a1f1dSLionel Sambuc       // Initialize default location for psource field of ident_t structure of
112*0a6a1f1dSLionel Sambuc       // all ident_t objects. Format is ";file;function;line;column;;".
113*0a6a1f1dSLionel Sambuc       // Taken from
114*0a6a1f1dSLionel Sambuc       // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
115*0a6a1f1dSLionel Sambuc       DefaultOpenMPPSource =
116*0a6a1f1dSLionel Sambuc           CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;");
117*0a6a1f1dSLionel Sambuc       DefaultOpenMPPSource =
118*0a6a1f1dSLionel Sambuc           llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
119*0a6a1f1dSLionel Sambuc     }
120*0a6a1f1dSLionel Sambuc     auto DefaultOpenMPLocation = new llvm::GlobalVariable(
121*0a6a1f1dSLionel Sambuc         CGM.getModule(), IdentTy, /*isConstant*/ true,
122*0a6a1f1dSLionel Sambuc         llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr);
123*0a6a1f1dSLionel Sambuc     DefaultOpenMPLocation->setUnnamedAddr(true);
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc     llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true);
126*0a6a1f1dSLionel Sambuc     llvm::Constant *Values[] = {Zero,
127*0a6a1f1dSLionel Sambuc                                 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
128*0a6a1f1dSLionel Sambuc                                 Zero, Zero, DefaultOpenMPPSource};
129*0a6a1f1dSLionel Sambuc     llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values);
130*0a6a1f1dSLionel Sambuc     DefaultOpenMPLocation->setInitializer(Init);
131*0a6a1f1dSLionel Sambuc     OpenMPDefaultLocMap[Flags] = DefaultOpenMPLocation;
132*0a6a1f1dSLionel Sambuc     return DefaultOpenMPLocation;
133*0a6a1f1dSLionel Sambuc   }
134*0a6a1f1dSLionel Sambuc   return Entry;
135*0a6a1f1dSLionel Sambuc }
136*0a6a1f1dSLionel Sambuc 
EmitOpenMPUpdateLocation(CodeGenFunction & CGF,SourceLocation Loc,OpenMPLocationFlags Flags)137*0a6a1f1dSLionel Sambuc llvm::Value *CGOpenMPRuntime::EmitOpenMPUpdateLocation(
138*0a6a1f1dSLionel Sambuc     CodeGenFunction &CGF, SourceLocation Loc, OpenMPLocationFlags Flags) {
139*0a6a1f1dSLionel Sambuc   // If no debug info is generated - return global default location.
140*0a6a1f1dSLionel Sambuc   if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo ||
141*0a6a1f1dSLionel Sambuc       Loc.isInvalid())
142*0a6a1f1dSLionel Sambuc     return GetOrCreateDefaultOpenMPLocation(Flags);
143*0a6a1f1dSLionel Sambuc 
144*0a6a1f1dSLionel Sambuc   assert(CGF.CurFn && "No function in current CodeGenFunction.");
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc   llvm::Value *LocValue = nullptr;
147*0a6a1f1dSLionel Sambuc   auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
148*0a6a1f1dSLionel Sambuc   if (I != OpenMPLocThreadIDMap.end())
149*0a6a1f1dSLionel Sambuc     LocValue = I->second.DebugLoc;
150*0a6a1f1dSLionel Sambuc   // OpenMPLocThreadIDMap may have null DebugLoc and non-null ThreadID, if
151*0a6a1f1dSLionel Sambuc   // GetOpenMPThreadID was called before this routine.
152*0a6a1f1dSLionel Sambuc   if (LocValue == nullptr) {
153*0a6a1f1dSLionel Sambuc     // Generate "ident_t .kmpc_loc.addr;"
154*0a6a1f1dSLionel Sambuc     llvm::AllocaInst *AI = CGF.CreateTempAlloca(IdentTy, ".kmpc_loc.addr");
155*0a6a1f1dSLionel Sambuc     AI->setAlignment(CGM.getDataLayout().getPrefTypeAlignment(IdentTy));
156*0a6a1f1dSLionel Sambuc     auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
157*0a6a1f1dSLionel Sambuc     Elem.second.DebugLoc = AI;
158*0a6a1f1dSLionel Sambuc     LocValue = AI;
159*0a6a1f1dSLionel Sambuc 
160*0a6a1f1dSLionel Sambuc     CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
161*0a6a1f1dSLionel Sambuc     CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
162*0a6a1f1dSLionel Sambuc     CGF.Builder.CreateMemCpy(LocValue, GetOrCreateDefaultOpenMPLocation(Flags),
163*0a6a1f1dSLionel Sambuc                              llvm::ConstantExpr::getSizeOf(IdentTy),
164*0a6a1f1dSLionel Sambuc                              CGM.PointerAlignInBytes);
165*0a6a1f1dSLionel Sambuc   }
166*0a6a1f1dSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc   // char **psource = &.kmpc_loc_<flags>.addr.psource;
168*0a6a1f1dSLionel Sambuc   auto *PSource =
169*0a6a1f1dSLionel Sambuc       CGF.Builder.CreateConstInBoundsGEP2_32(LocValue, 0, IdentField_PSource);
170*0a6a1f1dSLionel Sambuc 
171*0a6a1f1dSLionel Sambuc   auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
172*0a6a1f1dSLionel Sambuc   if (OMPDebugLoc == nullptr) {
173*0a6a1f1dSLionel Sambuc     SmallString<128> Buffer2;
174*0a6a1f1dSLionel Sambuc     llvm::raw_svector_ostream OS2(Buffer2);
175*0a6a1f1dSLionel Sambuc     // Build debug location
176*0a6a1f1dSLionel Sambuc     PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
177*0a6a1f1dSLionel Sambuc     OS2 << ";" << PLoc.getFilename() << ";";
178*0a6a1f1dSLionel Sambuc     if (const FunctionDecl *FD =
179*0a6a1f1dSLionel Sambuc             dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
180*0a6a1f1dSLionel Sambuc       OS2 << FD->getQualifiedNameAsString();
181*0a6a1f1dSLionel Sambuc     }
182*0a6a1f1dSLionel Sambuc     OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
183*0a6a1f1dSLionel Sambuc     OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
184*0a6a1f1dSLionel Sambuc     OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
185*0a6a1f1dSLionel Sambuc   }
186*0a6a1f1dSLionel Sambuc   // *psource = ";<File>;<Function>;<Line>;<Column>;;";
187*0a6a1f1dSLionel Sambuc   CGF.Builder.CreateStore(OMPDebugLoc, PSource);
188*0a6a1f1dSLionel Sambuc 
189*0a6a1f1dSLionel Sambuc   return LocValue;
190*0a6a1f1dSLionel Sambuc }
191*0a6a1f1dSLionel Sambuc 
GetOpenMPThreadID(CodeGenFunction & CGF,SourceLocation Loc)192*0a6a1f1dSLionel Sambuc llvm::Value *CGOpenMPRuntime::GetOpenMPThreadID(CodeGenFunction &CGF,
193*0a6a1f1dSLionel Sambuc                                                 SourceLocation Loc) {
194*0a6a1f1dSLionel Sambuc   assert(CGF.CurFn && "No function in current CodeGenFunction.");
195*0a6a1f1dSLionel Sambuc 
196*0a6a1f1dSLionel Sambuc   llvm::Value *ThreadID = nullptr;
197*0a6a1f1dSLionel Sambuc   // Check whether we've already cached a load of the thread id in this
198*0a6a1f1dSLionel Sambuc   // function.
199*0a6a1f1dSLionel Sambuc   auto I = OpenMPLocThreadIDMap.find(CGF.CurFn);
200*0a6a1f1dSLionel Sambuc   if (I != OpenMPLocThreadIDMap.end()) {
201*0a6a1f1dSLionel Sambuc     ThreadID = I->second.ThreadID;
202*0a6a1f1dSLionel Sambuc     if (ThreadID != nullptr)
203*0a6a1f1dSLionel Sambuc       return ThreadID;
204*0a6a1f1dSLionel Sambuc   }
205*0a6a1f1dSLionel Sambuc   if (auto OMPRegionInfo =
206*0a6a1f1dSLionel Sambuc           dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
207*0a6a1f1dSLionel Sambuc     // Check if this an outlined function with thread id passed as argument.
208*0a6a1f1dSLionel Sambuc     auto ThreadIDVar = OMPRegionInfo->getThreadIDVariable();
209*0a6a1f1dSLionel Sambuc     auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
210*0a6a1f1dSLionel Sambuc     auto RVal = CGF.EmitLoadOfLValue(LVal, Loc);
211*0a6a1f1dSLionel Sambuc     LVal = CGF.MakeNaturalAlignAddrLValue(RVal.getScalarVal(),
212*0a6a1f1dSLionel Sambuc                                           ThreadIDVar->getType());
213*0a6a1f1dSLionel Sambuc     ThreadID = CGF.EmitLoadOfLValue(LVal, Loc).getScalarVal();
214*0a6a1f1dSLionel Sambuc     // If value loaded in entry block, cache it and use it everywhere in
215*0a6a1f1dSLionel Sambuc     // function.
216*0a6a1f1dSLionel Sambuc     if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
217*0a6a1f1dSLionel Sambuc       auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
218*0a6a1f1dSLionel Sambuc       Elem.second.ThreadID = ThreadID;
219*0a6a1f1dSLionel Sambuc     }
220*0a6a1f1dSLionel Sambuc   } else {
221*0a6a1f1dSLionel Sambuc     // This is not an outlined function region - need to call __kmpc_int32
222*0a6a1f1dSLionel Sambuc     // kmpc_global_thread_num(ident_t *loc).
223*0a6a1f1dSLionel Sambuc     // Generate thread id value and cache this value for use across the
224*0a6a1f1dSLionel Sambuc     // function.
225*0a6a1f1dSLionel Sambuc     CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
226*0a6a1f1dSLionel Sambuc     CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
227*0a6a1f1dSLionel Sambuc     llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc)};
228*0a6a1f1dSLionel Sambuc     ThreadID = CGF.EmitRuntimeCall(
229*0a6a1f1dSLionel Sambuc         CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num), Args);
230*0a6a1f1dSLionel Sambuc     auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
231*0a6a1f1dSLionel Sambuc     Elem.second.ThreadID = ThreadID;
232*0a6a1f1dSLionel Sambuc   }
233*0a6a1f1dSLionel Sambuc   return ThreadID;
234*0a6a1f1dSLionel Sambuc }
235*0a6a1f1dSLionel Sambuc 
FunctionFinished(CodeGenFunction & CGF)236*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::FunctionFinished(CodeGenFunction &CGF) {
237*0a6a1f1dSLionel Sambuc   assert(CGF.CurFn && "No function in current CodeGenFunction.");
238*0a6a1f1dSLionel Sambuc   if (OpenMPLocThreadIDMap.count(CGF.CurFn))
239*0a6a1f1dSLionel Sambuc     OpenMPLocThreadIDMap.erase(CGF.CurFn);
240*0a6a1f1dSLionel Sambuc }
241*0a6a1f1dSLionel Sambuc 
getIdentTyPointerTy()242*0a6a1f1dSLionel Sambuc llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
243*0a6a1f1dSLionel Sambuc   return llvm::PointerType::getUnqual(IdentTy);
244*0a6a1f1dSLionel Sambuc }
245*0a6a1f1dSLionel Sambuc 
getKmpc_MicroPointerTy()246*0a6a1f1dSLionel Sambuc llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
247*0a6a1f1dSLionel Sambuc   return llvm::PointerType::getUnqual(Kmpc_MicroTy);
248*0a6a1f1dSLionel Sambuc }
249*0a6a1f1dSLionel Sambuc 
250*0a6a1f1dSLionel Sambuc llvm::Constant *
CreateRuntimeFunction(OpenMPRTLFunction Function)251*0a6a1f1dSLionel Sambuc CGOpenMPRuntime::CreateRuntimeFunction(OpenMPRTLFunction Function) {
252*0a6a1f1dSLionel Sambuc   llvm::Constant *RTLFn = nullptr;
253*0a6a1f1dSLionel Sambuc   switch (Function) {
254*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_fork_call: {
255*0a6a1f1dSLionel Sambuc     // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
256*0a6a1f1dSLionel Sambuc     // microtask, ...);
257*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
258*0a6a1f1dSLionel Sambuc                                 getKmpc_MicroPointerTy()};
259*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
260*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
261*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
262*0a6a1f1dSLionel Sambuc     break;
263*0a6a1f1dSLionel Sambuc   }
264*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_global_thread_num: {
265*0a6a1f1dSLionel Sambuc     // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
266*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
267*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
268*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
269*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
270*0a6a1f1dSLionel Sambuc     break;
271*0a6a1f1dSLionel Sambuc   }
272*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_threadprivate_cached: {
273*0a6a1f1dSLionel Sambuc     // Build void *__kmpc_threadprivate_cached(ident_t *loc,
274*0a6a1f1dSLionel Sambuc     // kmp_int32 global_tid, void *data, size_t size, void ***cache);
275*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
276*0a6a1f1dSLionel Sambuc                                 CGM.VoidPtrTy, CGM.SizeTy,
277*0a6a1f1dSLionel Sambuc                                 CGM.VoidPtrTy->getPointerTo()->getPointerTo()};
278*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
279*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg*/ false);
280*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_cached");
281*0a6a1f1dSLionel Sambuc     break;
282*0a6a1f1dSLionel Sambuc   }
283*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_critical: {
284*0a6a1f1dSLionel Sambuc     // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
285*0a6a1f1dSLionel Sambuc     // kmp_critical_name *crit);
286*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {
287*0a6a1f1dSLionel Sambuc         getIdentTyPointerTy(), CGM.Int32Ty,
288*0a6a1f1dSLionel Sambuc         llvm::PointerType::getUnqual(KmpCriticalNameTy)};
289*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
290*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
291*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
292*0a6a1f1dSLionel Sambuc     break;
293*0a6a1f1dSLionel Sambuc   }
294*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_threadprivate_register: {
295*0a6a1f1dSLionel Sambuc     // Build void __kmpc_threadprivate_register(ident_t *, void *data,
296*0a6a1f1dSLionel Sambuc     // kmpc_ctor ctor, kmpc_cctor cctor, kmpc_dtor dtor);
297*0a6a1f1dSLionel Sambuc     // typedef void *(*kmpc_ctor)(void *);
298*0a6a1f1dSLionel Sambuc     auto KmpcCtorTy =
299*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
300*0a6a1f1dSLionel Sambuc                                 /*isVarArg*/ false)->getPointerTo();
301*0a6a1f1dSLionel Sambuc     // typedef void *(*kmpc_cctor)(void *, void *);
302*0a6a1f1dSLionel Sambuc     llvm::Type *KmpcCopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
303*0a6a1f1dSLionel Sambuc     auto KmpcCopyCtorTy =
304*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidPtrTy, KmpcCopyCtorTyArgs,
305*0a6a1f1dSLionel Sambuc                                 /*isVarArg*/ false)->getPointerTo();
306*0a6a1f1dSLionel Sambuc     // typedef void (*kmpc_dtor)(void *);
307*0a6a1f1dSLionel Sambuc     auto KmpcDtorTy =
308*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy, /*isVarArg*/ false)
309*0a6a1f1dSLionel Sambuc             ->getPointerTo();
310*0a6a1f1dSLionel Sambuc     llvm::Type *FnTyArgs[] = {getIdentTyPointerTy(), CGM.VoidPtrTy, KmpcCtorTy,
311*0a6a1f1dSLionel Sambuc                               KmpcCopyCtorTy, KmpcDtorTy};
312*0a6a1f1dSLionel Sambuc     auto FnTy = llvm::FunctionType::get(CGM.VoidTy, FnTyArgs,
313*0a6a1f1dSLionel Sambuc                                         /*isVarArg*/ false);
314*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_threadprivate_register");
315*0a6a1f1dSLionel Sambuc     break;
316*0a6a1f1dSLionel Sambuc   }
317*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_end_critical: {
318*0a6a1f1dSLionel Sambuc     // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
319*0a6a1f1dSLionel Sambuc     // kmp_critical_name *crit);
320*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {
321*0a6a1f1dSLionel Sambuc         getIdentTyPointerTy(), CGM.Int32Ty,
322*0a6a1f1dSLionel Sambuc         llvm::PointerType::getUnqual(KmpCriticalNameTy)};
323*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
324*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
325*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
326*0a6a1f1dSLionel Sambuc     break;
327*0a6a1f1dSLionel Sambuc   }
328*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_cancel_barrier: {
329*0a6a1f1dSLionel Sambuc     // Build kmp_int32 __kmpc_cancel_barrier(ident_t *loc, kmp_int32
330*0a6a1f1dSLionel Sambuc     // global_tid);
331*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
332*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
333*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
334*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_cancel_barrier");
335*0a6a1f1dSLionel Sambuc     break;
336*0a6a1f1dSLionel Sambuc   }
337*0a6a1f1dSLionel Sambuc   // Build __kmpc_for_static_init*(
338*0a6a1f1dSLionel Sambuc   //               ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
339*0a6a1f1dSLionel Sambuc   //               kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
340*0a6a1f1dSLionel Sambuc   //               kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
341*0a6a1f1dSLionel Sambuc   //               kmp_int[32|64] incr, kmp_int[32|64] chunk);
342*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_for_static_init_4: {
343*0a6a1f1dSLionel Sambuc     auto ITy = CGM.Int32Ty;
344*0a6a1f1dSLionel Sambuc     auto PtrTy = llvm::PointerType::getUnqual(ITy);
345*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {
346*0a6a1f1dSLionel Sambuc         getIdentTyPointerTy(),                     // loc
347*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // tid
348*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // schedtype
349*0a6a1f1dSLionel Sambuc         llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
350*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_lower
351*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_upper
352*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_stride
353*0a6a1f1dSLionel Sambuc         ITy,                                       // incr
354*0a6a1f1dSLionel Sambuc         ITy                                        // chunk
355*0a6a1f1dSLionel Sambuc     };
356*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
357*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
358*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_4");
359*0a6a1f1dSLionel Sambuc     break;
360*0a6a1f1dSLionel Sambuc   }
361*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_for_static_init_4u: {
362*0a6a1f1dSLionel Sambuc     auto ITy = CGM.Int32Ty;
363*0a6a1f1dSLionel Sambuc     auto PtrTy = llvm::PointerType::getUnqual(ITy);
364*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {
365*0a6a1f1dSLionel Sambuc         getIdentTyPointerTy(),                     // loc
366*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // tid
367*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // schedtype
368*0a6a1f1dSLionel Sambuc         llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
369*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_lower
370*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_upper
371*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_stride
372*0a6a1f1dSLionel Sambuc         ITy,                                       // incr
373*0a6a1f1dSLionel Sambuc         ITy                                        // chunk
374*0a6a1f1dSLionel Sambuc     };
375*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
376*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
377*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_4u");
378*0a6a1f1dSLionel Sambuc     break;
379*0a6a1f1dSLionel Sambuc   }
380*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_for_static_init_8: {
381*0a6a1f1dSLionel Sambuc     auto ITy = CGM.Int64Ty;
382*0a6a1f1dSLionel Sambuc     auto PtrTy = llvm::PointerType::getUnqual(ITy);
383*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {
384*0a6a1f1dSLionel Sambuc         getIdentTyPointerTy(),                     // loc
385*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // tid
386*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // schedtype
387*0a6a1f1dSLionel Sambuc         llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
388*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_lower
389*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_upper
390*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_stride
391*0a6a1f1dSLionel Sambuc         ITy,                                       // incr
392*0a6a1f1dSLionel Sambuc         ITy                                        // chunk
393*0a6a1f1dSLionel Sambuc     };
394*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
395*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
396*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_8");
397*0a6a1f1dSLionel Sambuc     break;
398*0a6a1f1dSLionel Sambuc   }
399*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_for_static_init_8u: {
400*0a6a1f1dSLionel Sambuc     auto ITy = CGM.Int64Ty;
401*0a6a1f1dSLionel Sambuc     auto PtrTy = llvm::PointerType::getUnqual(ITy);
402*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {
403*0a6a1f1dSLionel Sambuc         getIdentTyPointerTy(),                     // loc
404*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // tid
405*0a6a1f1dSLionel Sambuc         CGM.Int32Ty,                               // schedtype
406*0a6a1f1dSLionel Sambuc         llvm::PointerType::getUnqual(CGM.Int32Ty), // p_lastiter
407*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_lower
408*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_upper
409*0a6a1f1dSLionel Sambuc         PtrTy,                                     // p_stride
410*0a6a1f1dSLionel Sambuc         ITy,                                       // incr
411*0a6a1f1dSLionel Sambuc         ITy                                        // chunk
412*0a6a1f1dSLionel Sambuc     };
413*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
414*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
415*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_init_8u");
416*0a6a1f1dSLionel Sambuc     break;
417*0a6a1f1dSLionel Sambuc   }
418*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_for_static_fini: {
419*0a6a1f1dSLionel Sambuc     // Build void __kmpc_for_static_fini(ident_t *loc, kmp_int32 global_tid);
420*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
421*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
422*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
423*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_for_static_fini");
424*0a6a1f1dSLionel Sambuc     break;
425*0a6a1f1dSLionel Sambuc   }
426*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_push_num_threads: {
427*0a6a1f1dSLionel Sambuc     // Build void __kmpc_push_num_threads(ident_t *loc, kmp_int32 global_tid,
428*0a6a1f1dSLionel Sambuc     // kmp_int32 num_threads)
429*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
430*0a6a1f1dSLionel Sambuc                                 CGM.Int32Ty};
431*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
432*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
433*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_push_num_threads");
434*0a6a1f1dSLionel Sambuc     break;
435*0a6a1f1dSLionel Sambuc   }
436*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_serialized_parallel: {
437*0a6a1f1dSLionel Sambuc     // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
438*0a6a1f1dSLionel Sambuc     // global_tid);
439*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
440*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
441*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
442*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
443*0a6a1f1dSLionel Sambuc     break;
444*0a6a1f1dSLionel Sambuc   }
445*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_end_serialized_parallel: {
446*0a6a1f1dSLionel Sambuc     // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
447*0a6a1f1dSLionel Sambuc     // global_tid);
448*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
449*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
450*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
451*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
452*0a6a1f1dSLionel Sambuc     break;
453*0a6a1f1dSLionel Sambuc   }
454*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_flush: {
455*0a6a1f1dSLionel Sambuc     // Build void __kmpc_flush(ident_t *loc, ...);
456*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
457*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
458*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
459*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_flush");
460*0a6a1f1dSLionel Sambuc     break;
461*0a6a1f1dSLionel Sambuc   }
462*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_master: {
463*0a6a1f1dSLionel Sambuc     // Build kmp_int32 __kmpc_master(ident_t *loc, kmp_int32 global_tid);
464*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
465*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
466*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
467*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_master");
468*0a6a1f1dSLionel Sambuc     break;
469*0a6a1f1dSLionel Sambuc   }
470*0a6a1f1dSLionel Sambuc   case OMPRTL__kmpc_end_master: {
471*0a6a1f1dSLionel Sambuc     // Build void __kmpc_end_master(ident_t *loc, kmp_int32 global_tid);
472*0a6a1f1dSLionel Sambuc     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
473*0a6a1f1dSLionel Sambuc     llvm::FunctionType *FnTy =
474*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
475*0a6a1f1dSLionel Sambuc     RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name=*/"__kmpc_end_master");
476*0a6a1f1dSLionel Sambuc     break;
477*0a6a1f1dSLionel Sambuc   }
478*0a6a1f1dSLionel Sambuc   }
479*0a6a1f1dSLionel Sambuc   return RTLFn;
480*0a6a1f1dSLionel Sambuc }
481*0a6a1f1dSLionel Sambuc 
482*0a6a1f1dSLionel Sambuc llvm::Constant *
getOrCreateThreadPrivateCache(const VarDecl * VD)483*0a6a1f1dSLionel Sambuc CGOpenMPRuntime::getOrCreateThreadPrivateCache(const VarDecl *VD) {
484*0a6a1f1dSLionel Sambuc   // Lookup the entry, lazily creating it if necessary.
485*0a6a1f1dSLionel Sambuc   return GetOrCreateInternalVariable(CGM.Int8PtrPtrTy,
486*0a6a1f1dSLionel Sambuc                                      Twine(CGM.getMangledName(VD)) + ".cache.");
487*0a6a1f1dSLionel Sambuc }
488*0a6a1f1dSLionel Sambuc 
getOMPAddrOfThreadPrivate(CodeGenFunction & CGF,const VarDecl * VD,llvm::Value * VDAddr,SourceLocation Loc)489*0a6a1f1dSLionel Sambuc llvm::Value *CGOpenMPRuntime::getOMPAddrOfThreadPrivate(CodeGenFunction &CGF,
490*0a6a1f1dSLionel Sambuc                                                         const VarDecl *VD,
491*0a6a1f1dSLionel Sambuc                                                         llvm::Value *VDAddr,
492*0a6a1f1dSLionel Sambuc                                                         SourceLocation Loc) {
493*0a6a1f1dSLionel Sambuc   auto VarTy = VDAddr->getType()->getPointerElementType();
494*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
495*0a6a1f1dSLionel Sambuc                          GetOpenMPThreadID(CGF, Loc),
496*0a6a1f1dSLionel Sambuc                          CGF.Builder.CreatePointerCast(VDAddr, CGM.Int8PtrTy),
497*0a6a1f1dSLionel Sambuc                          CGM.getSize(CGM.GetTargetTypeStoreSize(VarTy)),
498*0a6a1f1dSLionel Sambuc                          getOrCreateThreadPrivateCache(VD)};
499*0a6a1f1dSLionel Sambuc   return CGF.EmitRuntimeCall(
500*0a6a1f1dSLionel Sambuc       CreateRuntimeFunction(OMPRTL__kmpc_threadprivate_cached), Args);
501*0a6a1f1dSLionel Sambuc }
502*0a6a1f1dSLionel Sambuc 
EmitOMPThreadPrivateVarInit(CodeGenFunction & CGF,llvm::Value * VDAddr,llvm::Value * Ctor,llvm::Value * CopyCtor,llvm::Value * Dtor,SourceLocation Loc)503*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPThreadPrivateVarInit(
504*0a6a1f1dSLionel Sambuc     CodeGenFunction &CGF, llvm::Value *VDAddr, llvm::Value *Ctor,
505*0a6a1f1dSLionel Sambuc     llvm::Value *CopyCtor, llvm::Value *Dtor, SourceLocation Loc) {
506*0a6a1f1dSLionel Sambuc   // Call kmp_int32 __kmpc_global_thread_num(&loc) to init OpenMP runtime
507*0a6a1f1dSLionel Sambuc   // library.
508*0a6a1f1dSLionel Sambuc   auto OMPLoc = EmitOpenMPUpdateLocation(CGF, Loc);
509*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num),
510*0a6a1f1dSLionel Sambuc                       OMPLoc);
511*0a6a1f1dSLionel Sambuc   // Call __kmpc_threadprivate_register(&loc, &var, ctor, cctor/*NULL*/, dtor)
512*0a6a1f1dSLionel Sambuc   // to register constructor/destructor for variable.
513*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {OMPLoc,
514*0a6a1f1dSLionel Sambuc                          CGF.Builder.CreatePointerCast(VDAddr, CGM.VoidPtrTy),
515*0a6a1f1dSLionel Sambuc                          Ctor, CopyCtor, Dtor};
516*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(
517*0a6a1f1dSLionel Sambuc       CreateRuntimeFunction(OMPRTL__kmpc_threadprivate_register), Args);
518*0a6a1f1dSLionel Sambuc }
519*0a6a1f1dSLionel Sambuc 
EmitOMPThreadPrivateVarDefinition(const VarDecl * VD,llvm::Value * VDAddr,SourceLocation Loc,bool PerformInit,CodeGenFunction * CGF)520*0a6a1f1dSLionel Sambuc llvm::Function *CGOpenMPRuntime::EmitOMPThreadPrivateVarDefinition(
521*0a6a1f1dSLionel Sambuc     const VarDecl *VD, llvm::Value *VDAddr, SourceLocation Loc,
522*0a6a1f1dSLionel Sambuc     bool PerformInit, CodeGenFunction *CGF) {
523*0a6a1f1dSLionel Sambuc   VD = VD->getDefinition(CGM.getContext());
524*0a6a1f1dSLionel Sambuc   if (VD && ThreadPrivateWithDefinition.count(VD) == 0) {
525*0a6a1f1dSLionel Sambuc     ThreadPrivateWithDefinition.insert(VD);
526*0a6a1f1dSLionel Sambuc     QualType ASTTy = VD->getType();
527*0a6a1f1dSLionel Sambuc 
528*0a6a1f1dSLionel Sambuc     llvm::Value *Ctor = nullptr, *CopyCtor = nullptr, *Dtor = nullptr;
529*0a6a1f1dSLionel Sambuc     auto Init = VD->getAnyInitializer();
530*0a6a1f1dSLionel Sambuc     if (CGM.getLangOpts().CPlusPlus && PerformInit) {
531*0a6a1f1dSLionel Sambuc       // Generate function that re-emits the declaration's initializer into the
532*0a6a1f1dSLionel Sambuc       // threadprivate copy of the variable VD
533*0a6a1f1dSLionel Sambuc       CodeGenFunction CtorCGF(CGM);
534*0a6a1f1dSLionel Sambuc       FunctionArgList Args;
535*0a6a1f1dSLionel Sambuc       ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
536*0a6a1f1dSLionel Sambuc                             /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
537*0a6a1f1dSLionel Sambuc       Args.push_back(&Dst);
538*0a6a1f1dSLionel Sambuc 
539*0a6a1f1dSLionel Sambuc       auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
540*0a6a1f1dSLionel Sambuc           CGM.getContext().VoidPtrTy, Args, FunctionType::ExtInfo(),
541*0a6a1f1dSLionel Sambuc           /*isVariadic=*/false);
542*0a6a1f1dSLionel Sambuc       auto FTy = CGM.getTypes().GetFunctionType(FI);
543*0a6a1f1dSLionel Sambuc       auto Fn = CGM.CreateGlobalInitOrDestructFunction(
544*0a6a1f1dSLionel Sambuc           FTy, ".__kmpc_global_ctor_.", Loc);
545*0a6a1f1dSLionel Sambuc       CtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidPtrTy, Fn, FI,
546*0a6a1f1dSLionel Sambuc                             Args, SourceLocation());
547*0a6a1f1dSLionel Sambuc       auto ArgVal = CtorCGF.EmitLoadOfScalar(
548*0a6a1f1dSLionel Sambuc           CtorCGF.GetAddrOfLocalVar(&Dst),
549*0a6a1f1dSLionel Sambuc           /*Volatile=*/false, CGM.PointerAlignInBytes,
550*0a6a1f1dSLionel Sambuc           CGM.getContext().VoidPtrTy, Dst.getLocation());
551*0a6a1f1dSLionel Sambuc       auto Arg = CtorCGF.Builder.CreatePointerCast(
552*0a6a1f1dSLionel Sambuc           ArgVal,
553*0a6a1f1dSLionel Sambuc           CtorCGF.ConvertTypeForMem(CGM.getContext().getPointerType(ASTTy)));
554*0a6a1f1dSLionel Sambuc       CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
555*0a6a1f1dSLionel Sambuc                                /*IsInitializer=*/true);
556*0a6a1f1dSLionel Sambuc       ArgVal = CtorCGF.EmitLoadOfScalar(
557*0a6a1f1dSLionel Sambuc           CtorCGF.GetAddrOfLocalVar(&Dst),
558*0a6a1f1dSLionel Sambuc           /*Volatile=*/false, CGM.PointerAlignInBytes,
559*0a6a1f1dSLionel Sambuc           CGM.getContext().VoidPtrTy, Dst.getLocation());
560*0a6a1f1dSLionel Sambuc       CtorCGF.Builder.CreateStore(ArgVal, CtorCGF.ReturnValue);
561*0a6a1f1dSLionel Sambuc       CtorCGF.FinishFunction();
562*0a6a1f1dSLionel Sambuc       Ctor = Fn;
563*0a6a1f1dSLionel Sambuc     }
564*0a6a1f1dSLionel Sambuc     if (VD->getType().isDestructedType() != QualType::DK_none) {
565*0a6a1f1dSLionel Sambuc       // Generate function that emits destructor call for the threadprivate copy
566*0a6a1f1dSLionel Sambuc       // of the variable VD
567*0a6a1f1dSLionel Sambuc       CodeGenFunction DtorCGF(CGM);
568*0a6a1f1dSLionel Sambuc       FunctionArgList Args;
569*0a6a1f1dSLionel Sambuc       ImplicitParamDecl Dst(CGM.getContext(), /*DC=*/nullptr, SourceLocation(),
570*0a6a1f1dSLionel Sambuc                             /*Id=*/nullptr, CGM.getContext().VoidPtrTy);
571*0a6a1f1dSLionel Sambuc       Args.push_back(&Dst);
572*0a6a1f1dSLionel Sambuc 
573*0a6a1f1dSLionel Sambuc       auto &FI = CGM.getTypes().arrangeFreeFunctionDeclaration(
574*0a6a1f1dSLionel Sambuc           CGM.getContext().VoidTy, Args, FunctionType::ExtInfo(),
575*0a6a1f1dSLionel Sambuc           /*isVariadic=*/false);
576*0a6a1f1dSLionel Sambuc       auto FTy = CGM.getTypes().GetFunctionType(FI);
577*0a6a1f1dSLionel Sambuc       auto Fn = CGM.CreateGlobalInitOrDestructFunction(
578*0a6a1f1dSLionel Sambuc           FTy, ".__kmpc_global_dtor_.", Loc);
579*0a6a1f1dSLionel Sambuc       DtorCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, Fn, FI, Args,
580*0a6a1f1dSLionel Sambuc                             SourceLocation());
581*0a6a1f1dSLionel Sambuc       auto ArgVal = DtorCGF.EmitLoadOfScalar(
582*0a6a1f1dSLionel Sambuc           DtorCGF.GetAddrOfLocalVar(&Dst),
583*0a6a1f1dSLionel Sambuc           /*Volatile=*/false, CGM.PointerAlignInBytes,
584*0a6a1f1dSLionel Sambuc           CGM.getContext().VoidPtrTy, Dst.getLocation());
585*0a6a1f1dSLionel Sambuc       DtorCGF.emitDestroy(ArgVal, ASTTy,
586*0a6a1f1dSLionel Sambuc                           DtorCGF.getDestroyer(ASTTy.isDestructedType()),
587*0a6a1f1dSLionel Sambuc                           DtorCGF.needsEHCleanup(ASTTy.isDestructedType()));
588*0a6a1f1dSLionel Sambuc       DtorCGF.FinishFunction();
589*0a6a1f1dSLionel Sambuc       Dtor = Fn;
590*0a6a1f1dSLionel Sambuc     }
591*0a6a1f1dSLionel Sambuc     // Do not emit init function if it is not required.
592*0a6a1f1dSLionel Sambuc     if (!Ctor && !Dtor)
593*0a6a1f1dSLionel Sambuc       return nullptr;
594*0a6a1f1dSLionel Sambuc 
595*0a6a1f1dSLionel Sambuc     llvm::Type *CopyCtorTyArgs[] = {CGM.VoidPtrTy, CGM.VoidPtrTy};
596*0a6a1f1dSLionel Sambuc     auto CopyCtorTy =
597*0a6a1f1dSLionel Sambuc         llvm::FunctionType::get(CGM.VoidPtrTy, CopyCtorTyArgs,
598*0a6a1f1dSLionel Sambuc                                 /*isVarArg=*/false)->getPointerTo();
599*0a6a1f1dSLionel Sambuc     // Copying constructor for the threadprivate variable.
600*0a6a1f1dSLionel Sambuc     // Must be NULL - reserved by runtime, but currently it requires that this
601*0a6a1f1dSLionel Sambuc     // parameter is always NULL. Otherwise it fires assertion.
602*0a6a1f1dSLionel Sambuc     CopyCtor = llvm::Constant::getNullValue(CopyCtorTy);
603*0a6a1f1dSLionel Sambuc     if (Ctor == nullptr) {
604*0a6a1f1dSLionel Sambuc       auto CtorTy = llvm::FunctionType::get(CGM.VoidPtrTy, CGM.VoidPtrTy,
605*0a6a1f1dSLionel Sambuc                                             /*isVarArg=*/false)->getPointerTo();
606*0a6a1f1dSLionel Sambuc       Ctor = llvm::Constant::getNullValue(CtorTy);
607*0a6a1f1dSLionel Sambuc     }
608*0a6a1f1dSLionel Sambuc     if (Dtor == nullptr) {
609*0a6a1f1dSLionel Sambuc       auto DtorTy = llvm::FunctionType::get(CGM.VoidTy, CGM.VoidPtrTy,
610*0a6a1f1dSLionel Sambuc                                             /*isVarArg=*/false)->getPointerTo();
611*0a6a1f1dSLionel Sambuc       Dtor = llvm::Constant::getNullValue(DtorTy);
612*0a6a1f1dSLionel Sambuc     }
613*0a6a1f1dSLionel Sambuc     if (!CGF) {
614*0a6a1f1dSLionel Sambuc       auto InitFunctionTy =
615*0a6a1f1dSLionel Sambuc           llvm::FunctionType::get(CGM.VoidTy, /*isVarArg*/ false);
616*0a6a1f1dSLionel Sambuc       auto InitFunction = CGM.CreateGlobalInitOrDestructFunction(
617*0a6a1f1dSLionel Sambuc           InitFunctionTy, ".__omp_threadprivate_init_.");
618*0a6a1f1dSLionel Sambuc       CodeGenFunction InitCGF(CGM);
619*0a6a1f1dSLionel Sambuc       FunctionArgList ArgList;
620*0a6a1f1dSLionel Sambuc       InitCGF.StartFunction(GlobalDecl(), CGM.getContext().VoidTy, InitFunction,
621*0a6a1f1dSLionel Sambuc                             CGM.getTypes().arrangeNullaryFunction(), ArgList,
622*0a6a1f1dSLionel Sambuc                             Loc);
623*0a6a1f1dSLionel Sambuc       EmitOMPThreadPrivateVarInit(InitCGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
624*0a6a1f1dSLionel Sambuc       InitCGF.FinishFunction();
625*0a6a1f1dSLionel Sambuc       return InitFunction;
626*0a6a1f1dSLionel Sambuc     }
627*0a6a1f1dSLionel Sambuc     EmitOMPThreadPrivateVarInit(*CGF, VDAddr, Ctor, CopyCtor, Dtor, Loc);
628*0a6a1f1dSLionel Sambuc   }
629*0a6a1f1dSLionel Sambuc   return nullptr;
630*0a6a1f1dSLionel Sambuc }
631*0a6a1f1dSLionel Sambuc 
EmitOMPParallelCall(CodeGenFunction & CGF,SourceLocation Loc,llvm::Value * OutlinedFn,llvm::Value * CapturedStruct)632*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPParallelCall(CodeGenFunction &CGF,
633*0a6a1f1dSLionel Sambuc                                           SourceLocation Loc,
634*0a6a1f1dSLionel Sambuc                                           llvm::Value *OutlinedFn,
635*0a6a1f1dSLionel Sambuc                                           llvm::Value *CapturedStruct) {
636*0a6a1f1dSLionel Sambuc   // Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/)
637*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {
638*0a6a1f1dSLionel Sambuc       EmitOpenMPUpdateLocation(CGF, Loc),
639*0a6a1f1dSLionel Sambuc       CGF.Builder.getInt32(1), // Number of arguments after 'microtask' argument
640*0a6a1f1dSLionel Sambuc       // (there is only one additional argument - 'context')
641*0a6a1f1dSLionel Sambuc       CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy()),
642*0a6a1f1dSLionel Sambuc       CGF.EmitCastToVoidPtr(CapturedStruct)};
643*0a6a1f1dSLionel Sambuc   auto RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_fork_call);
644*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
645*0a6a1f1dSLionel Sambuc }
646*0a6a1f1dSLionel Sambuc 
EmitOMPSerialCall(CodeGenFunction & CGF,SourceLocation Loc,llvm::Value * OutlinedFn,llvm::Value * CapturedStruct)647*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPSerialCall(CodeGenFunction &CGF,
648*0a6a1f1dSLionel Sambuc                                         SourceLocation Loc,
649*0a6a1f1dSLionel Sambuc                                         llvm::Value *OutlinedFn,
650*0a6a1f1dSLionel Sambuc                                         llvm::Value *CapturedStruct) {
651*0a6a1f1dSLionel Sambuc   auto ThreadID = GetOpenMPThreadID(CGF, Loc);
652*0a6a1f1dSLionel Sambuc   // Build calls:
653*0a6a1f1dSLionel Sambuc   // __kmpc_serialized_parallel(&Loc, GTid);
654*0a6a1f1dSLionel Sambuc   llvm::Value *SerArgs[] = {EmitOpenMPUpdateLocation(CGF, Loc), ThreadID};
655*0a6a1f1dSLionel Sambuc   auto RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_serialized_parallel);
656*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, SerArgs);
657*0a6a1f1dSLionel Sambuc 
658*0a6a1f1dSLionel Sambuc   // OutlinedFn(&GTid, &zero, CapturedStruct);
659*0a6a1f1dSLionel Sambuc   auto ThreadIDAddr = EmitThreadIDAddress(CGF, Loc);
660*0a6a1f1dSLionel Sambuc   auto Int32Ty =
661*0a6a1f1dSLionel Sambuc       CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
662*0a6a1f1dSLionel Sambuc   auto ZeroAddr = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".zero.addr");
663*0a6a1f1dSLionel Sambuc   CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
664*0a6a1f1dSLionel Sambuc   llvm::Value *OutlinedFnArgs[] = {ThreadIDAddr, ZeroAddr, CapturedStruct};
665*0a6a1f1dSLionel Sambuc   CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
666*0a6a1f1dSLionel Sambuc 
667*0a6a1f1dSLionel Sambuc   // __kmpc_end_serialized_parallel(&Loc, GTid);
668*0a6a1f1dSLionel Sambuc   llvm::Value *EndSerArgs[] = {EmitOpenMPUpdateLocation(CGF, Loc), ThreadID};
669*0a6a1f1dSLionel Sambuc   RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_end_serialized_parallel);
670*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, EndSerArgs);
671*0a6a1f1dSLionel Sambuc }
672*0a6a1f1dSLionel Sambuc 
673*0a6a1f1dSLionel Sambuc // If we're inside an (outlined) parallel region, use the region info's
674*0a6a1f1dSLionel Sambuc // thread-ID variable (it is passed in a first argument of the outlined function
675*0a6a1f1dSLionel Sambuc // as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
676*0a6a1f1dSLionel Sambuc // regular serial code region, get thread ID by calling kmp_int32
677*0a6a1f1dSLionel Sambuc // kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
678*0a6a1f1dSLionel Sambuc // return the address of that temp.
EmitThreadIDAddress(CodeGenFunction & CGF,SourceLocation Loc)679*0a6a1f1dSLionel Sambuc llvm::Value *CGOpenMPRuntime::EmitThreadIDAddress(CodeGenFunction &CGF,
680*0a6a1f1dSLionel Sambuc                                                   SourceLocation Loc) {
681*0a6a1f1dSLionel Sambuc   if (auto OMPRegionInfo =
682*0a6a1f1dSLionel Sambuc           dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
683*0a6a1f1dSLionel Sambuc     return CGF.EmitLoadOfLValue(OMPRegionInfo->getThreadIDVariableLValue(CGF),
684*0a6a1f1dSLionel Sambuc                                 SourceLocation()).getScalarVal();
685*0a6a1f1dSLionel Sambuc   auto ThreadID = GetOpenMPThreadID(CGF, Loc);
686*0a6a1f1dSLionel Sambuc   auto Int32Ty =
687*0a6a1f1dSLionel Sambuc       CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
688*0a6a1f1dSLionel Sambuc   auto ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
689*0a6a1f1dSLionel Sambuc   CGF.EmitStoreOfScalar(ThreadID,
690*0a6a1f1dSLionel Sambuc                         CGF.MakeNaturalAlignAddrLValue(ThreadIDTemp, Int32Ty));
691*0a6a1f1dSLionel Sambuc 
692*0a6a1f1dSLionel Sambuc   return ThreadIDTemp;
693*0a6a1f1dSLionel Sambuc }
694*0a6a1f1dSLionel Sambuc 
695*0a6a1f1dSLionel Sambuc llvm::Constant *
GetOrCreateInternalVariable(llvm::Type * Ty,const llvm::Twine & Name)696*0a6a1f1dSLionel Sambuc CGOpenMPRuntime::GetOrCreateInternalVariable(llvm::Type *Ty,
697*0a6a1f1dSLionel Sambuc                                              const llvm::Twine &Name) {
698*0a6a1f1dSLionel Sambuc   SmallString<256> Buffer;
699*0a6a1f1dSLionel Sambuc   llvm::raw_svector_ostream Out(Buffer);
700*0a6a1f1dSLionel Sambuc   Out << Name;
701*0a6a1f1dSLionel Sambuc   auto RuntimeName = Out.str();
702*0a6a1f1dSLionel Sambuc   auto &Elem = *InternalVars.insert(std::make_pair(RuntimeName, nullptr)).first;
703*0a6a1f1dSLionel Sambuc   if (Elem.second) {
704*0a6a1f1dSLionel Sambuc     assert(Elem.second->getType()->getPointerElementType() == Ty &&
705*0a6a1f1dSLionel Sambuc            "OMP internal variable has different type than requested");
706*0a6a1f1dSLionel Sambuc     return &*Elem.second;
707*0a6a1f1dSLionel Sambuc   }
708*0a6a1f1dSLionel Sambuc 
709*0a6a1f1dSLionel Sambuc   return Elem.second = new llvm::GlobalVariable(
710*0a6a1f1dSLionel Sambuc              CGM.getModule(), Ty, /*IsConstant*/ false,
711*0a6a1f1dSLionel Sambuc              llvm::GlobalValue::CommonLinkage, llvm::Constant::getNullValue(Ty),
712*0a6a1f1dSLionel Sambuc              Elem.first());
713*0a6a1f1dSLionel Sambuc }
714*0a6a1f1dSLionel Sambuc 
GetCriticalRegionLock(StringRef CriticalName)715*0a6a1f1dSLionel Sambuc llvm::Value *CGOpenMPRuntime::GetCriticalRegionLock(StringRef CriticalName) {
716*0a6a1f1dSLionel Sambuc   llvm::Twine Name(".gomp_critical_user_", CriticalName);
717*0a6a1f1dSLionel Sambuc   return GetOrCreateInternalVariable(KmpCriticalNameTy, Name.concat(".var"));
718*0a6a1f1dSLionel Sambuc }
719*0a6a1f1dSLionel Sambuc 
EmitOMPCriticalRegion(CodeGenFunction & CGF,StringRef CriticalName,const std::function<void ()> & CriticalOpGen,SourceLocation Loc)720*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPCriticalRegion(
721*0a6a1f1dSLionel Sambuc     CodeGenFunction &CGF, StringRef CriticalName,
722*0a6a1f1dSLionel Sambuc     const std::function<void()> &CriticalOpGen, SourceLocation Loc) {
723*0a6a1f1dSLionel Sambuc   auto RegionLock = GetCriticalRegionLock(CriticalName);
724*0a6a1f1dSLionel Sambuc   // __kmpc_critical(ident_t *, gtid, Lock);
725*0a6a1f1dSLionel Sambuc   // CriticalOpGen();
726*0a6a1f1dSLionel Sambuc   // __kmpc_end_critical(ident_t *, gtid, Lock);
727*0a6a1f1dSLionel Sambuc   // Prepare arguments and build a call to __kmpc_critical
728*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
729*0a6a1f1dSLionel Sambuc                          GetOpenMPThreadID(CGF, Loc), RegionLock};
730*0a6a1f1dSLionel Sambuc   auto RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_critical);
731*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
732*0a6a1f1dSLionel Sambuc   CriticalOpGen();
733*0a6a1f1dSLionel Sambuc   // Build a call to __kmpc_end_critical
734*0a6a1f1dSLionel Sambuc   RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_end_critical);
735*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
736*0a6a1f1dSLionel Sambuc }
737*0a6a1f1dSLionel Sambuc 
EmitOMPIfStmt(CodeGenFunction & CGF,llvm::Value * IfCond,const std::function<void ()> & BodyOpGen)738*0a6a1f1dSLionel Sambuc static void EmitOMPIfStmt(CodeGenFunction &CGF, llvm::Value *IfCond,
739*0a6a1f1dSLionel Sambuc                           const std::function<void()> &BodyOpGen) {
740*0a6a1f1dSLionel Sambuc   llvm::Value *CallBool = CGF.EmitScalarConversion(
741*0a6a1f1dSLionel Sambuc       IfCond,
742*0a6a1f1dSLionel Sambuc       CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/true),
743*0a6a1f1dSLionel Sambuc       CGF.getContext().BoolTy);
744*0a6a1f1dSLionel Sambuc 
745*0a6a1f1dSLionel Sambuc   auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
746*0a6a1f1dSLionel Sambuc   auto *ContBlock = CGF.createBasicBlock("omp_if.end");
747*0a6a1f1dSLionel Sambuc   // Generate the branch (If-stmt)
748*0a6a1f1dSLionel Sambuc   CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
749*0a6a1f1dSLionel Sambuc   CGF.EmitBlock(ThenBlock);
750*0a6a1f1dSLionel Sambuc   BodyOpGen();
751*0a6a1f1dSLionel Sambuc   // Emit the rest of bblocks/branches
752*0a6a1f1dSLionel Sambuc   CGF.EmitBranch(ContBlock);
753*0a6a1f1dSLionel Sambuc   CGF.EmitBlock(ContBlock, true);
754*0a6a1f1dSLionel Sambuc }
755*0a6a1f1dSLionel Sambuc 
EmitOMPMasterRegion(CodeGenFunction & CGF,const std::function<void ()> & MasterOpGen,SourceLocation Loc)756*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPMasterRegion(
757*0a6a1f1dSLionel Sambuc     CodeGenFunction &CGF, const std::function<void()> &MasterOpGen,
758*0a6a1f1dSLionel Sambuc     SourceLocation Loc) {
759*0a6a1f1dSLionel Sambuc   // if(__kmpc_master(ident_t *, gtid)) {
760*0a6a1f1dSLionel Sambuc   //   MasterOpGen();
761*0a6a1f1dSLionel Sambuc   //   __kmpc_end_master(ident_t *, gtid);
762*0a6a1f1dSLionel Sambuc   // }
763*0a6a1f1dSLionel Sambuc   // Prepare arguments and build a call to __kmpc_master
764*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
765*0a6a1f1dSLionel Sambuc                          GetOpenMPThreadID(CGF, Loc)};
766*0a6a1f1dSLionel Sambuc   auto RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_master);
767*0a6a1f1dSLionel Sambuc   auto *IsMaster = CGF.EmitRuntimeCall(RTLFn, Args);
768*0a6a1f1dSLionel Sambuc   EmitOMPIfStmt(CGF, IsMaster, [&]() -> void {
769*0a6a1f1dSLionel Sambuc     MasterOpGen();
770*0a6a1f1dSLionel Sambuc     // Build a call to __kmpc_end_master.
771*0a6a1f1dSLionel Sambuc     // OpenMP [1.2.2 OpenMP Language Terminology]
772*0a6a1f1dSLionel Sambuc     // For C/C++, an executable statement, possibly compound, with a single
773*0a6a1f1dSLionel Sambuc     // entry at the top and a single exit at the bottom, or an OpenMP construct.
774*0a6a1f1dSLionel Sambuc     // * Access to the structured block must not be the result of a branch.
775*0a6a1f1dSLionel Sambuc     // * The point of exit cannot be a branch out of the structured block.
776*0a6a1f1dSLionel Sambuc     // * The point of entry must not be a call to setjmp().
777*0a6a1f1dSLionel Sambuc     // * longjmp() and throw() must not violate the entry/exit criteria.
778*0a6a1f1dSLionel Sambuc     // * An expression statement, iteration statement, selection statement, or
779*0a6a1f1dSLionel Sambuc     // try block is considered to be a structured block if the corresponding
780*0a6a1f1dSLionel Sambuc     // compound statement obtained by enclosing it in { and } would be a
781*0a6a1f1dSLionel Sambuc     // structured block.
782*0a6a1f1dSLionel Sambuc     // It is analyzed in Sema, so we can just call __kmpc_end_master() on
783*0a6a1f1dSLionel Sambuc     // fallthrough rather than pushing a normal cleanup for it.
784*0a6a1f1dSLionel Sambuc     RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_end_master);
785*0a6a1f1dSLionel Sambuc     CGF.EmitRuntimeCall(RTLFn, Args);
786*0a6a1f1dSLionel Sambuc   });
787*0a6a1f1dSLionel Sambuc }
788*0a6a1f1dSLionel Sambuc 
EmitOMPBarrierCall(CodeGenFunction & CGF,SourceLocation Loc,bool IsExplicit)789*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPBarrierCall(CodeGenFunction &CGF,
790*0a6a1f1dSLionel Sambuc                                          SourceLocation Loc, bool IsExplicit) {
791*0a6a1f1dSLionel Sambuc   // Build call __kmpc_cancel_barrier(loc, thread_id);
792*0a6a1f1dSLionel Sambuc   auto Flags = static_cast<OpenMPLocationFlags>(
793*0a6a1f1dSLionel Sambuc       OMP_IDENT_KMPC |
794*0a6a1f1dSLionel Sambuc       (IsExplicit ? OMP_IDENT_BARRIER_EXPL : OMP_IDENT_BARRIER_IMPL));
795*0a6a1f1dSLionel Sambuc   // Build call __kmpc_cancel_barrier(loc, thread_id);
796*0a6a1f1dSLionel Sambuc   // Replace __kmpc_barrier() function by __kmpc_cancel_barrier() because this
797*0a6a1f1dSLionel Sambuc   // one provides the same functionality and adds initial support for
798*0a6a1f1dSLionel Sambuc   // cancellation constructs introduced in OpenMP 4.0. __kmpc_cancel_barrier()
799*0a6a1f1dSLionel Sambuc   // is provided default by the runtime library so it safe to make such
800*0a6a1f1dSLionel Sambuc   // replacement.
801*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc, Flags),
802*0a6a1f1dSLionel Sambuc                          GetOpenMPThreadID(CGF, Loc)};
803*0a6a1f1dSLionel Sambuc   auto RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_cancel_barrier);
804*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
805*0a6a1f1dSLionel Sambuc }
806*0a6a1f1dSLionel Sambuc 
807*0a6a1f1dSLionel Sambuc /// \brief Schedule types for 'omp for' loops (these enumerators are taken from
808*0a6a1f1dSLionel Sambuc /// the enum sched_type in kmp.h).
809*0a6a1f1dSLionel Sambuc enum OpenMPSchedType {
810*0a6a1f1dSLionel Sambuc   /// \brief Lower bound for default (unordered) versions.
811*0a6a1f1dSLionel Sambuc   OMP_sch_lower = 32,
812*0a6a1f1dSLionel Sambuc   OMP_sch_static_chunked = 33,
813*0a6a1f1dSLionel Sambuc   OMP_sch_static = 34,
814*0a6a1f1dSLionel Sambuc   OMP_sch_dynamic_chunked = 35,
815*0a6a1f1dSLionel Sambuc   OMP_sch_guided_chunked = 36,
816*0a6a1f1dSLionel Sambuc   OMP_sch_runtime = 37,
817*0a6a1f1dSLionel Sambuc   OMP_sch_auto = 38,
818*0a6a1f1dSLionel Sambuc   /// \brief Lower bound for 'ordered' versions.
819*0a6a1f1dSLionel Sambuc   OMP_ord_lower = 64,
820*0a6a1f1dSLionel Sambuc   /// \brief Lower bound for 'nomerge' versions.
821*0a6a1f1dSLionel Sambuc   OMP_nm_lower = 160,
822*0a6a1f1dSLionel Sambuc };
823*0a6a1f1dSLionel Sambuc 
824*0a6a1f1dSLionel Sambuc /// \brief Map the OpenMP loop schedule to the runtime enumeration.
getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind,bool Chunked)825*0a6a1f1dSLionel Sambuc static OpenMPSchedType getRuntimeSchedule(OpenMPScheduleClauseKind ScheduleKind,
826*0a6a1f1dSLionel Sambuc                                           bool Chunked) {
827*0a6a1f1dSLionel Sambuc   switch (ScheduleKind) {
828*0a6a1f1dSLionel Sambuc   case OMPC_SCHEDULE_static:
829*0a6a1f1dSLionel Sambuc     return Chunked ? OMP_sch_static_chunked : OMP_sch_static;
830*0a6a1f1dSLionel Sambuc   case OMPC_SCHEDULE_dynamic:
831*0a6a1f1dSLionel Sambuc     return OMP_sch_dynamic_chunked;
832*0a6a1f1dSLionel Sambuc   case OMPC_SCHEDULE_guided:
833*0a6a1f1dSLionel Sambuc     return OMP_sch_guided_chunked;
834*0a6a1f1dSLionel Sambuc   case OMPC_SCHEDULE_auto:
835*0a6a1f1dSLionel Sambuc     return OMP_sch_auto;
836*0a6a1f1dSLionel Sambuc   case OMPC_SCHEDULE_runtime:
837*0a6a1f1dSLionel Sambuc     return OMP_sch_runtime;
838*0a6a1f1dSLionel Sambuc   case OMPC_SCHEDULE_unknown:
839*0a6a1f1dSLionel Sambuc     assert(!Chunked && "chunk was specified but schedule kind not known");
840*0a6a1f1dSLionel Sambuc     return OMP_sch_static;
841*0a6a1f1dSLionel Sambuc   }
842*0a6a1f1dSLionel Sambuc   llvm_unreachable("Unexpected runtime schedule");
843*0a6a1f1dSLionel Sambuc }
844*0a6a1f1dSLionel Sambuc 
isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,bool Chunked) const845*0a6a1f1dSLionel Sambuc bool CGOpenMPRuntime::isStaticNonchunked(OpenMPScheduleClauseKind ScheduleKind,
846*0a6a1f1dSLionel Sambuc                                          bool Chunked) const {
847*0a6a1f1dSLionel Sambuc   auto Schedule = getRuntimeSchedule(ScheduleKind, Chunked);
848*0a6a1f1dSLionel Sambuc   return Schedule == OMP_sch_static;
849*0a6a1f1dSLionel Sambuc }
850*0a6a1f1dSLionel Sambuc 
EmitOMPForInit(CodeGenFunction & CGF,SourceLocation Loc,OpenMPScheduleClauseKind ScheduleKind,unsigned IVSize,bool IVSigned,llvm::Value * IL,llvm::Value * LB,llvm::Value * UB,llvm::Value * ST,llvm::Value * Chunk)851*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPForInit(CodeGenFunction &CGF, SourceLocation Loc,
852*0a6a1f1dSLionel Sambuc                                      OpenMPScheduleClauseKind ScheduleKind,
853*0a6a1f1dSLionel Sambuc                                      unsigned IVSize, bool IVSigned,
854*0a6a1f1dSLionel Sambuc                                      llvm::Value *IL, llvm::Value *LB,
855*0a6a1f1dSLionel Sambuc                                      llvm::Value *UB, llvm::Value *ST,
856*0a6a1f1dSLionel Sambuc                                      llvm::Value *Chunk) {
857*0a6a1f1dSLionel Sambuc   OpenMPSchedType Schedule = getRuntimeSchedule(ScheduleKind, Chunk != nullptr);
858*0a6a1f1dSLionel Sambuc   // Call __kmpc_for_static_init(
859*0a6a1f1dSLionel Sambuc   //          ident_t *loc, kmp_int32 tid, kmp_int32 schedtype,
860*0a6a1f1dSLionel Sambuc   //          kmp_int32 *p_lastiter, kmp_int[32|64] *p_lower,
861*0a6a1f1dSLionel Sambuc   //          kmp_int[32|64] *p_upper, kmp_int[32|64] *p_stride,
862*0a6a1f1dSLionel Sambuc   //          kmp_int[32|64] incr, kmp_int[32|64] chunk);
863*0a6a1f1dSLionel Sambuc   // TODO: Implement dynamic schedule.
864*0a6a1f1dSLionel Sambuc 
865*0a6a1f1dSLionel Sambuc   // If the Chunk was not specified in the clause - use default value 1.
866*0a6a1f1dSLionel Sambuc   if (Chunk == nullptr)
867*0a6a1f1dSLionel Sambuc     Chunk = CGF.Builder.getIntN(IVSize, /*C*/ 1);
868*0a6a1f1dSLionel Sambuc 
869*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {
870*0a6a1f1dSLionel Sambuc       EmitOpenMPUpdateLocation(CGF, Loc, OMP_IDENT_KMPC),
871*0a6a1f1dSLionel Sambuc       GetOpenMPThreadID(CGF, Loc),
872*0a6a1f1dSLionel Sambuc       CGF.Builder.getInt32(Schedule), // Schedule type
873*0a6a1f1dSLionel Sambuc       IL,                             // &isLastIter
874*0a6a1f1dSLionel Sambuc       LB,                             // &LB
875*0a6a1f1dSLionel Sambuc       UB,                             // &UB
876*0a6a1f1dSLionel Sambuc       ST,                             // &Stride
877*0a6a1f1dSLionel Sambuc       CGF.Builder.getIntN(IVSize, 1), // Incr
878*0a6a1f1dSLionel Sambuc       Chunk                           // Chunk
879*0a6a1f1dSLionel Sambuc   };
880*0a6a1f1dSLionel Sambuc   assert((IVSize == 32 || IVSize == 64) &&
881*0a6a1f1dSLionel Sambuc          "Index size is not compatible with the omp runtime");
882*0a6a1f1dSLionel Sambuc   auto F = IVSize == 32 ? (IVSigned ? OMPRTL__kmpc_for_static_init_4
883*0a6a1f1dSLionel Sambuc                                     : OMPRTL__kmpc_for_static_init_4u)
884*0a6a1f1dSLionel Sambuc                         : (IVSigned ? OMPRTL__kmpc_for_static_init_8
885*0a6a1f1dSLionel Sambuc                                     : OMPRTL__kmpc_for_static_init_8u);
886*0a6a1f1dSLionel Sambuc   auto RTLFn = CreateRuntimeFunction(F);
887*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
888*0a6a1f1dSLionel Sambuc }
889*0a6a1f1dSLionel Sambuc 
EmitOMPForFinish(CodeGenFunction & CGF,SourceLocation Loc,OpenMPScheduleClauseKind ScheduleKind)890*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPForFinish(CodeGenFunction &CGF, SourceLocation Loc,
891*0a6a1f1dSLionel Sambuc                                        OpenMPScheduleClauseKind ScheduleKind) {
892*0a6a1f1dSLionel Sambuc   assert((ScheduleKind == OMPC_SCHEDULE_static ||
893*0a6a1f1dSLionel Sambuc           ScheduleKind == OMPC_SCHEDULE_unknown) &&
894*0a6a1f1dSLionel Sambuc          "Non-static schedule kinds are not yet implemented");
895*0a6a1f1dSLionel Sambuc   // Call __kmpc_for_static_fini(ident_t *loc, kmp_int32 tid);
896*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc, OMP_IDENT_KMPC),
897*0a6a1f1dSLionel Sambuc                          GetOpenMPThreadID(CGF, Loc)};
898*0a6a1f1dSLionel Sambuc   auto RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_for_static_fini);
899*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
900*0a6a1f1dSLionel Sambuc }
901*0a6a1f1dSLionel Sambuc 
EmitOMPNumThreadsClause(CodeGenFunction & CGF,llvm::Value * NumThreads,SourceLocation Loc)902*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPNumThreadsClause(CodeGenFunction &CGF,
903*0a6a1f1dSLionel Sambuc                                               llvm::Value *NumThreads,
904*0a6a1f1dSLionel Sambuc                                               SourceLocation Loc) {
905*0a6a1f1dSLionel Sambuc   // Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
906*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {
907*0a6a1f1dSLionel Sambuc       EmitOpenMPUpdateLocation(CGF, Loc), GetOpenMPThreadID(CGF, Loc),
908*0a6a1f1dSLionel Sambuc       CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
909*0a6a1f1dSLionel Sambuc   llvm::Constant *RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_push_num_threads);
910*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
911*0a6a1f1dSLionel Sambuc }
912*0a6a1f1dSLionel Sambuc 
EmitOMPFlush(CodeGenFunction & CGF,ArrayRef<const Expr * >,SourceLocation Loc)913*0a6a1f1dSLionel Sambuc void CGOpenMPRuntime::EmitOMPFlush(CodeGenFunction &CGF, ArrayRef<const Expr *>,
914*0a6a1f1dSLionel Sambuc                                    SourceLocation Loc) {
915*0a6a1f1dSLionel Sambuc   // Build call void __kmpc_flush(ident_t *loc, ...)
916*0a6a1f1dSLionel Sambuc   // FIXME: List of variables is ignored by libiomp5 runtime, no need to
917*0a6a1f1dSLionel Sambuc   // generate it, just request full memory fence.
918*0a6a1f1dSLionel Sambuc   llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
919*0a6a1f1dSLionel Sambuc                          llvm::ConstantInt::get(CGM.Int32Ty, 0)};
920*0a6a1f1dSLionel Sambuc   auto *RTLFn = CreateRuntimeFunction(OMPRTL__kmpc_flush);
921*0a6a1f1dSLionel Sambuc   CGF.EmitRuntimeCall(RTLFn, Args);
922*0a6a1f1dSLionel Sambuc }
923