blob: c439833cfe73e67afdafa8647eb9b90e0d26b470 [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"
16#include "clang/AST/Decl.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/GlobalValue.h"
20#include "llvm/IR/Value.h"
21#include "llvm/Support/raw_ostream.h"
Alexey Bataev23b69422014-06-18 07:08:49 +000022#include <cassert>
Alexey Bataev9959db52014-05-06 10:08:46 +000023
24using namespace clang;
25using namespace CodeGen;
26
Alexey Bataev4a5bb772014-10-08 14:01:46 +000027void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, Stmt *S) {
28 CodeGenFunction::OuterDeclMapTy OuterDeclMap;
29 CGF.EmitOMPFirstprivateClause(Directive, OuterDeclMap);
30 if (!OuterDeclMap.empty()) {
31 // Emit implicit barrier to synchronize threads and avoid data races.
32 auto Flags = static_cast<CGOpenMPRuntime::OpenMPLocationFlags>(
33 CGOpenMPRuntime::OMP_IDENT_KMPC |
34 CGOpenMPRuntime::OMP_IDENT_BARRIER_IMPL);
35 CGF.CGM.getOpenMPRuntime().EmitOMPBarrierCall(CGF, Directive.getLocStart(),
36 Flags);
37 // Remap captured variables to use their private copies in the outlined
38 // function.
39 for (auto I : OuterDeclMap) {
40 CGF.LocalDeclMap[I.first] = I.second;
41 }
42 }
43 CGCapturedStmtInfo::EmitBody(CGF, S);
44 // Clear mappings of captured private variables.
45 for (auto I : OuterDeclMap) {
46 CGF.LocalDeclMap.erase(I.first);
47 }
48}
49
Alexey Bataev9959db52014-05-06 10:08:46 +000050CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
51 : CGM(CGM), DefaultOpenMPPSource(nullptr) {
52 IdentTy = llvm::StructType::create(
53 "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
54 CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
Alexander Musmanfdfa8552014-09-11 08:10:57 +000055 CGM.Int8PtrTy /* psource */, nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +000056 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
Alexey Bataev23b69422014-06-18 07:08:49 +000057 llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty),
58 llvm::PointerType::getUnqual(CGM.Int32Ty)};
Alexey Bataev9959db52014-05-06 10:08:46 +000059 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +000060 KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8);
Alexey Bataev9959db52014-05-06 10:08:46 +000061}
62
63llvm::Value *
64CGOpenMPRuntime::GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags) {
Alexey Bataev15007ba2014-05-07 06:18:01 +000065 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
Alexey Bataev9959db52014-05-06 10:08:46 +000066 if (!Entry) {
67 if (!DefaultOpenMPPSource) {
68 // Initialize default location for psource field of ident_t structure of
69 // all ident_t objects. Format is ";file;function;line;column;;".
70 // Taken from
71 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
72 DefaultOpenMPPSource =
73 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;");
74 DefaultOpenMPPSource =
75 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
76 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +000077 auto DefaultOpenMPLocation = new llvm::GlobalVariable(
78 CGM.getModule(), IdentTy, /*isConstant*/ true,
79 llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr);
Alexey Bataev9959db52014-05-06 10:08:46 +000080 DefaultOpenMPLocation->setUnnamedAddr(true);
Alexey Bataev9959db52014-05-06 10:08:46 +000081
82 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true);
Alexey Bataev23b69422014-06-18 07:08:49 +000083 llvm::Constant *Values[] = {Zero,
84 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
85 Zero, Zero, DefaultOpenMPPSource};
Alexey Bataev9959db52014-05-06 10:08:46 +000086 llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values);
87 DefaultOpenMPLocation->setInitializer(Init);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000088 OpenMPDefaultLocMap[Flags] = DefaultOpenMPLocation;
Alexey Bataev9959db52014-05-06 10:08:46 +000089 return DefaultOpenMPLocation;
90 }
91 return Entry;
92}
93
94llvm::Value *CGOpenMPRuntime::EmitOpenMPUpdateLocation(
95 CodeGenFunction &CGF, SourceLocation Loc, OpenMPLocationFlags Flags) {
96 // If no debug info is generated - return global default location.
97 if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo ||
98 Loc.isInvalid())
99 return GetOrCreateDefaultOpenMPLocation(Flags);
100
101 assert(CGF.CurFn && "No function in current CodeGenFunction.");
102
Alexey Bataev9959db52014-05-06 10:08:46 +0000103 llvm::Value *LocValue = nullptr;
104 OpenMPLocMapTy::iterator I = OpenMPLocMap.find(CGF.CurFn);
105 if (I != OpenMPLocMap.end()) {
106 LocValue = I->second;
107 } else {
Alexey Bataev15007ba2014-05-07 06:18:01 +0000108 // Generate "ident_t .kmpc_loc.addr;"
109 llvm::AllocaInst *AI = CGF.CreateTempAlloca(IdentTy, ".kmpc_loc.addr");
Alexey Bataev9959db52014-05-06 10:08:46 +0000110 AI->setAlignment(CGM.getDataLayout().getPrefTypeAlignment(IdentTy));
111 OpenMPLocMap[CGF.CurFn] = AI;
112 LocValue = AI;
113
114 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
115 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
116 CGF.Builder.CreateMemCpy(LocValue, GetOrCreateDefaultOpenMPLocation(Flags),
117 llvm::ConstantExpr::getSizeOf(IdentTy),
118 CGM.PointerAlignInBytes);
119 }
120
121 // char **psource = &.kmpc_loc_<flags>.addr.psource;
122 llvm::Value *PSource =
123 CGF.Builder.CreateConstInBoundsGEP2_32(LocValue, 0, IdentField_PSource);
124
Alexey Bataevf002aca2014-05-30 05:48:40 +0000125 auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding());
126 if (OMPDebugLoc == nullptr) {
127 SmallString<128> Buffer2;
128 llvm::raw_svector_ostream OS2(Buffer2);
129 // Build debug location
130 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
131 OS2 << ";" << PLoc.getFilename() << ";";
132 if (const FunctionDecl *FD =
133 dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
134 OS2 << FD->getQualifiedNameAsString();
135 }
136 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
137 OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str());
138 OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc;
Alexey Bataev9959db52014-05-06 10:08:46 +0000139 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000140 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
Alexey Bataevf002aca2014-05-30 05:48:40 +0000141 CGF.Builder.CreateStore(OMPDebugLoc, PSource);
142
Alexey Bataev9959db52014-05-06 10:08:46 +0000143 return LocValue;
144}
145
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000146llvm::Value *CGOpenMPRuntime::GetOpenMPThreadID(CodeGenFunction &CGF,
147 SourceLocation Loc) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000148 assert(CGF.CurFn && "No function in current CodeGenFunction.");
149
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000150 llvm::Value *ThreadID = nullptr;
151 OpenMPThreadIDMapTy::iterator I = OpenMPThreadIDMap.find(CGF.CurFn);
152 if (I != OpenMPThreadIDMap.end()) {
153 ThreadID = I->second;
Alexey Bataev9959db52014-05-06 10:08:46 +0000154 } else {
Alexey Bataevd6c57552014-07-25 07:55:17 +0000155 // Check if current function is a function which has first parameter
156 // with type int32 and name ".global_tid.".
157 if (!CGF.CurFn->arg_empty() &&
158 CGF.CurFn->arg_begin()->getType()->isPointerTy() &&
159 CGF.CurFn->arg_begin()
160 ->getType()
161 ->getPointerElementType()
162 ->isIntegerTy() &&
163 CGF.CurFn->arg_begin()
164 ->getType()
165 ->getPointerElementType()
166 ->getIntegerBitWidth() == 32 &&
167 CGF.CurFn->arg_begin()->hasName() &&
168 CGF.CurFn->arg_begin()->getName() == ".global_tid.") {
169 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
170 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000171 ThreadID = CGF.Builder.CreateLoad(CGF.CurFn->arg_begin());
Alexey Bataevd6c57552014-07-25 07:55:17 +0000172 } else {
173 // Generate "int32 .kmpc_global_thread_num.addr;"
174 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
175 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
176 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc)};
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000177 ThreadID = CGF.EmitRuntimeCall(
Alexey Bataevd6c57552014-07-25 07:55:17 +0000178 CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num), Args);
179 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000180 OpenMPThreadIDMap[CGF.CurFn] = ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +0000181 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000182 return ThreadID;
Alexey Bataev9959db52014-05-06 10:08:46 +0000183}
184
185void CGOpenMPRuntime::FunctionFinished(CodeGenFunction &CGF) {
186 assert(CGF.CurFn && "No function in current CodeGenFunction.");
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000187 if (OpenMPThreadIDMap.count(CGF.CurFn))
188 OpenMPThreadIDMap.erase(CGF.CurFn);
Alexey Bataev9959db52014-05-06 10:08:46 +0000189 if (OpenMPLocMap.count(CGF.CurFn))
190 OpenMPLocMap.erase(CGF.CurFn);
191}
192
193llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
194 return llvm::PointerType::getUnqual(IdentTy);
195}
196
197llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
198 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
199}
200
201llvm::Constant *
202CGOpenMPRuntime::CreateRuntimeFunction(OpenMPRTLFunction Function) {
203 llvm::Constant *RTLFn = nullptr;
204 switch (Function) {
205 case OMPRTL__kmpc_fork_call: {
206 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
207 // microtask, ...);
Alexey Bataev23b69422014-06-18 07:08:49 +0000208 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty,
209 getKmpc_MicroPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000210 llvm::FunctionType *FnTy =
211 llvm::FunctionType::get(CGM.VoidTy, TypeParams, true);
212 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
213 break;
214 }
215 case OMPRTL__kmpc_global_thread_num: {
216 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
Alexey Bataev23b69422014-06-18 07:08:49 +0000217 llvm::Type *TypeParams[] = {getIdentTyPointerTy()};
Alexey Bataev9959db52014-05-06 10:08:46 +0000218 llvm::FunctionType *FnTy =
219 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, false);
220 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
221 break;
222 }
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000223 case OMPRTL__kmpc_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000224 // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid,
225 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000226 llvm::Type *TypeParams[] = {
227 getIdentTyPointerTy(), CGM.Int32Ty,
228 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
229 llvm::FunctionType *FnTy =
230 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
231 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_critical");
232 break;
233 }
234 case OMPRTL__kmpc_end_critical: {
Alexey Bataevf9472182014-09-22 12:32:31 +0000235 // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid,
236 // kmp_critical_name *crit);
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000237 llvm::Type *TypeParams[] = {
238 getIdentTyPointerTy(), CGM.Int32Ty,
239 llvm::PointerType::getUnqual(KmpCriticalNameTy)};
240 llvm::FunctionType *FnTy =
241 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
242 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_critical");
243 break;
244 }
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000245 case OMPRTL__kmpc_barrier: {
246 // Build void __kmpc_barrier(ident_t *loc, kmp_int32 global_tid);
247 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
248 llvm::FunctionType *FnTy =
249 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
250 RTLFn = CGM.CreateRuntimeFunction(FnTy, /*Name*/ "__kmpc_barrier");
251 break;
252 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000253 }
254 return RTLFn;
255}
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000256
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000257void CGOpenMPRuntime::EmitOMPParallelCall(CodeGenFunction &CGF,
258 SourceLocation Loc,
259 llvm::Value *OutlinedFn,
260 llvm::Value *CapturedStruct) {
261 // Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/)
262 llvm::Value *Args[] = {
263 EmitOpenMPUpdateLocation(CGF, Loc),
264 CGF.Builder.getInt32(1), // Number of arguments after 'microtask' argument
265 // (there is only one additional argument - 'context')
266 CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy()),
267 CGF.EmitCastToVoidPtr(CapturedStruct)};
268 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_fork_call);
269 CGF.EmitRuntimeCall(RTLFn, Args);
270}
271
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000272llvm::Value *CGOpenMPRuntime::GetCriticalRegionLock(StringRef CriticalName) {
273 SmallString<256> Buffer;
274 llvm::raw_svector_ostream Out(Buffer);
275 Out << ".gomp_critical_user_" << CriticalName << ".var";
276 auto RuntimeCriticalName = Out.str();
277 auto &Elem = CriticalRegionVarNames.GetOrCreateValue(RuntimeCriticalName);
278 if (Elem.getValue() != nullptr)
279 return Elem.getValue();
280
281 auto Lock = new llvm::GlobalVariable(
282 CGM.getModule(), KmpCriticalNameTy, /*IsConstant*/ false,
283 llvm::GlobalValue::CommonLinkage,
284 llvm::Constant::getNullValue(KmpCriticalNameTy), Elem.getKey());
285 Elem.setValue(Lock);
286 return Lock;
287}
288
289void CGOpenMPRuntime::EmitOMPCriticalRegionStart(CodeGenFunction &CGF,
290 llvm::Value *RegionLock,
291 SourceLocation Loc) {
292 // Prepare other arguments and build a call to __kmpc_critical
293 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000294 GetOpenMPThreadID(CGF, Loc), RegionLock};
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000295 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_critical);
296 CGF.EmitRuntimeCall(RTLFn, Args);
297}
298
299void CGOpenMPRuntime::EmitOMPCriticalRegionEnd(CodeGenFunction &CGF,
300 llvm::Value *RegionLock,
301 SourceLocation Loc) {
Alexey Bataevf9472182014-09-22 12:32:31 +0000302 // Prepare other arguments and build a call to __kmpc_end_critical
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000303 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc),
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000304 GetOpenMPThreadID(CGF, Loc), RegionLock};
Alexey Bataev3a3bf0b2014-09-22 10:01:53 +0000305 auto RTLFn =
306 CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_end_critical);
307 CGF.EmitRuntimeCall(RTLFn, Args);
308}
Alexey Bataev4a5bb772014-10-08 14:01:46 +0000309
310void CGOpenMPRuntime::EmitOMPBarrierCall(CodeGenFunction &CGF,
311 SourceLocation Loc,
312 OpenMPLocationFlags Flags) {
313 // Build call __kmpc_barrier(loc, thread_id)
314 llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc, Flags),
315 GetOpenMPThreadID(CGF, Loc)};
316 auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_barrier);
317 CGF.EmitRuntimeCall(RTLFn, Args);
318}
319