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