blob: da86e2b52d0d9cf61e98a229a911e42e8d5e3c9f [file] [log] [blame]
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001//===----- 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"
22#include <assert.h>
23
24using namespace clang;
25using namespace CodeGen;
26
27CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM)
28 : CGM(CGM), DefaultOpenMPPSource(nullptr) {
29 IdentTy = llvm::StructType::create(
30 "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */,
31 CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */,
32 CGM.Int8PtrTy /* psource */, NULL);
33 // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...)
34 llvm::Type *MicroParams[] = { llvm::PointerType::getUnqual(CGM.Int32Ty),
35 llvm::PointerType::getUnqual(CGM.Int32Ty) };
36 Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true);
37}
38
39llvm::Value *
40CGOpenMPRuntime::GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags) {
41 llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags);
42 if (!Entry) {
43 if (!DefaultOpenMPPSource) {
44 // Initialize default location for psource field of ident_t structure of
45 // all ident_t objects. Format is ";file;function;line;column;;".
46 // Taken from
47 // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c
48 DefaultOpenMPPSource =
49 CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;");
50 DefaultOpenMPPSource =
51 llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy);
52 }
53 llvm::GlobalVariable *DefaultOpenMPLocation = cast<llvm::GlobalVariable>(
54 CGM.CreateRuntimeVariable(IdentTy, ".kmpc_default_loc.addr"));
55 DefaultOpenMPLocation->setUnnamedAddr(true);
56 DefaultOpenMPLocation->setConstant(true);
57 DefaultOpenMPLocation->setLinkage(llvm::GlobalValue::PrivateLinkage);
58
59 llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true);
60 llvm::Constant *Values[] = { Zero,
61 llvm::ConstantInt::get(CGM.Int32Ty, Flags),
62 Zero, Zero, DefaultOpenMPPSource };
63 llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values);
64 DefaultOpenMPLocation->setInitializer(Init);
65 return DefaultOpenMPLocation;
66 }
67 return Entry;
68}
69
70llvm::Value *CGOpenMPRuntime::EmitOpenMPUpdateLocation(
71 CodeGenFunction &CGF, SourceLocation Loc, OpenMPLocationFlags Flags) {
72 // If no debug info is generated - return global default location.
73 if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo ||
74 Loc.isInvalid())
75 return GetOrCreateDefaultOpenMPLocation(Flags);
76
77 assert(CGF.CurFn && "No function in current CodeGenFunction.");
78
79 llvm::Value *LocValue = nullptr;
80 OpenMPLocMapTy::iterator I = OpenMPLocMap.find(CGF.CurFn);
81 if (I != OpenMPLocMap.end()) {
82 LocValue = I->second;
83 } else {
84 // Generate "ident_t .kmpc_loc.addr;"
85 llvm::AllocaInst *AI = CGF.CreateTempAlloca(IdentTy, ".kmpc_loc.addr");
86 AI->setAlignment(CGM.getDataLayout().getPrefTypeAlignment(IdentTy));
87 OpenMPLocMap[CGF.CurFn] = AI;
88 LocValue = AI;
89
90 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
91 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
92 CGF.Builder.CreateMemCpy(LocValue, GetOrCreateDefaultOpenMPLocation(Flags),
93 llvm::ConstantExpr::getSizeOf(IdentTy),
94 CGM.PointerAlignInBytes);
95 }
96
97 // char **psource = &.kmpc_loc_<flags>.addr.psource;
98 llvm::Value *PSource =
99 CGF.Builder.CreateConstInBoundsGEP2_32(LocValue, 0, IdentField_PSource);
100
101 SmallString<128> Buffer2;
102 llvm::raw_svector_ostream OS2(Buffer2);
103 // Build debug location
104 PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc);
105 OS2 << ";" << PLoc.getFilename() << ";";
106 if (const FunctionDecl *FD =
107 dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) {
108 OS2 << FD->getQualifiedNameAsString();
109 }
110 OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;";
111 // *psource = ";<File>;<Function>;<Line>;<Column>;;";
112 CGF.Builder.CreateStore(CGF.Builder.CreateGlobalStringPtr(OS2.str()),
113 PSource);
114 return LocValue;
115}
116
117llvm::Value *CGOpenMPRuntime::GetOpenMPGlobalThreadNum(CodeGenFunction &CGF,
118 SourceLocation Loc) {
119 assert(CGF.CurFn && "No function in current CodeGenFunction.");
120
121 llvm::Value *GTid = nullptr;
122 OpenMPGtidMapTy::iterator I = OpenMPGtidMap.find(CGF.CurFn);
123 if (I != OpenMPGtidMap.end()) {
124 GTid = I->second;
125 } else {
126 // Generate "int32 .kmpc_global_thread_num.addr;"
127 CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
128 CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
129 llvm::Value *Args[] = { EmitOpenMPUpdateLocation(CGF, Loc) };
130 GTid = CGF.EmitRuntimeCall(
131 CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num), Args);
132 OpenMPGtidMap[CGF.CurFn] = GTid;
133 }
134 return GTid;
135}
136
137void CGOpenMPRuntime::FunctionFinished(CodeGenFunction &CGF) {
138 assert(CGF.CurFn && "No function in current CodeGenFunction.");
139 if (OpenMPGtidMap.count(CGF.CurFn))
140 OpenMPGtidMap.erase(CGF.CurFn);
141 if (OpenMPLocMap.count(CGF.CurFn))
142 OpenMPLocMap.erase(CGF.CurFn);
143}
144
145llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() {
146 return llvm::PointerType::getUnqual(IdentTy);
147}
148
149llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() {
150 return llvm::PointerType::getUnqual(Kmpc_MicroTy);
151}
152
153llvm::Constant *
154CGOpenMPRuntime::CreateRuntimeFunction(OpenMPRTLFunction Function) {
155 llvm::Constant *RTLFn = nullptr;
156 switch (Function) {
157 case OMPRTL__kmpc_fork_call: {
158 // Build void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
159 // microtask, ...);
160 llvm::Type *TypeParams[] = { getIdentTyPointerTy(), CGM.Int32Ty,
161 getKmpc_MicroPointerTy() };
162 llvm::FunctionType *FnTy =
163 llvm::FunctionType::get(CGM.VoidTy, TypeParams, true);
164 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_fork_call");
165 break;
166 }
167 case OMPRTL__kmpc_global_thread_num: {
168 // Build kmp_int32 __kmpc_global_thread_num(ident_t *loc);
169 llvm::Type *TypeParams[] = { getIdentTyPointerTy() };
170 llvm::FunctionType *FnTy =
171 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, false);
172 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num");
173 break;
174 }
175 }
176 return RTLFn;
177}