[flang-commits] [flang] 87d8872 - [flang][OpenMP] Do not emit barrier after SECTIONS with LASTPRIVATE and NOWAIT (#216018)

via flang-commits flang-commits at lists.llvm.org
Mon Aug 17 05:02:23 PDT 2026


Author: ejose02
Date: 2026-08-17T17:32:17+05:30
New Revision: 87d8872732bfc3dafb86de00eb67f178f3acab34

URL: https://github.com/llvm/llvm-project/commit/87d8872732bfc3dafb86de00eb67f178f3acab34
DIFF: https://github.com/llvm/llvm-project/commit/87d8872732bfc3dafb86de00eb67f178f3acab34.diff

LOG: [flang][OpenMP] Do not emit barrier after SECTIONS with LASTPRIVATE and NOWAIT (#216018)

Fixes #192907

Flang inserted an explicit omp.barrier after SECTIONS when both
lastprivate and nowait were present, which serialized all threads at the
end of the construct and prevented nowait from taking effect.

Limit the barrier to lastprivate(conditional:) cases, where reduction
results must be finalized before the post-sections copy-back. Regular
lastprivate + nowait now matches wsloop lowering behavior.

Added: 
    

Modified: 
    flang/lib/Lower/OpenMP/OpenMP.cpp
    flang/test/Lower/OpenMP/sections.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 64b8acd817b34..280f71de33388 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3688,9 +3688,6 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
   llvm::SmallDenseSet<const semantics::Symbol *> condLpSymSet(
       condLpSyms.begin(), condLpSyms.end());
 
-  // Track whether any non-conditional lastprivate copy-backs were emitted.
-  bool hasNonCondLastprivate = false;
-
   if (!lastprivates.empty()) {
     mlir::Region &sectionsBody = sectionsOp.getRegion();
     assert(sectionsBody.hasOneBlock());
@@ -3712,7 +3709,6 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
         // Skip conditional LP symbols — handled by the reduction path.
         if (condLpSymSet.count(sym))
           continue;
-        hasNonCondLastprivate = true;
         if (const auto *common =
                 sym->detailsIf<semantics::CommonBlockDetails>()) {
           for (const auto &obj : common->objects())
@@ -3727,16 +3723,9 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
   // Perform DataSharingProcessor's step2 out of SECTIONS
   builder.setInsertionPointAfter(sectionsOp.getOperation());
   dsp.processStep2(sectionsOp, false);
-  // Emit barrier when nowait is present and there are lastprivate copy-backs
-  // (either non-conditional or conditional).  The barrier ensures all threads
-  // have completed their work before lastprivate values are read/copied.
-  //
-  // NOTE: The LLVM OpenMP runtime currently imposes an implicit barrier
-  // inside __kmpc_reduce for tree reductions.  If the runtime were modified
-  // to release losing threads early when nowait is specified, we could use
-  // the return value from the tree reduction (case 1 = winner) to let the
-  // winner thread perform the copy-back without a separate barrier.
-  if (clauseOps.nowait && (hasNonCondLastprivate || !condLpSyms.empty()))
+  // Emit barrier when nowait is present and conditional lastprivate reduction
+  // results must be finalized before copy-back.
+  if (clauseOps.nowait && !condLpSyms.empty())
     mlir::omp::BarrierOp::create(builder, loc);
 
   // Copy-back: copy winning values from the shared reduction struct to the

diff  --git a/flang/test/Lower/OpenMP/sections.f90 b/flang/test/Lower/OpenMP/sections.f90
index e3155661f35bc..e3aa4a464d0a7 100644
--- a/flang/test/Lower/OpenMP/sections.f90
+++ b/flang/test/Lower/OpenMP/sections.f90
@@ -209,7 +209,7 @@ subroutine lastprivate()
             x = x + 1
 !CHECK: omp.terminator
 !CHECK: }
-!CHECK: omp.barrier
+!CHECK-NOT: omp.barrier
      !$omp end sections nowait
 
 !CHECK: %[[PRIVATE_X:.*]] = fir.alloca i32 {bindc_name = "x", pinned, uniq_name = "_QFlastprivateEx"}


        


More information about the flang-commits mailing list