Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===----- 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 Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 17 | #include "clang/AST/Decl.h" |
| 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/GlobalValue.h" |
| 21 | #include "llvm/IR/Value.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 23 | #include <cassert> |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame^] | 28 | namespace { |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 29 | /// \brief API for captured statement code generation in OpenMP constructs. |
| 30 | class CGOpenMPRegionInfo : public CodeGenFunction::CGCapturedStmtInfo { |
| 31 | public: |
| 32 | CGOpenMPRegionInfo(const OMPExecutableDirective &D, const CapturedStmt &CS, |
| 33 | const VarDecl *ThreadIDVar) |
| 34 | : CGCapturedStmtInfo(CS, CR_OpenMP), ThreadIDVar(ThreadIDVar), |
| 35 | Directive(D) { |
| 36 | assert(ThreadIDVar != nullptr && "No ThreadID in OpenMP region."); |
| 37 | } |
| 38 | |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 39 | /// \brief Gets a variable or parameter for storing global thread id |
| 40 | /// inside OpenMP construct. |
| 41 | const VarDecl *getThreadIDVariable() const { return ThreadIDVar; } |
| 42 | |
| 43 | /// \brief Gets an LValue for the current ThreadID variable. |
| 44 | LValue getThreadIDVariableLValue(CodeGenFunction &CGF); |
| 45 | |
| 46 | static bool classof(const CGCapturedStmtInfo *Info) { |
| 47 | return Info->getKind() == CR_OpenMP; |
| 48 | } |
| 49 | |
| 50 | /// \brief Emit the captured statement body. |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame^] | 51 | void EmitBody(CodeGenFunction &CGF, Stmt *S) override; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 52 | |
| 53 | /// \brief Get the name of the capture helper. |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame^] | 54 | StringRef getHelperName() const override { return ".omp_outlined."; } |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | /// \brief A variable or parameter storing global thread id for OpenMP |
| 58 | /// constructs. |
| 59 | const VarDecl *ThreadIDVar; |
| 60 | /// \brief OpenMP executable directive associated with the region. |
| 61 | const OMPExecutableDirective &Directive; |
| 62 | }; |
Benjamin Kramer | c52193f | 2014-10-10 13:57:57 +0000 | [diff] [blame^] | 63 | } // namespace |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 64 | |
| 65 | LValue CGOpenMPRegionInfo::getThreadIDVariableLValue(CodeGenFunction &CGF) { |
| 66 | return CGF.MakeNaturalAlignAddrLValue( |
| 67 | CGF.GetAddrOfLocalVar(ThreadIDVar), |
| 68 | CGF.getContext().getPointerType(ThreadIDVar->getType())); |
| 69 | } |
| 70 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 71 | void CGOpenMPRegionInfo::EmitBody(CodeGenFunction &CGF, Stmt *S) { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 72 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 73 | CGF.EmitOMPFirstprivateClause(Directive, PrivateScope); |
| 74 | if (PrivateScope.Privatize()) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 75 | // Emit implicit barrier to synchronize threads and avoid data races. |
| 76 | auto Flags = static_cast<CGOpenMPRuntime::OpenMPLocationFlags>( |
| 77 | CGOpenMPRuntime::OMP_IDENT_KMPC | |
| 78 | CGOpenMPRuntime::OMP_IDENT_BARRIER_IMPL); |
| 79 | CGF.CGM.getOpenMPRuntime().EmitOMPBarrierCall(CGF, Directive.getLocStart(), |
| 80 | Flags); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 81 | } |
| 82 | CGCapturedStmtInfo::EmitBody(CGF, S); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 85 | CGOpenMPRuntime::CGOpenMPRuntime(CodeGenModule &CGM) |
| 86 | : CGM(CGM), DefaultOpenMPPSource(nullptr) { |
| 87 | IdentTy = llvm::StructType::create( |
| 88 | "ident_t", CGM.Int32Ty /* reserved_1 */, CGM.Int32Ty /* flags */, |
| 89 | CGM.Int32Ty /* reserved_2 */, CGM.Int32Ty /* reserved_3 */, |
Alexander Musman | fdfa855 | 2014-09-11 08:10:57 +0000 | [diff] [blame] | 90 | CGM.Int8PtrTy /* psource */, nullptr); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 91 | // Build void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid,...) |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 92 | llvm::Type *MicroParams[] = {llvm::PointerType::getUnqual(CGM.Int32Ty), |
| 93 | llvm::PointerType::getUnqual(CGM.Int32Ty)}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 94 | Kmpc_MicroTy = llvm::FunctionType::get(CGM.VoidTy, MicroParams, true); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 95 | KmpCriticalNameTy = llvm::ArrayType::get(CGM.Int32Ty, /*NumElements*/ 8); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | llvm::Value * |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 99 | CGOpenMPRuntime::EmitOpenMPOutlinedFunction(const OMPExecutableDirective &D, |
| 100 | const VarDecl *ThreadIDVar) { |
| 101 | const CapturedStmt *CS = cast<CapturedStmt>(D.getAssociatedStmt()); |
| 102 | CodeGenFunction CGF(CGM, true); |
| 103 | CGOpenMPRegionInfo CGInfo(D, *CS, ThreadIDVar); |
| 104 | CGF.CapturedStmtInfo = &CGInfo; |
| 105 | return CGF.GenerateCapturedStmtFunction(*CS); |
| 106 | } |
| 107 | |
| 108 | llvm::Value * |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 109 | CGOpenMPRuntime::GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags) { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 110 | llvm::Value *Entry = OpenMPDefaultLocMap.lookup(Flags); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 111 | if (!Entry) { |
| 112 | if (!DefaultOpenMPPSource) { |
| 113 | // Initialize default location for psource field of ident_t structure of |
| 114 | // all ident_t objects. Format is ";file;function;line;column;;". |
| 115 | // Taken from |
| 116 | // http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp_str.c |
| 117 | DefaultOpenMPPSource = |
| 118 | CGM.GetAddrOfConstantCString(";unknown;unknown;0;0;;"); |
| 119 | DefaultOpenMPPSource = |
| 120 | llvm::ConstantExpr::getBitCast(DefaultOpenMPPSource, CGM.Int8PtrTy); |
| 121 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 122 | auto DefaultOpenMPLocation = new llvm::GlobalVariable( |
| 123 | CGM.getModule(), IdentTy, /*isConstant*/ true, |
| 124 | llvm::GlobalValue::PrivateLinkage, /*Initializer*/ nullptr); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 125 | DefaultOpenMPLocation->setUnnamedAddr(true); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 126 | |
| 127 | llvm::Constant *Zero = llvm::ConstantInt::get(CGM.Int32Ty, 0, true); |
Alexey Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 128 | llvm::Constant *Values[] = {Zero, |
| 129 | llvm::ConstantInt::get(CGM.Int32Ty, Flags), |
| 130 | Zero, Zero, DefaultOpenMPPSource}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 131 | llvm::Constant *Init = llvm::ConstantStruct::get(IdentTy, Values); |
| 132 | DefaultOpenMPLocation->setInitializer(Init); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 133 | OpenMPDefaultLocMap[Flags] = DefaultOpenMPLocation; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 134 | return DefaultOpenMPLocation; |
| 135 | } |
| 136 | return Entry; |
| 137 | } |
| 138 | |
| 139 | llvm::Value *CGOpenMPRuntime::EmitOpenMPUpdateLocation( |
| 140 | CodeGenFunction &CGF, SourceLocation Loc, OpenMPLocationFlags Flags) { |
| 141 | // If no debug info is generated - return global default location. |
| 142 | if (CGM.getCodeGenOpts().getDebugInfo() == CodeGenOptions::NoDebugInfo || |
| 143 | Loc.isInvalid()) |
| 144 | return GetOrCreateDefaultOpenMPLocation(Flags); |
| 145 | |
| 146 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 147 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 148 | llvm::Value *LocValue = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 149 | OpenMPLocThreadIDMapTy::iterator I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 150 | if (I != OpenMPLocThreadIDMap.end()) { |
| 151 | LocValue = I->second.DebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 152 | } else { |
Alexey Bataev | 15007ba | 2014-05-07 06:18:01 +0000 | [diff] [blame] | 153 | // Generate "ident_t .kmpc_loc.addr;" |
| 154 | llvm::AllocaInst *AI = CGF.CreateTempAlloca(IdentTy, ".kmpc_loc.addr"); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 155 | AI->setAlignment(CGM.getDataLayout().getPrefTypeAlignment(IdentTy)); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 156 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 157 | Elem.second.DebugLoc = AI; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 158 | LocValue = AI; |
| 159 | |
| 160 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 161 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
| 162 | CGF.Builder.CreateMemCpy(LocValue, GetOrCreateDefaultOpenMPLocation(Flags), |
| 163 | llvm::ConstantExpr::getSizeOf(IdentTy), |
| 164 | CGM.PointerAlignInBytes); |
| 165 | } |
| 166 | |
| 167 | // char **psource = &.kmpc_loc_<flags>.addr.psource; |
| 168 | llvm::Value *PSource = |
| 169 | CGF.Builder.CreateConstInBoundsGEP2_32(LocValue, 0, IdentField_PSource); |
| 170 | |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 171 | auto OMPDebugLoc = OpenMPDebugLocMap.lookup(Loc.getRawEncoding()); |
| 172 | if (OMPDebugLoc == nullptr) { |
| 173 | SmallString<128> Buffer2; |
| 174 | llvm::raw_svector_ostream OS2(Buffer2); |
| 175 | // Build debug location |
| 176 | PresumedLoc PLoc = CGF.getContext().getSourceManager().getPresumedLoc(Loc); |
| 177 | OS2 << ";" << PLoc.getFilename() << ";"; |
| 178 | if (const FunctionDecl *FD = |
| 179 | dyn_cast_or_null<FunctionDecl>(CGF.CurFuncDecl)) { |
| 180 | OS2 << FD->getQualifiedNameAsString(); |
| 181 | } |
| 182 | OS2 << ";" << PLoc.getLine() << ";" << PLoc.getColumn() << ";;"; |
| 183 | OMPDebugLoc = CGF.Builder.CreateGlobalStringPtr(OS2.str()); |
| 184 | OpenMPDebugLocMap[Loc.getRawEncoding()] = OMPDebugLoc; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 185 | } |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 186 | // *psource = ";<File>;<Function>;<Line>;<Column>;;"; |
Alexey Bataev | f002aca | 2014-05-30 05:48:40 +0000 | [diff] [blame] | 187 | CGF.Builder.CreateStore(OMPDebugLoc, PSource); |
| 188 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 189 | return LocValue; |
| 190 | } |
| 191 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 192 | llvm::Value *CGOpenMPRuntime::GetOpenMPThreadID(CodeGenFunction &CGF, |
| 193 | SourceLocation Loc) { |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 194 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
| 195 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 196 | llvm::Value *ThreadID = nullptr; |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 197 | // Check whether we've already cached a load of the thread id in this |
| 198 | // function. |
| 199 | OpenMPLocThreadIDMapTy::iterator I = OpenMPLocThreadIDMap.find(CGF.CurFn); |
| 200 | if (I != OpenMPLocThreadIDMap.end()) { |
| 201 | ThreadID = I->second.ThreadID; |
| 202 | } else if (auto OMPRegionInfo = |
| 203 | dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) { |
| 204 | // Check if this an outlined function with thread id passed as argument. |
| 205 | auto ThreadIDVar = OMPRegionInfo->getThreadIDVariable(); |
| 206 | auto LVal = OMPRegionInfo->getThreadIDVariableLValue(CGF); |
| 207 | auto RVal = CGF.EmitLoadOfLValue(LVal, Loc); |
| 208 | LVal = CGF.MakeNaturalAlignAddrLValue(RVal.getScalarVal(), |
| 209 | ThreadIDVar->getType()); |
| 210 | ThreadID = CGF.EmitLoadOfLValue(LVal, Loc).getScalarVal(); |
| 211 | // If value loaded in entry block, cache it and use it everywhere in |
| 212 | // function. |
| 213 | if (CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) { |
| 214 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 215 | Elem.second.ThreadID = ThreadID; |
Alexey Bataev | d6c5755 | 2014-07-25 07:55:17 +0000 | [diff] [blame] | 216 | } |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 217 | } else { |
| 218 | // This is not an outlined function region - need to call __kmpc_int32 |
| 219 | // kmpc_global_thread_num(ident_t *loc). |
| 220 | // Generate thread id value and cache this value for use across the |
| 221 | // function. |
| 222 | CGBuilderTy::InsertPointGuard IPG(CGF.Builder); |
| 223 | CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt); |
| 224 | llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc)}; |
| 225 | ThreadID = CGF.EmitRuntimeCall( |
| 226 | CreateRuntimeFunction(OMPRTL__kmpc_global_thread_num), Args); |
| 227 | auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn); |
| 228 | Elem.second.ThreadID = ThreadID; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 229 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 230 | return ThreadID; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | void CGOpenMPRuntime::FunctionFinished(CodeGenFunction &CGF) { |
| 234 | assert(CGF.CurFn && "No function in current CodeGenFunction."); |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 235 | if (OpenMPLocThreadIDMap.count(CGF.CurFn)) |
| 236 | OpenMPLocThreadIDMap.erase(CGF.CurFn); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | llvm::Type *CGOpenMPRuntime::getIdentTyPointerTy() { |
| 240 | return llvm::PointerType::getUnqual(IdentTy); |
| 241 | } |
| 242 | |
| 243 | llvm::Type *CGOpenMPRuntime::getKmpc_MicroPointerTy() { |
| 244 | return llvm::PointerType::getUnqual(Kmpc_MicroTy); |
| 245 | } |
| 246 | |
| 247 | llvm::Constant * |
| 248 | CGOpenMPRuntime::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 Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 254 | llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty, |
| 255 | getKmpc_MicroPointerTy()}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 256 | llvm::FunctionType *FnTy = |
| 257 | llvm::FunctionType::get(CGM.VoidTy, TypeParams, true); |
| 258 | 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 Bataev | 23b6942 | 2014-06-18 07:08:49 +0000 | [diff] [blame] | 263 | llvm::Type *TypeParams[] = {getIdentTyPointerTy()}; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 264 | llvm::FunctionType *FnTy = |
| 265 | llvm::FunctionType::get(CGM.Int32Ty, TypeParams, false); |
| 266 | RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_global_thread_num"); |
| 267 | break; |
| 268 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 269 | case OMPRTL__kmpc_critical: { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 270 | // Build void __kmpc_critical(ident_t *loc, kmp_int32 global_tid, |
| 271 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 272 | 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 Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 281 | // Build void __kmpc_end_critical(ident_t *loc, kmp_int32 global_tid, |
| 282 | // kmp_critical_name *crit); |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 283 | 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 Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 291 | 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 Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 299 | } |
| 300 | return RTLFn; |
| 301 | } |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 302 | |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 303 | void CGOpenMPRuntime::EmitOMPParallelCall(CodeGenFunction &CGF, |
| 304 | SourceLocation Loc, |
| 305 | llvm::Value *OutlinedFn, |
| 306 | llvm::Value *CapturedStruct) { |
| 307 | // Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/) |
| 308 | llvm::Value *Args[] = { |
| 309 | EmitOpenMPUpdateLocation(CGF, Loc), |
| 310 | CGF.Builder.getInt32(1), // Number of arguments after 'microtask' argument |
| 311 | // (there is only one additional argument - 'context') |
| 312 | CGF.Builder.CreateBitCast(OutlinedFn, getKmpc_MicroPointerTy()), |
| 313 | CGF.EmitCastToVoidPtr(CapturedStruct)}; |
| 314 | auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_fork_call); |
| 315 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 316 | } |
| 317 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 318 | llvm::Value *CGOpenMPRuntime::GetCriticalRegionLock(StringRef CriticalName) { |
| 319 | SmallString<256> Buffer; |
| 320 | llvm::raw_svector_ostream Out(Buffer); |
| 321 | Out << ".gomp_critical_user_" << CriticalName << ".var"; |
| 322 | auto RuntimeCriticalName = Out.str(); |
| 323 | auto &Elem = CriticalRegionVarNames.GetOrCreateValue(RuntimeCriticalName); |
| 324 | if (Elem.getValue() != nullptr) |
| 325 | return Elem.getValue(); |
| 326 | |
| 327 | auto Lock = new llvm::GlobalVariable( |
| 328 | CGM.getModule(), KmpCriticalNameTy, /*IsConstant*/ false, |
| 329 | llvm::GlobalValue::CommonLinkage, |
| 330 | llvm::Constant::getNullValue(KmpCriticalNameTy), Elem.getKey()); |
| 331 | Elem.setValue(Lock); |
| 332 | return Lock; |
| 333 | } |
| 334 | |
| 335 | void CGOpenMPRuntime::EmitOMPCriticalRegionStart(CodeGenFunction &CGF, |
| 336 | llvm::Value *RegionLock, |
| 337 | SourceLocation Loc) { |
| 338 | // Prepare other arguments and build a call to __kmpc_critical |
| 339 | llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc), |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 340 | GetOpenMPThreadID(CGF, Loc), RegionLock}; |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 341 | auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_critical); |
| 342 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 343 | } |
| 344 | |
| 345 | void CGOpenMPRuntime::EmitOMPCriticalRegionEnd(CodeGenFunction &CGF, |
| 346 | llvm::Value *RegionLock, |
| 347 | SourceLocation Loc) { |
Alexey Bataev | f947218 | 2014-09-22 12:32:31 +0000 | [diff] [blame] | 348 | // Prepare other arguments and build a call to __kmpc_end_critical |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 349 | llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc), |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 350 | GetOpenMPThreadID(CGF, Loc), RegionLock}; |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 351 | auto RTLFn = |
| 352 | CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_end_critical); |
| 353 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 354 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 355 | |
| 356 | void CGOpenMPRuntime::EmitOMPBarrierCall(CodeGenFunction &CGF, |
| 357 | SourceLocation Loc, |
| 358 | OpenMPLocationFlags Flags) { |
| 359 | // Build call __kmpc_barrier(loc, thread_id) |
| 360 | llvm::Value *Args[] = {EmitOpenMPUpdateLocation(CGF, Loc, Flags), |
| 361 | GetOpenMPThreadID(CGF, Loc)}; |
| 362 | auto RTLFn = CreateRuntimeFunction(CGOpenMPRuntime::OMPRTL__kmpc_barrier); |
| 363 | CGF.EmitRuntimeCall(RTLFn, Args); |
| 364 | } |
| 365 | |