diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp --- a/clang/lib/CodeGen/CGStmtOpenMP.cpp +++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp @@ -1683,13 +1683,65 @@ const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); const Stmt *ParallelRegionBodyStmt = CS->getCapturedStmt(); - auto BodyGenCB = [ParallelRegionBodyStmt, - this](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, - llvm::BasicBlock &ContinuationBB) { + OMPParallelScope Scope(*this, S); + llvm::SmallDenseMap + CapturedVarsInfoMap; + + OMPBuilderCBHelpers::GenerateOpenMPCapturedVars(*this, *CS, + CapturedVarsInfoMap); + + auto BodyGenCB = [ParallelRegionBodyStmt, &S, this, &OMPBuilder, + &CapturedVarsInfoMap](InsertPointTy AllocaIP, + InsertPointTy CodeGenIP, + llvm::BasicBlock &ContinuationBB) { OMPBuilderCBHelpers::OutlinedRegionBodyRAII ORB(*this, AllocaIP, ContinuationBB); + + OMPPrivateScope PrivateScope(*this); + llvm::BasicBlock *OMP_Entry = AllocaIP.getBlock(); + // Emitting Copyin clause + Builder.SetInsertPoint(OMP_Entry->getTerminator()); + bool Copyins = + OMPBuilderCBHelpers::EmitOMPCopyinClause(*this, S, AllocaIP); + + // restoring alloca insertion point to entry block since it moved while + // emitting 'copyin' blocks + AllocaInsertPt = OMP_Entry->getFirstNonPHIOrDbgOrLifetime(); + llvm::BranchInst *EntryBI = + cast(OMP_Entry->getTerminator()); + EntryBI->removeFromParent(); + + if (Builder.GetInsertBlock() == OMP_Entry) + Builder.SetInsertPoint(OMP_Entry); + OMPBuilderCBHelpers::EmitOMPFirstprivateClause(*this, S, PrivateScope, + CapturedVarsInfoMap); + if (Copyins) { + // Emit implicit barrier to synchronize threads and avoid data races on + // propagation master's thread values of threadprivate variables to + // local instances of that variables of all other implicit threads. + OMPBuilder->CreateBarrier(Builder, OMPD_barrier, /*EmitChecks=*/false, + /*ForceSimpleCall=*/true); + } + + EmitOMPPrivateClause(S, PrivateScope); + (void)PrivateScope.Privatize(); + + if (!OMP_Entry->getTerminator()) { + OMP_Entry->getInstList().push_back(EntryBI); + } else if (Builder.GetInsertBlock()->getTerminator()) { + EntryBI->dropAllReferences(); + EntryBI->deleteValue(); + } else { + Builder.Insert(EntryBI); + } + OMPBuilderCBHelpers::EmitOMPRegionBody(*this, ParallelRegionBodyStmt, CodeGenIP, ContinuationBB); + llvm::Instruction *ContTI = ContinuationBB.getTerminator(); + ContTI->removeFromParent(); + Builder.SetInsertPoint(&ContinuationBB); + PrivateScope.ForceCleanup(); + Builder.Insert(ContTI); }; CGCapturedStmtInfo CGSI(*CS, CR_OpenMP);