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