[flang-commits] [flang] [flang][OpenMP] Move privatizations out of sections (PR #88191)

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Wed Apr 10 05:24:23 PDT 2024


================
@@ -1983,30 +1980,68 @@ genOMP(Fortran::lower::AbstractConverter &converter,
         std::get<Fortran::parser::OmpEndSectionsDirective>(sectionsConstruct.t);
     const auto &endSectionsClauseList =
         std::get<Fortran::parser::OmpClauseList>(endSectionsDirective.t);
-    ClauseProcessor(converter, semaCtx, endSectionsClauseList)
-        .processNowait(nowaitClauseOperand);
+    hasNowait = ClauseProcessor(converter, semaCtx, endSectionsClauseList)
+                    .processNowait(nowaitClauseOperand);
   }
 
+  // Insert privatizations before SECTIONS
+  symTable.pushScope();
+  DataSharingProcessor dsp(converter, semaCtx, sectionsClauseList, eval);
+  dsp.processStep1();
+
   // SECTIONS construct
-  genOpWithBody<mlir::omp::SectionsOp>(
+  auto sectionsOp = genOpWithBody<mlir::omp::SectionsOp>(
       OpWithBodyGenInfo(converter, semaCtx, currentLocation, eval)
           .setGenNested(false),
       /*reduction_vars=*/mlir::ValueRange(),
       /*reductions=*/nullptr, allocateOperands, allocatorOperands,
       nowaitClauseOperand);
 
+  std::optional<Clause> lastPrivateClause;
+  for (const Fortran::parser::OmpClause &clause : sectionsClauseList.v) {
+    if (std::holds_alternative<Fortran::parser::OmpClause::Lastprivate>(
+            clause.u)) {
+      lastPrivateClause = makeClause(clause, semaCtx);
+    }
+  }
+
   const auto &sectionBlocks =
       std::get<Fortran::parser::OmpSectionBlocks>(sectionsConstruct.t);
   auto &firOpBuilder = converter.getFirOpBuilder();
   auto ip = firOpBuilder.saveInsertionPoint();
-  for (const auto &[nblock, neval] :
-       llvm::zip(sectionBlocks.v, eval.getNestedEvaluations())) {
-    symTable.pushScope();
-    genSectionOp(converter, semaCtx, neval, /*genNested=*/true, currentLocation,
-                 sectionsClauseList);
-    symTable.popScope();
+  auto zippy = llvm::zip(sectionBlocks.v, eval.getNestedEvaluations());
+  auto it = zippy.begin(), next = it, end = zippy.end();
+  ++next;
+  for (; it != end; it = next, ++next) {
+    const auto &[nblock, neval] = *it;
+    mlir::omp::SectionOp sectionOp = genSectionOp(
+        converter, semaCtx, neval, /*genNested=*/true, currentLocation);
+    // For `omp.sections`, lastprivatized variables occur in
+    // lexically final `omp.section` operation.
+    if (next == end && lastPrivateClause) {
----------------
kparzysz wrote:

This code doesn't need to be in the loop that creates section ops.  You can just create all section ops, and only remember the most recently created SectionOp.  Then after the loop, if lastprivate was present, insert the copying code.

https://github.com/llvm/llvm-project/pull/88191


More information about the flang-commits mailing list