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