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 | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame^] | 675 | static Address castToBase(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 676 | LValue BaseLV, llvm::Value *Addr) { |
| 677 | Address Tmp = Address::invalid(); |
| 678 | Address TopTmp = Address::invalid(); |
| 679 | Address MostTopTmp = Address::invalid(); |
| 680 | BaseTy = BaseTy.getNonReferenceType(); |
| 681 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 682 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 683 | Tmp = CGF.CreateMemTemp(BaseTy); |
| 684 | if (TopTmp.isValid()) |
| 685 | CGF.Builder.CreateStore(Tmp.getPointer(), TopTmp); |
| 686 | else |
| 687 | MostTopTmp = Tmp; |
| 688 | TopTmp = Tmp; |
| 689 | BaseTy = BaseTy->getPointeeType(); |
| 690 | } |
| 691 | llvm::Type *Ty = BaseLV.getPointer()->getType(); |
| 692 | if (Tmp.isValid()) |
| 693 | Ty = Tmp.getElementType(); |
| 694 | Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, Ty); |
| 695 | if (Tmp.isValid()) { |
| 696 | CGF.Builder.CreateStore(Addr, Tmp); |
| 697 | return MostTopTmp; |
| 698 | } |
| 699 | return Address(Addr, BaseLV.getAlignment()); |
| 700 | } |
| 701 | |
| 702 | static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy, |
| 703 | LValue BaseLV) { |
| 704 | BaseTy = BaseTy.getNonReferenceType(); |
| 705 | while ((BaseTy->isPointerType() || BaseTy->isReferenceType()) && |
| 706 | !CGF.getContext().hasSameType(BaseTy, ElTy)) { |
| 707 | if (auto *PtrTy = BaseTy->getAs<PointerType>()) |
| 708 | BaseLV = CGF.EmitLoadOfPointerLValue(BaseLV.getAddress(), PtrTy); |
| 709 | else { |
| 710 | BaseLV = CGF.EmitLoadOfReferenceLValue(BaseLV.getAddress(), |
| 711 | BaseTy->castAs<ReferenceType>()); |
| 712 | } |
| 713 | BaseTy = BaseTy->getPointeeType(); |
| 714 | } |
| 715 | return CGF.MakeAddrLValue( |
| 716 | Address( |
| 717 | CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 718 | BaseLV.getPointer(), CGF.ConvertTypeForMem(ElTy)->getPointerTo()), |
| 719 | BaseLV.getAlignment()), |
| 720 | BaseLV.getType(), BaseLV.getAlignmentSource()); |
| 721 | } |
| 722 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 723 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 724 | const OMPExecutableDirective &D, |
| 725 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 726 | if (!HaveInsertPoint()) |
| 727 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 728 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 729 | auto ILHS = C->lhs_exprs().begin(); |
| 730 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 731 | auto IPriv = C->privates().begin(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 732 | for (auto IRef : C->varlists()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 733 | auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 734 | auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 735 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
| 736 | if (auto *OASE = dyn_cast<OMPArraySectionExpr>(IRef)) { |
| 737 | auto *Base = OASE->getBase()->IgnoreParenImpCasts(); |
| 738 | while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) |
| 739 | Base = TempOASE->getBase()->IgnoreParenImpCasts(); |
| 740 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 741 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 742 | auto *DE = cast<DeclRefExpr>(Base); |
| 743 | auto *OrigVD = cast<VarDecl>(DE->getDecl()); |
| 744 | auto OASELValueLB = EmitOMPArraySectionExpr(OASE); |
| 745 | auto OASELValueUB = |
| 746 | EmitOMPArraySectionExpr(OASE, /*IsLowerBound=*/false); |
| 747 | auto OriginalBaseLValue = EmitLValue(DE); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame^] | 748 | LValue BaseLValue = |
| 749 | loadToBegin(*this, OrigVD->getType(), OASELValueLB.getType(), |
| 750 | OriginalBaseLValue); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 751 | // Store the address of the original variable associated with the LHS |
| 752 | // implicit variable. |
| 753 | PrivateScope.addPrivate(LHSVD, [this, OASELValueLB]() -> Address { |
| 754 | return OASELValueLB.getAddress(); |
| 755 | }); |
| 756 | // Emit reduction copy. |
| 757 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame^] | 758 | OrigVD, [this, OrigVD, PrivateVD, BaseLValue, OASELValueLB, |
| 759 | OASELValueUB, OriginalBaseLValue]() -> Address { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 760 | // Emit VarDecl with copy init for arrays. |
| 761 | // Get the address of the original variable captured in current |
| 762 | // captured region. |
| 763 | auto *Size = Builder.CreatePtrDiff(OASELValueUB.getPointer(), |
| 764 | OASELValueLB.getPointer()); |
| 765 | Size = Builder.CreateNUWAdd( |
| 766 | Size, llvm::ConstantInt::get(Size->getType(), /*V=*/1)); |
| 767 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 768 | *this, cast<OpaqueValueExpr>( |
| 769 | getContext() |
| 770 | .getAsVariableArrayType(PrivateVD->getType()) |
| 771 | ->getSizeExpr()), |
| 772 | RValue::get(Size)); |
| 773 | EmitVariablyModifiedType(PrivateVD->getType()); |
| 774 | auto Emission = EmitAutoVarAlloca(*PrivateVD); |
| 775 | auto Addr = Emission.getAllocatedAddress(); |
| 776 | auto *Init = PrivateVD->getInit(); |
| 777 | EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), Init); |
| 778 | EmitAutoVarCleanups(Emission); |
| 779 | // Emit private VarDecl with reduction init. |
| 780 | auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), |
| 781 | OASELValueLB.getPointer()); |
| 782 | auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame^] | 783 | return castToBase(*this, OrigVD->getType(), |
| 784 | OASELValueLB.getType(), OriginalBaseLValue, |
| 785 | Ptr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 786 | }); |
| 787 | assert(IsRegistered && "private var already registered as private"); |
| 788 | // Silence the warning about unused variable. |
| 789 | (void)IsRegistered; |
| 790 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { |
| 791 | return GetAddrOfLocalVar(PrivateVD); |
| 792 | }); |
| 793 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(IRef)) { |
| 794 | auto *Base = ASE->getBase()->IgnoreParenImpCasts(); |
| 795 | while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) |
| 796 | Base = TempASE->getBase()->IgnoreParenImpCasts(); |
| 797 | auto *DE = cast<DeclRefExpr>(Base); |
| 798 | auto *OrigVD = cast<VarDecl>(DE->getDecl()); |
| 799 | auto ASELValue = EmitLValue(ASE); |
| 800 | auto OriginalBaseLValue = EmitLValue(DE); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame^] | 801 | LValue BaseLValue = loadToBegin( |
| 802 | *this, OrigVD->getType(), ASELValue.getType(), OriginalBaseLValue); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 803 | // Store the address of the original variable associated with the LHS |
| 804 | // implicit variable. |
| 805 | PrivateScope.addPrivate(LHSVD, [this, ASELValue]() -> Address { |
| 806 | return ASELValue.getAddress(); |
| 807 | }); |
| 808 | // Emit reduction copy. |
| 809 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame^] | 810 | OrigVD, [this, OrigVD, PrivateVD, BaseLValue, ASELValue, |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 811 | OriginalBaseLValue]() -> Address { |
| 812 | // Emit private VarDecl with reduction init. |
| 813 | EmitDecl(*PrivateVD); |
| 814 | auto Addr = GetAddrOfLocalVar(PrivateVD); |
| 815 | auto *Offset = Builder.CreatePtrDiff(BaseLValue.getPointer(), |
| 816 | ASELValue.getPointer()); |
| 817 | auto *Ptr = Builder.CreateGEP(Addr.getPointer(), Offset); |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame^] | 818 | return castToBase(*this, OrigVD->getType(), ASELValue.getType(), |
| 819 | OriginalBaseLValue, Ptr); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 820 | }); |
| 821 | assert(IsRegistered && "private var already registered as private"); |
| 822 | // Silence the warning about unused variable. |
| 823 | (void)IsRegistered; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 824 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { |
| 825 | return Builder.CreateElementBitCast( |
| 826 | GetAddrOfLocalVar(PrivateVD), ConvertTypeForMem(RHSVD->getType()), |
| 827 | "rhs.begin"); |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 828 | }); |
| 829 | } else { |
| 830 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 831 | QualType Type = PrivateVD->getType(); |
| 832 | if (getContext().getAsArrayType(Type)) { |
| 833 | // Store the address of the original variable associated with the LHS |
| 834 | // implicit variable. |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 835 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 836 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 837 | IRef->getType(), VK_LValue, IRef->getExprLoc()); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 838 | Address OriginalAddr = EmitLValue(&DRE).getAddress(); |
| 839 | PrivateScope.addPrivate(LHSVD, [this, OriginalAddr, |
| 840 | LHSVD]() -> Address { |
| 841 | return Builder.CreateElementBitCast( |
| 842 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), |
| 843 | "lhs.begin"); |
| 844 | }); |
| 845 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 846 | if (Type->isVariablyModifiedType()) { |
| 847 | CodeGenFunction::OpaqueValueMapping OpaqueMap( |
| 848 | *this, cast<OpaqueValueExpr>( |
| 849 | getContext() |
| 850 | .getAsVariableArrayType(PrivateVD->getType()) |
| 851 | ->getSizeExpr()), |
| 852 | RValue::get( |
| 853 | getTypeSize(OrigVD->getType().getNonReferenceType()))); |
| 854 | EmitVariablyModifiedType(Type); |
| 855 | } |
| 856 | auto Emission = EmitAutoVarAlloca(*PrivateVD); |
| 857 | auto Addr = Emission.getAllocatedAddress(); |
| 858 | auto *Init = PrivateVD->getInit(); |
| 859 | EmitOMPAggregateInit(*this, Addr, PrivateVD->getType(), Init); |
| 860 | EmitAutoVarCleanups(Emission); |
| 861 | return Emission.getAllocatedAddress(); |
| 862 | }); |
| 863 | assert(IsRegistered && "private var already registered as private"); |
| 864 | // Silence the warning about unused variable. |
| 865 | (void)IsRegistered; |
| 866 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { |
| 867 | return Builder.CreateElementBitCast( |
| 868 | GetAddrOfLocalVar(PrivateVD), |
| 869 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin"); |
| 870 | }); |
| 871 | } else { |
| 872 | // Store the address of the original variable associated with the LHS |
| 873 | // implicit variable. |
| 874 | PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef]() -> Address { |
| 875 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 876 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 877 | IRef->getType(), VK_LValue, IRef->getExprLoc()); |
| 878 | return EmitLValue(&DRE).getAddress(); |
| 879 | }); |
| 880 | // Emit reduction copy. |
| 881 | bool IsRegistered = |
| 882 | PrivateScope.addPrivate(OrigVD, [this, PrivateVD]() -> Address { |
| 883 | // Emit private VarDecl with reduction init. |
| 884 | EmitDecl(*PrivateVD); |
| 885 | return GetAddrOfLocalVar(PrivateVD); |
| 886 | }); |
| 887 | assert(IsRegistered && "private var already registered as private"); |
| 888 | // Silence the warning about unused variable. |
| 889 | (void)IsRegistered; |
| 890 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { |
| 891 | return GetAddrOfLocalVar(PrivateVD); |
| 892 | }); |
| 893 | } |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 894 | } |
| 895 | ++ILHS, ++IRHS, ++IPriv; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
| 901 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 902 | if (!HaveInsertPoint()) |
| 903 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 904 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 905 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 906 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 907 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 908 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 909 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 910 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 911 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 912 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 913 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 914 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 915 | } |
| 916 | if (HasAtLeastOneReduction) { |
| 917 | // Emit nowait reduction if nowait clause is present or directive is a |
| 918 | // parallel directive (it always has implicit barrier). |
| 919 | CGM.getOpenMPRuntime().emitReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 920 | *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 921 | D.getSingleClause<OMPNowaitClause>() || |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 922 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
| 923 | D.getDirectiveKind() == OMPD_simd, |
| 924 | D.getDirectiveKind() == OMPD_simd); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 928 | static void emitCommonOMPParallelDirective(CodeGenFunction &CGF, |
| 929 | const OMPExecutableDirective &S, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 930 | OpenMPDirectiveKind InnermostKind, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 931 | const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 932 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 933 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 934 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 935 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 936 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 937 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 938 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 939 | auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 940 | /*IgnoreResultAssign*/ true); |
| 941 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
| 942 | CGF, NumThreads, NumThreadsClause->getLocStart()); |
| 943 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 944 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 945 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 946 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
| 947 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart()); |
| 948 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 949 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 950 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 951 | if (C->getNameModifier() == OMPD_unknown || |
| 952 | C->getNameModifier() == OMPD_parallel) { |
| 953 | IfCond = C->getCondition(); |
| 954 | break; |
| 955 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 956 | } |
| 957 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 958 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
| 962 | LexicalScope Scope(*this, S.getSourceRange()); |
| 963 | // Emit parallel region as a standalone region. |
| 964 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 965 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 966 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
| 967 | bool Firstprivates = CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 968 | if (Copyins || Firstprivates) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 969 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 970 | // initialization of firstprivate variables or propagation master's thread |
| 971 | // values of threadprivate variables to local instances of that variables |
| 972 | // of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 973 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 974 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 975 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 976 | } |
| 977 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 978 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 979 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 980 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 981 | CGF.EmitOMPReductionClauseFinal(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 982 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 983 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 984 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 985 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 986 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 987 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 988 | RunCleanupsScope BodyScope(*this); |
| 989 | // Update counters values on current iteration. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 990 | for (auto I : D.updates()) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 991 | EmitIgnoredExpr(I); |
| 992 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 993 | // Update the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 994 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 995 | for (auto U : C->updates()) { |
| 996 | EmitIgnoredExpr(U); |
| 997 | } |
| 998 | } |
| 999 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1000 | // On a continue in the body, jump to the end. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1001 | auto Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1002 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1003 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1004 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1005 | // The end (updates/cleanups). |
| 1006 | EmitBlock(Continue.getBlock()); |
| 1007 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1008 | // TODO: Update lastprivates if the SeparateIter flag is true. |
| 1009 | // This will be implemented in a follow-up OMPLastprivateClause patch, but |
| 1010 | // result should be still correct without it, as we do not make these |
| 1011 | // variables private yet. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1014 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1015 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1016 | const Expr *IncExpr, |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1017 | const llvm::function_ref<void(CodeGenFunction &)> &BodyGen, |
| 1018 | const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1019 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1020 | |
| 1021 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1022 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1023 | EmitBlock(CondBlock); |
| 1024 | LoopStack.push(CondBlock); |
| 1025 | |
| 1026 | // If there are any cleanups between here and the loop-exit scope, |
| 1027 | // create a block to stage a loop exit along. |
| 1028 | auto ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1029 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1030 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1031 | |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1032 | auto LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1033 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1034 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1035 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1036 | if (ExitBlock != LoopExit.getBlock()) { |
| 1037 | EmitBlock(ExitBlock); |
| 1038 | EmitBranchThroughCleanup(LoopExit); |
| 1039 | } |
| 1040 | |
| 1041 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1042 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1043 | |
| 1044 | // Create a block for the increment. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1045 | auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1046 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1047 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1048 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1049 | |
| 1050 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1051 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1052 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1053 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1054 | BreakContinueStack.pop_back(); |
| 1055 | EmitBranch(CondBlock); |
| 1056 | LoopStack.pop(); |
| 1057 | // Emit the fall-through block. |
| 1058 | EmitBlock(LoopExit.getBlock()); |
| 1059 | } |
| 1060 | |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1061 | void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1062 | if (!HaveInsertPoint()) |
| 1063 | return; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1064 | // Emit inits for the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1065 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1066 | for (auto Init : C->inits()) { |
| 1067 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1068 | auto *OrigVD = cast<VarDecl>( |
| 1069 | cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())->getDecl()); |
| 1070 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1071 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1072 | VD->getInit()->getType(), VK_LValue, |
| 1073 | VD->getInit()->getExprLoc()); |
| 1074 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 1075 | EmitExprAsInit(&DRE, VD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1076 | MakeAddrLValue(Emission.getAllocatedAddress(), VD->getType()), |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1077 | /*capturedByInit=*/false); |
| 1078 | EmitAutoVarCleanups(Emission); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1079 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1080 | // Emit the linear steps for the linear clauses. |
| 1081 | // If a step is not constant, it is pre-calculated before the loop. |
| 1082 | if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1083 | if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1084 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1085 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1086 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1087 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1088 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | static void emitLinearClauseFinal(CodeGenFunction &CGF, |
| 1092 | const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1093 | if (!CGF.HaveInsertPoint()) |
| 1094 | return; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1095 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1096 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1097 | auto IC = C->varlist_begin(); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1098 | for (auto F : C->finals()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1099 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
| 1100 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1101 | CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1102 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1103 | Address OrigAddr = CGF.EmitLValue(&DRE).getAddress(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1104 | CodeGenFunction::OMPPrivateScope VarScope(CGF); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1105 | VarScope.addPrivate(OrigVD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1106 | [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1107 | (void)VarScope.Privatize(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1108 | CGF.EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1109 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1114 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1115 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1116 | if (!CGF.HaveInsertPoint()) |
| 1117 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1118 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1119 | unsigned ClauseAlignment = 0; |
| 1120 | if (auto AlignmentExpr = Clause->getAlignment()) { |
| 1121 | auto AlignmentCI = |
| 1122 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 1123 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1124 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1125 | for (auto E : Clause->varlists()) { |
| 1126 | unsigned Alignment = ClauseAlignment; |
| 1127 | if (Alignment == 0) { |
| 1128 | // OpenMP [2.8.1, Description] |
| 1129 | // If no optional parameter is specified, implementation-defined default |
| 1130 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1131 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1132 | CGF.getContext() |
| 1133 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1134 | E->getType()->getPointeeType())) |
| 1135 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1136 | } |
| 1137 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 1138 | "alignment is not power of 2"); |
| 1139 | if (Alignment != 0) { |
| 1140 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
| 1141 | CGF.EmitAlignmentAssumption(PtrValue, Alignment); |
| 1142 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1147 | static void emitPrivateLoopCounters(CodeGenFunction &CGF, |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1148 | CodeGenFunction::OMPPrivateScope &LoopScope, |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1149 | ArrayRef<Expr *> Counters, |
| 1150 | ArrayRef<Expr *> PrivateCounters) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1151 | if (!CGF.HaveInsertPoint()) |
| 1152 | return; |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1153 | auto I = PrivateCounters.begin(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1154 | for (auto *E : Counters) { |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1155 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1156 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1157 | Address Addr = Address::invalid(); |
| 1158 | (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1159 | // Emit var without initialization. |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1160 | auto VarEmission = CGF.EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1161 | CGF.EmitAutoVarCleanups(VarEmission); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1162 | Addr = VarEmission.getAllocatedAddress(); |
| 1163 | return Addr; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1164 | }); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1165 | (void)LoopScope.addPrivate(VD, [&]() -> Address { return Addr; }); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1166 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1167 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1170 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1171 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1172 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1173 | if (!CGF.HaveInsertPoint()) |
| 1174 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1175 | { |
| 1176 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1177 | emitPrivateLoopCounters(CGF, PreCondScope, S.counters(), |
| 1178 | S.private_counters()); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1179 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1180 | // Get initial values of real counters. |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 1181 | for (auto I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1182 | CGF.EmitIgnoredExpr(I); |
| 1183 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1184 | } |
| 1185 | // Check that loop is executed at least one time. |
| 1186 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
| 1187 | } |
| 1188 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1189 | static void |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1190 | emitPrivateLinearVars(CodeGenFunction &CGF, const OMPExecutableDirective &D, |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1191 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1192 | if (!CGF.HaveInsertPoint()) |
| 1193 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1194 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1195 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 1196 | for (auto *E : C->varlists()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1197 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1198 | auto *PrivateVD = |
| 1199 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1200 | bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1201 | // Emit private VarDecl with copy init. |
| 1202 | CGF.EmitVarDecl(*PrivateVD); |
| 1203 | return CGF.GetAddrOfLocalVar(PrivateVD); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1204 | }); |
| 1205 | assert(IsRegistered && "linear var already registered as private"); |
| 1206 | // Silence the warning about unused variable. |
| 1207 | (void)IsRegistered; |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1208 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1213 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1214 | const OMPExecutableDirective &D, |
| 1215 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1216 | if (!CGF.HaveInsertPoint()) |
| 1217 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1218 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1219 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1220 | /*ignoreResult=*/true); |
| 1221 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
| 1222 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1223 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1224 | // the memory instructions parallel, because loop-carried |
| 1225 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1226 | if (!IsMonotonic) |
| 1227 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1228 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1229 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1230 | /*ignoreResult=*/true); |
| 1231 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1232 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1233 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1234 | // the memory instructions parallel, because loop-carried |
| 1235 | // dependences of 'safelen' iterations are possible. |
| 1236 | CGF.LoopStack.setParallel(false); |
| 1237 | } |
| 1238 | } |
| 1239 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1240 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1241 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1242 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1243 | LoopStack.setParallel(!IsMonotonic); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1244 | LoopStack.setVectorizeEnable(true); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1245 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1249 | if (!HaveInsertPoint()) |
| 1250 | return; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1251 | auto IC = D.counters().begin(); |
| 1252 | for (auto F : D.finals()) { |
| 1253 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1254 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD)) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1255 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1256 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1257 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1258 | Address OrigAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1259 | OMPPrivateScope VarScope(*this); |
| 1260 | VarScope.addPrivate(OrigVD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1261 | [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1262 | (void)VarScope.Privatize(); |
| 1263 | EmitIgnoredExpr(F); |
| 1264 | } |
| 1265 | ++IC; |
| 1266 | } |
| 1267 | emitLinearClauseFinal(*this, D); |
| 1268 | } |
| 1269 | |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1270 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1271 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1272 | // if (PreCond) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1273 | // for (IV in 0..LastIteration) BODY; |
| 1274 | // <Final counter/linear vars updates>; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1275 | // } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1276 | // |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1277 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1278 | // Emit: if (PreCond) - begin. |
| 1279 | // If the condition constant folds and can be elided, avoid emitting the |
| 1280 | // whole loop. |
| 1281 | bool CondConstant; |
| 1282 | llvm::BasicBlock *ContBlock = nullptr; |
| 1283 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1284 | if (!CondConstant) |
| 1285 | return; |
| 1286 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1287 | auto *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
| 1288 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1289 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 1290 | CGF.getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1291 | CGF.EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1292 | CGF.incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1293 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1294 | |
| 1295 | // Emit the loop iteration variable. |
| 1296 | const Expr *IVExpr = S.getIterationVariable(); |
| 1297 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 1298 | CGF.EmitVarDecl(*IVDecl); |
| 1299 | CGF.EmitIgnoredExpr(S.getInit()); |
| 1300 | |
| 1301 | // Emit the iterations count variable. |
| 1302 | // 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] | 1303 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1304 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 1305 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1306 | // Emit calculation of the iterations count. |
| 1307 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1308 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1309 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1310 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1311 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1312 | emitAlignedClause(CGF, S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1313 | CGF.EmitOMPLinearClauseInit(S); |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1314 | bool HasLastprivateClause; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1315 | { |
| 1316 | OMPPrivateScope LoopScope(CGF); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1317 | emitPrivateLoopCounters(CGF, LoopScope, S.counters(), |
| 1318 | S.private_counters()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1319 | emitPrivateLinearVars(CGF, S, LoopScope); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1320 | CGF.EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 1321 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1322 | HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1323 | (void)LoopScope.Privatize(); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1324 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1325 | S.getInc(), |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1326 | [&S](CodeGenFunction &CGF) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1327 | CGF.EmitOMPLoopBody(S, JumpDest()); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1328 | CGF.EmitStopPoint(&S); |
| 1329 | }, |
| 1330 | [](CodeGenFunction &) {}); |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1331 | // Emit final copy of the lastprivate variables at the end of loops. |
| 1332 | if (HasLastprivateClause) { |
| 1333 | CGF.EmitOMPLastprivateClauseFinal(S); |
| 1334 | } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 1335 | CGF.EmitOMPReductionClauseFinal(S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1336 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1337 | CGF.EmitOMPSimdFinal(S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1338 | // Emit: if (PreCond) - end. |
| 1339 | if (ContBlock) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1340 | CGF.EmitBranch(ContBlock); |
| 1341 | CGF.EmitBlock(ContBlock, true); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1342 | } |
| 1343 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1344 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1347 | void CodeGenFunction::EmitOMPForOuterLoop( |
| 1348 | OpenMPScheduleClauseKind ScheduleKind, bool IsMonotonic, |
| 1349 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
| 1350 | Address LB, Address UB, Address ST, Address IL, llvm::Value *Chunk) { |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1351 | auto &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1352 | |
| 1353 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1354 | const bool DynamicOrOrdered = Ordered || RT.isDynamic(ScheduleKind); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1355 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1356 | assert((Ordered || |
| 1357 | !RT.isStaticNonchunked(ScheduleKind, /*Chunked=*/Chunk != nullptr)) && |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1358 | "static non-chunked schedule does not need outer loop"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1359 | |
| 1360 | // Emit outer loop. |
| 1361 | // |
| 1362 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1363 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 1364 | // distributed to threads in the team in chunks as the threads request them. |
| 1365 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1366 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 1367 | // iterations, except for the last chunk to be distributed, which may have |
| 1368 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 1369 | // |
| 1370 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 1371 | // to threads in the team in chunks as the executing threads request them. |
| 1372 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1373 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 1374 | // each chunk is proportional to the number of unassigned iterations divided |
| 1375 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 1376 | // with value k (greater than 1), the size of each chunk is determined in the |
| 1377 | // same way, with the restriction that the chunks do not contain fewer than k |
| 1378 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 1379 | // than k iterations). |
| 1380 | // |
| 1381 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 1382 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 1383 | // implementation the freedom to choose any possible mapping of iterations to |
| 1384 | // threads in the team. |
| 1385 | // |
| 1386 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 1387 | // deferred until run time, and the schedule and chunk size are taken from the |
| 1388 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 1389 | // implementation defined |
| 1390 | // |
| 1391 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 1392 | // idx = LB; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1393 | // while (idx <= UB) { BODY; ++idx; |
| 1394 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 1395 | // } // inner loop |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1396 | // } |
| 1397 | // |
| 1398 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1399 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 1400 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 1401 | // the team in a round-robin fashion in the order of the thread number. |
| 1402 | // |
| 1403 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 1404 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 1405 | // LB = LB + ST; |
| 1406 | // UB = UB + ST; |
| 1407 | // } |
| 1408 | // |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1409 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1410 | const Expr *IVExpr = S.getIterationVariable(); |
| 1411 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1412 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1413 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1414 | if (DynamicOrOrdered) { |
| 1415 | llvm::Value *UBVal = EmitScalarExpr(S.getLastIteration()); |
| 1416 | RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, |
| 1417 | IVSize, IVSigned, Ordered, UBVal, Chunk); |
| 1418 | } else { |
| 1419 | RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, |
| 1420 | IVSize, IVSigned, Ordered, IL, LB, UB, ST, Chunk); |
| 1421 | } |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1422 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1423 | auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
| 1424 | |
| 1425 | // Start the loop with a block that tests the condition. |
| 1426 | auto CondBlock = createBasicBlock("omp.dispatch.cond"); |
| 1427 | EmitBlock(CondBlock); |
| 1428 | LoopStack.push(CondBlock); |
| 1429 | |
| 1430 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1431 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1432 | // UB = min(UB, GlobalUB) |
| 1433 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 1434 | // IV = LB |
| 1435 | EmitIgnoredExpr(S.getInit()); |
| 1436 | // IV < UB |
Alexey Bataev | ae05c29 | 2015-06-16 11:59:36 +0000 | [diff] [blame] | 1437 | BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1438 | } else { |
| 1439 | BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, |
| 1440 | IL, LB, UB, ST); |
| 1441 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1442 | |
| 1443 | // If there are any cleanups between here and the loop-exit scope, |
| 1444 | // create a block to stage a loop exit along. |
| 1445 | auto ExitBlock = LoopExit.getBlock(); |
| 1446 | if (LoopScope.requiresCleanups()) |
| 1447 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 1448 | |
| 1449 | auto LoopBody = createBasicBlock("omp.dispatch.body"); |
| 1450 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 1451 | if (ExitBlock != LoopExit.getBlock()) { |
| 1452 | EmitBlock(ExitBlock); |
| 1453 | EmitBranchThroughCleanup(LoopExit); |
| 1454 | } |
| 1455 | EmitBlock(LoopBody); |
| 1456 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1457 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 1458 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1459 | if (DynamicOrOrdered) |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1460 | EmitIgnoredExpr(S.getInit()); |
| 1461 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1462 | // Create a block for the increment. |
| 1463 | auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
| 1464 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1465 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1466 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 1467 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1468 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1469 | LoopStack.setParallel(!IsMonotonic); |
| 1470 | else |
| 1471 | EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1472 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1473 | SourceLocation Loc = S.getLocStart(); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1474 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), |
| 1475 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 1476 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1477 | CGF.EmitStopPoint(&S); |
| 1478 | }, |
| 1479 | [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) { |
| 1480 | if (Ordered) { |
| 1481 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd( |
| 1482 | CGF, Loc, IVSize, IVSigned); |
| 1483 | } |
| 1484 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1485 | |
| 1486 | EmitBlock(Continue.getBlock()); |
| 1487 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1488 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1489 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
| 1490 | EmitIgnoredExpr(S.getNextLowerBound()); |
| 1491 | EmitIgnoredExpr(S.getNextUpperBound()); |
| 1492 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1493 | |
| 1494 | EmitBranch(CondBlock); |
| 1495 | LoopStack.pop(); |
| 1496 | // Emit the fall-through block. |
| 1497 | EmitBlock(LoopExit.getBlock()); |
| 1498 | |
| 1499 | // Tell the runtime we are done. |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1500 | if (!DynamicOrOrdered) |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1501 | RT.emitForStaticFinish(*this, S.getLocEnd()); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1504 | /// \brief Emit a helper variable and return corresponding lvalue. |
| 1505 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1506 | const DeclRefExpr *Helper) { |
| 1507 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1508 | CGF.EmitVarDecl(*VDecl); |
| 1509 | return CGF.EmitLValue(Helper); |
| 1510 | } |
| 1511 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1512 | namespace { |
| 1513 | struct ScheduleKindModifiersTy { |
| 1514 | OpenMPScheduleClauseKind Kind; |
| 1515 | OpenMPScheduleClauseModifier M1; |
| 1516 | OpenMPScheduleClauseModifier M2; |
| 1517 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 1518 | OpenMPScheduleClauseModifier M1, |
| 1519 | OpenMPScheduleClauseModifier M2) |
| 1520 | : Kind(Kind), M1(M1), M2(M2) {} |
| 1521 | }; |
| 1522 | } // namespace |
| 1523 | |
| 1524 | static std::pair<llvm::Value * /*Chunk*/, ScheduleKindModifiersTy> |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1525 | emitScheduleClause(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1526 | bool OuterRegion) { |
| 1527 | // Detect the loop schedule kind and chunk. |
| 1528 | auto ScheduleKind = OMPC_SCHEDULE_unknown; |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1529 | OpenMPScheduleClauseModifier M1 = OMPC_SCHEDULE_MODIFIER_unknown; |
| 1530 | OpenMPScheduleClauseModifier M2 = OMPC_SCHEDULE_MODIFIER_unknown; |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1531 | llvm::Value *Chunk = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1532 | if (const auto *C = S.getSingleClause<OMPScheduleClause>()) { |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1533 | ScheduleKind = C->getScheduleKind(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1534 | M1 = C->getFirstScheduleModifier(); |
| 1535 | M2 = C->getSecondScheduleModifier(); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1536 | if (const auto *Ch = C->getChunkSize()) { |
| 1537 | if (auto *ImpRef = cast_or_null<DeclRefExpr>(C->getHelperChunkSize())) { |
| 1538 | if (OuterRegion) { |
| 1539 | const VarDecl *ImpVar = cast<VarDecl>(ImpRef->getDecl()); |
| 1540 | CGF.EmitVarDecl(*ImpVar); |
| 1541 | CGF.EmitStoreThroughLValue( |
| 1542 | CGF.EmitAnyExpr(Ch), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1543 | CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(ImpVar), |
| 1544 | ImpVar->getType())); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1545 | } else { |
| 1546 | Ch = ImpRef; |
| 1547 | } |
| 1548 | } |
| 1549 | if (!C->getHelperChunkSize() || !OuterRegion) { |
| 1550 | Chunk = CGF.EmitScalarExpr(Ch); |
| 1551 | Chunk = CGF.EmitScalarConversion(Chunk, Ch->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 1552 | S.getIterationVariable()->getType(), |
| 1553 | S.getLocStart()); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | } |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1557 | return std::make_pair(Chunk, ScheduleKindModifiersTy(ScheduleKind, M1, M2)); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1560 | bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1561 | // Emit the loop iteration variable. |
| 1562 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 1563 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 1564 | EmitVarDecl(*IVDecl); |
| 1565 | |
| 1566 | // Emit the iterations count variable. |
| 1567 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 1568 | // iteration (e.g., it is foldable into a constant). |
| 1569 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 1570 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1571 | // Emit calculation of the iterations count. |
| 1572 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 1573 | } |
| 1574 | |
| 1575 | auto &RT = CGM.getOpenMPRuntime(); |
| 1576 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1577 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1578 | // Check pre-condition. |
| 1579 | { |
| 1580 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1581 | // If the condition constant folds and can be elided, avoid emitting the |
| 1582 | // whole loop. |
| 1583 | bool CondConstant; |
| 1584 | llvm::BasicBlock *ContBlock = nullptr; |
| 1585 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1586 | if (!CondConstant) |
| 1587 | return false; |
| 1588 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1589 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 1590 | ContBlock = createBasicBlock("omp.precond.end"); |
| 1591 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1592 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1593 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1594 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1595 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1596 | |
| 1597 | emitAlignedClause(*this, S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1598 | EmitOMPLinearClauseInit(S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1599 | // Emit 'then' code. |
| 1600 | { |
| 1601 | // Emit helper vars inits. |
| 1602 | LValue LB = |
| 1603 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 1604 | LValue UB = |
| 1605 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 1606 | LValue ST = |
| 1607 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 1608 | LValue IL = |
| 1609 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 1610 | |
| 1611 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1612 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
| 1613 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 1614 | // initialization of firstprivate variables. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1615 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 1616 | *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 1617 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1618 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 1619 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1620 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 1621 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1622 | emitPrivateLoopCounters(*this, LoopScope, S.counters(), |
| 1623 | S.private_counters()); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1624 | emitPrivateLinearVars(*this, S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 1625 | (void)LoopScope.Privatize(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Detect the loop schedule kind and chunk. |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1628 | llvm::Value *Chunk; |
| 1629 | OpenMPScheduleClauseKind ScheduleKind; |
| 1630 | auto ScheduleInfo = |
| 1631 | emitScheduleClause(*this, S, /*OuterRegion=*/false); |
| 1632 | Chunk = ScheduleInfo.first; |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1633 | ScheduleKind = ScheduleInfo.second.Kind; |
| 1634 | const OpenMPScheduleClauseModifier M1 = ScheduleInfo.second.M1; |
| 1635 | const OpenMPScheduleClauseModifier M2 = ScheduleInfo.second.M2; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1636 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1637 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1638 | const bool Ordered = S.getSingleClause<OMPOrderedClause>() != nullptr; |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1639 | // OpenMP 4.5, 2.7.1 Loop Construct, Description. |
| 1640 | // If the static schedule kind is specified or if the ordered clause is |
| 1641 | // specified, and if no monotonic modifier is specified, the effect will |
| 1642 | // be as if the monotonic modifier was specified. |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1643 | if (RT.isStaticNonchunked(ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1644 | /* Chunked */ Chunk != nullptr) && |
| 1645 | !Ordered) { |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1646 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1647 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1648 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1649 | // When no chunk_size is specified, the iteration space is divided into |
| 1650 | // chunks that are approximately equal in size, and at most one chunk is |
| 1651 | // distributed to each thread. Note that the size of the chunks is |
| 1652 | // unspecified in this case. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1653 | RT.emitForStaticInit(*this, S.getLocStart(), ScheduleKind, |
| 1654 | IVSize, IVSigned, Ordered, |
| 1655 | IL.getAddress(), LB.getAddress(), |
| 1656 | UB.getAddress(), ST.getAddress()); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1657 | auto LoopExit = |
| 1658 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1659 | // UB = min(UB, GlobalUB); |
| 1660 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 1661 | // IV = LB; |
| 1662 | EmitIgnoredExpr(S.getInit()); |
| 1663 | // while (idx <= UB) { BODY; ++idx; } |
Alexey Bataev | ae05c29 | 2015-06-16 11:59:36 +0000 | [diff] [blame] | 1664 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1665 | S.getInc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1666 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 1667 | CGF.EmitOMPLoopBody(S, LoopExit); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1668 | CGF.EmitStopPoint(&S); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1669 | }, |
| 1670 | [](CodeGenFunction &) {}); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1671 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1672 | // Tell the runtime we are done. |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1673 | RT.emitForStaticFinish(*this, S.getLocStart()); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1674 | } else { |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1675 | const bool IsMonotonic = Ordered || |
| 1676 | ScheduleKind == OMPC_SCHEDULE_static || |
| 1677 | ScheduleKind == OMPC_SCHEDULE_unknown || |
| 1678 | M1 == OMPC_SCHEDULE_MODIFIER_monotonic || |
| 1679 | M2 == OMPC_SCHEDULE_MODIFIER_monotonic; |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1680 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 1681 | // runtime and runs the inner loop to process it. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1682 | EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1683 | LB.getAddress(), UB.getAddress(), ST.getAddress(), |
| 1684 | IL.getAddress(), Chunk); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1685 | } |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 1686 | EmitOMPReductionClauseFinal(S); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1687 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 1688 | if (HasLastprivateClause) |
| 1689 | EmitOMPLastprivateClauseFinal( |
| 1690 | S, Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1691 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1692 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 1693 | EmitOMPSimdFinal(S); |
| 1694 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1695 | // 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] | 1696 | if (ContBlock) { |
| 1697 | EmitBranch(ContBlock); |
| 1698 | EmitBlock(ContBlock, true); |
| 1699 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1700 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1701 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1705 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1706 | bool HasLastprivates = false; |
| 1707 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) { |
| 1708 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S); |
| 1709 | }; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1710 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 1711 | S.hasCancel()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1714 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1715 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 1716 | } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1717 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1718 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1719 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
| 1720 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1721 | bool HasLastprivates = false; |
| 1722 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) { |
| 1723 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S); |
| 1724 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1725 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1726 | |
| 1727 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1728 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1729 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 1730 | } |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1733 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 1734 | const Twine &Name, |
| 1735 | llvm::Value *Init = nullptr) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1736 | auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1737 | if (Init) |
| 1738 | CGF.EmitScalarInit(Init, LVal); |
| 1739 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1742 | OpenMPDirectiveKind |
| 1743 | CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1744 | auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt(); |
| 1745 | auto *CS = dyn_cast<CompoundStmt>(Stmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1746 | bool HasLastprivates = false; |
| 1747 | auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF) { |
| 1748 | auto &C = CGF.CGM.getContext(); |
| 1749 | auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1750 | // Emit helper vars inits. |
| 1751 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 1752 | CGF.Builder.getInt32(0)); |
| 1753 | auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1) |
| 1754 | : CGF.Builder.getInt32(0); |
| 1755 | LValue UB = |
| 1756 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 1757 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 1758 | CGF.Builder.getInt32(1)); |
| 1759 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 1760 | CGF.Builder.getInt32(0)); |
| 1761 | // Loop counter. |
| 1762 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
| 1763 | OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 1764 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
| 1765 | OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 1766 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 1767 | // Generate condition for loop. |
| 1768 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
| 1769 | OK_Ordinary, S.getLocStart(), |
| 1770 | /*fpContractable=*/false); |
| 1771 | // Increment for loop counter. |
| 1772 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
| 1773 | S.getLocStart()); |
| 1774 | auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) { |
| 1775 | // Iterate through all sections and emit a switch construct: |
| 1776 | // switch (IV) { |
| 1777 | // case 0: |
| 1778 | // <SectionStmt[0]>; |
| 1779 | // break; |
| 1780 | // ... |
| 1781 | // case <NumSection> - 1: |
| 1782 | // <SectionStmt[<NumSection> - 1]>; |
| 1783 | // break; |
| 1784 | // } |
| 1785 | // .omp.sections.exit: |
| 1786 | auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 1787 | auto *SwitchStmt = CGF.Builder.CreateSwitch( |
| 1788 | CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB, |
| 1789 | CS == nullptr ? 1 : CS->size()); |
| 1790 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1791 | unsigned CaseNumber = 0; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 1792 | for (auto *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1793 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 1794 | CGF.EmitBlock(CaseBB); |
| 1795 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 1796 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1797 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 1798 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1799 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1800 | } else { |
| 1801 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 1802 | CGF.EmitBlock(CaseBB); |
| 1803 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
| 1804 | CGF.EmitStmt(Stmt); |
| 1805 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1806 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1807 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1808 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1809 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1810 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 1811 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1812 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 1813 | // initialization of firstprivate variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1814 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 1815 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 1816 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1817 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1818 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 1819 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 1820 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 1821 | (void)LoopScope.Privatize(); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1822 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1823 | // Emit static non-chunked loop. |
| 1824 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
| 1825 | CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32, |
| 1826 | /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), LB.getAddress(), |
| 1827 | UB.getAddress(), ST.getAddress()); |
| 1828 | // UB = min(UB, GlobalUB); |
| 1829 | auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart()); |
| 1830 | auto *MinUBGlobalUB = CGF.Builder.CreateSelect( |
| 1831 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 1832 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 1833 | // IV = LB; |
| 1834 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV); |
| 1835 | // while (idx <= UB) { BODY; ++idx; } |
| 1836 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 1837 | [](CodeGenFunction &) {}); |
| 1838 | // Tell the runtime we are done. |
| 1839 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart()); |
| 1840 | CGF.EmitOMPReductionClauseFinal(S); |
| 1841 | |
| 1842 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 1843 | if (HasLastprivates) |
| 1844 | CGF.EmitOMPLastprivateClauseFinal( |
| 1845 | S, CGF.Builder.CreateIsNotNull( |
| 1846 | CGF.EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1847 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1848 | |
| 1849 | bool HasCancel = false; |
| 1850 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 1851 | HasCancel = OSD->hasCancel(); |
| 1852 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 1853 | HasCancel = OPSD->hasCancel(); |
| 1854 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 1855 | HasCancel); |
| 1856 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 1857 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 1858 | // directive. |
| 1859 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1860 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 1861 | // initialization of firstprivate variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1862 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 1863 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1864 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 1865 | return OMPD_sections; |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1866 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1867 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1868 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
| 1869 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1870 | OpenMPDirectiveKind EmittedAs = EmitSections(S); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1871 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1872 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1873 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), EmittedAs); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1874 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1878 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1879 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1880 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1881 | }; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1882 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 1883 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1886 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1887 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1888 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1889 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1890 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1891 | // Check if there are any 'copyprivate' clauses associated with this |
| 1892 | // 'single' |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1893 | // construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1894 | // Build a list of copyprivate variables along with helper expressions |
| 1895 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1896 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1897 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1898 | DestExprs.append(C->destination_exprs().begin(), |
| 1899 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1900 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1901 | AssignmentOps.append(C->assignment_ops().begin(), |
| 1902 | C->assignment_ops().end()); |
| 1903 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1904 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1905 | // Emit code for 'single' region along with 'copyprivate' clauses |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 1906 | bool HasFirstprivates; |
| 1907 | auto &&CodeGen = [&S, &HasFirstprivates](CodeGenFunction &CGF) { |
| 1908 | CodeGenFunction::OMPPrivateScope SingleScope(CGF); |
| 1909 | HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope); |
Alexey Bataev | 59c654a | 2015-04-27 03:48:52 +0000 | [diff] [blame] | 1910 | CGF.EmitOMPPrivateClause(S, SingleScope); |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 1911 | (void)SingleScope.Privatize(); |
| 1912 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1913 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1914 | }; |
| 1915 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1916 | CopyprivateVars, DestExprs, SrcExprs, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1917 | AssignmentOps); |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 1918 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 1919 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1920 | if ((!S.getSingleClause<OMPNowaitClause>() || HasFirstprivates) && |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 1921 | CopyprivateVars.empty()) { |
| 1922 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 1923 | *this, S.getLocStart(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1924 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1925 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1928 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1929 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1930 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1931 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1932 | }; |
| 1933 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1936 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1937 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1938 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1939 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1940 | }; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 1941 | Expr *Hint = nullptr; |
| 1942 | if (auto *HintClause = S.getSingleClause<OMPHintClause>()) |
| 1943 | Hint = HintClause->getHint(); |
| 1944 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 1945 | S.getDirectiveName().getAsString(), |
| 1946 | CodeGen, S.getLocStart(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 1949 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 1950 | const OMPParallelForDirective &S) { |
| 1951 | // Emit directive as a combined directive that consists of two implicit |
| 1952 | // directives: 'parallel' with 'for' directive. |
| 1953 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1954 | (void)emitScheduleClause(*this, S, /*OuterRegion=*/true); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 1955 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1956 | CGF.EmitOMPWorksharingLoop(S); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 1957 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1958 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1961 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1962 | const OMPParallelForSimdDirective &S) { |
| 1963 | // Emit directive as a combined directive that consists of two implicit |
| 1964 | // directives: 'parallel' with 'for' directive. |
| 1965 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1966 | (void)emitScheduleClause(*this, S, /*OuterRegion=*/true); |
| 1967 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1968 | CGF.EmitOMPWorksharingLoop(S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1969 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1970 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1973 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1974 | const OMPParallelSectionsDirective &S) { |
| 1975 | // Emit directive as a combined directive that consists of two implicit |
| 1976 | // directives: 'parallel' with 'sections' directive. |
| 1977 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1978 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1979 | (void)CGF.EmitSections(S); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1980 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1981 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1984 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 1985 | // Emit outlined function for task construct. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1986 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1987 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 1988 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 1989 | auto *I = CS->getCapturedDecl()->param_begin(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1990 | auto *PartId = std::next(I); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1991 | // The first function argument for tasks is a thread id, the second one is a |
| 1992 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1993 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 1994 | // Get list of private variables. |
| 1995 | llvm::SmallVector<const Expr *, 8> PrivateVars; |
| 1996 | llvm::SmallVector<const Expr *, 8> PrivateCopies; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1997 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1998 | auto IRef = C->varlist_begin(); |
| 1999 | for (auto *IInit : C->private_copies()) { |
| 2000 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2001 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2002 | PrivateVars.push_back(*IRef); |
| 2003 | PrivateCopies.push_back(IInit); |
| 2004 | } |
| 2005 | ++IRef; |
| 2006 | } |
| 2007 | } |
| 2008 | EmittedAsPrivate.clear(); |
| 2009 | // Get list of firstprivate variables. |
| 2010 | llvm::SmallVector<const Expr *, 8> FirstprivateVars; |
| 2011 | llvm::SmallVector<const Expr *, 8> FirstprivateCopies; |
| 2012 | llvm::SmallVector<const Expr *, 8> FirstprivateInits; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2013 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2014 | auto IRef = C->varlist_begin(); |
| 2015 | auto IElemInitRef = C->inits().begin(); |
| 2016 | for (auto *IInit : C->private_copies()) { |
| 2017 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2018 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2019 | FirstprivateVars.push_back(*IRef); |
| 2020 | FirstprivateCopies.push_back(IInit); |
| 2021 | FirstprivateInits.push_back(*IElemInitRef); |
| 2022 | } |
| 2023 | ++IRef, ++IElemInitRef; |
| 2024 | } |
| 2025 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2026 | // Build list of dependences. |
| 2027 | llvm::SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 8> |
| 2028 | Dependences; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2029 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) { |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2030 | for (auto *IRef : C->varlists()) { |
| 2031 | Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); |
| 2032 | } |
| 2033 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2034 | auto &&CodeGen = [PartId, &S, &PrivateVars, &FirstprivateVars]( |
| 2035 | CodeGenFunction &CGF) { |
| 2036 | // Set proper addresses for generated private copies. |
| 2037 | auto *CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2038 | OMPPrivateScope Scope(CGF); |
| 2039 | if (!PrivateVars.empty() || !FirstprivateVars.empty()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2040 | auto *CopyFn = CGF.Builder.CreateLoad( |
| 2041 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); |
| 2042 | auto *PrivatesPtr = CGF.Builder.CreateLoad( |
| 2043 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2044 | // Map privates. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2045 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2046 | PrivatePtrs; |
| 2047 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 2048 | CallArgs.push_back(PrivatesPtr); |
| 2049 | for (auto *E : PrivateVars) { |
| 2050 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2051 | Address PrivatePtr = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2052 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType())); |
| 2053 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2054 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2055 | } |
| 2056 | for (auto *E : FirstprivateVars) { |
| 2057 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2058 | Address PrivatePtr = |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2059 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType())); |
| 2060 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2061 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2062 | } |
| 2063 | CGF.EmitRuntimeCall(CopyFn, CallArgs); |
| 2064 | for (auto &&Pair : PrivatePtrs) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2065 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 2066 | CGF.getContext().getDeclAlign(Pair.first)); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2067 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 2068 | } |
| 2069 | } |
| 2070 | (void)Scope.Privatize(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2071 | if (*PartId) { |
| 2072 | // TODO: emit code for untied tasks. |
| 2073 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2074 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2075 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2076 | auto OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
| 2077 | S, *I, OMPD_task, CodeGen); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2078 | // Check if we should emit tied or untied task. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2079 | bool Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2080 | // Check if the task is final |
| 2081 | llvm::PointerIntPair<llvm::Value *, 1, bool> Final; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2082 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2083 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2084 | // the condition and the dead arm of the if/else. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2085 | auto *Cond = Clause->getCondition(); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2086 | bool CondConstant; |
| 2087 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 2088 | Final.setInt(CondConstant); |
| 2089 | else |
| 2090 | Final.setPointer(EvaluateExprAsBool(Cond)); |
| 2091 | } else { |
| 2092 | // By default the task is not final. |
| 2093 | Final.setInt(/*IntVal=*/false); |
| 2094 | } |
| 2095 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2096 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 2097 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 2098 | if (C->getNameModifier() == OMPD_unknown || |
| 2099 | C->getNameModifier() == OMPD_task) { |
| 2100 | IfCond = C->getCondition(); |
| 2101 | break; |
| 2102 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 2103 | } |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 2104 | CGM.getOpenMPRuntime().emitTaskCall( |
| 2105 | *this, S.getLocStart(), S, Tied, Final, OutlinedFn, SharedsTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2106 | CapturedStruct, IfCond, PrivateVars, PrivateCopies, FirstprivateVars, |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2107 | FirstprivateCopies, FirstprivateInits, Dependences); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 2110 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 2111 | const OMPTaskyieldDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2112 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 2115 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2116 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 2119 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
| 2120 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2123 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 2124 | const OMPTaskgroupDirective &S) { |
| 2125 | LexicalScope Scope(*this, S.getSourceRange()); |
| 2126 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 2127 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 2128 | }; |
| 2129 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart()); |
| 2130 | } |
| 2131 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 2132 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2133 | CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2134 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2135 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 2136 | FlushClause->varlist_end()); |
| 2137 | } |
| 2138 | return llvm::None; |
| 2139 | }(), S.getLocStart()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 2142 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 2143 | const OMPDistributeDirective &S) { |
| 2144 | llvm_unreachable("CodeGen for 'omp distribute' is not supported yet."); |
| 2145 | } |
| 2146 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2147 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
| 2148 | const CapturedStmt *S) { |
| 2149 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 2150 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 2151 | CGF.CapturedStmtInfo = &CapStmtInfo; |
| 2152 | auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); |
| 2153 | Fn->addFnAttr(llvm::Attribute::NoInline); |
| 2154 | return Fn; |
| 2155 | } |
| 2156 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2157 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 2158 | if (!S.getAssociatedStmt()) |
| 2159 | return; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2160 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2161 | auto *C = S.getSingleClause<OMPSIMDClause>(); |
| 2162 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF) { |
| 2163 | if (C) { |
| 2164 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2165 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 2166 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
| 2167 | auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); |
| 2168 | CGF.EmitNounwindRuntimeCall(OutlinedFn, CapturedVars); |
| 2169 | } else { |
| 2170 | CGF.EmitStmt( |
| 2171 | cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 2172 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2173 | }; |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 2174 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2177 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2178 | QualType SrcType, QualType DestType, |
| 2179 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2180 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 2181 | "DestType must have scalar evaluation kind."); |
| 2182 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
| 2183 | return Val.isScalar() |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2184 | ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType, |
| 2185 | Loc) |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2186 | : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2187 | DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | static CodeGenFunction::ComplexPairTy |
| 2191 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2192 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2193 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 2194 | "DestType must have complex evaluation kind."); |
| 2195 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 2196 | if (Val.isScalar()) { |
| 2197 | // Convert the input element to the element type of the complex. |
| 2198 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2199 | auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 2200 | DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2201 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 2202 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 2203 | } else { |
| 2204 | assert(Val.isComplex() && "Must be a scalar or complex."); |
| 2205 | auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 2206 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
| 2207 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2208 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2209 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2210 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2211 | } |
| 2212 | return ComplexVal; |
| 2213 | } |
| 2214 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2215 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 2216 | LValue LVal, RValue RVal) { |
| 2217 | if (LVal.isGlobalReg()) { |
| 2218 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 2219 | } else { |
| 2220 | CGF.EmitAtomicStore(RVal, LVal, IsSeqCst ? llvm::SequentiallyConsistent |
| 2221 | : llvm::Monotonic, |
| 2222 | LVal.isVolatile(), /*IsInit=*/false); |
| 2223 | } |
| 2224 | } |
| 2225 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 2226 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 2227 | QualType RValTy, SourceLocation Loc) { |
| 2228 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2229 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 2230 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 2231 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 2232 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2233 | break; |
| 2234 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 2235 | EmitStoreOfComplex( |
| 2236 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2237 | /*isInit=*/false); |
| 2238 | break; |
| 2239 | case TEK_Aggregate: |
| 2240 | llvm_unreachable("Must be a scalar or complex."); |
| 2241 | } |
| 2242 | } |
| 2243 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2244 | static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 2245 | const Expr *X, const Expr *V, |
| 2246 | SourceLocation Loc) { |
| 2247 | // v = x; |
| 2248 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 2249 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 2250 | LValue XLValue = CGF.EmitLValue(X); |
| 2251 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 2252 | RValue Res = XLValue.isGlobalReg() |
| 2253 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
| 2254 | : CGF.EmitAtomicLoad(XLValue, Loc, |
| 2255 | IsSeqCst ? llvm::SequentiallyConsistent |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 2256 | : llvm::Monotonic, |
| 2257 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2258 | // OpenMP, 2.12.6, atomic Construct |
| 2259 | // Any atomic construct with a seq_cst clause forces the atomically |
| 2260 | // performed operation to include an implicit flush operation without a |
| 2261 | // list. |
| 2262 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 2263 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 2264 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 2267 | static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 2268 | const Expr *X, const Expr *E, |
| 2269 | SourceLocation Loc) { |
| 2270 | // x = expr; |
| 2271 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2272 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 2273 | // OpenMP, 2.12.6, atomic Construct |
| 2274 | // Any atomic construct with a seq_cst clause forces the atomically |
| 2275 | // performed operation to include an implicit flush operation without a |
| 2276 | // list. |
| 2277 | if (IsSeqCst) |
| 2278 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 2279 | } |
| 2280 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 2281 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 2282 | RValue Update, |
| 2283 | BinaryOperatorKind BO, |
| 2284 | llvm::AtomicOrdering AO, |
| 2285 | bool IsXLHSInRHSPart) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2286 | auto &Context = CGF.CGM.getContext(); |
| 2287 | // 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] | 2288 | // expression is simple and atomic is allowed for the given type for the |
| 2289 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2290 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 2291 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 2292 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 2293 | (Update.getScalarVal()->getType() != |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2294 | X.getAddress().getElementType())) || |
| 2295 | !X.getAddress().getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2296 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 2297 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2298 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2299 | |
| 2300 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 2301 | switch (BO) { |
| 2302 | case BO_Add: |
| 2303 | RMWOp = llvm::AtomicRMWInst::Add; |
| 2304 | break; |
| 2305 | case BO_Sub: |
| 2306 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2307 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2308 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 2309 | break; |
| 2310 | case BO_And: |
| 2311 | RMWOp = llvm::AtomicRMWInst::And; |
| 2312 | break; |
| 2313 | case BO_Or: |
| 2314 | RMWOp = llvm::AtomicRMWInst::Or; |
| 2315 | break; |
| 2316 | case BO_Xor: |
| 2317 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 2318 | break; |
| 2319 | case BO_LT: |
| 2320 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 2321 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 2322 | : llvm::AtomicRMWInst::Max) |
| 2323 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 2324 | : llvm::AtomicRMWInst::UMax); |
| 2325 | break; |
| 2326 | case BO_GT: |
| 2327 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 2328 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 2329 | : llvm::AtomicRMWInst::Min) |
| 2330 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 2331 | : llvm::AtomicRMWInst::UMin); |
| 2332 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2333 | case BO_Assign: |
| 2334 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 2335 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2336 | case BO_Mul: |
| 2337 | case BO_Div: |
| 2338 | case BO_Rem: |
| 2339 | case BO_Shl: |
| 2340 | case BO_Shr: |
| 2341 | case BO_LAnd: |
| 2342 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2343 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2344 | case BO_PtrMemD: |
| 2345 | case BO_PtrMemI: |
| 2346 | case BO_LE: |
| 2347 | case BO_GE: |
| 2348 | case BO_EQ: |
| 2349 | case BO_NE: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2350 | case BO_AddAssign: |
| 2351 | case BO_SubAssign: |
| 2352 | case BO_AndAssign: |
| 2353 | case BO_OrAssign: |
| 2354 | case BO_XorAssign: |
| 2355 | case BO_MulAssign: |
| 2356 | case BO_DivAssign: |
| 2357 | case BO_RemAssign: |
| 2358 | case BO_ShlAssign: |
| 2359 | case BO_ShrAssign: |
| 2360 | case BO_Comma: |
| 2361 | llvm_unreachable("Unsupported atomic update operation"); |
| 2362 | } |
| 2363 | auto *UpdateVal = Update.getScalarVal(); |
| 2364 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 2365 | UpdateVal = CGF.Builder.CreateIntCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2366 | IC, X.getAddress().getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2367 | X.getType()->hasSignedIntegerRepresentation()); |
| 2368 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2369 | auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2370 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2373 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2374 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 2375 | llvm::AtomicOrdering AO, SourceLocation Loc, |
| 2376 | const llvm::function_ref<RValue(RValue)> &CommonGen) { |
| 2377 | // Update expressions are allowed to have the following forms: |
| 2378 | // x binop= expr; -> xrval + expr; |
| 2379 | // x++, ++x -> xrval + 1; |
| 2380 | // x--, --x -> xrval - 1; |
| 2381 | // x = x binop expr; -> xrval binop expr |
| 2382 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2383 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 2384 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2385 | if (X.isGlobalReg()) { |
| 2386 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 2387 | // 'xrval'. |
| 2388 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 2389 | } else { |
| 2390 | // Perform compare-and-swap procedure. |
| 2391 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2392 | } |
| 2393 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2394 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 2398 | const Expr *X, const Expr *E, |
| 2399 | const Expr *UE, bool IsXLHSInRHSPart, |
| 2400 | SourceLocation Loc) { |
| 2401 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 2402 | "Update expr in 'atomic update' must be a binary operator."); |
| 2403 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 2404 | // Update expressions are allowed to have the following forms: |
| 2405 | // x binop= expr; -> xrval + expr; |
| 2406 | // x++, ++x -> xrval + 1; |
| 2407 | // x--, --x -> xrval - 1; |
| 2408 | // x = x binop expr; -> xrval binop expr |
| 2409 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2410 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2411 | LValue XLValue = CGF.EmitLValue(X); |
| 2412 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2413 | auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 2414 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 2415 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 2416 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 2417 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 2418 | auto Gen = |
| 2419 | [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue { |
| 2420 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 2421 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 2422 | return CGF.EmitAnyExpr(UE); |
| 2423 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2424 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 2425 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 2426 | // OpenMP, 2.12.6, atomic Construct |
| 2427 | // Any atomic construct with a seq_cst clause forces the atomically |
| 2428 | // performed operation to include an implicit flush operation without a |
| 2429 | // list. |
| 2430 | if (IsSeqCst) |
| 2431 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 2432 | } |
| 2433 | |
| 2434 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2435 | QualType SourceType, QualType ResType, |
| 2436 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2437 | switch (CGF.getEvaluationKind(ResType)) { |
| 2438 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2439 | return RValue::get( |
| 2440 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2441 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2442 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2443 | return RValue::getComplex(Res.first, Res.second); |
| 2444 | } |
| 2445 | case TEK_Aggregate: |
| 2446 | break; |
| 2447 | } |
| 2448 | llvm_unreachable("Must be a scalar or complex."); |
| 2449 | } |
| 2450 | |
| 2451 | static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 2452 | bool IsPostfixUpdate, const Expr *V, |
| 2453 | const Expr *X, const Expr *E, |
| 2454 | const Expr *UE, bool IsXLHSInRHSPart, |
| 2455 | SourceLocation Loc) { |
| 2456 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 2457 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 2458 | RValue NewVVal; |
| 2459 | LValue VLValue = CGF.EmitLValue(V); |
| 2460 | LValue XLValue = CGF.EmitLValue(X); |
| 2461 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
| 2462 | auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic; |
| 2463 | QualType NewVValType; |
| 2464 | if (UE) { |
| 2465 | // 'x' is updated with some additional value. |
| 2466 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 2467 | "Update expr in 'atomic capture' must be a binary operator."); |
| 2468 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 2469 | // Update expressions are allowed to have the following forms: |
| 2470 | // x binop= expr; -> xrval + expr; |
| 2471 | // x++, ++x -> xrval + 1; |
| 2472 | // x--, --x -> xrval - 1; |
| 2473 | // x = x binop expr; -> xrval binop expr |
| 2474 | // x = expr Op x; - > expr binop xrval; |
| 2475 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 2476 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 2477 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 2478 | NewVValType = XRValExpr->getType(); |
| 2479 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 2480 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
| 2481 | IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue { |
| 2482 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 2483 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 2484 | RValue Res = CGF.EmitAnyExpr(UE); |
| 2485 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 2486 | return Res; |
| 2487 | }; |
| 2488 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 2489 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 2490 | if (Res.first) { |
| 2491 | // 'atomicrmw' instruction was generated. |
| 2492 | if (IsPostfixUpdate) { |
| 2493 | // Use old value from 'atomicrmw'. |
| 2494 | NewVVal = Res.second; |
| 2495 | } else { |
| 2496 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 2497 | // value of 'x'. |
| 2498 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 2499 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 2500 | NewVVal = CGF.EmitAnyExpr(UE); |
| 2501 | } |
| 2502 | } |
| 2503 | } else { |
| 2504 | // 'x' is simply rewritten with some 'expr'. |
| 2505 | NewVValType = X->getType().getNonReferenceType(); |
| 2506 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 2507 | X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2508 | auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue { |
| 2509 | NewVVal = XRValue; |
| 2510 | return ExprRValue; |
| 2511 | }; |
| 2512 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 2513 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 2514 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 2515 | Loc, Gen); |
| 2516 | if (Res.first) { |
| 2517 | // 'atomicrmw' instruction was generated. |
| 2518 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 2519 | } |
| 2520 | } |
| 2521 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 2522 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2523 | // OpenMP, 2.12.6, atomic Construct |
| 2524 | // Any atomic construct with a seq_cst clause forces the atomically |
| 2525 | // performed operation to include an implicit flush operation without a |
| 2526 | // list. |
| 2527 | if (IsSeqCst) |
| 2528 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 2529 | } |
| 2530 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2531 | static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2532 | bool IsSeqCst, bool IsPostfixUpdate, |
| 2533 | const Expr *X, const Expr *V, const Expr *E, |
| 2534 | const Expr *UE, bool IsXLHSInRHSPart, |
| 2535 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2536 | switch (Kind) { |
| 2537 | case OMPC_read: |
| 2538 | EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
| 2539 | break; |
| 2540 | case OMPC_write: |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 2541 | EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
| 2542 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2543 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2544 | case OMPC_update: |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2545 | EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
| 2546 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2547 | case OMPC_capture: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2548 | EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
| 2549 | IsXLHSInRHSPart, Loc); |
| 2550 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2551 | case OMPC_if: |
| 2552 | case OMPC_final: |
| 2553 | case OMPC_num_threads: |
| 2554 | case OMPC_private: |
| 2555 | case OMPC_firstprivate: |
| 2556 | case OMPC_lastprivate: |
| 2557 | case OMPC_reduction: |
| 2558 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 2559 | case OMPC_simdlen: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2560 | case OMPC_collapse: |
| 2561 | case OMPC_default: |
| 2562 | case OMPC_seq_cst: |
| 2563 | case OMPC_shared: |
| 2564 | case OMPC_linear: |
| 2565 | case OMPC_aligned: |
| 2566 | case OMPC_copyin: |
| 2567 | case OMPC_copyprivate: |
| 2568 | case OMPC_flush: |
| 2569 | case OMPC_proc_bind: |
| 2570 | case OMPC_schedule: |
| 2571 | case OMPC_ordered: |
| 2572 | case OMPC_nowait: |
| 2573 | case OMPC_untied: |
| 2574 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2575 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2576 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 2577 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 2578 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 2579 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 2580 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 2581 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 2582 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 2583 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 2584 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 2585 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 2586 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 2587 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 2588 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 2589 | case OMPC_defaultmap: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2590 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2595 | bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2596 | OpenMPClauseKind Kind = OMPC_unknown; |
| 2597 | for (auto *C : S.clauses()) { |
| 2598 | // Find first clause (skip seq_cst clause, if it is first). |
| 2599 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 2600 | Kind = C->getClauseKind(); |
| 2601 | break; |
| 2602 | } |
| 2603 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 2604 | |
| 2605 | const auto *CS = |
| 2606 | S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2607 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) { |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 2608 | enterFullExpression(EWC); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2609 | } |
| 2610 | // Processing for statements under 'atomic capture'. |
| 2611 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
| 2612 | for (const auto *C : Compound->body()) { |
| 2613 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) { |
| 2614 | enterFullExpression(EWC); |
| 2615 | } |
| 2616 | } |
| 2617 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 2618 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2619 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 2620 | auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF) { |
| 2621 | CGF.EmitStopPoint(CS); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2622 | EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
| 2623 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
| 2624 | S.isXLHSInRHSPart(), S.getLocStart()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2625 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2626 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2629 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 2630 | LexicalScope Scope(*this, S.getSourceRange()); |
| 2631 | const CapturedStmt &CS = *cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2632 | |
| 2633 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 2634 | GenerateOpenMPCapturedVars(CS, CapturedVars); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2635 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2636 | llvm::Function *Fn = nullptr; |
| 2637 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2638 | |
| 2639 | // Check if we have any if clause associated with the directive. |
| 2640 | const Expr *IfCond = nullptr; |
| 2641 | |
| 2642 | if (auto *C = S.getSingleClause<OMPIfClause>()) { |
| 2643 | IfCond = C->getCondition(); |
| 2644 | } |
| 2645 | |
| 2646 | // Check if we have any device clause associated with the directive. |
| 2647 | const Expr *Device = nullptr; |
| 2648 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) { |
| 2649 | Device = C->getDevice(); |
| 2650 | } |
| 2651 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 2652 | // Check if we have an if clause whose conditional always evaluates to false |
| 2653 | // or if we do not have any targets specified. If so the target region is not |
| 2654 | // an offload entry point. |
| 2655 | bool IsOffloadEntry = true; |
| 2656 | if (IfCond) { |
| 2657 | bool Val; |
| 2658 | if (ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
| 2659 | IsOffloadEntry = false; |
| 2660 | } |
| 2661 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 2662 | IsOffloadEntry = false; |
| 2663 | |
| 2664 | assert(CurFuncDecl && "No parent declaration for target region!"); |
| 2665 | StringRef ParentName; |
| 2666 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 2667 | // the mangling of the device outlined kernel. |
| 2668 | if (auto *D = dyn_cast<CXXConstructorDecl>(CurFuncDecl)) |
| 2669 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
| 2670 | else if (auto *D = dyn_cast<CXXDestructorDecl>(CurFuncDecl)) |
| 2671 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 2672 | else |
| 2673 | ParentName = |
| 2674 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CurFuncDecl))); |
| 2675 | |
| 2676 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 2677 | IsOffloadEntry); |
| 2678 | |
| 2679 | CGM.getOpenMPRuntime().emitTargetCall(*this, S, Fn, FnID, IfCond, Device, |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 2680 | CapturedVars); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2683 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) { |
| 2684 | llvm_unreachable("CodeGen for 'omp teams' is not supported yet."); |
| 2685 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2686 | |
| 2687 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 2688 | const OMPCancellationPointDirective &S) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2689 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(), |
| 2690 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2693 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 2694 | const Expr *IfCond = nullptr; |
| 2695 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 2696 | if (C->getNameModifier() == OMPD_unknown || |
| 2697 | C->getNameModifier() == OMPD_cancel) { |
| 2698 | IfCond = C->getCondition(); |
| 2699 | break; |
| 2700 | } |
| 2701 | } |
| 2702 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2703 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2706 | CodeGenFunction::JumpDest |
| 2707 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
| 2708 | if (Kind == OMPD_parallel || Kind == OMPD_task) |
| 2709 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2710 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2711 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2712 | return BreakContinueStack.back().BreakBlock; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2713 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2714 | |
| 2715 | // Generate the instructions for '#pragma omp target data' directive. |
| 2716 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 2717 | const OMPTargetDataDirective &S) { |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2718 | // emit the code inside the construct for now |
| 2719 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
Michael Wong | b5c1698 | 2015-08-11 04:52:01 +0000 | [diff] [blame] | 2720 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2721 | *this, OMPD_target_data, |
| 2722 | [&CS](CodeGenFunction &CGF) { CGF.EmitStmt(CS->getCapturedStmt()); }); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2723 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2724 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 2725 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 2726 | const OMPTargetEnterDataDirective &S) { |
| 2727 | // TODO: codegen for target enter data. |
| 2728 | } |
| 2729 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 2730 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 2731 | const OMPTargetExitDataDirective &S) { |
| 2732 | // TODO: codegen for target exit data. |
| 2733 | } |
| 2734 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 2735 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 2736 | const OMPTargetParallelDirective &S) { |
| 2737 | // TODO: codegen for target parallel. |
| 2738 | } |
| 2739 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 2740 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 2741 | const OMPTargetParallelForDirective &S) { |
| 2742 | // TODO: codegen for target parallel for. |
| 2743 | } |
| 2744 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 2745 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
| 2746 | // emit the code inside the construct for now |
| 2747 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2748 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2749 | *this, OMPD_taskloop, |
| 2750 | [&CS](CodeGenFunction &CGF) { CGF.EmitStmt(CS->getCapturedStmt()); }); |
| 2751 | } |
| 2752 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 2753 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 2754 | const OMPTaskLoopSimdDirective &S) { |
| 2755 | // emit the code inside the construct for now |
| 2756 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2757 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2758 | *this, OMPD_taskloop_simd, |
| 2759 | [&CS](CodeGenFunction &CGF) { CGF.EmitStmt(CS->getCapturedStmt()); }); |
| 2760 | } |
| 2761 | |