Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===// |
| 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 contains code to emit OpenMP nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 14 | #include "CGCleanup.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 15 | #include "CGOpenMPRuntime.h" |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 18 | #include "TargetInfo.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 19 | #include "clang/AST/Stmt.h" |
| 20 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | /// Lexical scope for OpenMP executable constructs, that handles correct codegen |
| 28 | /// for captured expressions. |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 29 | class OMPLexicalScope : public CodeGenFunction::LexicalScope { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 30 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 31 | for (const auto *C : S.clauses()) { |
| 32 | if (auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 33 | if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 34 | for (const auto *I : PreInit->decls()) { |
| 35 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) |
| 36 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 37 | else { |
| 38 | CodeGenFunction::AutoVarEmission Emission = |
| 39 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 40 | CGF.EmitAutoVarCleanups(Emission); |
| 41 | } |
| 42 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 47 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 48 | |
| 49 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 50 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 51 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 52 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl)); |
| 53 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 54 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 55 | public: |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 56 | OMPLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S, |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 57 | bool AsInlined = false, bool EmitPreInitStmt = true) |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 58 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 59 | InlinedShareds(CGF) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 60 | if (EmitPreInitStmt) |
| 61 | emitPreInitStmt(CGF, S); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 62 | if (AsInlined) { |
| 63 | if (S.hasAssociatedStmt()) { |
| 64 | auto *CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 65 | for (auto &C : CS->captures()) { |
| 66 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 67 | auto *VD = C.getCapturedVar(); |
| 68 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 69 | isCapturedVar(CGF, VD) || |
| 70 | (CGF.CapturedStmtInfo && |
| 71 | InlinedShareds.isGlobalVarCaptured(VD)), |
| 72 | VD->getType().getNonReferenceType(), VK_LValue, |
| 73 | SourceLocation()); |
| 74 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 75 | return CGF.EmitLValue(&DRE).getAddress(); |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | (void)InlinedShareds.Privatize(); |
| 80 | } |
| 81 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 84 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 85 | /// Lexical scope for OpenMP parallel construct, that handles correct codegen |
| 86 | /// for captured expressions. |
| 87 | class OMPParallelScope final : public OMPLexicalScope { |
| 88 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 89 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
| 90 | return !isOpenMPTargetExecutionDirective(Kind) && |
| 91 | isOpenMPParallelDirective(Kind); |
| 92 | } |
| 93 | |
| 94 | public: |
| 95 | OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 96 | : OMPLexicalScope(CGF, S, |
| 97 | /*AsInlined=*/false, |
| 98 | /*EmitPreInitStmt=*/EmitPreInitStmt(S)) {} |
| 99 | }; |
| 100 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 101 | /// Lexical scope for OpenMP teams construct, that handles correct codegen |
| 102 | /// for captured expressions. |
| 103 | class OMPTeamsScope final : public OMPLexicalScope { |
| 104 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 105 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
| 106 | return !isOpenMPTargetExecutionDirective(Kind) && |
| 107 | isOpenMPTeamsDirective(Kind); |
| 108 | } |
| 109 | |
| 110 | public: |
| 111 | OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 112 | : OMPLexicalScope(CGF, S, |
| 113 | /*AsInlined=*/false, |
| 114 | /*EmitPreInitStmt=*/EmitPreInitStmt(S)) {} |
| 115 | }; |
| 116 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 117 | /// Private scope for OpenMP loop-based directives, that supports capturing |
| 118 | /// of used expression from loop statement. |
| 119 | class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { |
| 120 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) { |
| 121 | if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) { |
| 122 | if (auto *PreInits = cast_or_null<DeclStmt>(LD->getPreInits())) { |
| 123 | for (const auto *I : PreInits->decls()) |
| 124 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | public: |
| 130 | OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) |
| 131 | : CodeGenFunction::RunCleanupsScope(CGF) { |
| 132 | emitPreInitStmt(CGF, S); |
| 133 | } |
| 134 | }; |
| 135 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 136 | } // namespace |
| 137 | |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 138 | llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { |
| 139 | auto &C = getContext(); |
| 140 | llvm::Value *Size = nullptr; |
| 141 | auto SizeInChars = C.getTypeSizeInChars(Ty); |
| 142 | if (SizeInChars.isZero()) { |
| 143 | // getTypeSizeInChars() returns 0 for a VLA. |
| 144 | while (auto *VAT = C.getAsVariableArrayType(Ty)) { |
| 145 | llvm::Value *ArraySize; |
| 146 | std::tie(ArraySize, Ty) = getVLASize(VAT); |
| 147 | Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize; |
| 148 | } |
| 149 | SizeInChars = C.getTypeSizeInChars(Ty); |
| 150 | if (SizeInChars.isZero()) |
| 151 | return llvm::ConstantInt::get(SizeTy, /*V=*/0); |
| 152 | Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); |
| 153 | } else |
| 154 | Size = CGM.getSize(SizeInChars); |
| 155 | return Size; |
| 156 | } |
| 157 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 158 | void CodeGenFunction::GenerateOpenMPCapturedVars( |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 159 | const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 160 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 161 | auto CurField = RD->field_begin(); |
| 162 | auto CurCap = S.captures().begin(); |
| 163 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 164 | E = S.capture_init_end(); |
| 165 | I != E; ++I, ++CurField, ++CurCap) { |
| 166 | if (CurField->hasCapturedVLAType()) { |
| 167 | auto VAT = CurField->getCapturedVLAType(); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 168 | auto *Val = VLASizeMap[VAT->getSizeExpr()]; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 169 | CapturedVars.push_back(Val); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 170 | } else if (CurCap->capturesThis()) |
| 171 | CapturedVars.push_back(CXXThisValue); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 172 | else if (CurCap->capturesVariableByCopy()) { |
| 173 | llvm::Value *CV = |
| 174 | EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal(); |
| 175 | |
| 176 | // If the field is not a pointer, we need to save the actual value |
| 177 | // and load it as a void pointer. |
| 178 | if (!CurField->getType()->isAnyPointerType()) { |
| 179 | auto &Ctx = getContext(); |
| 180 | auto DstAddr = CreateMemTemp( |
| 181 | Ctx.getUIntPtrType(), |
| 182 | Twine(CurCap->getCapturedVar()->getName()) + ".casted"); |
| 183 | LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); |
| 184 | |
| 185 | auto *SrcAddrVal = EmitScalarConversion( |
| 186 | DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), |
| 187 | Ctx.getPointerType(CurField->getType()), SourceLocation()); |
| 188 | LValue SrcLV = |
| 189 | MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType()); |
| 190 | |
| 191 | // Store the value using the source type pointer. |
| 192 | EmitStoreThroughLValue(RValue::get(CV), SrcLV); |
| 193 | |
| 194 | // Load the value using the destination type pointer. |
| 195 | CV = EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal(); |
| 196 | } |
| 197 | CapturedVars.push_back(CV); |
| 198 | } else { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 199 | assert(CurCap->capturesVariable() && "Expected capture by reference."); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 200 | CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer()); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 201 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 205 | static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType, |
| 206 | StringRef Name, LValue AddrLV, |
| 207 | bool isReferenceType = false) { |
| 208 | ASTContext &Ctx = CGF.getContext(); |
| 209 | |
| 210 | auto *CastedPtr = CGF.EmitScalarConversion( |
| 211 | AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(), |
| 212 | Ctx.getPointerType(DstType), SourceLocation()); |
| 213 | auto TmpAddr = |
| 214 | CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) |
| 215 | .getAddress(); |
| 216 | |
| 217 | // If we are dealing with references we need to return the address of the |
| 218 | // reference instead of the reference of the value. |
| 219 | if (isReferenceType) { |
| 220 | QualType RefType = Ctx.getLValueReferenceType(DstType); |
| 221 | auto *RefVal = TmpAddr.getPointer(); |
| 222 | TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref"); |
| 223 | auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType); |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 224 | CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit*/ true); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return TmpAddr; |
| 228 | } |
| 229 | |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame^] | 230 | static QualType getCanonicalParamType(ASTContext &C, QualType T) { |
| 231 | if (T->isLValueReferenceType()) { |
| 232 | return C.getLValueReferenceType( |
| 233 | getCanonicalParamType(C, T.getNonReferenceType()), |
| 234 | /*SpelledAsLValue=*/false); |
| 235 | } |
| 236 | if (T->isPointerType()) |
| 237 | return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); |
| 238 | return C.getCanonicalParamType(T); |
| 239 | } |
| 240 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 241 | llvm::Function * |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 242 | CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 243 | assert( |
| 244 | CapturedStmtInfo && |
| 245 | "CapturedStmtInfo should be set when generating the captured function"); |
| 246 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 247 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 248 | assert(CD->hasBody() && "missing CapturedDecl body"); |
| 249 | |
| 250 | // Build the argument list. |
| 251 | ASTContext &Ctx = CGM.getContext(); |
| 252 | FunctionArgList Args; |
| 253 | Args.append(CD->param_begin(), |
| 254 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
| 255 | auto I = S.captures().begin(); |
| 256 | for (auto *FD : RD->fields()) { |
| 257 | QualType ArgType = FD->getType(); |
| 258 | IdentifierInfo *II = nullptr; |
| 259 | VarDecl *CapVar = nullptr; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 260 | |
| 261 | // If this is a capture by copy and the type is not a pointer, the outlined |
| 262 | // function argument type should be uintptr and the value properly casted to |
| 263 | // uintptr. This is necessary given that the runtime library is only able to |
| 264 | // deal with pointers. We can pass in the same way the VLA type sizes to the |
| 265 | // outlined function. |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 266 | if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || |
| 267 | I->capturesVariableArrayType()) |
| 268 | ArgType = Ctx.getUIntPtrType(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 269 | |
| 270 | if (I->capturesVariable() || I->capturesVariableByCopy()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 271 | CapVar = I->getCapturedVar(); |
| 272 | II = CapVar->getIdentifier(); |
| 273 | } else if (I->capturesThis()) |
| 274 | II = &getContext().Idents.get("this"); |
| 275 | else { |
| 276 | assert(I->capturesVariableArrayType()); |
| 277 | II = &getContext().Idents.get("vla"); |
| 278 | } |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 279 | if (ArgType->isVariablyModifiedType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 280 | ArgType = |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame^] | 281 | getCanonicalParamType(getContext(), ArgType.getNonReferenceType()); |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 282 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 283 | Args.push_back(ImplicitParamDecl::Create(getContext(), nullptr, |
| 284 | FD->getLocation(), II, ArgType)); |
| 285 | ++I; |
| 286 | } |
| 287 | Args.append( |
| 288 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 289 | CD->param_end()); |
| 290 | |
| 291 | // Create the function declaration. |
| 292 | FunctionType::ExtInfo ExtInfo; |
| 293 | const CGFunctionInfo &FuncInfo = |
John McCall | c56a8b3 | 2016-03-11 04:30:31 +0000 | [diff] [blame] | 294 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 295 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 296 | |
| 297 | llvm::Function *F = llvm::Function::Create( |
| 298 | FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 299 | CapturedStmtInfo->getHelperName(), &CGM.getModule()); |
| 300 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 301 | if (CD->isNothrow()) |
| 302 | F->addFnAttr(llvm::Attribute::NoUnwind); |
| 303 | |
| 304 | // Generate the function. |
| 305 | StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(), |
| 306 | CD->getBody()->getLocStart()); |
| 307 | unsigned Cnt = CD->getContextParamPosition(); |
| 308 | I = S.captures().begin(); |
| 309 | for (auto *FD : RD->fields()) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 310 | // If we are capturing a pointer by copy we don't need to do anything, just |
| 311 | // use the value that we get from the arguments. |
| 312 | if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 313 | const VarDecl *CurVD = I->getCapturedVar(); |
| 314 | Address LocalAddr = GetAddrOfLocalVar(Args[Cnt]); |
| 315 | // If the variable is a reference we need to materialize it here. |
| 316 | if (CurVD->getType()->isReferenceType()) { |
| 317 | Address RefAddr = CreateMemTemp(CurVD->getType(), getPointerAlign(), |
| 318 | ".materialized_ref"); |
| 319 | EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr, /*Volatile=*/false, |
| 320 | CurVD->getType()); |
| 321 | LocalAddr = RefAddr; |
| 322 | } |
| 323 | setAddrOfLocalVar(CurVD, LocalAddr); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 324 | ++Cnt; |
| 325 | ++I; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 326 | continue; |
| 327 | } |
| 328 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 329 | LValue ArgLVal = |
| 330 | MakeAddrLValue(GetAddrOfLocalVar(Args[Cnt]), Args[Cnt]->getType(), |
| 331 | AlignmentSource::Decl); |
| 332 | if (FD->hasCapturedVLAType()) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 333 | LValue CastedArgLVal = |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 334 | MakeAddrLValue(castValueFromUintptr(*this, FD->getType(), |
| 335 | Args[Cnt]->getName(), ArgLVal), |
| 336 | FD->getType(), AlignmentSource::Decl); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 337 | auto *ExprArg = |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 338 | EmitLoadOfLValue(CastedArgLVal, SourceLocation()).getScalarVal(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 339 | auto VAT = FD->getCapturedVLAType(); |
| 340 | VLASizeMap[VAT->getSizeExpr()] = ExprArg; |
| 341 | } else if (I->capturesVariable()) { |
| 342 | auto *Var = I->getCapturedVar(); |
| 343 | QualType VarTy = Var->getType(); |
| 344 | Address ArgAddr = ArgLVal.getAddress(); |
| 345 | if (!VarTy->isReferenceType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 346 | if (ArgLVal.getType()->isLValueReferenceType()) { |
| 347 | ArgAddr = EmitLoadOfReference( |
| 348 | ArgAddr, ArgLVal.getType()->castAs<ReferenceType>()); |
Alexey Bataev | ac5eabb | 2016-11-07 11:16:04 +0000 | [diff] [blame] | 349 | } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 350 | assert(ArgLVal.getType()->isPointerType()); |
| 351 | ArgAddr = EmitLoadOfPointer( |
| 352 | ArgAddr, ArgLVal.getType()->castAs<PointerType>()); |
| 353 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 354 | } |
Alexey Bataev | c71a409 | 2015-09-11 10:29:41 +0000 | [diff] [blame] | 355 | setAddrOfLocalVar( |
| 356 | Var, Address(ArgAddr.getPointer(), getContext().getDeclAlign(Var))); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 357 | } else if (I->capturesVariableByCopy()) { |
| 358 | assert(!FD->getType()->isAnyPointerType() && |
| 359 | "Not expecting a captured pointer."); |
| 360 | auto *Var = I->getCapturedVar(); |
| 361 | QualType VarTy = Var->getType(); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 362 | setAddrOfLocalVar(Var, castValueFromUintptr(*this, FD->getType(), |
| 363 | Args[Cnt]->getName(), ArgLVal, |
| 364 | VarTy->isReferenceType())); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 365 | } else { |
| 366 | // If 'this' is captured, load it into CXXThisValue. |
| 367 | assert(I->capturesThis()); |
| 368 | CXXThisValue = |
| 369 | EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation()).getScalarVal(); |
| 370 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 371 | ++Cnt; |
| 372 | ++I; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 375 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 376 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
| 377 | FinishFunction(CD->getBodyRBrace()); |
| 378 | |
| 379 | return F; |
| 380 | } |
| 381 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 382 | //===----------------------------------------------------------------------===// |
| 383 | // OpenMP Directive Emission |
| 384 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 385 | void CodeGenFunction::EmitOMPAggregateAssign( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 386 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
| 387 | const llvm::function_ref<void(Address, Address)> &CopyGen) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 388 | // Perform element-by-element initialization. |
| 389 | QualType ElementTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 390 | |
| 391 | // Drill down to the base element type on both arrays. |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 392 | auto ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 393 | auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); |
| 394 | SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 395 | |
| 396 | auto SrcBegin = SrcAddr.getPointer(); |
| 397 | auto DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 398 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 399 | auto DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
| 400 | // The basic structure here is a while-do loop. |
| 401 | auto BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 402 | auto DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 403 | auto IsEmpty = |
| 404 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 405 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 406 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 407 | // Enter the loop body, making that address the current address. |
| 408 | auto EntryBB = Builder.GetInsertBlock(); |
| 409 | EmitBlock(BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 410 | |
| 411 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); |
| 412 | |
| 413 | llvm::PHINode *SrcElementPHI = |
| 414 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 415 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 416 | Address SrcElementCurrent = |
| 417 | Address(SrcElementPHI, |
| 418 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 419 | |
| 420 | llvm::PHINode *DestElementPHI = |
| 421 | Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 422 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 423 | Address DestElementCurrent = |
| 424 | Address(DestElementPHI, |
| 425 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 426 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 427 | // Emit copy. |
| 428 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 429 | |
| 430 | // Shift the address forward by one element. |
| 431 | auto DestElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 432 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 433 | auto SrcElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 434 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 435 | // Check whether we've reached the end. |
| 436 | auto Done = |
| 437 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 438 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 439 | DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 440 | SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 441 | |
| 442 | // Done. |
| 443 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 444 | } |
| 445 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 446 | /// Check if the combiner is a call to UDR combiner and if it is so return the |
| 447 | /// UDR decl used for reduction. |
| 448 | static const OMPDeclareReductionDecl * |
| 449 | getReductionInit(const Expr *ReductionOp) { |
| 450 | if (auto *CE = dyn_cast<CallExpr>(ReductionOp)) |
| 451 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(CE->getCallee())) |
| 452 | if (auto *DRE = |
| 453 | dyn_cast<DeclRefExpr>(OVE->getSourceExpr()->IgnoreImpCasts())) |
| 454 | if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(DRE->getDecl())) |
| 455 | return DRD; |
| 456 | return nullptr; |
| 457 | } |
| 458 | |
| 459 | static void emitInitWithReductionInitializer(CodeGenFunction &CGF, |
| 460 | const OMPDeclareReductionDecl *DRD, |
| 461 | const Expr *InitOp, |
| 462 | Address Private, Address Original, |
| 463 | QualType Ty) { |
| 464 | if (DRD->getInitializer()) { |
| 465 | std::pair<llvm::Function *, llvm::Function *> Reduction = |
| 466 | CGF.CGM.getOpenMPRuntime().getUserDefinedReduction(DRD); |
| 467 | auto *CE = cast<CallExpr>(InitOp); |
| 468 | auto *OVE = cast<OpaqueValueExpr>(CE->getCallee()); |
| 469 | const Expr *LHS = CE->getArg(/*Arg=*/0)->IgnoreParenImpCasts(); |
| 470 | const Expr *RHS = CE->getArg(/*Arg=*/1)->IgnoreParenImpCasts(); |
| 471 | auto *LHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(LHS)->getSubExpr()); |
| 472 | auto *RHSDRE = cast<DeclRefExpr>(cast<UnaryOperator>(RHS)->getSubExpr()); |
| 473 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 474 | PrivateScope.addPrivate(cast<VarDecl>(LHSDRE->getDecl()), |
| 475 | [=]() -> Address { return Private; }); |
| 476 | PrivateScope.addPrivate(cast<VarDecl>(RHSDRE->getDecl()), |
| 477 | [=]() -> Address { return Original; }); |
| 478 | (void)PrivateScope.Privatize(); |
| 479 | RValue Func = RValue::get(Reduction.second); |
| 480 | CodeGenFunction::OpaqueValueMapping Map(CGF, OVE, Func); |
| 481 | CGF.EmitIgnoredExpr(InitOp); |
| 482 | } else { |
| 483 | llvm::Constant *Init = CGF.CGM.EmitNullConstant(Ty); |
| 484 | auto *GV = new llvm::GlobalVariable( |
| 485 | CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true, |
| 486 | llvm::GlobalValue::PrivateLinkage, Init, ".init"); |
| 487 | LValue LV = CGF.MakeNaturalAlignAddrLValue(GV, Ty); |
| 488 | RValue InitRVal; |
| 489 | switch (CGF.getEvaluationKind(Ty)) { |
| 490 | case TEK_Scalar: |
| 491 | InitRVal = CGF.EmitLoadOfLValue(LV, SourceLocation()); |
| 492 | break; |
| 493 | case TEK_Complex: |
| 494 | InitRVal = |
| 495 | RValue::getComplex(CGF.EmitLoadOfComplex(LV, SourceLocation())); |
| 496 | break; |
| 497 | case TEK_Aggregate: |
| 498 | InitRVal = RValue::getAggregate(LV.getAddress()); |
| 499 | break; |
| 500 | } |
| 501 | OpaqueValueExpr OVE(SourceLocation(), Ty, VK_RValue); |
| 502 | CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE, InitRVal); |
| 503 | CGF.EmitAnyExprToMem(&OVE, Private, Ty.getQualifiers(), |
| 504 | /*IsInitializer=*/false); |
| 505 | } |
| 506 | } |
| 507 | |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 508 | /// \brief Emit initialization of arrays of complex types. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 509 | /// \param DestAddr Address of the array. |
| 510 | /// \param Type Type of array. |
| 511 | /// \param Init Initial expression of array. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 512 | /// \param SrcAddr Address of the original array. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 513 | static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr, |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 514 | QualType Type, const Expr *Init, |
| 515 | Address SrcAddr = Address::invalid()) { |
| 516 | auto *DRD = getReductionInit(Init); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 517 | // Perform element-by-element initialization. |
| 518 | QualType ElementTy; |
| 519 | |
| 520 | // Drill down to the base element type on both arrays. |
| 521 | auto ArrayTy = Type->getAsArrayTypeUnsafe(); |
| 522 | auto NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr); |
| 523 | DestAddr = |
| 524 | CGF.Builder.CreateElementBitCast(DestAddr, DestAddr.getElementType()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 525 | if (DRD) |
| 526 | SrcAddr = |
| 527 | CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 528 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 529 | llvm::Value *SrcBegin = nullptr; |
| 530 | if (DRD) |
| 531 | SrcBegin = SrcAddr.getPointer(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 532 | auto DestBegin = DestAddr.getPointer(); |
| 533 | // Cast from pointer to array type to pointer to single element. |
| 534 | auto DestEnd = CGF.Builder.CreateGEP(DestBegin, NumElements); |
| 535 | // The basic structure here is a while-do loop. |
| 536 | auto BodyBB = CGF.createBasicBlock("omp.arrayinit.body"); |
| 537 | auto DoneBB = CGF.createBasicBlock("omp.arrayinit.done"); |
| 538 | auto IsEmpty = |
| 539 | CGF.Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arrayinit.isempty"); |
| 540 | CGF.Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
| 541 | |
| 542 | // Enter the loop body, making that address the current address. |
| 543 | auto EntryBB = CGF.Builder.GetInsertBlock(); |
| 544 | CGF.EmitBlock(BodyBB); |
| 545 | |
| 546 | CharUnits ElementSize = CGF.getContext().getTypeSizeInChars(ElementTy); |
| 547 | |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 548 | llvm::PHINode *SrcElementPHI = nullptr; |
| 549 | Address SrcElementCurrent = Address::invalid(); |
| 550 | if (DRD) { |
| 551 | SrcElementPHI = CGF.Builder.CreatePHI(SrcBegin->getType(), 2, |
| 552 | "omp.arraycpy.srcElementPast"); |
| 553 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 554 | SrcElementCurrent = |
| 555 | Address(SrcElementPHI, |
| 556 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 557 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 558 | llvm::PHINode *DestElementPHI = CGF.Builder.CreatePHI( |
| 559 | DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 560 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 561 | Address DestElementCurrent = |
| 562 | Address(DestElementPHI, |
| 563 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 564 | |
| 565 | // Emit copy. |
| 566 | { |
| 567 | CodeGenFunction::RunCleanupsScope InitScope(CGF); |
Alexey Bataev | 8fbae8cf | 2016-04-27 11:38:05 +0000 | [diff] [blame] | 568 | if (DRD && (DRD->getInitializer() || !Init)) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 569 | emitInitWithReductionInitializer(CGF, DRD, Init, DestElementCurrent, |
| 570 | SrcElementCurrent, ElementTy); |
| 571 | } else |
| 572 | CGF.EmitAnyExprToMem(Init, DestElementCurrent, ElementTy.getQualifiers(), |
| 573 | /*IsInitializer=*/false); |
| 574 | } |
| 575 | |
| 576 | if (DRD) { |
| 577 | // Shift the address forward by one element. |
| 578 | auto SrcElementNext = CGF.Builder.CreateConstGEP1_32( |
| 579 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 580 | SrcElementPHI->addIncoming(SrcElementNext, CGF.Builder.GetInsertBlock()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // Shift the address forward by one element. |
| 584 | auto DestElementNext = CGF.Builder.CreateConstGEP1_32( |
| 585 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 586 | // Check whether we've reached the end. |
| 587 | auto Done = |
| 588 | CGF.Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 589 | CGF.Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 590 | DestElementPHI->addIncoming(DestElementNext, CGF.Builder.GetInsertBlock()); |
| 591 | |
| 592 | // Done. |
| 593 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 594 | } |
| 595 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 596 | void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, |
| 597 | Address SrcAddr, const VarDecl *DestVD, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 598 | const VarDecl *SrcVD, const Expr *Copy) { |
| 599 | if (OriginalType->isArrayType()) { |
| 600 | auto *BO = dyn_cast<BinaryOperator>(Copy); |
| 601 | if (BO && BO->getOpcode() == BO_Assign) { |
| 602 | // Perform simple memcpy for simple copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 603 | EmitAggregateAssign(DestAddr, SrcAddr, OriginalType); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 604 | } else { |
| 605 | // For arrays with complex element types perform element by element |
| 606 | // copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 607 | EmitOMPAggregateAssign( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 608 | DestAddr, SrcAddr, OriginalType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 609 | [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 610 | // Working with the single array element, so have to remap |
| 611 | // destination and source variables to corresponding array |
| 612 | // elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 613 | CodeGenFunction::OMPPrivateScope Remap(*this); |
| 614 | Remap.addPrivate(DestVD, [DestElement]() -> Address { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 615 | return DestElement; |
| 616 | }); |
| 617 | Remap.addPrivate( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 618 | SrcVD, [SrcElement]() -> Address { return SrcElement; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 619 | (void)Remap.Privatize(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 620 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 621 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 622 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 623 | } else { |
| 624 | // Remap pseudo source variable to private copy. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 625 | CodeGenFunction::OMPPrivateScope Remap(*this); |
| 626 | Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; }); |
| 627 | Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 628 | (void)Remap.Privatize(); |
| 629 | // Emit copying of the whole variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 630 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 631 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 634 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 635 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 636 | if (!HaveInsertPoint()) |
| 637 | return false; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 638 | bool FirstprivateIsLastprivate = false; |
| 639 | llvm::DenseSet<const VarDecl *> Lastprivates; |
| 640 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 641 | for (const auto *D : C->varlists()) |
| 642 | Lastprivates.insert( |
| 643 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 644 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 645 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | 9afe575 | 2016-05-24 07:40:12 +0000 | [diff] [blame] | 646 | CGCapturedStmtInfo CapturesInfo(cast<CapturedStmt>(*D.getAssociatedStmt())); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 647 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 648 | auto IRef = C->varlist_begin(); |
| 649 | auto InitsRef = C->inits().begin(); |
| 650 | for (auto IInit : C->private_copies()) { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 651 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 652 | bool ThisFirstprivateIsLastprivate = |
| 653 | Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; |
Alexey Bataev | 9afe575 | 2016-05-24 07:40:12 +0000 | [diff] [blame] | 654 | auto *CapFD = CapturesInfo.lookup(OrigVD); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 655 | auto *FD = CapturedStmtInfo->lookup(OrigVD); |
Alexey Bataev | 9afe575 | 2016-05-24 07:40:12 +0000 | [diff] [blame] | 656 | if (!ThisFirstprivateIsLastprivate && FD && (FD == CapFD) && |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 657 | !FD->getType()->isReferenceType()) { |
| 658 | EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); |
| 659 | ++IRef; |
| 660 | ++InitsRef; |
| 661 | continue; |
| 662 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 663 | FirstprivateIsLastprivate = |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 664 | FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 665 | if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 666 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 667 | auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
| 668 | bool IsRegistered; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 669 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 670 | /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, |
| 671 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 672 | Address OriginalAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | feddd64 | 2016-04-22 09:05:03 +0000 | [diff] [blame] | 673 | QualType Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 674 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 675 | // Emit VarDecl with copy init for arrays. |
| 676 | // Get the address of the original variable captured in current |
| 677 | // captured region. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 678 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 679 | auto Emission = EmitAutoVarAlloca(*VD); |
| 680 | auto *Init = VD->getInit(); |
| 681 | if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) { |
| 682 | // Perform simple memcpy. |
| 683 | EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr, |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 684 | Type); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 685 | } else { |
| 686 | EmitOMPAggregateAssign( |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 687 | Emission.getAllocatedAddress(), OriginalAddr, Type, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 688 | [this, VDInit, Init](Address DestElement, |
| 689 | Address SrcElement) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 690 | // Clean up any temporaries needed by the initialization. |
| 691 | RunCleanupsScope InitScope(*this); |
| 692 | // Emit initialization for single element. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 693 | setAddrOfLocalVar(VDInit, SrcElement); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 694 | EmitAnyExprToMem(Init, DestElement, |
| 695 | Init->getType().getQualifiers(), |
| 696 | /*IsInitializer*/ false); |
| 697 | LocalDeclMap.erase(VDInit); |
| 698 | }); |
| 699 | } |
| 700 | EmitAutoVarCleanups(Emission); |
| 701 | return Emission.getAllocatedAddress(); |
| 702 | }); |
| 703 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 704 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 705 | // Emit private VarDecl with copy init. |
| 706 | // Remap temp VDInit variable to the address of the original |
| 707 | // variable |
| 708 | // (for proper handling of captured global variables). |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 709 | setAddrOfLocalVar(VDInit, OriginalAddr); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 710 | EmitDecl(*VD); |
| 711 | LocalDeclMap.erase(VDInit); |
| 712 | return GetAddrOfLocalVar(VD); |
| 713 | }); |
| 714 | } |
| 715 | assert(IsRegistered && |
| 716 | "firstprivate var already registered as private"); |
| 717 | // Silence the warning about unused variable. |
| 718 | (void)IsRegistered; |
| 719 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 720 | ++IRef; |
| 721 | ++InitsRef; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 724 | return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 727 | void CodeGenFunction::EmitOMPPrivateClause( |
| 728 | const OMPExecutableDirective &D, |
| 729 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 730 | if (!HaveInsertPoint()) |
| 731 | return; |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 732 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 733 | for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 734 | auto IRef = C->varlist_begin(); |
| 735 | for (auto IInit : C->private_copies()) { |
| 736 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 737 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 738 | auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 739 | bool IsRegistered = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 740 | PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 741 | // Emit private VarDecl with copy init. |
| 742 | EmitDecl(*VD); |
| 743 | return GetAddrOfLocalVar(VD); |
| 744 | }); |
| 745 | assert(IsRegistered && "private var already registered as private"); |
| 746 | // Silence the warning about unused variable. |
| 747 | (void)IsRegistered; |
| 748 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 749 | ++IRef; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 754 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 755 | if (!HaveInsertPoint()) |
| 756 | return false; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 757 | // threadprivate_var1 = master_threadprivate_var1; |
| 758 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 759 | // ... |
| 760 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 761 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 762 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 763 | for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 764 | auto IRef = C->varlist_begin(); |
| 765 | auto ISrcRef = C->source_exprs().begin(); |
| 766 | auto IDestRef = C->destination_exprs().begin(); |
| 767 | for (auto *AssignOp : C->assignment_ops()) { |
| 768 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 769 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 770 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 771 | // Get the address of the master variable. If we are emitting code with |
| 772 | // TLS support, the address is passed from the master as field in the |
| 773 | // captured declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 774 | Address MasterAddr = Address::invalid(); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 775 | if (getLangOpts().OpenMPUseTLS && |
| 776 | getContext().getTargetInfo().isTLSSupported()) { |
| 777 | assert(CapturedStmtInfo->lookup(VD) && |
| 778 | "Copyin threadprivates should have been captured!"); |
| 779 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(), |
| 780 | VK_LValue, (*IRef)->getExprLoc()); |
| 781 | MasterAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 782 | LocalDeclMap.erase(VD); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 783 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 784 | MasterAddr = |
| 785 | Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 786 | : CGM.GetAddrOfGlobal(VD), |
| 787 | getContext().getDeclAlign(VD)); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 788 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 789 | // Get the address of the threadprivate variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 790 | Address PrivateAddr = EmitLValue(*IRef).getAddress(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 791 | if (CopiedVars.size() == 1) { |
| 792 | // At first check if current thread is a master thread. If it is, no |
| 793 | // need to copy data. |
| 794 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 795 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 796 | Builder.CreateCondBr( |
| 797 | Builder.CreateICmpNE( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 798 | Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), |
| 799 | Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)), |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 800 | CopyBegin, CopyEnd); |
| 801 | EmitBlock(CopyBegin); |
| 802 | } |
| 803 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 804 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 805 | EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 806 | } |
| 807 | ++IRef; |
| 808 | ++ISrcRef; |
| 809 | ++IDestRef; |
| 810 | } |
| 811 | } |
| 812 | if (CopyEnd) { |
| 813 | // Exit out of copying procedure for non-master thread. |
| 814 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 815 | return true; |
| 816 | } |
| 817 | return false; |
| 818 | } |
| 819 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 820 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 821 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 822 | if (!HaveInsertPoint()) |
| 823 | return false; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 824 | bool HasAtLeastOneLastprivate = false; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 825 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 826 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 827 | auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 828 | for (auto *C : LoopDirective->counters()) { |
| 829 | SIMDLCVs.insert( |
| 830 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 831 | } |
| 832 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 833 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 834 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 835 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 836 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind())) |
| 837 | break; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 838 | auto IRef = C->varlist_begin(); |
| 839 | auto IDestRef = C->destination_exprs().begin(); |
| 840 | for (auto *IInit : C->private_copies()) { |
| 841 | // Keep the address of the original variable for future update at the end |
| 842 | // of the loop. |
| 843 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 844 | // Taskloops do not require additional initialization, it is done in |
| 845 | // runtime support library. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 846 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
| 847 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 848 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 849 | DeclRefExpr DRE( |
| 850 | const_cast<VarDecl *>(OrigVD), |
| 851 | /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( |
| 852 | OrigVD) != nullptr, |
| 853 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
| 854 | return EmitLValue(&DRE).getAddress(); |
| 855 | }); |
| 856 | // Check if the variable is also a firstprivate: in this case IInit is |
| 857 | // not generated. Initialization of this variable will happen in codegen |
| 858 | // for 'firstprivate' clause. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 859 | if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 860 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 861 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 862 | // Emit private VarDecl with copy init. |
| 863 | EmitDecl(*VD); |
| 864 | return GetAddrOfLocalVar(VD); |
| 865 | }); |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 866 | assert(IsRegistered && |
| 867 | "lastprivate var already registered as private"); |
| 868 | (void)IsRegistered; |
| 869 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 870 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 871 | ++IRef; |
| 872 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | return HasAtLeastOneLastprivate; |
| 876 | } |
| 877 | |
| 878 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 879 | const OMPExecutableDirective &D, bool NoFinals, |
| 880 | llvm::Value *IsLastIterCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 881 | if (!HaveInsertPoint()) |
| 882 | return; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 883 | // Emit following code: |
| 884 | // if (<IsLastIterCond>) { |
| 885 | // orig_var1 = private_orig_var1; |
| 886 | // ... |
| 887 | // orig_varn = private_orig_varn; |
| 888 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 889 | llvm::BasicBlock *ThenBB = nullptr; |
| 890 | llvm::BasicBlock *DoneBB = nullptr; |
| 891 | if (IsLastIterCond) { |
| 892 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 893 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 894 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 895 | EmitBlock(ThenBB); |
| 896 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 897 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
| 898 | llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 899 | if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 900 | auto IC = LoopDirective->counters().begin(); |
| 901 | for (auto F : LoopDirective->finals()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 902 | auto *D = |
| 903 | cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); |
| 904 | if (NoFinals) |
| 905 | AlreadyEmittedVars.insert(D); |
| 906 | else |
| 907 | LoopCountersAndUpdates[D] = F; |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 908 | ++IC; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 911 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 912 | auto IRef = C->varlist_begin(); |
| 913 | auto ISrcRef = C->source_exprs().begin(); |
| 914 | auto IDestRef = C->destination_exprs().begin(); |
| 915 | for (auto *AssignOp : C->assignment_ops()) { |
| 916 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 917 | QualType Type = PrivateVD->getType(); |
| 918 | auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
| 919 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 920 | // If lastprivate variable is a loop control variable for loop-based |
| 921 | // directive, update its value before copyin back to original |
| 922 | // variable. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 923 | if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) |
| 924 | EmitIgnoredExpr(FinalExpr); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 925 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 926 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 927 | // Get the address of the original variable. |
| 928 | Address OriginalAddr = GetAddrOfLocalVar(DestVD); |
| 929 | // Get the address of the private variable. |
| 930 | Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
| 931 | if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>()) |
| 932 | PrivateAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 933 | Address(Builder.CreateLoad(PrivateAddr), |
| 934 | getNaturalTypeAlignment(RefTy->getPointeeType())); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 935 | EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 936 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 937 | ++IRef; |
| 938 | ++ISrcRef; |
| 939 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 940 | } |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 941 | if (auto *PostUpdate = C->getPostUpdateExpr()) |
| 942 | EmitIgnoredExpr(PostUpdate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 943 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 944 | if (IsLastIterCond) |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 945 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 948 | static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 949 | LValue BaseLV, llvm::Value *Addr) { |
| 950 | Address Tmp = Address::invalid(); |
| 951 | Address TopTmp = Address::invalid(); |
| 952 | Address MostTopTmp = Address::invalid(); |
| 953 | BaseTy = BaseTy.getNonReferenceType(); |
| 954 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 955 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 956 | Tmp = CGF.CreateMemTemp(BaseTy); |
| 957 | if (TopTmp.isValid()) |
| 958 | CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); |
| 959 | else |
| 960 | MostTopTmp = Tmp; |
| 961 | TopTmp = Tmp; |
| 962 | BaseTy = BaseTy->getPointeeType(); |
| 963 | } |
| 964 | llvm::Type *Ty = BaseLV.getPointer()->getType(); |
| 965 | if (Tmp.isValid()) |
| 966 | Ty = Tmp.getElementType(); |
| 967 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); |
| 968 | if (Tmp.isValid()) { |
| 969 | CGF.Builder.CreateStore(Addr, Tmp); |
| 970 | return MostTopTmp; |
| 971 | } |
| 972 | return Address(Addr, BaseLV.getAlignment()); |
| 973 | } |
| 974 | |
| 975 | static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 976 | LValue BaseLV) { |
| 977 | BaseTy = BaseTy.getNonReferenceType(); |
| 978 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 979 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 980 | if (auto *PtrTy = BaseTy->getAs<PointerType>()) |
| 981 | BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); |
| 982 | else { |
| 983 | BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(), |
| 984 | BaseTy->castAs<ReferenceType>()); |
| 985 | } |
| 986 | BaseTy = BaseTy->getPointeeType(); |
| 987 | } |
| 988 | return CGF.MakeAddrLValue( |
| 989 | Address( |
| 990 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 991 | BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()), |
| 992 | BaseLV.getAlignment()), |
| 993 | BaseLV.getType(), BaseLV.getAlignmentSource()); |
| 994 | } |
| 995 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 996 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 997 | const OMPExecutableDirective &D, |
| 998 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 999 | if (!HaveInsertPoint()) |
| 1000 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1001 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1002 | auto ILHS = C->lhs_exprs().begin(); |
| 1003 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1004 | auto IPriv = C->privates().begin(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1005 | auto IRed = C->reduction_ops().begin(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1006 | for (auto IRef : C->varlists()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1007 | auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1008 | auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 1009 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1010 | auto *DRD = getReductionInit(*IRed); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1011 | if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) { |
| 1012 | auto *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 1013 | while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
| 1014 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
| 1015 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 1016 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1017 | auto *DE = cast<DeclRefExpr>(Base); |
| 1018 | auto *OrigVD = cast<VarDecl>(DE->getDecl()); |
| 1019 | auto OASELValueLB = EmitOMPArraySectionExpr(OASE); |
| 1020 | auto OASELValueUB = |
| 1021 | EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); |
| 1022 | auto OriginalBaseLValue = EmitLValue(DE); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1023 | LValue BaseLValue = |
| 1024 | loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(), |
| 1025 | OriginalBaseLValue); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1026 | // Store the address of the original variable associated with the LHS |
| 1027 | // implicit variable. |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 1028 | PrivateScope.addPrivate(LHSVD, [OASELValueLB]() -> Address { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1029 | return OASELValueLB.getAddress(); |
| 1030 | }); |
| 1031 | // Emit reduction copy. |
| 1032 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1033 | OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB, |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1034 | OASELValueUB, OriginalBaseLValue, DRD, IRed]() -> Address { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1035 | // Emit VarDecl with copy init for arrays. |
| 1036 | // Get the address of the original variable captured in current |
| 1037 | // captured region. |
| 1038 | auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(), |
| 1039 | OASELValueLB.getPointer()); |
| 1040 | Size = Builder.CreateNUWAdd( |
| 1041 | Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); |
| 1042 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1043 | *this, cast<OpaqueValueExpr>( |
| 1044 | getContext() |
| 1045 | .getAsVariableArrayType(PrivateVD->getType()) |
| 1046 | ->getSizeExpr()), |
| 1047 | RValue::get(Size)); |
| 1048 | EmitVariablyModifiedType(PrivateVD->getType()); |
| 1049 | auto Emission = EmitAutoVarAlloca(*PrivateVD); |
| 1050 | auto Addr = Emission.getAllocatedAddress(); |
| 1051 | auto *Init = PrivateVD->getInit(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1052 | EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), |
| 1053 | DRD ? *IRed : Init, |
| 1054 | OASELValueLB.getAddress()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1055 | EmitAutoVarCleanups(Emission); |
| 1056 | // Emit private VarDecl with reduction init. |
| 1057 | auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), |
| 1058 | OASELValueLB.getPointer()); |
| 1059 | auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1060 | return castToBase(*this, OrigVD->getType(), |
| 1061 | OASELValueLB.getType(), OriginalBaseLValue, |
| 1062 | Ptr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1063 | }); |
| 1064 | assert(IsRegistered && "private var already registered as private"); |
| 1065 | // Silence the warning about unused variable. |
| 1066 | (void)IsRegistered; |
| 1067 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { |
| 1068 | return GetAddrOfLocalVar(PrivateVD); |
| 1069 | }); |
| 1070 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) { |
| 1071 | auto *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 1072 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 1073 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 1074 | auto *DE = cast<DeclRefExpr>(Base); |
| 1075 | auto *OrigVD = cast<VarDecl>(DE->getDecl()); |
| 1076 | auto ASELValue = EmitLValue(ASE); |
| 1077 | auto OriginalBaseLValue = EmitLValue(DE); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1078 | LValue BaseLValue = loadToBegin( |
| 1079 | *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1080 | // Store the address of the original variable associated with the LHS |
| 1081 | // implicit variable. |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 1082 | PrivateScope.addPrivate( |
| 1083 | LHSVD, [ASELValue]() -> Address { return ASELValue.getAddress(); }); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1084 | // Emit reduction copy. |
| 1085 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1086 | OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue, |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1087 | OriginalBaseLValue, DRD, IRed]() -> Address { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1088 | // Emit private VarDecl with reduction init. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1089 | AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); |
| 1090 | auto Addr = Emission.getAllocatedAddress(); |
Alexey Bataev | 8fbae8cf | 2016-04-27 11:38:05 +0000 | [diff] [blame] | 1091 | if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1092 | emitInitWithReductionInitializer(*this, DRD, *IRed, Addr, |
| 1093 | ASELValue.getAddress(), |
| 1094 | ASELValue.getType()); |
| 1095 | } else |
| 1096 | EmitAutoVarInit(Emission); |
| 1097 | EmitAutoVarCleanups(Emission); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1098 | auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), |
| 1099 | ASELValue.getPointer()); |
| 1100 | auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 1101 | return castToBase(*this, OrigVD->getType(), ASELValue.getType(), |
| 1102 | OriginalBaseLValue, Ptr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1103 | }); |
| 1104 | assert(IsRegistered && "private var already registered as private"); |
| 1105 | // Silence the warning about unused variable. |
| 1106 | (void)IsRegistered; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1107 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { |
| 1108 | return Builder.CreateElementBitCast( |
| 1109 | GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()), |
| 1110 | "rhs.begin"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1111 | }); |
| 1112 | } else { |
| 1113 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1114 | QualType Type = PrivateVD->getType(); |
| 1115 | if (getContext().getAsArrayType(Type)) { |
| 1116 | // Store the address of the original variable associated with the LHS |
| 1117 | // implicit variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1118 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1119 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1120 | IRef->getType(), VK_LValue, IRef->getExprLoc()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1121 | Address OriginalAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1122 | PrivateScope.addPrivate(LHSVD, [this, &OriginalAddr, |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1123 | LHSVD]() -> Address { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1124 | OriginalAddr = Builder.CreateElementBitCast( |
| 1125 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); |
| 1126 | return OriginalAddr; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1127 | }); |
| 1128 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 1129 | if (Type->isVariablyModifiedType()) { |
| 1130 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 1131 | *this, cast<OpaqueValueExpr>( |
| 1132 | getContext() |
| 1133 | .getAsVariableArrayType(PrivateVD->getType()) |
| 1134 | ->getSizeExpr()), |
| 1135 | RValue::get( |
| 1136 | getTypeSize(OrigVD->getType().getNonReferenceType()))); |
| 1137 | EmitVariablyModifiedType(Type); |
| 1138 | } |
| 1139 | auto Emission = EmitAutoVarAlloca(*PrivateVD); |
| 1140 | auto Addr = Emission.getAllocatedAddress(); |
| 1141 | auto *Init = PrivateVD->getInit(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1142 | EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), |
| 1143 | DRD ? *IRed : Init, OriginalAddr); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1144 | EmitAutoVarCleanups(Emission); |
| 1145 | return Emission.getAllocatedAddress(); |
| 1146 | }); |
| 1147 | assert(IsRegistered && "private var already registered as private"); |
| 1148 | // Silence the warning about unused variable. |
| 1149 | (void)IsRegistered; |
| 1150 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { |
| 1151 | return Builder.CreateElementBitCast( |
| 1152 | GetAddrOfLocalVar(PrivateVD), |
| 1153 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin"); |
| 1154 | }); |
| 1155 | } else { |
| 1156 | // Store the address of the original variable associated with the LHS |
| 1157 | // implicit variable. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1158 | Address OriginalAddr = Address::invalid(); |
| 1159 | PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef, |
| 1160 | &OriginalAddr]() -> Address { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1161 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1162 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1163 | IRef->getType(), VK_LValue, IRef->getExprLoc()); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1164 | OriginalAddr = EmitLValue(&DRE).getAddress(); |
| 1165 | return OriginalAddr; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1166 | }); |
| 1167 | // Emit reduction copy. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1168 | bool IsRegistered = PrivateScope.addPrivate( |
| 1169 | OrigVD, [this, PrivateVD, OriginalAddr, DRD, IRed]() -> Address { |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1170 | // Emit private VarDecl with reduction init. |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1171 | AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); |
| 1172 | auto Addr = Emission.getAllocatedAddress(); |
Alexey Bataev | 8fbae8cf | 2016-04-27 11:38:05 +0000 | [diff] [blame] | 1173 | if (DRD && (DRD->getInitializer() || !PrivateVD->hasInit())) { |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1174 | emitInitWithReductionInitializer(*this, DRD, *IRed, Addr, |
| 1175 | OriginalAddr, |
| 1176 | PrivateVD->getType()); |
| 1177 | } else |
| 1178 | EmitAutoVarInit(Emission); |
| 1179 | EmitAutoVarCleanups(Emission); |
| 1180 | return Addr; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 1181 | }); |
| 1182 | assert(IsRegistered && "private var already registered as private"); |
| 1183 | // Silence the warning about unused variable. |
| 1184 | (void)IsRegistered; |
| 1185 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { |
| 1186 | return GetAddrOfLocalVar(PrivateVD); |
| 1187 | }); |
| 1188 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1189 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 1190 | ++ILHS; |
| 1191 | ++IRHS; |
| 1192 | ++IPriv; |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1193 | ++IRed; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1199 | const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1200 | if (!HaveInsertPoint()) |
| 1201 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1202 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1203 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 1204 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 1205 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1206 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1207 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1208 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1209 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1210 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 1211 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 1212 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 1213 | } |
| 1214 | if (HasAtLeastOneReduction) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1215 | bool WithNowait = D.getSingleClause<OMPNowaitClause>() || |
| 1216 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
| 1217 | D.getDirectiveKind() == OMPD_simd; |
| 1218 | bool SimpleReduction = D.getDirectiveKind() == OMPD_simd; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1219 | // Emit nowait reduction if nowait clause is present or directive is a |
| 1220 | // parallel directive (it always has implicit barrier). |
| 1221 | CGM.getOpenMPRuntime().emitReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1222 | *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1223 | {WithNowait, SimpleReduction, ReductionKind}); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1227 | static void emitPostUpdateForReductionClause( |
| 1228 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 1229 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
| 1230 | if (!CGF.HaveInsertPoint()) |
| 1231 | return; |
| 1232 | llvm::BasicBlock *DoneBB = nullptr; |
| 1233 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
| 1234 | if (auto *PostUpdate = C->getPostUpdateExpr()) { |
| 1235 | if (!DoneBB) { |
| 1236 | if (auto *Cond = CondGen(CGF)) { |
| 1237 | // If the first post-update expression is found, emit conditional |
| 1238 | // block if it was requested. |
| 1239 | auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); |
| 1240 | DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); |
| 1241 | CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1242 | CGF.EmitBlock(ThenBB); |
| 1243 | } |
| 1244 | } |
| 1245 | CGF.EmitIgnoredExpr(PostUpdate); |
| 1246 | } |
| 1247 | } |
| 1248 | if (DoneBB) |
| 1249 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 1250 | } |
| 1251 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1252 | static void emitCommonOMPParallelDirective(CodeGenFunction &CGF, |
| 1253 | const OMPExecutableDirective &S, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1254 | OpenMPDirectiveKind InnermostKind, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1255 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1256 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
| 1257 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
| 1258 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1259 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1260 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1261 | auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1262 | /*IgnoreResultAssign*/ true); |
| 1263 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
| 1264 | CGF, NumThreads, NumThreadsClause->getLocStart()); |
| 1265 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1266 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1267 | CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1268 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
| 1269 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart()); |
| 1270 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1271 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 1272 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 1273 | if (C->getNameModifier() == OMPD_unknown || |
| 1274 | C->getNameModifier() == OMPD_parallel) { |
| 1275 | IfCond = C->getCondition(); |
| 1276 | break; |
| 1277 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1278 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1279 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1280 | OMPParallelScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1281 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 1282 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1283 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1284 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1288 | // Emit parallel region as a standalone region. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1289 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1290 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1291 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1292 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 1293 | if (Copyins) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1294 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1295 | // propagation master's thread values of threadprivate variables to local |
| 1296 | // instances of that variables of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1297 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 1298 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 1299 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1300 | } |
| 1301 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 1302 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 1303 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1304 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1305 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1306 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1307 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1308 | emitPostUpdateForReductionClause( |
| 1309 | *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1310 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1311 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1312 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 1313 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1314 | RunCleanupsScope BodyScope(*this); |
| 1315 | // Update counters values on current iteration. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1316 | for (auto I : D.updates()) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1317 | EmitIgnoredExpr(I); |
| 1318 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1319 | // Update the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1320 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1321 | for (auto *U : C->updates()) |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1322 | EmitIgnoredExpr(U); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1325 | // On a continue in the body, jump to the end. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1326 | auto Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1327 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1328 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1329 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1330 | // The end (updates/cleanups). |
| 1331 | EmitBlock(Continue.getBlock()); |
| 1332 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1335 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1336 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1337 | const Expr *IncExpr, |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1338 | const llvm::function_ref<void(CodeGenFunction &)> &BodyGen, |
| 1339 | const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1340 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1343 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1344 | EmitBlock(CondBlock); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1345 | const SourceRange &R = S.getSourceRange(); |
| 1346 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1347 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1348 | |
| 1349 | // If there are any cleanups between here and the loop-exit scope, |
| 1350 | // create a block to stage a loop exit along. |
| 1351 | auto ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1352 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1353 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1354 | |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1355 | auto LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1356 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1357 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1358 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1359 | if (ExitBlock != LoopExit.getBlock()) { |
| 1360 | EmitBlock(ExitBlock); |
| 1361 | EmitBranchThroughCleanup(LoopExit); |
| 1362 | } |
| 1363 | |
| 1364 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1365 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Create a block for the increment. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1368 | auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1369 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1370 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1371 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1372 | |
| 1373 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1374 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1375 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1376 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1377 | BreakContinueStack.pop_back(); |
| 1378 | EmitBranch(CondBlock); |
| 1379 | LoopStack.pop(); |
| 1380 | // Emit the fall-through block. |
| 1381 | EmitBlock(LoopExit.getBlock()); |
| 1382 | } |
| 1383 | |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1384 | void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1385 | if (!HaveInsertPoint()) |
| 1386 | return; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1387 | // Emit inits for the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1388 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1389 | for (auto *Init : C->inits()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1390 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1391 | if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { |
| 1392 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 1393 | auto *OrigVD = cast<VarDecl>(Ref->getDecl()); |
| 1394 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1395 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1396 | VD->getInit()->getType(), VK_LValue, |
| 1397 | VD->getInit()->getExprLoc()); |
| 1398 | EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), |
| 1399 | VD->getType()), |
| 1400 | /*capturedByInit=*/false); |
| 1401 | EmitAutoVarCleanups(Emission); |
| 1402 | } else |
| 1403 | EmitVarDecl(*VD); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1404 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1405 | // Emit the linear steps for the linear clauses. |
| 1406 | // If a step is not constant, it is pre-calculated before the loop. |
| 1407 | if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1408 | if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1409 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1410 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1411 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1412 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1413 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1416 | void CodeGenFunction::EmitOMPLinearClauseFinal( |
| 1417 | const OMPLoopDirective &D, |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1418 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1419 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1420 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1421 | llvm::BasicBlock *DoneBB = nullptr; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1422 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1423 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1424 | auto IC = C->varlist_begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1425 | for (auto *F : C->finals()) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1426 | if (!DoneBB) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1427 | if (auto *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1428 | // If the first post-update expression is found, emit conditional |
| 1429 | // block if it was requested. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1430 | auto *ThenBB = createBasicBlock(".omp.linear.pu"); |
| 1431 | DoneBB = createBasicBlock(".omp.linear.pu.done"); |
| 1432 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1433 | EmitBlock(ThenBB); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1436 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
| 1437 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1438 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1439 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1440 | Address OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1441 | CodeGenFunction::OMPPrivateScope VarScope(*this); |
| 1442 | VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1443 | (void)VarScope.Privatize(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1444 | EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1445 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1446 | } |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 1447 | if (auto *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1448 | EmitIgnoredExpr(PostUpdate); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1449 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1450 | if (DoneBB) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1451 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1454 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1455 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1456 | if (!CGF.HaveInsertPoint()) |
| 1457 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1458 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1459 | unsigned ClauseAlignment = 0; |
| 1460 | if (auto AlignmentExpr = Clause->getAlignment()) { |
| 1461 | auto AlignmentCI = |
| 1462 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 1463 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1464 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1465 | for (auto E : Clause->varlists()) { |
| 1466 | unsigned Alignment = ClauseAlignment; |
| 1467 | if (Alignment == 0) { |
| 1468 | // OpenMP [2.8.1, Description] |
| 1469 | // If no optional parameter is specified, implementation-defined default |
| 1470 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1471 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1472 | CGF.getContext() |
| 1473 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1474 | E->getType()->getPointeeType())) |
| 1475 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1476 | } |
| 1477 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 1478 | "alignment is not power of 2"); |
| 1479 | if (Alignment != 0) { |
| 1480 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
| 1481 | CGF.EmitAlignmentAssumption(PtrValue, Alignment); |
| 1482 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1487 | void CodeGenFunction::EmitOMPPrivateLoopCounters( |
| 1488 | const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { |
| 1489 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1490 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1491 | auto I = S.private_counters().begin(); |
| 1492 | for (auto *E : S.counters()) { |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1493 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1494 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1495 | (void)LoopScope.addPrivate(VD, [&]() -> Address { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1496 | // Emit var without initialization. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1497 | if (!LocalDeclMap.count(PrivateVD)) { |
| 1498 | auto VarEmission = EmitAutoVarAlloca(*PrivateVD); |
| 1499 | EmitAutoVarCleanups(VarEmission); |
| 1500 | } |
| 1501 | DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), |
| 1502 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1503 | (*I)->getType(), VK_LValue, (*I)->getExprLoc()); |
| 1504 | return EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1505 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1506 | if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || |
| 1507 | VD->hasGlobalStorage()) { |
| 1508 | (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address { |
| 1509 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 1510 | LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), |
| 1511 | E->getType(), VK_LValue, E->getExprLoc()); |
| 1512 | return EmitLValue(&DRE).getAddress(); |
| 1513 | }); |
| 1514 | } |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1515 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1516 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1519 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1520 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1521 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1522 | if (!CGF.HaveInsertPoint()) |
| 1523 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1524 | { |
| 1525 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1526 | CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1527 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1528 | // Get initial values of real counters. |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 1529 | for (auto I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1530 | CGF.EmitIgnoredExpr(I); |
| 1531 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1532 | } |
| 1533 | // Check that loop is executed at least one time. |
| 1534 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
| 1535 | } |
| 1536 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1537 | void CodeGenFunction::EmitOMPLinearClause( |
| 1538 | const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { |
| 1539 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1540 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1541 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1542 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 1543 | auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1544 | for (auto *C : LoopDirective->counters()) { |
| 1545 | SIMDLCVs.insert( |
| 1546 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1547 | } |
| 1548 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1549 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1550 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 1551 | for (auto *E : C->varlists()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1552 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1553 | auto *PrivateVD = |
| 1554 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1555 | if (!SIMDLCVs.count(VD->getCanonicalDecl())) { |
| 1556 | bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address { |
| 1557 | // Emit private VarDecl with copy init. |
| 1558 | EmitVarDecl(*PrivateVD); |
| 1559 | return GetAddrOfLocalVar(PrivateVD); |
| 1560 | }); |
| 1561 | assert(IsRegistered && "linear var already registered as private"); |
| 1562 | // Silence the warning about unused variable. |
| 1563 | (void)IsRegistered; |
| 1564 | } else |
| 1565 | EmitVarDecl(*PrivateVD); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1566 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | } |
| 1570 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1571 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1572 | const OMPExecutableDirective &D, |
| 1573 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1574 | if (!CGF.HaveInsertPoint()) |
| 1575 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1576 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1577 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1578 | /*ignoreResult=*/true); |
| 1579 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
| 1580 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1581 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1582 | // the memory instructions parallel, because loop-carried |
| 1583 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1584 | if (!IsMonotonic) |
| 1585 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1586 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1587 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1588 | /*ignoreResult=*/true); |
| 1589 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1590 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1591 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1592 | // the memory instructions parallel, because loop-carried |
| 1593 | // dependences of 'safelen' iterations are possible. |
| 1594 | CGF.LoopStack.setParallel(false); |
| 1595 | } |
| 1596 | } |
| 1597 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1598 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1599 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1600 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1601 | LoopStack.setParallel(!IsMonotonic); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1602 | LoopStack.setVectorizeEnable(true); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1603 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1606 | void CodeGenFunction::EmitOMPSimdFinal( |
| 1607 | const OMPLoopDirective &D, |
| 1608 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1609 | if (!HaveInsertPoint()) |
| 1610 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1611 | llvm::BasicBlock *DoneBB = nullptr; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1612 | auto IC = D.counters().begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1613 | auto IPC = D.private_counters().begin(); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1614 | for (auto F : D.finals()) { |
| 1615 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1616 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); |
| 1617 | auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); |
| 1618 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || |
| 1619 | OrigVD->hasGlobalStorage() || CED) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1620 | if (!DoneBB) { |
| 1621 | if (auto *Cond = CondGen(*this)) { |
| 1622 | // If the first post-update expression is found, emit conditional |
| 1623 | // block if it was requested. |
| 1624 | auto *ThenBB = createBasicBlock(".omp.final.then"); |
| 1625 | DoneBB = createBasicBlock(".omp.final.done"); |
| 1626 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1627 | EmitBlock(ThenBB); |
| 1628 | } |
| 1629 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1630 | Address OrigAddr = Address::invalid(); |
| 1631 | if (CED) |
| 1632 | OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); |
| 1633 | else { |
| 1634 | DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), |
| 1635 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1636 | (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); |
| 1637 | OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1638 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1639 | OMPPrivateScope VarScope(*this); |
| 1640 | VarScope.addPrivate(OrigVD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1641 | [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1642 | (void)VarScope.Privatize(); |
| 1643 | EmitIgnoredExpr(F); |
| 1644 | } |
| 1645 | ++IC; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1646 | ++IPC; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1647 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1648 | if (DoneBB) |
| 1649 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1652 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1653 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 1654 | OMPLoopScope PreInitScope(CGF, S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1655 | // if (PreCond) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1656 | // for (IV in 0..LastIteration) BODY; |
| 1657 | // <Final counter/linear vars updates>; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1658 | // } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1659 | // |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1660 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1661 | // Emit: if (PreCond) - begin. |
| 1662 | // If the condition constant folds and can be elided, avoid emitting the |
| 1663 | // whole loop. |
| 1664 | bool CondConstant; |
| 1665 | llvm::BasicBlock *ContBlock = nullptr; |
| 1666 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1667 | if (!CondConstant) |
| 1668 | return; |
| 1669 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1670 | auto *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
| 1671 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1672 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 1673 | CGF.getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1674 | CGF.EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1675 | CGF.incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1676 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Emit the loop iteration variable. |
| 1679 | const Expr *IVExpr = S.getIterationVariable(); |
| 1680 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 1681 | CGF.EmitVarDecl(*IVDecl); |
| 1682 | CGF.EmitIgnoredExpr(S.getInit()); |
| 1683 | |
| 1684 | // Emit the iterations count variable. |
| 1685 | // If it is not a variable, Sema decided to calculate iterations count on |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1686 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1687 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 1688 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1689 | // Emit calculation of the iterations count. |
| 1690 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1691 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1692 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1693 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1694 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1695 | emitAlignedClause(CGF, S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1696 | CGF.EmitOMPLinearClauseInit(S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1697 | { |
| 1698 | OMPPrivateScope LoopScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1699 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
| 1700 | CGF.EmitOMPLinearClause(S, LoopScope); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1701 | CGF.EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 1702 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1703 | bool HasLastprivateClause = |
| 1704 | CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1705 | (void)LoopScope.Privatize(); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1706 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1707 | S.getInc(), |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1708 | [&S](CodeGenFunction &CGF) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1709 | CGF.EmitOMPLoopBody(S, JumpDest()); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1710 | CGF.EmitStopPoint(&S); |
| 1711 | }, |
| 1712 | [](CodeGenFunction &) {}); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1713 | CGF.EmitOMPSimdFinal( |
| 1714 | S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1715 | // Emit final copy of the lastprivate variables at the end of loops. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1716 | if (HasLastprivateClause) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1717 | CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1718 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1719 | emitPostUpdateForReductionClause( |
| 1720 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1721 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1722 | CGF.EmitOMPLinearClauseFinal( |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1723 | S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1724 | // Emit: if (PreCond) - end. |
| 1725 | if (ContBlock) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1726 | CGF.EmitBranch(ContBlock); |
| 1727 | CGF.EmitBlock(ContBlock, true); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1728 | } |
| 1729 | }; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 1730 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1731 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1734 | void CodeGenFunction::EmitOMPOuterLoop(bool DynamicOrOrdered, bool IsMonotonic, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1735 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
| 1736 | Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1737 | auto &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1738 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1739 | const Expr *IVExpr = S.getIterationVariable(); |
| 1740 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1741 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1742 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1743 | auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
| 1744 | |
| 1745 | // Start the loop with a block that tests the condition. |
| 1746 | auto CondBlock = createBasicBlock("omp.dispatch.cond"); |
| 1747 | EmitBlock(CondBlock); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1748 | const SourceRange &R = S.getSourceRange(); |
| 1749 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1750 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1751 | |
| 1752 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1753 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1754 | // UB = min(UB, GlobalUB) |
| 1755 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 1756 | // IV = LB |
| 1757 | EmitIgnoredExpr(S.getInit()); |
| 1758 | // IV < UB |
Alexey Bataev | ae05c29 | 2015-06-16 11:59:36 +0000 | [diff] [blame] | 1759 | BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1760 | } else { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 1761 | BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, IL, |
| 1762 | LB, UB, ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1763 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1764 | |
| 1765 | // If there are any cleanups between here and the loop-exit scope, |
| 1766 | // create a block to stage a loop exit along. |
| 1767 | auto ExitBlock = LoopExit.getBlock(); |
| 1768 | if (LoopScope.requiresCleanups()) |
| 1769 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 1770 | |
| 1771 | auto LoopBody = createBasicBlock("omp.dispatch.body"); |
| 1772 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 1773 | if (ExitBlock != LoopExit.getBlock()) { |
| 1774 | EmitBlock(ExitBlock); |
| 1775 | EmitBranchThroughCleanup(LoopExit); |
| 1776 | } |
| 1777 | EmitBlock(LoopBody); |
| 1778 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1779 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 1780 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1781 | if (DynamicOrOrdered) |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1782 | EmitIgnoredExpr(S.getInit()); |
| 1783 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1784 | // Create a block for the increment. |
| 1785 | auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
| 1786 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1787 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1788 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 1789 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1790 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1791 | LoopStack.setParallel(!IsMonotonic); |
| 1792 | else |
| 1793 | EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1794 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1795 | SourceLocation Loc = S.getLocStart(); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1796 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), |
| 1797 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 1798 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1799 | CGF.EmitStopPoint(&S); |
| 1800 | }, |
| 1801 | [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) { |
| 1802 | if (Ordered) { |
| 1803 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd( |
| 1804 | CGF, Loc, IVSize, IVSigned); |
| 1805 | } |
| 1806 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1807 | |
| 1808 | EmitBlock(Continue.getBlock()); |
| 1809 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1810 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1811 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
| 1812 | EmitIgnoredExpr(S.getNextLowerBound()); |
| 1813 | EmitIgnoredExpr(S.getNextUpperBound()); |
| 1814 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1815 | |
| 1816 | EmitBranch(CondBlock); |
| 1817 | LoopStack.pop(); |
| 1818 | // Emit the fall-through block. |
| 1819 | EmitBlock(LoopExit.getBlock()); |
| 1820 | |
| 1821 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1822 | auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { |
| 1823 | if (!DynamicOrOrdered) |
| 1824 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd()); |
| 1825 | }; |
| 1826 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | void CodeGenFunction::EmitOMPForOuterLoop( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1830 | const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1831 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
| 1832 | Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { |
| 1833 | auto &RT = CGM.getOpenMPRuntime(); |
| 1834 | |
| 1835 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1836 | const bool DynamicOrOrdered = |
| 1837 | Ordered || RT.isDynamic(ScheduleKind.Schedule); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1838 | |
| 1839 | assert((Ordered || |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1840 | !RT.isStaticNonchunked(ScheduleKind.Schedule, |
| 1841 | /*Chunked=*/Chunk != nullptr)) && |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1842 | "static non-chunked schedule does not need outer loop"); |
| 1843 | |
| 1844 | // Emit outer loop. |
| 1845 | // |
| 1846 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1847 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 1848 | // distributed to threads in the team in chunks as the threads request them. |
| 1849 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1850 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 1851 | // iterations, except for the last chunk to be distributed, which may have |
| 1852 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 1853 | // |
| 1854 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 1855 | // to threads in the team in chunks as the executing threads request them. |
| 1856 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1857 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 1858 | // each chunk is proportional to the number of unassigned iterations divided |
| 1859 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 1860 | // with value k (greater than 1), the size of each chunk is determined in the |
| 1861 | // same way, with the restriction that the chunks do not contain fewer than k |
| 1862 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 1863 | // than k iterations). |
| 1864 | // |
| 1865 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 1866 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 1867 | // implementation the freedom to choose any possible mapping of iterations to |
| 1868 | // threads in the team. |
| 1869 | // |
| 1870 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 1871 | // deferred until run time, and the schedule and chunk size are taken from the |
| 1872 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 1873 | // implementation defined |
| 1874 | // |
| 1875 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 1876 | // idx = LB; |
| 1877 | // while (idx <= UB) { BODY; ++idx; |
| 1878 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 1879 | // } // inner loop |
| 1880 | // } |
| 1881 | // |
| 1882 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1883 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 1884 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 1885 | // the team in a round-robin fashion in the order of the thread number. |
| 1886 | // |
| 1887 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 1888 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 1889 | // LB = LB + ST; |
| 1890 | // UB = UB + ST; |
| 1891 | // } |
| 1892 | // |
| 1893 | |
| 1894 | const Expr *IVExpr = S.getIterationVariable(); |
| 1895 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1896 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1897 | |
| 1898 | if (DynamicOrOrdered) { |
| 1899 | llvm::Value *UBVal = EmitScalarExpr(S.getLastIteration()); |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1900 | RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, IVSize, |
| 1901 | IVSigned, Ordered, UBVal, Chunk); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1902 | } else { |
| 1903 | RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, |
| 1904 | Ordered, IL, LB, UB, ST, Chunk); |
| 1905 | } |
| 1906 | |
Carlo Bertolli | 0ff587d | 2016-03-07 16:19:13 +0000 | [diff] [blame] | 1907 | EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, Ordered, LB, UB, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1908 | ST, IL, Chunk); |
| 1909 | } |
| 1910 | |
| 1911 | void CodeGenFunction::EmitOMPDistributeOuterLoop( |
| 1912 | OpenMPDistScheduleClauseKind ScheduleKind, |
| 1913 | const OMPDistributeDirective &S, OMPPrivateScope &LoopScope, |
| 1914 | Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { |
| 1915 | |
| 1916 | auto &RT = CGM.getOpenMPRuntime(); |
| 1917 | |
| 1918 | // Emit outer loop. |
| 1919 | // Same behavior as a OMPForOuterLoop, except that schedule cannot be |
| 1920 | // dynamic |
| 1921 | // |
| 1922 | |
| 1923 | const Expr *IVExpr = S.getIterationVariable(); |
| 1924 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1925 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1926 | |
| 1927 | RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, |
| 1928 | IVSize, IVSigned, /* Ordered = */ false, |
| 1929 | IL, LB, UB, ST, Chunk); |
| 1930 | |
| 1931 | EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, |
| 1932 | S, LoopScope, /* Ordered = */ false, LB, UB, ST, IL, Chunk); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 1935 | void CodeGenFunction::EmitOMPDistributeParallelForDirective( |
| 1936 | const OMPDistributeParallelForDirective &S) { |
| 1937 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 1938 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 1939 | *this, OMPD_distribute_parallel_for, |
| 1940 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 1941 | OMPLoopScope PreInitScope(CGF, S); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1942 | OMPCancelStackRAII CancelRegion(CGF, OMPD_distribute_parallel_for, |
| 1943 | /*HasCancel=*/false); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 1944 | CGF.EmitStmt( |
| 1945 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1946 | }); |
| 1947 | } |
| 1948 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 1949 | void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( |
| 1950 | const OMPDistributeParallelForSimdDirective &S) { |
| 1951 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 1952 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 1953 | *this, OMPD_distribute_parallel_for_simd, |
| 1954 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 1955 | OMPLoopScope PreInitScope(CGF, S); |
| 1956 | CGF.EmitStmt( |
| 1957 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1958 | }); |
| 1959 | } |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 1960 | |
| 1961 | void CodeGenFunction::EmitOMPDistributeSimdDirective( |
| 1962 | const OMPDistributeSimdDirective &S) { |
| 1963 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 1964 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 1965 | *this, OMPD_distribute_simd, |
| 1966 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 1967 | OMPLoopScope PreInitScope(CGF, S); |
| 1968 | CGF.EmitStmt( |
| 1969 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1970 | }); |
| 1971 | } |
| 1972 | |
Kelvin Li | a579b91 | 2016-07-14 02:54:56 +0000 | [diff] [blame] | 1973 | void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( |
| 1974 | const OMPTargetParallelForSimdDirective &S) { |
| 1975 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 1976 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 1977 | *this, OMPD_target_parallel_for_simd, |
| 1978 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 1979 | OMPLoopScope PreInitScope(CGF, S); |
| 1980 | CGF.EmitStmt( |
| 1981 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1982 | }); |
| 1983 | } |
| 1984 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 1985 | void CodeGenFunction::EmitOMPTargetSimdDirective( |
| 1986 | const OMPTargetSimdDirective &S) { |
| 1987 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 1988 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 1989 | *this, OMPD_target_simd, [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 1990 | OMPLoopScope PreInitScope(CGF, S); |
| 1991 | CGF.EmitStmt( |
| 1992 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1993 | }); |
| 1994 | } |
| 1995 | |
Kelvin Li | 0253287 | 2016-08-05 14:37:37 +0000 | [diff] [blame] | 1996 | void CodeGenFunction::EmitOMPTeamsDistributeDirective( |
| 1997 | const OMPTeamsDistributeDirective &S) { |
| 1998 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 1999 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2000 | *this, OMPD_teams_distribute, |
| 2001 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2002 | OMPLoopScope PreInitScope(CGF, S); |
| 2003 | CGF.EmitStmt( |
| 2004 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2005 | }); |
| 2006 | } |
| 2007 | |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2008 | void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( |
| 2009 | const OMPTeamsDistributeSimdDirective &S) { |
| 2010 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 2011 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2012 | *this, OMPD_teams_distribute_simd, |
| 2013 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2014 | OMPLoopScope PreInitScope(CGF, S); |
| 2015 | CGF.EmitStmt( |
| 2016 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2017 | }); |
| 2018 | } |
| 2019 | |
Kelvin Li | 579e41c | 2016-11-30 23:51:03 +0000 | [diff] [blame] | 2020 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( |
| 2021 | const OMPTeamsDistributeParallelForSimdDirective &S) { |
| 2022 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 2023 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2024 | *this, OMPD_teams_distribute_parallel_for_simd, |
| 2025 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2026 | OMPLoopScope PreInitScope(CGF, S); |
| 2027 | CGF.EmitStmt( |
| 2028 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2029 | }); |
| 2030 | } |
Kelvin Li | 4e325f7 | 2016-10-25 12:50:55 +0000 | [diff] [blame] | 2031 | |
Kelvin Li | 7ade93f | 2016-12-09 03:24:30 +0000 | [diff] [blame] | 2032 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( |
| 2033 | const OMPTeamsDistributeParallelForDirective &S) { |
| 2034 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
| 2035 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2036 | *this, OMPD_teams_distribute_parallel_for, |
| 2037 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2038 | OMPLoopScope PreInitScope(CGF, S); |
| 2039 | CGF.EmitStmt( |
| 2040 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2041 | }); |
| 2042 | } |
| 2043 | |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2044 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( |
| 2045 | const OMPTargetTeamsDistributeDirective &S) { |
Kelvin Li | 26fd21a | 2016-12-28 17:57:07 +0000 | [diff] [blame] | 2046 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2047 | *this, OMPD_target_teams_distribute, |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2048 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Kelvin Li | 26fd21a | 2016-12-28 17:57:07 +0000 | [diff] [blame] | 2049 | CGF.EmitStmt( |
| 2050 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Kelvin Li | 83c451e | 2016-12-25 04:52:54 +0000 | [diff] [blame] | 2051 | }); |
| 2052 | } |
| 2053 | |
Kelvin Li | 80e8f56 | 2016-12-29 22:16:30 +0000 | [diff] [blame] | 2054 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( |
| 2055 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 2056 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2057 | *this, OMPD_target_teams_distribute_parallel_for, |
| 2058 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2059 | CGF.EmitStmt( |
| 2060 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2061 | }); |
| 2062 | } |
| 2063 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2064 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2065 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 2066 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2067 | *this, OMPD_target_teams_distribute_parallel_for_simd, |
| 2068 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2069 | CGF.EmitStmt( |
| 2070 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2071 | }); |
| 2072 | } |
| 2073 | |
Kelvin Li | da68118 | 2017-01-10 18:08:18 +0000 | [diff] [blame] | 2074 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( |
| 2075 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 2076 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2077 | *this, OMPD_target_teams_distribute_simd, |
| 2078 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2079 | CGF.EmitStmt( |
| 2080 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2081 | }); |
| 2082 | } |
| 2083 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2084 | /// \brief Emit a helper variable and return corresponding lvalue. |
| 2085 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 2086 | const DeclRefExpr *Helper) { |
| 2087 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 2088 | CGF.EmitVarDecl(*VDecl); |
| 2089 | return CGF.EmitLValue(Helper); |
| 2090 | } |
| 2091 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2092 | namespace { |
| 2093 | struct ScheduleKindModifiersTy { |
| 2094 | OpenMPScheduleClauseKind Kind; |
| 2095 | OpenMPScheduleClauseModifier M1; |
| 2096 | OpenMPScheduleClauseModifier M2; |
| 2097 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 2098 | OpenMPScheduleClauseModifier M1, |
| 2099 | OpenMPScheduleClauseModifier M2) |
| 2100 | : Kind(Kind), M1(M1), M2(M2) {} |
| 2101 | }; |
| 2102 | } // namespace |
| 2103 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2104 | bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2105 | // Emit the loop iteration variable. |
| 2106 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2107 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 2108 | EmitVarDecl(*IVDecl); |
| 2109 | |
| 2110 | // Emit the iterations count variable. |
| 2111 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2112 | // iteration (e.g., it is foldable into a constant). |
| 2113 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 2114 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2115 | // Emit calculation of the iterations count. |
| 2116 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2117 | } |
| 2118 | |
| 2119 | auto &RT = CGM.getOpenMPRuntime(); |
| 2120 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2121 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2122 | // Check pre-condition. |
| 2123 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2124 | OMPLoopScope PreInitScope(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2125 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2126 | // If the condition constant folds and can be elided, avoid emitting the |
| 2127 | // whole loop. |
| 2128 | bool CondConstant; |
| 2129 | llvm::BasicBlock *ContBlock = nullptr; |
| 2130 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2131 | if (!CondConstant) |
| 2132 | return false; |
| 2133 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2134 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 2135 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2136 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2137 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2138 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2139 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2140 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2141 | |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2142 | bool Ordered = false; |
| 2143 | if (auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) { |
| 2144 | if (OrderedClause->getNumForLoops()) |
| 2145 | RT.emitDoacrossInit(*this, S); |
| 2146 | else |
| 2147 | Ordered = true; |
| 2148 | } |
| 2149 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2150 | llvm::DenseSet<const Expr *> EmittedFinals; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2151 | emitAlignedClause(*this, S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2152 | EmitOMPLinearClauseInit(S); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2153 | // Emit helper vars inits. |
| 2154 | LValue LB = |
| 2155 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 2156 | LValue UB = |
| 2157 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 2158 | LValue ST = |
| 2159 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 2160 | LValue IL = |
| 2161 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 2162 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2163 | // Emit 'then' code. |
| 2164 | { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2165 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2166 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
| 2167 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2168 | // initialization of firstprivate variables and post-update of |
| 2169 | // lastprivate variables. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2170 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 2171 | *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 2172 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2173 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 2174 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2175 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 2176 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2177 | EmitOMPPrivateLoopCounters(S, LoopScope); |
| 2178 | EmitOMPLinearClause(S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 2179 | (void)LoopScope.Privatize(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2180 | |
| 2181 | // Detect the loop schedule kind and chunk. |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2182 | llvm::Value *Chunk = nullptr; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2183 | OpenMPScheduleTy ScheduleKind; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2184 | if (auto *C = S.getSingleClause<OMPScheduleClause>()) { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2185 | ScheduleKind.Schedule = C->getScheduleKind(); |
| 2186 | ScheduleKind.M1 = C->getFirstScheduleModifier(); |
| 2187 | ScheduleKind.M2 = C->getSecondScheduleModifier(); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2188 | if (const auto *Ch = C->getChunkSize()) { |
| 2189 | Chunk = EmitScalarExpr(Ch); |
| 2190 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
| 2191 | S.getIterationVariable()->getType(), |
| 2192 | S.getLocStart()); |
| 2193 | } |
| 2194 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2195 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2196 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2197 | // OpenMP 4.5, 2.7.1 Loop Construct, Description. |
| 2198 | // If the static schedule kind is specified or if the ordered clause is |
| 2199 | // specified, and if no monotonic modifier is specified, the effect will |
| 2200 | // be as if the monotonic modifier was specified. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2201 | if (RT.isStaticNonchunked(ScheduleKind.Schedule, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2202 | /* Chunked */ Chunk != nullptr) && |
| 2203 | !Ordered) { |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2204 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 2205 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2206 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2207 | // When no chunk_size is specified, the iteration space is divided into |
| 2208 | // chunks that are approximately equal in size, and at most one chunk is |
| 2209 | // distributed to each thread. Note that the size of the chunks is |
| 2210 | // unspecified in this case. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2211 | RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, |
| 2212 | IVSize, IVSigned, Ordered, |
| 2213 | IL.getAddress(), LB.getAddress(), |
| 2214 | UB.getAddress(), ST.getAddress()); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2215 | auto LoopExit = |
| 2216 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2217 | // UB = min(UB, GlobalUB); |
| 2218 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 2219 | // IV = LB; |
| 2220 | EmitIgnoredExpr(S.getInit()); |
| 2221 | // while (idx <= UB) { BODY; ++idx; } |
Alexey Bataev | ae05c29 | 2015-06-16 11:59:36 +0000 | [diff] [blame] | 2222 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 2223 | S.getInc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2224 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 2225 | CGF.EmitOMPLoopBody(S, LoopExit); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2226 | CGF.EmitStopPoint(&S); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2227 | }, |
| 2228 | [](CodeGenFunction &) {}); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2229 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2230 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2231 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 2232 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd()); |
| 2233 | }; |
| 2234 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2235 | } else { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2236 | const bool IsMonotonic = |
| 2237 | Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static || |
| 2238 | ScheduleKind.Schedule == OMPC_SCHEDULE_unknown || |
| 2239 | ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic || |
| 2240 | ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic; |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2241 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 2242 | // runtime and runs the inner loop to process it. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2243 | EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2244 | LB.getAddress(), UB.getAddress(), ST.getAddress(), |
| 2245 | IL.getAddress(), Chunk); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2246 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2247 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2248 | EmitOMPSimdFinal(S, |
| 2249 | [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 2250 | return CGF.Builder.CreateIsNotNull( |
| 2251 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2252 | }); |
| 2253 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2254 | EmitOMPReductionClauseFinal( |
| 2255 | S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind()) |
| 2256 | ? /*Parallel and Simd*/ OMPD_parallel_for_simd |
| 2257 | : /*Parallel only*/ OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2258 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2259 | emitPostUpdateForReductionClause( |
| 2260 | *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 2261 | return CGF.Builder.CreateIsNotNull( |
| 2262 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2263 | }); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2264 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2265 | if (HasLastprivateClause) |
| 2266 | EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2267 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 2268 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2269 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2270 | EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2271 | return CGF.Builder.CreateIsNotNull( |
| 2272 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2273 | }); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2274 | // We're now done with the loop, so jump to the continuation block. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2275 | if (ContBlock) { |
| 2276 | EmitBranch(ContBlock); |
| 2277 | EmitBlock(ContBlock, true); |
| 2278 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2279 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2280 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2284 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2285 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2286 | PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2287 | OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2288 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S); |
| 2289 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2290 | { |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2291 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2292 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 2293 | S.hasCancel()); |
| 2294 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2295 | |
| 2296 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2297 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2298 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 2299 | } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2300 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2301 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2302 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2303 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2304 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2305 | PrePostActionTy &) { |
| 2306 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S); |
| 2307 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2308 | { |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2309 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2310 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2311 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2312 | |
| 2313 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2314 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2315 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 2316 | } |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2319 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 2320 | const Twine &Name, |
| 2321 | llvm::Value *Init = nullptr) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2322 | auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2323 | if (Init) |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 2324 | CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2325 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2328 | void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2329 | auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt(); |
| 2330 | auto *CS = dyn_cast<CompoundStmt>(Stmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2331 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2332 | auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF, |
| 2333 | PrePostActionTy &) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2334 | auto &C = CGF.CGM.getContext(); |
| 2335 | auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 2336 | // Emit helper vars inits. |
| 2337 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 2338 | CGF.Builder.getInt32(0)); |
| 2339 | auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1) |
| 2340 | : CGF.Builder.getInt32(0); |
| 2341 | LValue UB = |
| 2342 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 2343 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 2344 | CGF.Builder.getInt32(1)); |
| 2345 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 2346 | CGF.Builder.getInt32(0)); |
| 2347 | // Loop counter. |
| 2348 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
| 2349 | OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 2350 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
| 2351 | OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 2352 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 2353 | // Generate condition for loop. |
| 2354 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 2355 | OK_Ordinary, S.getLocStart(), FPOptions()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2356 | // Increment for loop counter. |
| 2357 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
| 2358 | S.getLocStart()); |
| 2359 | auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) { |
| 2360 | // Iterate through all sections and emit a switch construct: |
| 2361 | // switch (IV) { |
| 2362 | // case 0: |
| 2363 | // <SectionStmt[0]>; |
| 2364 | // break; |
| 2365 | // ... |
| 2366 | // case <NumSection> - 1: |
| 2367 | // <SectionStmt[<NumSection> - 1]>; |
| 2368 | // break; |
| 2369 | // } |
| 2370 | // .omp.sections.exit: |
| 2371 | auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 2372 | auto *SwitchStmt = CGF.Builder.CreateSwitch( |
| 2373 | CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB, |
| 2374 | CS == nullptr ? 1 : CS->size()); |
| 2375 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2376 | unsigned CaseNumber = 0; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2377 | for (auto *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2378 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2379 | CGF.EmitBlock(CaseBB); |
| 2380 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2381 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2382 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2383 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2384 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2385 | } else { |
| 2386 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2387 | CGF.EmitBlock(CaseBB); |
| 2388 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
| 2389 | CGF.EmitStmt(Stmt); |
| 2390 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2391 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2392 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2393 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2394 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2395 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2396 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2397 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2398 | // initialization of firstprivate variables and post-update of lastprivate |
| 2399 | // variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2400 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 2401 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 2402 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2403 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2404 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 2405 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2406 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 2407 | (void)LoopScope.Privatize(); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2408 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2409 | // Emit static non-chunked loop. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2410 | OpenMPScheduleTy ScheduleKind; |
| 2411 | ScheduleKind.Schedule = OMPC_SCHEDULE_static; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2412 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2413 | CGF, S.getLocStart(), ScheduleKind, /*IVSize=*/32, |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2414 | /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(), |
| 2415 | UB.getAddress(), ST.getAddress()); |
| 2416 | // UB = min(UB, GlobalUB); |
| 2417 | auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart()); |
| 2418 | auto *MinUBGlobalUB = CGF.Builder.CreateSelect( |
| 2419 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 2420 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 2421 | // IV = LB; |
| 2422 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV); |
| 2423 | // while (idx <= UB) { BODY; ++idx; } |
| 2424 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 2425 | [](CodeGenFunction &) {}); |
| 2426 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2427 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 2428 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd()); |
| 2429 | }; |
| 2430 | CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2431 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2432 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2433 | emitPostUpdateForReductionClause( |
| 2434 | CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 2435 | return CGF.Builder.CreateIsNotNull( |
| 2436 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2437 | }); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2438 | |
| 2439 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2440 | if (HasLastprivates) |
| 2441 | CGF.EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2442 | S, /*NoFinals=*/false, |
| 2443 | CGF.Builder.CreateIsNotNull( |
| 2444 | CGF.EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2445 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2446 | |
| 2447 | bool HasCancel = false; |
| 2448 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 2449 | HasCancel = OSD->hasCancel(); |
| 2450 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 2451 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2452 | OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2453 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 2454 | HasCancel); |
| 2455 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 2456 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 2457 | // directive. |
| 2458 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2459 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 2460 | // initialization of firstprivate variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2461 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 2462 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2463 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2464 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2465 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2466 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2467 | { |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2468 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2469 | EmitSections(S); |
| 2470 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2471 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2472 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2473 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 2474 | OMPD_sections); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2475 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2479 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2480 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2481 | }; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2482 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2483 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 2484 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2487 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2488 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2489 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2490 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2491 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2492 | // Check if there are any 'copyprivate' clauses associated with this |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2493 | // 'single' construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2494 | // Build a list of copyprivate variables along with helper expressions |
| 2495 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2496 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2497 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2498 | DestExprs.append(C->destination_exprs().begin(), |
| 2499 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2500 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2501 | AssignmentOps.append(C->assignment_ops().begin(), |
| 2502 | C->assignment_ops().end()); |
| 2503 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2504 | // Emit code for 'single' region along with 'copyprivate' clauses |
| 2505 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2506 | Action.Enter(CGF); |
| 2507 | OMPPrivateScope SingleScope(CGF); |
| 2508 | (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); |
| 2509 | CGF.EmitOMPPrivateClause(S, SingleScope); |
| 2510 | (void)SingleScope.Privatize(); |
| 2511 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2512 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2513 | { |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2514 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2515 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), |
| 2516 | CopyprivateVars, DestExprs, |
| 2517 | SrcExprs, AssignmentOps); |
| 2518 | } |
| 2519 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 2520 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2521 | if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 2522 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 2523 | *this, S.getLocStart(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2524 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2525 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2528 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2529 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2530 | Action.Enter(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2531 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2532 | }; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2533 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2534 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2537 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2538 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2539 | Action.Enter(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2540 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2541 | }; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2542 | Expr *Hint = nullptr; |
| 2543 | if (auto *HintClause = S.getSingleClause<OMPHintClause>()) |
| 2544 | Hint = HintClause->getHint(); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2545 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2546 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 2547 | S.getDirectiveName().getAsString(), |
| 2548 | CodeGen, S.getLocStart(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2551 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 2552 | const OMPParallelForDirective &S) { |
| 2553 | // Emit directive as a combined directive that consists of two implicit |
| 2554 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2555 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2556 | OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2557 | CGF.EmitOMPWorksharingLoop(S); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2558 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2559 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2562 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2563 | const OMPParallelForSimdDirective &S) { |
| 2564 | // Emit directive as a combined directive that consists of two implicit |
| 2565 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2566 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2567 | CGF.EmitOMPWorksharingLoop(S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2568 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2569 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2572 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2573 | const OMPParallelSectionsDirective &S) { |
| 2574 | // Emit directive as a combined directive that consists of two implicit |
| 2575 | // directives: 'parallel' with 'sections' directive. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2576 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2577 | CGF.EmitSections(S); |
| 2578 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2579 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2582 | void CodeGenFunction::EmitOMPTaskBasedDirective(const OMPExecutableDirective &S, |
| 2583 | const RegionCodeGenTy &BodyGen, |
| 2584 | const TaskGenTy &TaskGen, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2585 | OMPTaskDataTy &Data) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2586 | // Emit outlined function for task construct. |
| 2587 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2588 | auto *I = CS->getCapturedDecl()->param_begin(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2589 | auto *PartId = std::next(I); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2590 | auto *TaskT = std::next(I, 4); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2591 | // Check if the task is final |
| 2592 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
| 2593 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2594 | // the condition and the dead arm of the if/else. |
| 2595 | auto *Cond = Clause->getCondition(); |
| 2596 | bool CondConstant; |
| 2597 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 2598 | Data.Final.setInt(CondConstant); |
| 2599 | else |
| 2600 | Data.Final.setPointer(EvaluateExprAsBool(Cond)); |
| 2601 | } else { |
| 2602 | // By default the task is not final. |
| 2603 | Data.Final.setInt(/*IntVal=*/false); |
| 2604 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2605 | // Check if the task has 'priority' clause. |
| 2606 | if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2607 | auto *Prio = Clause->getPriority(); |
Alexey Bataev | 5140e74 | 2016-07-19 04:21:09 +0000 | [diff] [blame] | 2608 | Data.Priority.setInt(/*IntVal=*/true); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 2609 | Data.Priority.setPointer(EmitScalarConversion( |
| 2610 | EmitScalarExpr(Prio), Prio->getType(), |
| 2611 | getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 2612 | Prio->getExprLoc())); |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2613 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2614 | // The first function argument for tasks is a thread id, the second one is a |
| 2615 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2616 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 2617 | // Get list of private variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2618 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2619 | auto IRef = C->varlist_begin(); |
| 2620 | for (auto *IInit : C->private_copies()) { |
| 2621 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2622 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2623 | Data.PrivateVars.push_back(*IRef); |
| 2624 | Data.PrivateCopies.push_back(IInit); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2625 | } |
| 2626 | ++IRef; |
| 2627 | } |
| 2628 | } |
| 2629 | EmittedAsPrivate.clear(); |
| 2630 | // Get list of firstprivate variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2631 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2632 | auto IRef = C->varlist_begin(); |
| 2633 | auto IElemInitRef = C->inits().begin(); |
| 2634 | for (auto *IInit : C->private_copies()) { |
| 2635 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2636 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2637 | Data.FirstprivateVars.push_back(*IRef); |
| 2638 | Data.FirstprivateCopies.push_back(IInit); |
| 2639 | Data.FirstprivateInits.push_back(*IElemInitRef); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2640 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 2641 | ++IRef; |
| 2642 | ++IElemInitRef; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2643 | } |
| 2644 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2645 | // Get list of lastprivate variables (for taskloops). |
| 2646 | llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; |
| 2647 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 2648 | auto IRef = C->varlist_begin(); |
| 2649 | auto ID = C->destination_exprs().begin(); |
| 2650 | for (auto *IInit : C->private_copies()) { |
| 2651 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2652 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2653 | Data.LastprivateVars.push_back(*IRef); |
| 2654 | Data.LastprivateCopies.push_back(IInit); |
| 2655 | } |
| 2656 | LastprivateDstsOrigs.insert( |
| 2657 | {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), |
| 2658 | cast<DeclRefExpr>(*IRef)}); |
| 2659 | ++IRef; |
| 2660 | ++ID; |
| 2661 | } |
| 2662 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2663 | // Build list of dependences. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2664 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
| 2665 | for (auto *IRef : C->varlists()) |
| 2666 | Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 2667 | auto &&CodeGen = [&Data, CS, &BodyGen, &LastprivateDstsOrigs]( |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2668 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2669 | // Set proper addresses for generated private copies. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2670 | OMPPrivateScope Scope(CGF); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2671 | if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || |
| 2672 | !Data.LastprivateVars.empty()) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2673 | auto *CopyFn = CGF.Builder.CreateLoad( |
| 2674 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); |
| 2675 | auto *PrivatesPtr = CGF.Builder.CreateLoad( |
| 2676 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); |
| 2677 | // Map privates. |
| 2678 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 2679 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 2680 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2681 | for (auto *E : Data.PrivateVars) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2682 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2683 | Address PrivatePtr = CGF.CreateMemTemp( |
| 2684 | CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); |
| 2685 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2686 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2687 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2688 | for (auto *E : Data.FirstprivateVars) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2689 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2690 | Address PrivatePtr = |
| 2691 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2692 | ".firstpriv.ptr.addr"); |
| 2693 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2694 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2695 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2696 | for (auto *E : Data.LastprivateVars) { |
| 2697 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2698 | Address PrivatePtr = |
| 2699 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2700 | ".lastpriv.ptr.addr"); |
| 2701 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2702 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 2703 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2704 | CGF.EmitRuntimeCall(CopyFn, CallArgs); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2705 | for (auto &&Pair : LastprivateDstsOrigs) { |
| 2706 | auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); |
| 2707 | DeclRefExpr DRE( |
| 2708 | const_cast<VarDecl *>(OrigVD), |
| 2709 | /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup( |
| 2710 | OrigVD) != nullptr, |
| 2711 | Pair.second->getType(), VK_LValue, Pair.second->getExprLoc()); |
| 2712 | Scope.addPrivate(Pair.first, [&CGF, &DRE]() { |
| 2713 | return CGF.EmitLValue(&DRE).getAddress(); |
| 2714 | }); |
| 2715 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2716 | for (auto &&Pair : PrivatePtrs) { |
| 2717 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 2718 | CGF.getContext().getDeclAlign(Pair.first)); |
| 2719 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 2720 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2721 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2722 | (void)Scope.Privatize(); |
| 2723 | |
| 2724 | Action.Enter(CGF); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2725 | BodyGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2726 | }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2727 | auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
| 2728 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, |
| 2729 | Data.NumberOfParts); |
| 2730 | OMPLexicalScope Scope(*this, S); |
| 2731 | TaskGen(*this, OutlinedFn, Data); |
| 2732 | } |
| 2733 | |
| 2734 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 2735 | // Emit outlined function for task construct. |
| 2736 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2737 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2738 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2739 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 2740 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 2741 | if (C->getNameModifier() == OMPD_unknown || |
| 2742 | C->getNameModifier() == OMPD_task) { |
| 2743 | IfCond = C->getCondition(); |
| 2744 | break; |
| 2745 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2746 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2747 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2748 | OMPTaskDataTy Data; |
| 2749 | // Check if we should emit tied or untied task. |
| 2750 | Data.Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2751 | auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2752 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 2753 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2754 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2755 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2756 | const OMPTaskDataTy &Data) { |
| 2757 | CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn, |
| 2758 | SharedsTy, CapturedStruct, IfCond, |
| 2759 | Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2760 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2761 | EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2764 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 2765 | const OMPTaskyieldDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2766 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 2769 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2770 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 2773 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
| 2774 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2777 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 2778 | const OMPTaskgroupDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2779 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2780 | Action.Enter(CGF); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2781 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2782 | }; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2783 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2784 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart()); |
| 2785 | } |
| 2786 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 2787 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2788 | CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2789 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2790 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 2791 | FlushClause->varlist_end()); |
| 2792 | } |
| 2793 | return llvm::None; |
| 2794 | }(), S.getLocStart()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2797 | void CodeGenFunction::EmitOMPDistributeLoop(const OMPDistributeDirective &S) { |
| 2798 | // Emit the loop iteration variable. |
| 2799 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2800 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 2801 | EmitVarDecl(*IVDecl); |
| 2802 | |
| 2803 | // Emit the iterations count variable. |
| 2804 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2805 | // iteration (e.g., it is foldable into a constant). |
| 2806 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 2807 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2808 | // Emit calculation of the iterations count. |
| 2809 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2810 | } |
| 2811 | |
| 2812 | auto &RT = CGM.getOpenMPRuntime(); |
| 2813 | |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 2814 | bool HasLastprivateClause = false; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2815 | // Check pre-condition. |
| 2816 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2817 | OMPLoopScope PreInitScope(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2818 | // Skip the entire loop if we don't meet the precondition. |
| 2819 | // If the condition constant folds and can be elided, avoid emitting the |
| 2820 | // whole loop. |
| 2821 | bool CondConstant; |
| 2822 | llvm::BasicBlock *ContBlock = nullptr; |
| 2823 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2824 | if (!CondConstant) |
| 2825 | return; |
| 2826 | } else { |
| 2827 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 2828 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2829 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
| 2830 | getProfileCount(&S)); |
| 2831 | EmitBlock(ThenBlock); |
| 2832 | incrementProfileCounter(&S); |
| 2833 | } |
| 2834 | |
| 2835 | // Emit 'then' code. |
| 2836 | { |
| 2837 | // Emit helper vars inits. |
| 2838 | LValue LB = |
| 2839 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 2840 | LValue UB = |
| 2841 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 2842 | LValue ST = |
| 2843 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 2844 | LValue IL = |
| 2845 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 2846 | |
| 2847 | OMPPrivateScope LoopScope(*this); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 2848 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
| 2849 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 2850 | // initialization of firstprivate variables and post-update of |
| 2851 | // lastprivate variables. |
| 2852 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 2853 | *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 2854 | /*ForceSimpleCall=*/true); |
| 2855 | } |
| 2856 | EmitOMPPrivateClause(S, LoopScope); |
| 2857 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2858 | EmitOMPPrivateLoopCounters(S, LoopScope); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2859 | (void)LoopScope.Privatize(); |
| 2860 | |
| 2861 | // Detect the distribute schedule kind and chunk. |
| 2862 | llvm::Value *Chunk = nullptr; |
| 2863 | OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; |
| 2864 | if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) { |
| 2865 | ScheduleKind = C->getDistScheduleKind(); |
| 2866 | if (const auto *Ch = C->getChunkSize()) { |
| 2867 | Chunk = EmitScalarExpr(Ch); |
| 2868 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
| 2869 | S.getIterationVariable()->getType(), |
| 2870 | S.getLocStart()); |
| 2871 | } |
| 2872 | } |
| 2873 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2874 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 2875 | |
| 2876 | // OpenMP [2.10.8, distribute Construct, Description] |
| 2877 | // If dist_schedule is specified, kind must be static. If specified, |
| 2878 | // iterations are divided into chunks of size chunk_size, chunks are |
| 2879 | // assigned to the teams of the league in a round-robin fashion in the |
| 2880 | // order of the team number. When no chunk_size is specified, the |
| 2881 | // iteration space is divided into chunks that are approximately equal |
| 2882 | // in size, and at most one chunk is distributed to each team of the |
| 2883 | // league. The size of the chunks is unspecified in this case. |
| 2884 | if (RT.isStaticNonchunked(ScheduleKind, |
| 2885 | /* Chunked */ Chunk != nullptr)) { |
| 2886 | RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, |
| 2887 | IVSize, IVSigned, /* Ordered = */ false, |
| 2888 | IL.getAddress(), LB.getAddress(), |
| 2889 | UB.getAddress(), ST.getAddress()); |
| 2890 | auto LoopExit = |
| 2891 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
| 2892 | // UB = min(UB, GlobalUB); |
| 2893 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 2894 | // IV = LB; |
| 2895 | EmitIgnoredExpr(S.getInit()); |
| 2896 | // while (idx <= UB) { BODY; ++idx; } |
| 2897 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 2898 | S.getInc(), |
| 2899 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 2900 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 2901 | CGF.EmitStopPoint(&S); |
| 2902 | }, |
| 2903 | [](CodeGenFunction &) {}); |
| 2904 | EmitBlock(LoopExit.getBlock()); |
| 2905 | // Tell the runtime we are done. |
| 2906 | RT.emitForStaticFinish(*this, S.getLocStart()); |
| 2907 | } else { |
| 2908 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 2909 | // runtime and runs the inner loop to process it. |
| 2910 | EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, |
| 2911 | LB.getAddress(), UB.getAddress(), ST.getAddress(), |
| 2912 | IL.getAddress(), Chunk); |
| 2913 | } |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 2914 | |
| 2915 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2916 | if (HasLastprivateClause) |
| 2917 | EmitOMPLastprivateClauseFinal( |
| 2918 | S, /*NoFinals=*/false, |
| 2919 | Builder.CreateIsNotNull( |
| 2920 | EmitLoadOfScalar(IL, S.getLocStart()))); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | // We're now done with the loop, so jump to the continuation block. |
| 2924 | if (ContBlock) { |
| 2925 | EmitBranch(ContBlock); |
| 2926 | EmitBlock(ContBlock, true); |
| 2927 | } |
| 2928 | } |
| 2929 | } |
| 2930 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2931 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 2932 | const OMPDistributeDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2933 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2934 | CGF.EmitOMPDistributeLoop(S); |
| 2935 | }; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2936 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2937 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen, |
| 2938 | false); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2941 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
| 2942 | const CapturedStmt *S) { |
| 2943 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 2944 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 2945 | CGF.CapturedStmtInfo = &CapStmtInfo; |
| 2946 | auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); |
| 2947 | Fn->addFnAttr(llvm::Attribute::NoInline); |
| 2948 | return Fn; |
| 2949 | } |
| 2950 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2951 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2952 | if (!S.getAssociatedStmt()) { |
| 2953 | for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) |
| 2954 | CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2955 | return; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2956 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2957 | auto *C = S.getSingleClause<OMPSIMDClause>(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2958 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, |
| 2959 | PrePostActionTy &Action) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2960 | if (C) { |
| 2961 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2962 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 2963 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
| 2964 | auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); |
| 2965 | CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars); |
| 2966 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2967 | Action.Enter(CGF); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2968 | CGF.EmitStmt( |
| 2969 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2970 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2971 | }; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 2972 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2973 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2976 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2977 | QualType SrcType, QualType DestType, |
| 2978 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2979 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 2980 | "DestType must have scalar evaluation kind."); |
| 2981 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
| 2982 | return Val.isScalar() |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2983 | ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType, |
| 2984 | Loc) |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2985 | : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2986 | DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | static CodeGenFunction::ComplexPairTy |
| 2990 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2991 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2992 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 2993 | "DestType must have complex evaluation kind."); |
| 2994 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 2995 | if (Val.isScalar()) { |
| 2996 | // Convert the input element to the element type of the complex. |
| 2997 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2998 | auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 2999 | DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3000 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 3001 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 3002 | } else { |
| 3003 | assert(Val.isComplex() && "Must be a scalar or complex."); |
| 3004 | auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 3005 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
| 3006 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3007 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3008 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3009 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3010 | } |
| 3011 | return ComplexVal; |
| 3012 | } |
| 3013 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3014 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 3015 | LValue LVal, RValue RVal) { |
| 3016 | if (LVal.isGlobalReg()) { |
| 3017 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 3018 | } else { |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3019 | CGF.EmitAtomicStore(RVal, LVal, |
| 3020 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3021 | : llvm::AtomicOrdering::Monotonic, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3022 | LVal.isVolatile(), /*IsInit=*/false); |
| 3023 | } |
| 3024 | } |
| 3025 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3026 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 3027 | QualType RValTy, SourceLocation Loc) { |
| 3028 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3029 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3030 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 3031 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 3032 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3033 | break; |
| 3034 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3035 | EmitStoreOfComplex( |
| 3036 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3037 | /*isInit=*/false); |
| 3038 | break; |
| 3039 | case TEK_Aggregate: |
| 3040 | llvm_unreachable("Must be a scalar or complex."); |
| 3041 | } |
| 3042 | } |
| 3043 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3044 | static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3045 | const Expr *X, const Expr *V, |
| 3046 | SourceLocation Loc) { |
| 3047 | // v = x; |
| 3048 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 3049 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 3050 | LValue XLValue = CGF.EmitLValue(X); |
| 3051 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 3052 | RValue Res = XLValue.isGlobalReg() |
| 3053 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3054 | : CGF.EmitAtomicLoad( |
| 3055 | XLValue, Loc, |
| 3056 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3057 | : llvm::AtomicOrdering::Monotonic, |
| 3058 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3059 | // OpenMP, 2.12.6, atomic Construct |
| 3060 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3061 | // performed operation to include an implicit flush operation without a |
| 3062 | // list. |
| 3063 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3064 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3065 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3068 | static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3069 | const Expr *X, const Expr *E, |
| 3070 | SourceLocation Loc) { |
| 3071 | // x = expr; |
| 3072 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3073 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3074 | // OpenMP, 2.12.6, atomic Construct |
| 3075 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3076 | // performed operation to include an implicit flush operation without a |
| 3077 | // list. |
| 3078 | if (IsSeqCst) |
| 3079 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3080 | } |
| 3081 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3082 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 3083 | RValue Update, |
| 3084 | BinaryOperatorKind BO, |
| 3085 | llvm::AtomicOrdering AO, |
| 3086 | bool IsXLHSInRHSPart) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3087 | auto &Context = CGF.CGM.getContext(); |
| 3088 | // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x' |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3089 | // expression is simple and atomic is allowed for the given type for the |
| 3090 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3091 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 3092 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 3093 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 3094 | (Update.getScalarVal()->getType() != |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3095 | X.getAddress().getElementType())) || |
| 3096 | !X.getAddress().getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3097 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 3098 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3099 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3100 | |
| 3101 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 3102 | switch (BO) { |
| 3103 | case BO_Add: |
| 3104 | RMWOp = llvm::AtomicRMWInst::Add; |
| 3105 | break; |
| 3106 | case BO_Sub: |
| 3107 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3108 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3109 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 3110 | break; |
| 3111 | case BO_And: |
| 3112 | RMWOp = llvm::AtomicRMWInst::And; |
| 3113 | break; |
| 3114 | case BO_Or: |
| 3115 | RMWOp = llvm::AtomicRMWInst::Or; |
| 3116 | break; |
| 3117 | case BO_Xor: |
| 3118 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 3119 | break; |
| 3120 | case BO_LT: |
| 3121 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3122 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 3123 | : llvm::AtomicRMWInst::Max) |
| 3124 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 3125 | : llvm::AtomicRMWInst::UMax); |
| 3126 | break; |
| 3127 | case BO_GT: |
| 3128 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3129 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 3130 | : llvm::AtomicRMWInst::Min) |
| 3131 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 3132 | : llvm::AtomicRMWInst::UMin); |
| 3133 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3134 | case BO_Assign: |
| 3135 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 3136 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3137 | case BO_Mul: |
| 3138 | case BO_Div: |
| 3139 | case BO_Rem: |
| 3140 | case BO_Shl: |
| 3141 | case BO_Shr: |
| 3142 | case BO_LAnd: |
| 3143 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3144 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3145 | case BO_PtrMemD: |
| 3146 | case BO_PtrMemI: |
| 3147 | case BO_LE: |
| 3148 | case BO_GE: |
| 3149 | case BO_EQ: |
| 3150 | case BO_NE: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3151 | case BO_AddAssign: |
| 3152 | case BO_SubAssign: |
| 3153 | case BO_AndAssign: |
| 3154 | case BO_OrAssign: |
| 3155 | case BO_XorAssign: |
| 3156 | case BO_MulAssign: |
| 3157 | case BO_DivAssign: |
| 3158 | case BO_RemAssign: |
| 3159 | case BO_ShlAssign: |
| 3160 | case BO_ShrAssign: |
| 3161 | case BO_Comma: |
| 3162 | llvm_unreachable("Unsupported atomic update operation"); |
| 3163 | } |
| 3164 | auto *UpdateVal = Update.getScalarVal(); |
| 3165 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 3166 | UpdateVal = CGF.Builder.CreateIntCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3167 | IC, X.getAddress().getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3168 | X.getType()->hasSignedIntegerRepresentation()); |
| 3169 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3170 | auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3171 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3174 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3175 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 3176 | llvm::AtomicOrdering AO, SourceLocation Loc, |
| 3177 | const llvm::function_ref<RValue(RValue)> &CommonGen) { |
| 3178 | // Update expressions are allowed to have the following forms: |
| 3179 | // x binop= expr; -> xrval + expr; |
| 3180 | // x++, ++x -> xrval + 1; |
| 3181 | // x--, --x -> xrval - 1; |
| 3182 | // x = x binop expr; -> xrval binop expr |
| 3183 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3184 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 3185 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3186 | if (X.isGlobalReg()) { |
| 3187 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 3188 | // 'xrval'. |
| 3189 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 3190 | } else { |
| 3191 | // Perform compare-and-swap procedure. |
| 3192 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3193 | } |
| 3194 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3195 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3199 | const Expr *X, const Expr *E, |
| 3200 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3201 | SourceLocation Loc) { |
| 3202 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3203 | "Update expr in 'atomic update' must be a binary operator."); |
| 3204 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 3205 | // Update expressions are allowed to have the following forms: |
| 3206 | // x binop= expr; -> xrval + expr; |
| 3207 | // x++, ++x -> xrval + 1; |
| 3208 | // x--, --x -> xrval - 1; |
| 3209 | // x = x binop expr; -> xrval binop expr |
| 3210 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3211 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3212 | LValue XLValue = CGF.EmitLValue(X); |
| 3213 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3214 | auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3215 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3216 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3217 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3218 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3219 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3220 | auto Gen = |
| 3221 | [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue { |
| 3222 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3223 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3224 | return CGF.EmitAnyExpr(UE); |
| 3225 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3226 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3227 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3228 | // OpenMP, 2.12.6, atomic Construct |
| 3229 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3230 | // performed operation to include an implicit flush operation without a |
| 3231 | // list. |
| 3232 | if (IsSeqCst) |
| 3233 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3234 | } |
| 3235 | |
| 3236 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3237 | QualType SourceType, QualType ResType, |
| 3238 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3239 | switch (CGF.getEvaluationKind(ResType)) { |
| 3240 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3241 | return RValue::get( |
| 3242 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3243 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3244 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3245 | return RValue::getComplex(Res.first, Res.second); |
| 3246 | } |
| 3247 | case TEK_Aggregate: |
| 3248 | break; |
| 3249 | } |
| 3250 | llvm_unreachable("Must be a scalar or complex."); |
| 3251 | } |
| 3252 | |
| 3253 | static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3254 | bool IsPostfixUpdate, const Expr *V, |
| 3255 | const Expr *X, const Expr *E, |
| 3256 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3257 | SourceLocation Loc) { |
| 3258 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 3259 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 3260 | RValue NewVVal; |
| 3261 | LValue VLValue = CGF.EmitLValue(V); |
| 3262 | LValue XLValue = CGF.EmitLValue(X); |
| 3263 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3264 | auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3265 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3266 | QualType NewVValType; |
| 3267 | if (UE) { |
| 3268 | // 'x' is updated with some additional value. |
| 3269 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3270 | "Update expr in 'atomic capture' must be a binary operator."); |
| 3271 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 3272 | // Update expressions are allowed to have the following forms: |
| 3273 | // x binop= expr; -> xrval + expr; |
| 3274 | // x++, ++x -> xrval + 1; |
| 3275 | // x--, --x -> xrval - 1; |
| 3276 | // x = x binop expr; -> xrval binop expr |
| 3277 | // x = expr Op x; - > expr binop xrval; |
| 3278 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3279 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3280 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3281 | NewVValType = XRValExpr->getType(); |
| 3282 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3283 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 3284 | IsPostfixUpdate](RValue XRValue) -> RValue { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3285 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3286 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3287 | RValue Res = CGF.EmitAnyExpr(UE); |
| 3288 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 3289 | return Res; |
| 3290 | }; |
| 3291 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3292 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3293 | if (Res.first) { |
| 3294 | // 'atomicrmw' instruction was generated. |
| 3295 | if (IsPostfixUpdate) { |
| 3296 | // Use old value from 'atomicrmw'. |
| 3297 | NewVVal = Res.second; |
| 3298 | } else { |
| 3299 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 3300 | // value of 'x'. |
| 3301 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3302 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 3303 | NewVVal = CGF.EmitAnyExpr(UE); |
| 3304 | } |
| 3305 | } |
| 3306 | } else { |
| 3307 | // 'x' is simply rewritten with some 'expr'. |
| 3308 | NewVValType = X->getType().getNonReferenceType(); |
| 3309 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3310 | X->getType().getNonReferenceType(), Loc); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 3311 | auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) -> RValue { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3312 | NewVVal = XRValue; |
| 3313 | return ExprRValue; |
| 3314 | }; |
| 3315 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 3316 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3317 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 3318 | Loc, Gen); |
| 3319 | if (Res.first) { |
| 3320 | // 'atomicrmw' instruction was generated. |
| 3321 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 3322 | } |
| 3323 | } |
| 3324 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3325 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3326 | // OpenMP, 2.12.6, atomic Construct |
| 3327 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3328 | // performed operation to include an implicit flush operation without a |
| 3329 | // list. |
| 3330 | if (IsSeqCst) |
| 3331 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3332 | } |
| 3333 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3334 | static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3335 | bool IsSeqCst, bool IsPostfixUpdate, |
| 3336 | const Expr *X, const Expr *V, const Expr *E, |
| 3337 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3338 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3339 | switch (Kind) { |
| 3340 | case OMPC_read: |
| 3341 | EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
| 3342 | break; |
| 3343 | case OMPC_write: |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3344 | EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
| 3345 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3346 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3347 | case OMPC_update: |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3348 | EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
| 3349 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3350 | case OMPC_capture: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3351 | EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
| 3352 | IsXLHSInRHSPart, Loc); |
| 3353 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3354 | case OMPC_if: |
| 3355 | case OMPC_final: |
| 3356 | case OMPC_num_threads: |
| 3357 | case OMPC_private: |
| 3358 | case OMPC_firstprivate: |
| 3359 | case OMPC_lastprivate: |
| 3360 | case OMPC_reduction: |
| 3361 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 3362 | case OMPC_simdlen: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3363 | case OMPC_collapse: |
| 3364 | case OMPC_default: |
| 3365 | case OMPC_seq_cst: |
| 3366 | case OMPC_shared: |
| 3367 | case OMPC_linear: |
| 3368 | case OMPC_aligned: |
| 3369 | case OMPC_copyin: |
| 3370 | case OMPC_copyprivate: |
| 3371 | case OMPC_flush: |
| 3372 | case OMPC_proc_bind: |
| 3373 | case OMPC_schedule: |
| 3374 | case OMPC_ordered: |
| 3375 | case OMPC_nowait: |
| 3376 | case OMPC_untied: |
| 3377 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 3378 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3379 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 3380 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 3381 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 3382 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 3383 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 3384 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 3385 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 3386 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 3387 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 3388 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 3389 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 3390 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 3391 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 3392 | case OMPC_defaultmap: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 3393 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 3394 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 3395 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 3396 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 3397 | case OMPC_is_device_ptr: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3398 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3403 | bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3404 | OpenMPClauseKind Kind = OMPC_unknown; |
| 3405 | for (auto *C : S.clauses()) { |
| 3406 | // Find first clause (skip seq_cst clause, if it is first). |
| 3407 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 3408 | Kind = C->getClauseKind(); |
| 3409 | break; |
| 3410 | } |
| 3411 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3412 | |
| 3413 | const auto *CS = |
| 3414 | S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3415 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) { |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3416 | enterFullExpression(EWC); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3417 | } |
| 3418 | // Processing for statements under 'atomic capture'. |
| 3419 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
| 3420 | for (const auto *C : Compound->body()) { |
| 3421 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) { |
| 3422 | enterFullExpression(EWC); |
| 3423 | } |
| 3424 | } |
| 3425 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3426 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3427 | auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, |
| 3428 | PrePostActionTy &) { |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 3429 | CGF.EmitStopPoint(CS); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3430 | EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
| 3431 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
| 3432 | S.isXLHSInRHSPart(), S.getLocStart()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3433 | }; |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 3434 | OMPLexicalScope Scope(*this, S, /*AsInlined=*/true); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3435 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3438 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 3439 | const OMPExecutableDirective &S, |
| 3440 | const RegionCodeGenTy &CodeGen) { |
| 3441 | assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); |
| 3442 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3443 | const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt()); |
| 3444 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3445 | llvm::Function *Fn = nullptr; |
| 3446 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3447 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3448 | const Expr *IfCond = nullptr; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 3449 | // Check for the at most one if clause associated with the target region. |
| 3450 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3451 | if (C->getNameModifier() == OMPD_unknown || |
| 3452 | C->getNameModifier() == OMPD_target) { |
| 3453 | IfCond = C->getCondition(); |
| 3454 | break; |
| 3455 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | // Check if we have any device clause associated with the directive. |
| 3459 | const Expr *Device = nullptr; |
| 3460 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) { |
| 3461 | Device = C->getDevice(); |
| 3462 | } |
| 3463 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3464 | // Check if we have an if clause whose conditional always evaluates to false |
| 3465 | // or if we do not have any targets specified. If so the target region is not |
| 3466 | // an offload entry point. |
| 3467 | bool IsOffloadEntry = true; |
| 3468 | if (IfCond) { |
| 3469 | bool Val; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3470 | if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3471 | IsOffloadEntry = false; |
| 3472 | } |
| 3473 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 3474 | IsOffloadEntry = false; |
| 3475 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3476 | assert(CGF.CurFuncDecl && "No parent declaration for target region!"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3477 | StringRef ParentName; |
| 3478 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 3479 | // the mangling of the device outlined kernel. |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3480 | if (auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3481 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3482 | else if (auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3483 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 3484 | else |
| 3485 | ParentName = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3486 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3487 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3488 | // Emit target region as a standalone region. |
| 3489 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 3490 | IsOffloadEntry, CodeGen); |
| 3491 | OMPLexicalScope Scope(CGF, S); |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 3492 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 3493 | CGF.GenerateOpenMPCapturedVars(CS, CapturedVars); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3494 | CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3495 | CapturedVars); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3498 | static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, |
| 3499 | PrePostActionTy &Action) { |
| 3500 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 3501 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 3502 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 3503 | (void)PrivateScope.Privatize(); |
| 3504 | |
| 3505 | Action.Enter(CGF); |
| 3506 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 3507 | } |
| 3508 | |
| 3509 | void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
| 3510 | StringRef ParentName, |
| 3511 | const OMPTargetDirective &S) { |
| 3512 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3513 | emitTargetRegion(CGF, S, Action); |
| 3514 | }; |
| 3515 | llvm::Function *Fn; |
| 3516 | llvm::Constant *Addr; |
| 3517 | // Emit target region as a standalone region. |
| 3518 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 3519 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 3520 | assert(Fn && Addr && "Target device function emission failed."); |
| 3521 | } |
| 3522 | |
| 3523 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 3524 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3525 | emitTargetRegion(CGF, S, Action); |
| 3526 | }; |
| 3527 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 3528 | } |
| 3529 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3530 | static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, |
| 3531 | const OMPExecutableDirective &S, |
| 3532 | OpenMPDirectiveKind InnermostKind, |
| 3533 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 3534 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); |
| 3535 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( |
| 3536 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 3537 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 3538 | const OMPNumTeamsClause *NT = S.getSingleClause<OMPNumTeamsClause>(); |
| 3539 | const OMPThreadLimitClause *TL = S.getSingleClause<OMPThreadLimitClause>(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3540 | if (NT || TL) { |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 3541 | Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr; |
| 3542 | Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3543 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 3544 | CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, |
| 3545 | S.getLocStart()); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 3548 | OMPTeamsScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3549 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 3550 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3551 | CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn, |
| 3552 | CapturedVars); |
| 3553 | } |
| 3554 | |
| 3555 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { |
Kelvin Li | 51336dd | 2016-12-15 17:55:32 +0000 | [diff] [blame] | 3556 | // Emit teams region as a standalone region. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3557 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3558 | OMPPrivateScope PrivateScope(CGF); |
Carlo Bertolli | 6ad7b5a | 2016-03-03 22:09:40 +0000 | [diff] [blame] | 3559 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 3560 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3561 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3562 | (void)PrivateScope.Privatize(); |
| 3563 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3564 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3565 | }; |
| 3566 | emitCommonOMPTeamsDirective(*this, S, OMPD_teams, CodeGen); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 3567 | emitPostUpdateForReductionClause( |
| 3568 | *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 3569 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 3570 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 3571 | static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 3572 | const OMPTargetTeamsDirective &S) { |
| 3573 | auto *CS = S.getCapturedStmt(OMPD_teams); |
| 3574 | Action.Enter(CGF); |
| 3575 | auto &&CodeGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3576 | // TODO: Add support for clauses. |
| 3577 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 3578 | }; |
| 3579 | emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); |
| 3580 | } |
| 3581 | |
| 3582 | void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| 3583 | CodeGenModule &CGM, StringRef ParentName, |
| 3584 | const OMPTargetTeamsDirective &S) { |
| 3585 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3586 | emitTargetTeamsRegion(CGF, Action, S); |
| 3587 | }; |
| 3588 | llvm::Function *Fn; |
| 3589 | llvm::Constant *Addr; |
| 3590 | // Emit target region as a standalone region. |
| 3591 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 3592 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 3593 | assert(Fn && Addr && "Target device function emission failed."); |
| 3594 | } |
| 3595 | |
| 3596 | void CodeGenFunction::EmitOMPTargetTeamsDirective( |
| 3597 | const OMPTargetTeamsDirective &S) { |
| 3598 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3599 | emitTargetTeamsRegion(CGF, Action, S); |
| 3600 | }; |
| 3601 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 3602 | } |
| 3603 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 3604 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 3605 | const OMPCancellationPointDirective &S) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 3606 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(), |
| 3607 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 3610 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 3611 | const Expr *IfCond = nullptr; |
| 3612 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3613 | if (C->getNameModifier() == OMPD_unknown || |
| 3614 | C->getNameModifier() == OMPD_cancel) { |
| 3615 | IfCond = C->getCondition(); |
| 3616 | break; |
| 3617 | } |
| 3618 | } |
| 3619 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 3620 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3623 | CodeGenFunction::JumpDest |
| 3624 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 3625 | if (Kind == OMPD_parallel || Kind == OMPD_task || |
| 3626 | Kind == OMPD_target_parallel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3627 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3628 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 3629 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || |
| 3630 | Kind == OMPD_distribute_parallel_for || |
| 3631 | Kind == OMPD_target_parallel_for); |
| 3632 | return OMPCancelStack.getExitBlock(); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3633 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 3634 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 3635 | void CodeGenFunction::EmitOMPUseDevicePtrClause( |
| 3636 | const OMPClause &NC, OMPPrivateScope &PrivateScope, |
| 3637 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { |
| 3638 | const auto &C = cast<OMPUseDevicePtrClause>(NC); |
| 3639 | auto OrigVarIt = C.varlist_begin(); |
| 3640 | auto InitIt = C.inits().begin(); |
| 3641 | for (auto PvtVarIt : C.private_copies()) { |
| 3642 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); |
| 3643 | auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); |
| 3644 | auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); |
| 3645 | |
| 3646 | // In order to identify the right initializer we need to match the |
| 3647 | // declaration used by the mapping logic. In some cases we may get |
| 3648 | // OMPCapturedExprDecl that refers to the original declaration. |
| 3649 | const ValueDecl *MatchingVD = OrigVD; |
| 3650 | if (auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { |
| 3651 | // OMPCapturedExprDecl are used to privative fields of the current |
| 3652 | // structure. |
| 3653 | auto *ME = cast<MemberExpr>(OED->getInit()); |
| 3654 | assert(isa<CXXThisExpr>(ME->getBase()) && |
| 3655 | "Base should be the current struct!"); |
| 3656 | MatchingVD = ME->getMemberDecl(); |
| 3657 | } |
| 3658 | |
| 3659 | // If we don't have information about the current list item, move on to |
| 3660 | // the next one. |
| 3661 | auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); |
| 3662 | if (InitAddrIt == CaptureDeviceAddrMap.end()) |
| 3663 | continue; |
| 3664 | |
| 3665 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 3666 | // Initialize the temporary initialization variable with the address we |
| 3667 | // get from the runtime library. We have to cast the source address |
| 3668 | // because it is always a void *. References are materialized in the |
| 3669 | // privatization scope, so the initialization here disregards the fact |
| 3670 | // the original variable is a reference. |
| 3671 | QualType AddrQTy = |
| 3672 | getContext().getPointerType(OrigVD->getType().getNonReferenceType()); |
| 3673 | llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); |
| 3674 | Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); |
| 3675 | setAddrOfLocalVar(InitVD, InitAddr); |
| 3676 | |
| 3677 | // Emit private declaration, it will be initialized by the value we |
| 3678 | // declaration we just added to the local declarations map. |
| 3679 | EmitDecl(*PvtVD); |
| 3680 | |
| 3681 | // The initialization variables reached its purpose in the emission |
| 3682 | // ofthe previous declaration, so we don't need it anymore. |
| 3683 | LocalDeclMap.erase(InitVD); |
| 3684 | |
| 3685 | // Return the address of the private variable. |
| 3686 | return GetAddrOfLocalVar(PvtVD); |
| 3687 | }); |
| 3688 | assert(IsRegistered && "firstprivate var already registered as private"); |
| 3689 | // Silence the warning about unused variable. |
| 3690 | (void)IsRegistered; |
| 3691 | |
| 3692 | ++OrigVarIt; |
| 3693 | ++InitIt; |
| 3694 | } |
| 3695 | } |
| 3696 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 3697 | // Generate the instructions for '#pragma omp target data' directive. |
| 3698 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 3699 | const OMPTargetDataDirective &S) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 3700 | CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); |
| 3701 | |
| 3702 | // Create a pre/post action to signal the privatization of the device pointer. |
| 3703 | // This action can be replaced by the OpenMP runtime code generation to |
| 3704 | // deactivate privatization. |
| 3705 | bool PrivatizeDevicePointers = false; |
| 3706 | class DevicePointerPrivActionTy : public PrePostActionTy { |
| 3707 | bool &PrivatizeDevicePointers; |
| 3708 | |
| 3709 | public: |
| 3710 | explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) |
| 3711 | : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} |
| 3712 | void Enter(CodeGenFunction &CGF) override { |
| 3713 | PrivatizeDevicePointers = true; |
| 3714 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 3715 | }; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 3716 | DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); |
| 3717 | |
| 3718 | auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( |
| 3719 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3720 | auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3721 | CGF.EmitStmt( |
| 3722 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 3723 | }; |
| 3724 | |
| 3725 | // Codegen that selects wheather to generate the privatization code or not. |
| 3726 | auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, |
| 3727 | &InnermostCodeGen](CodeGenFunction &CGF, |
| 3728 | PrePostActionTy &Action) { |
| 3729 | RegionCodeGenTy RCG(InnermostCodeGen); |
| 3730 | PrivatizeDevicePointers = false; |
| 3731 | |
| 3732 | // Call the pre-action to change the status of PrivatizeDevicePointers if |
| 3733 | // needed. |
| 3734 | Action.Enter(CGF); |
| 3735 | |
| 3736 | if (PrivatizeDevicePointers) { |
| 3737 | OMPPrivateScope PrivateScope(CGF); |
| 3738 | // Emit all instances of the use_device_ptr clause. |
| 3739 | for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 3740 | CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, |
| 3741 | Info.CaptureDeviceAddrMap); |
| 3742 | (void)PrivateScope.Privatize(); |
| 3743 | RCG(CGF); |
| 3744 | } else |
| 3745 | RCG(CGF); |
| 3746 | }; |
| 3747 | |
| 3748 | // Forward the provided action to the privatization codegen. |
| 3749 | RegionCodeGenTy PrivRCG(PrivCodeGen); |
| 3750 | PrivRCG.setAction(Action); |
| 3751 | |
| 3752 | // Notwithstanding the body of the region is emitted as inlined directive, |
| 3753 | // we don't use an inline scope as changes in the references inside the |
| 3754 | // region are expected to be visible outside, so we do not privative them. |
| 3755 | OMPLexicalScope Scope(CGF, S); |
| 3756 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, |
| 3757 | PrivRCG); |
| 3758 | }; |
| 3759 | |
| 3760 | RegionCodeGenTy RCG(CodeGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 3761 | |
| 3762 | // If we don't have target devices, don't bother emitting the data mapping |
| 3763 | // code. |
| 3764 | if (CGM.getLangOpts().OMPTargetTriples.empty()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 3765 | RCG(*this); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 3766 | return; |
| 3767 | } |
| 3768 | |
| 3769 | // Check if we have any if clause associated with the directive. |
| 3770 | const Expr *IfCond = nullptr; |
| 3771 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 3772 | IfCond = C->getCondition(); |
| 3773 | |
| 3774 | // Check if we have any device clause associated with the directive. |
| 3775 | const Expr *Device = nullptr; |
| 3776 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 3777 | Device = C->getDevice(); |
| 3778 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 3779 | // Set the action to signal privatization of device pointers. |
| 3780 | RCG.setAction(PrivAction); |
| 3781 | |
| 3782 | // Emit region code. |
| 3783 | CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, |
| 3784 | Info); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 3785 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 3786 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 3787 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 3788 | const OMPTargetEnterDataDirective &S) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 3789 | // If we don't have target devices, don't bother emitting the data mapping |
| 3790 | // code. |
| 3791 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 3792 | return; |
| 3793 | |
| 3794 | // Check if we have any if clause associated with the directive. |
| 3795 | const Expr *IfCond = nullptr; |
| 3796 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 3797 | IfCond = C->getCondition(); |
| 3798 | |
| 3799 | // Check if we have any device clause associated with the directive. |
| 3800 | const Expr *Device = nullptr; |
| 3801 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 3802 | Device = C->getDevice(); |
| 3803 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 3804 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 3807 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 3808 | const OMPTargetExitDataDirective &S) { |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 3809 | // If we don't have target devices, don't bother emitting the data mapping |
| 3810 | // code. |
| 3811 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 3812 | return; |
| 3813 | |
| 3814 | // Check if we have any if clause associated with the directive. |
| 3815 | const Expr *IfCond = nullptr; |
| 3816 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 3817 | IfCond = C->getCondition(); |
| 3818 | |
| 3819 | // Check if we have any device clause associated with the directive. |
| 3820 | const Expr *Device = nullptr; |
| 3821 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 3822 | Device = C->getDevice(); |
| 3823 | |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 3824 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 3827 | static void emitTargetParallelRegion(CodeGenFunction &CGF, |
| 3828 | const OMPTargetParallelDirective &S, |
| 3829 | PrePostActionTy &Action) { |
| 3830 | // Get the captured statement associated with the 'parallel' region. |
| 3831 | auto *CS = S.getCapturedStmt(OMPD_parallel); |
| 3832 | Action.Enter(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3833 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3834 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 3835 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 3836 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 3837 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 3838 | (void)PrivateScope.Privatize(); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 3839 | // TODO: Add support for clauses. |
| 3840 | CGF.EmitStmt(CS->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3841 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 3842 | }; |
| 3843 | emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3844 | emitPostUpdateForReductionClause( |
| 3845 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| 3849 | CodeGenModule &CGM, StringRef ParentName, |
| 3850 | const OMPTargetParallelDirective &S) { |
| 3851 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3852 | emitTargetParallelRegion(CGF, S, Action); |
| 3853 | }; |
| 3854 | llvm::Function *Fn; |
| 3855 | llvm::Constant *Addr; |
| 3856 | // Emit target region as a standalone region. |
| 3857 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 3858 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 3859 | assert(Fn && Addr && "Target device function emission failed."); |
| 3860 | } |
| 3861 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 3862 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 3863 | const OMPTargetParallelDirective &S) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 3864 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3865 | emitTargetParallelRegion(CGF, S, Action); |
| 3866 | }; |
| 3867 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 3870 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 3871 | const OMPTargetParallelForDirective &S) { |
| 3872 | // TODO: codegen for target parallel for. |
| 3873 | } |
| 3874 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3875 | /// Emit a helper variable and return corresponding lvalue. |
| 3876 | static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, |
| 3877 | const ImplicitParamDecl *PVD, |
| 3878 | CodeGenFunction::OMPPrivateScope &Privates) { |
| 3879 | auto *VDecl = cast<VarDecl>(Helper->getDecl()); |
| 3880 | Privates.addPrivate( |
| 3881 | VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); }); |
| 3882 | } |
| 3883 | |
| 3884 | void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { |
| 3885 | assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); |
| 3886 | // Emit outlined function for task construct. |
| 3887 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 3888 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3889 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 3890 | const Expr *IfCond = nullptr; |
| 3891 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3892 | if (C->getNameModifier() == OMPD_unknown || |
| 3893 | C->getNameModifier() == OMPD_taskloop) { |
| 3894 | IfCond = C->getCondition(); |
| 3895 | break; |
| 3896 | } |
| 3897 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3898 | |
| 3899 | OMPTaskDataTy Data; |
| 3900 | // Check if taskloop must be emitted without taskgroup. |
| 3901 | Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3902 | // TODO: Check if we should emit tied or untied task. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3903 | Data.Tied = true; |
| 3904 | // Set scheduling for taskloop |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 3905 | if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { |
| 3906 | // grainsize clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3907 | Data.Schedule.setInt(/*IntVal=*/false); |
| 3908 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 3909 | } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { |
| 3910 | // num_tasks clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3911 | Data.Schedule.setInt(/*IntVal=*/true); |
| 3912 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 3913 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3914 | |
| 3915 | auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3916 | // if (PreCond) { |
| 3917 | // for (IV in 0..LastIteration) BODY; |
| 3918 | // <Final counter/linear vars updates>; |
| 3919 | // } |
| 3920 | // |
| 3921 | |
| 3922 | // Emit: if (PreCond) - begin. |
| 3923 | // If the condition constant folds and can be elided, avoid emitting the |
| 3924 | // whole loop. |
| 3925 | bool CondConstant; |
| 3926 | llvm::BasicBlock *ContBlock = nullptr; |
| 3927 | OMPLoopScope PreInitScope(CGF, S); |
| 3928 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 3929 | if (!CondConstant) |
| 3930 | return; |
| 3931 | } else { |
| 3932 | auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); |
| 3933 | ContBlock = CGF.createBasicBlock("taskloop.if.end"); |
| 3934 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 3935 | CGF.getProfileCount(&S)); |
| 3936 | CGF.EmitBlock(ThenBlock); |
| 3937 | CGF.incrementProfileCounter(&S); |
| 3938 | } |
| 3939 | |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 3940 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 3941 | CGF.EmitOMPSimdInit(S); |
| 3942 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3943 | OMPPrivateScope LoopScope(CGF); |
| 3944 | // Emit helper vars inits. |
| 3945 | enum { LowerBound = 5, UpperBound, Stride, LastIter }; |
| 3946 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 3947 | auto *LBP = std::next(I, LowerBound); |
| 3948 | auto *UBP = std::next(I, UpperBound); |
| 3949 | auto *STP = std::next(I, Stride); |
| 3950 | auto *LIP = std::next(I, LastIter); |
| 3951 | mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, |
| 3952 | LoopScope); |
| 3953 | mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, |
| 3954 | LoopScope); |
| 3955 | mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); |
| 3956 | mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, |
| 3957 | LoopScope); |
| 3958 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3959 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3960 | (void)LoopScope.Privatize(); |
| 3961 | // Emit the loop iteration variable. |
| 3962 | const Expr *IVExpr = S.getIterationVariable(); |
| 3963 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 3964 | CGF.EmitVarDecl(*IVDecl); |
| 3965 | CGF.EmitIgnoredExpr(S.getInit()); |
| 3966 | |
| 3967 | // Emit the iterations count variable. |
| 3968 | // If it is not a variable, Sema decided to calculate iterations count on |
| 3969 | // each iteration (e.g., it is foldable into a constant). |
| 3970 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 3971 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 3972 | // Emit calculation of the iterations count. |
| 3973 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 3974 | } |
| 3975 | |
| 3976 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 3977 | S.getInc(), |
| 3978 | [&S](CodeGenFunction &CGF) { |
| 3979 | CGF.EmitOMPLoopBody(S, JumpDest()); |
| 3980 | CGF.EmitStopPoint(&S); |
| 3981 | }, |
| 3982 | [](CodeGenFunction &) {}); |
| 3983 | // Emit: if (PreCond) - end. |
| 3984 | if (ContBlock) { |
| 3985 | CGF.EmitBranch(ContBlock); |
| 3986 | CGF.EmitBlock(ContBlock, true); |
| 3987 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3988 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 3989 | if (HasLastprivateClause) { |
| 3990 | CGF.EmitOMPLastprivateClauseFinal( |
| 3991 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 3992 | CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( |
| 3993 | CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
| 3994 | (*LIP)->getType(), S.getLocStart()))); |
| 3995 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3996 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3997 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
| 3998 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
| 3999 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4000 | auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4001 | OMPLoopScope PreInitScope(CGF, S); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4002 | CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S, |
| 4003 | OutlinedFn, SharedsTy, |
| 4004 | CapturedStruct, IfCond, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4005 | }; |
| 4006 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, |
| 4007 | CodeGen); |
| 4008 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4009 | EmitOMPTaskBasedDirective(S, BodyGen, TaskGen, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4012 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4013 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4016 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 4017 | const OMPTaskLoopSimdDirective &S) { |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4018 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4019 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4020 | |
| 4021 | // Generate the instructions for '#pragma omp target update' directive. |
| 4022 | void CodeGenFunction::EmitOMPTargetUpdateDirective( |
| 4023 | const OMPTargetUpdateDirective &S) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 4024 | // If we don't have target devices, don't bother emitting the data mapping |
| 4025 | // code. |
| 4026 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4027 | return; |
| 4028 | |
| 4029 | // Check if we have any if clause associated with the directive. |
| 4030 | const Expr *IfCond = nullptr; |
| 4031 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4032 | IfCond = C->getCondition(); |
| 4033 | |
| 4034 | // Check if we have any device clause associated with the directive. |
| 4035 | const Expr *Device = nullptr; |
| 4036 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4037 | Device = C->getDevice(); |
| 4038 | |
| 4039 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4040 | } |