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 @@ -3546,6 +3546,72 @@ } void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { + if (CGM.getLangOpts().OpenMPIRBuilder) { + llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder(); + using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; + using BodyGenCallbackTy = llvm::OpenMPIRBuilder::BGenCallbackTy; + using FinalizationCallbackTy = llvm::OpenMPIRBuilder::FinalizeCallbackTy; + + auto FiniCB = [this](InsertPointTy IP) { + OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); + }; + // For now, it is the same as FiniCB, but is called for each of the sections + // Will be updated in the future as required + auto SectionFiniCB = [this](InsertPointTy IP) { + OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); + }; + + const CapturedStmt *ICS = S.getInnermostCapturedStmt(); + const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt(); + const auto *CS = dyn_cast(CapturedStmt); + llvm::SmallVector SectionCBVector; + llvm::SmallVector SectionFiniCBVector; + if (CS) { + for (const Stmt *SubStmt : CS->children()) { + auto SectionCB = [this, SubStmt](InsertPointTy AllocaIP, + InsertPointTy CodeGenIP, + llvm::BasicBlock &FiniBB) { + OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, + FiniBB); + OMPBuilderCBHelpers::EmitOMPRegionBody(*this, SubStmt, CodeGenIP, + FiniBB); + }; + SectionCBVector.push_back(SectionCB); + SectionFiniCBVector.push_back(SectionFiniCB); + } + } else { + auto SectionCB = [this, CapturedStmt](InsertPointTy AllocaIP, + InsertPointTy CodeGenIP, + llvm::BasicBlock &FiniBB) { + OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, FiniBB); + OMPBuilderCBHelpers::EmitOMPRegionBody(*this, CapturedStmt, CodeGenIP, + FiniBB); + }; + SectionCBVector.push_back(SectionCB); + } + + // Privatization callback that performs appropriate action for + // shared/private/firstprivate/lastprivate/copyin/... variables. + // + // TODO: This defaults to shared right now. + auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, + llvm::Value &, llvm::Value &Val, llvm::Value *&ReplVal) { + // The next line is appropriate only for variables (Val) with the + // data-sharing attribute "shared". + ReplVal = &Val; + + return CodeGenIP; + }; + + CGCapturedStmtInfo CGSI(*ICS, CR_OpenMP); + CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI); + llvm::OpenMPIRBuilder::InsertPointTy AllocaIP( + AllocaInsertPt->getParent(), AllocaInsertPt->getIterator()); + Builder.restoreIP(OMPBuilder.createSections( + Builder, AllocaIP, SectionCBVector, SectionFiniCBVector, PrivCB, FiniCB, + S.hasCancel(), S.getSingleClause())); + return; + } { auto LPCRegion = CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); @@ -5970,7 +6036,9 @@ llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder(); // TODO: This check is necessary as we only generate `omp parallel` through // the OpenMPIRBuilder for now. - if (S.getCancelRegion() == OMPD_parallel) { + if (S.getCancelRegion() == OMPD_parallel || + S.getCancelRegion() == OMPD_sections || + S.getCancelRegion() == OMPD_section) { llvm::Value *IfCondition = nullptr; if (IfCond) IfCondition = EmitScalarExpr(IfCond,