blob: 53d4b6cb4048b595193aeb14c793f41ca008467c [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===----- CGOpenMPRuntime.cpp - Interface to OpenMP Runtimes -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a class for OpenMP runtime code generation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGOpenMPRuntime.h"
15#include "CodeGenFunction.h"
Alexey Bataev18095712014-10-10 12:19:54 +000016#include "clang/AST/StmtOpenMP.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000017#include "clang/AST/Decl.h"
18#include "llvm/ADT/ArrayRef.h"
Alexey Bataevd74d0602014-10-13 06:02:40 +000019#include "llvm/IR/CallSite.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000020#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/GlobalValue.h"
22#include "llvm/IR/Value.h"
23#include "llvm/Support/raw_ostream.h"
Alexey Bataev23b69422014-06-18 07:08:49 +000024#include <cassert>
Alexey Bataev9959db52014-05-06 10:08:46 +000025
26using namespace clang;
27using namespace CodeGen;
28
Benjamin Kramerc52193f2014-10-10 13:57:57 +000029namespace {
Alexey Bataev18095712014-10-10 12:19:54 +000030/// \brief API for captured statement code generation in OpenMP constructs.
31class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo {
32public:
33 CGOpenMPRegionInfo(const OMPExecutableDirective &D, const CapturedStmt &CS,
34 const VarDecl *ThreadIDVar)
35 : CGCapturedStmtInfo(CS, CR_OpenMP), ThreadIDVar(ThreadIDVar),
36 Directive(D) {
37 assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region.");
38 }
39
Alexey Bataev18095712014-10-10 12:19:54 +000040 /// \brief Gets a variable or parameter for storing global thread id
41 /// inside OpenMP construct.
42 const VarDecl *getThreadIDVariable() const { return ThreadIDVar; }
43
44 /// \brief Gets an LValue for the current ThreadID variable.
45 LValue getThreadIDVariableLValue(CodeGenFunction &CGF);
46
47 static bool classof(const CGCapturedStmtInfo *Info) {
48 return Info->getKind() == CR_OpenMP;
49 }
50
51 /// \brief Emit the captured statement body.
Benjamin Kramerc52193f2014-10-10 13:57:57 +000052 void EmitBody(CodeGenFunction &CGF, Stmt *S) override;
Alexey Bataev18095712014-10-10 12:19:54 +000053
54 /// \brief Get the name of the capture helper.
Benjamin Kramerc52193f2014-10-10 13:57:57 +000055 StringRef getHelperName() const override { return ".omp_outlined."; }
Alexey Bataev18095712014-10-10 12:19:54 +000056
57private:
58 /// \brief A variable or parameter storing global thread id for OpenMP
59 /// constructs.
60 const VarDecl *ThreadIDVar;
61 /// \brief OpenMP executable directive associated with the region.
62 const OMPExecutableDirective &Directive;
63};
Benjamin Kramerc52193f2014-10-10 13:57:57 +000064} // namespace
Alexey Bataev18095712014-10-10 12:19:54 +000065
66LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) {
67 return CGF.MakeNaturalAlignAddrLValue(
68 CGF.GetAddrOfLocalVar(ThreadIDVar),
69 CGF.getContext().getPointerType(ThreadIDVar->getType()));
70}
71
Alexey Bataev4a5bb772014-10-08 14:01:46 +000072void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, Stmt *S) {
Alexey Bataev435ad7b2014-10-10 09:48:26 +000073 CodeGenFunction::OMPPrivateScope PrivateScope(CGF);
74 CGF.EmitOMPFirstprivateClause(Directive, PrivateScope);
75 if (PrivateScope.Privatize()) {
Alexey Bataev4a5bb772014-10-08 14:01:46 +000076 // Emit implicit barrier to synchronize threads and avoid data races.
77 auto Flags = static_cast<CGOpenMPRuntime::OpenMPLocationFlags>(
78 CGOpenMPRuntime::OMP_IDENT_KMPC |
79 CGOpenMPRuntime::OMP_IDENT_BARRIER_IMPL);
80 CGF.CGM.getOpenMPRuntime().EmitOMPBarrierCall(CGF, Directive.getLocStart(),
81 Flags);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000082 }
83 CGCapturedStmtInfo::EmitBody(CGF, S);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000084}
85
Alexey Bataev9959db52014-05-06 10:08:46 +000086CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
87 : CGM(CGM), DefaultOpenMPPSource(nullptr) {
88 IdentTy = llvm::StructType::create(
89 "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
90 CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
Alexander Musmanfdfa8552014-09-11 08:10:57 +000091 CGM.Int8PtrTy /* psource */, nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +000092 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
Alexey Bataev23b69422014-06-18 07:08:49 +000093 llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
94 llvm::PointerType::getUnqual(CGM.Int32Ty)};
Alexey Bataev9959db52014-05-06 10:08:46 +000095 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000096 KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
Alexey Bataev9959db52014-05-06 10:08:46 +000097}
98
99llvm::Value *
Alexey Bataev18095712014-10-10 12:19:54 +0000100CGOpenMPRuntime::EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D,
101 const VarDecl *ThreadIDVar) {
102 const CapturedStmt *CS = cast<CapturedStmt>(D.getAssociatedStmt());
103 CodeGenFunction CGF(CGM, true);
104 CGOpenMPRegionInfo CGInfo(D, *CS, ThreadIDVar);
105 CGF.CapturedStmtInfo = &CGInfo;
106 return CGF.GenerateCapturedStmtFunction(*CS);
107}
108
109llvm::Value *
Alexey Bataev9959db52014-05-06 10:08:46 +0000110CGOpenMPRuntime::GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags) {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000111 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +0000112 if (!Entry) {
113 if (!DefaultOpenMPPSource) {
114 // Initialize default location for psource field of ident_t structure of
115 // all ident_t objects. Format is ";file;function;line;column;;".
116 // Taken from
117 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
118 DefaultOpenMPPSource =
119 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;");
120 DefaultOpenMPPSource =
121 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
122 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000123 auto DefaultOpenMPLocation = new llvm::GlobalVariable(
124 CGM.getModule(), IdentTy, /*isConstant*/ true,
125 llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +0000126 DefaultOpenMPLocation->setUnnamedAddr(true);
Alexey Bataev9959db52014-05-06 10:08:46 +0000127
128 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true);
Alexey Bataev23b69422014-06-18 07:08:49 +0000129 llvm::Constant *Values[] = {Zero,
130 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
131 Zero, Zero, DefaultOpenMPPSource};
Alexey Bataev9959db52014-05-06 10:08:46 +0000132 llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values);
133 DefaultOpenMPLocation->setInitializer(Init);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000134 OpenMPDefaultLocMap[Flags] = DefaultOpenMPLocation;
Alexey Bataev9959db52014-05-06 10:08:46 +0000135 return DefaultOpenMPLocation;
136 }
137 return Entry;
138}
139
140llvm::Value *CGOpenMPRuntime::EmitOpenMPUpdateLocation(
141 CodeGenFunction &CGF, SourceLocation Loc, OpenMPLocationFlags Flags) {
142 // If no debug info is generated - return global default location.
143 if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo ||
144 Loc.isInvalid())
145 return GetOrCreateDefaultOpenMPLocation(Flags);
146
147 assert(CGF.CurFn && "No function in current CodeGenFunction.");
148
Alexey Bataev9959db52014-05-06 10:08:46 +0000149 llvm::Value *LocValue = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000150 OpenMPLocThreadIDMapTy::iterator I = OpenMPLocThreadIDMap.find(CGF.CurFn);
151 if (I != OpenMPLocThreadIDMap.end()) {
152 LocValue = I->second.DebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +0000153 } else {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000154 // Generate "ident_t .kmpc_loc.addr;"
155 llvm::AllocaInst *AI = CGF.CreateTempAlloca(IdentTy, ".kmpc_loc.addr");
Alexey Bataev9959db52014-05-06 10:08:46 +0000156 AI->setAlignment(CGM.getDataLayout().getPrefTypeAlignment(IdentTy));
Alexey Bataev18095712014-10-10 12:19:54 +0000157 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
158 Elem.second.DebugLoc = AI;
Alexey Bataev9959db52014-05-06 10:08:46 +0000159 LocValue = AI;
160
161 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
162 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
163 CGF.Builder.CreateMemCpy(LocValue, GetOrCreateDefaultOpenMPLocation(Flags),
164 llvm::ConstantExpr::getSizeOf(IdentTy),
165 CGM.PointerAlignInBytes);
166 }
167
168 // char **psource = &.kmpc_loc_<flags>.addr.psource;
169 llvm::Value *PSource =
170 CGF.Builder.CreateConstInBoundsGEP2_32(LocValue, 0, IdentField_PSource);
171
Alexey Bataevf002aca2014-05-30 05:48:40 +0000172 auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
173 if (OMPDebugLoc == nullptr) {
174 SmallString<128> Buffer2;
175 llvm::raw_svector_ostream OS2(Buffer2);
176 // Build debug location
177 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
178 OS2 << ";" << PLoc.getFilename() << ";";
179 if (const FunctionDecl *FD =
180 dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
181 OS2 << FD->getQualifiedNameAsString();
182 }
183 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
184 OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
185 OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +0000186 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000187 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
Alexey Bataevf002aca2014-05-30 05:48:40 +0000188 CGF.Builder.CreateStore(OMPDebugLoc, PSource);
189
Alexey Bataev9959db52014-05-06 10:08:46 +0000190 return LocValue;
191}
192
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000193llvm::Value *CGOpenMPRuntime::GetOpenMPThreadID(CodeGenFunction &CGF,
194 SourceLocation Loc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000195 assert(CGF.CurFn && "No function in current CodeGenFunction.");
196
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000197 llvm::Value *ThreadID = nullptr;
Alexey Bataev18095712014-10-10 12:19:54 +0000198 // Check whether we've already cached a load of the thread id in this
199 // function.
200 OpenMPLocThreadIDMapTy::iterator I = OpenMPLocThreadIDMap.find(CGF.CurFn);
201 if (I != OpenMPLocThreadIDMap.end()) {
202 ThreadID = I->second.ThreadID;
203 } else if (auto OMPRegionInfo =
204 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
205 // Check if this an outlined function with thread id passed as argument.
206 auto ThreadIDVar = OMPRegionInfo->getThreadIDVariable();
207 auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF);
208 auto RVal = CGF.EmitLoadOfLValue(LVal, Loc);
209 LVal = CGF.MakeNaturalAlignAddrLValue(RVal.getScalarVal(),
210 ThreadIDVar->getType());
211 ThreadID = CGF.EmitLoadOfLValue(LVal, Loc).getScalarVal();
212 // If value loaded in entry block, cache it and use it everywhere in
213 // function.
214 if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
215 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
216 Elem.second.ThreadID = ThreadID;
Alexey Bataevd6c57552014-07-25 07:55:17 +0000217 }
Alexey Bataev18095712014-10-10 12:19:54 +0000218 } else {
219 // This is not an outlined function region - need to call __kmpc_int32
220 // kmpc_global_thread_num(ident_t *loc).
221 // Generate thread id value and cache this value for use across the
222 // function.
223 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
224 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
225 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc)};
226 ThreadID = CGF.EmitRuntimeCall(
227 CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num), Args);
228 auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
229 Elem.second.ThreadID = ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +0000230 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000231 return ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +0000232}
233
234void CGOpenMPRuntime::FunctionFinished(CodeGenFunction &CGF) {
235 assert(CGF.CurFn && "No function in current CodeGenFunction.");
Benjamin Kramerad8e0792014-10-10 15:32:48 +0000236 OpenMPLocThreadIDMap.erase(CGF.CurFn);
Alexey Bataev9959db52014-05-06 10:08:46 +0000237}
238
239llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
240 return llvm::PointerType::getUnqual(IdentTy);
241}
242
243llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
244 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
245}
246
247llvm::Constant *
248CGOpenMPRuntime::CreateRuntimeFunction(OpenMPRTLFunction Function) {
249 llvm::Constant *RTLFn = nullptr;
250 switch (Function) {
251 case OMPRTL__kmpc_fork_call: {
252 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
253 // microtask, ...);
Alexey Bataev23b69422014-06-18 07:08:49 +0000254 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
255 getKmpc_MicroPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000256 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +0000257 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ true);
Alexey Bataev9959db52014-05-06 10:08:46 +0000258 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
259 break;
260 }
261 case OMPRTL__kmpc_global_thread_num: {
262 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
Alexey Bataev23b69422014-06-18 07:08:49 +0000263 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000264 llvm::FunctionType *FnTy =
Alexey Bataevd74d0602014-10-13 06:02:40 +0000265 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
Alexey Bataev9959db52014-05-06 10:08:46 +0000266 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
267 break;
268 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000269 case OMPRTL__kmpc_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000270 // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
271 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000272 llvm::Type *TypeParams[] = {
273 getIdentTyPointerTy(), CGM.Int32Ty,
274 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
275 llvm::FunctionType *FnTy =
276 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
277 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
278 break;
279 }
280 case OMPRTL__kmpc_end_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000281 // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
282 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000283 llvm::Type *TypeParams[] = {
284 getIdentTyPointerTy(), CGM.Int32Ty,
285 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
286 llvm::FunctionType *FnTy =
287 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
288 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
289 break;
290 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000291 case OMPRTL__kmpc_barrier: {
292 // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
293 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
294 llvm::FunctionType *FnTy =
295 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
296 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier");
297 break;
298 }
Alexey Bataevd74d0602014-10-13 06:02:40 +0000299 case OMPRTL__kmpc_serialized_parallel: {
300 // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
301 // global_tid);
302 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
303 llvm::FunctionType *FnTy =
304 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
305 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
306 break;
307 }
308 case OMPRTL__kmpc_end_serialized_parallel: {
309 // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
310 // global_tid);
311 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
312 llvm::FunctionType *FnTy =
313 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
314 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
315 break;
316 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000317 }
318 return RTLFn;
319}
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000320
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000321void CGOpenMPRuntime::EmitOMPParallelCall(CodeGenFunction &CGF,
322 SourceLocation Loc,
323 llvm::Value *OutlinedFn,
324 llvm::Value *CapturedStruct) {
325 // Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/)
326 llvm::Value *Args[] = {
327 EmitOpenMPUpdateLocation(CGF, Loc),
328 CGF.Builder.getInt32(1), // Number of arguments after 'microtask' argument
329 // (there is only one additional argument - 'context')
330 CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy()),
331 CGF.EmitCastToVoidPtr(CapturedStruct)};
332 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_fork_call);
333 CGF.EmitRuntimeCall(RTLFn, Args);
334}
335
Alexey Bataevd74d0602014-10-13 06:02:40 +0000336void CGOpenMPRuntime::EmitOMPSerialCall(CodeGenFunction &CGF,
337 SourceLocation Loc,
338 llvm::Value *OutlinedFn,
339 llvm::Value *CapturedStruct) {
340 auto ThreadID = GetOpenMPThreadID(CGF, Loc);
341 // Build calls:
342 // __kmpc_serialized_parallel(&Loc, GTid);
343 llvm::Value *SerArgs[] = {EmitOpenMPUpdateLocation(CGF, Loc), ThreadID};
344 auto RTLFn =
345 CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_serialized_parallel);
346 CGF.EmitRuntimeCall(RTLFn, SerArgs);
347
348 // OutlinedFn(&GTid, &zero, CapturedStruct);
349 auto ThreadIDAddr = EmitThreadIDAddress(CGF, Loc);
350 auto Int32Ty =
351 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
352 auto ZeroAddr = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".zero.addr");
353 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
354 llvm::Value *OutlinedFnArgs[] = {ThreadIDAddr, ZeroAddr, CapturedStruct};
355 CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
356
357 // __kmpc_end_serialized_parallel(&Loc, GTid);
358 llvm::Value *EndSerArgs[] = {EmitOpenMPUpdateLocation(CGF, Loc), ThreadID};
359 RTLFn = CreateRuntimeFunction(
360 CGOpenMPRuntime::OMPRTL__kmpc_end_serialized_parallel);
361 CGF.EmitRuntimeCall(RTLFn, EndSerArgs);
362}
363
364// If we’re inside an (outlined) parallel region, use the region info’s
365// thread-ID variable (it is passed in a first argument of the outlined function
366// as "kmp_int32 *gtid"). Otherwise, if we're not inside parallel region, but in
367// regular serial code region, get thread ID by calling kmp_int32
368// kmpc_global_thread_num(ident_t *loc), stash this thread ID in a temporary and
369// return the address of that temp.
370llvm::Value *CGOpenMPRuntime::EmitThreadIDAddress(CodeGenFunction &CGF,
371 SourceLocation Loc) {
372 if (auto OMPRegionInfo =
373 dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo))
374 return CGF.EmitLoadOfLValue(OMPRegionInfo->getThreadIDVariableLValue(CGF),
375 SourceLocation()).getScalarVal();
376 auto ThreadID = GetOpenMPThreadID(CGF, Loc);
377 auto Int32Ty =
378 CGF.getContext().getIntTypeForBitwidth(/*DestWidth*/ 32, /*Signed*/ true);
379 auto ThreadIDTemp = CGF.CreateMemTemp(Int32Ty, /*Name*/ ".threadid_temp.");
380 CGF.EmitStoreOfScalar(ThreadID,
381 CGF.MakeNaturalAlignAddrLValue(ThreadIDTemp, Int32Ty));
382
383 return ThreadIDTemp;
384}
385
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000386llvm::Value *CGOpenMPRuntime::GetCriticalRegionLock(StringRef CriticalName) {
387 SmallString<256> Buffer;
388 llvm::raw_svector_ostream Out(Buffer);
389 Out << ".gomp_critical_user_" << CriticalName << ".var";
390 auto RuntimeCriticalName = Out.str();
391 auto &Elem = CriticalRegionVarNames.GetOrCreateValue(RuntimeCriticalName);
392 if (Elem.getValue() != nullptr)
393 return Elem.getValue();
394
395 auto Lock = new llvm::GlobalVariable(
396 CGM.getModule(), KmpCriticalNameTy, /*IsConstant*/ false,
397 llvm::GlobalValue::CommonLinkage,
398 llvm::Constant::getNullValue(KmpCriticalNameTy), Elem.getKey());
399 Elem.setValue(Lock);
400 return Lock;
401}
402
403void CGOpenMPRuntime::EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
404 llvm::Value *RegionLock,
405 SourceLocation Loc) {
406 // Prepare other arguments and build a call to __kmpc_critical
407 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000408 GetOpenMPThreadID(CGF, Loc), RegionLock};
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000409 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_critical);
410 CGF.EmitRuntimeCall(RTLFn, Args);
411}
412
413void CGOpenMPRuntime::EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
414 llvm::Value *RegionLock,
415 SourceLocation Loc) {
Alexey Bataevf9472182014-09-22 12:32:31 +0000416 // Prepare other arguments and build a call to __kmpc_end_critical
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000417 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000418 GetOpenMPThreadID(CGF, Loc), RegionLock};
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000419 auto RTLFn =
420 CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_end_critical);
421 CGF.EmitRuntimeCall(RTLFn, Args);
422}
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000423
424void CGOpenMPRuntime::EmitOMPBarrierCall(CodeGenFunction &CGF,
425 SourceLocation Loc,
426 OpenMPLocationFlags Flags) {
427 // Build call __kmpc_barrier(loc, thread_id)
428 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc, Flags),
429 GetOpenMPThreadID(CGF, Loc)};
430 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_barrier);
431 CGF.EmitRuntimeCall(RTLFn, Args);
432}
433