diff --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp --- a/flang/lib/Lower/OpenMP.cpp +++ b/flang/lib/Lower/OpenMP.cpp @@ -723,6 +723,8 @@ genOMP(Fortran::lower::AbstractConverter &converter, Fortran::lower::pft::Evaluation &eval, const Fortran::parser::OpenMPSectionConstruct §ionConstruct) { + if (eval.getNestedEvaluations().size() == 0) + return; auto &firOpBuilder = converter.getFirOpBuilder(); auto currentLocation = converter.getCurrentLocation(); @@ -736,6 +738,15 @@ genOMP(Fortran::lower::AbstractConverter &converter, Fortran::lower::pft::Evaluation &eval, const Fortran::parser::OpenMPSectionsConstruct §ionsConstruct) { + + bool emptySections = true; + for (Fortran::lower::pft::Evaluation &childEval : + eval.getNestedEvaluations()) { + emptySections &= childEval.getNestedEvaluations().size() == 0; + } + if (emptySections) + return; + auto &firOpBuilder = converter.getFirOpBuilder(); auto currentLocation = converter.getCurrentLocation(); SmallVector reductionVars, allocateOperands, allocatorOperands; diff --git a/flang/test/Lower/OpenMP/sections.f90 b/flang/test/Lower/OpenMP/sections.f90 --- a/flang/test/Lower/OpenMP/sections.f90 +++ b/flang/test/Lower/OpenMP/sections.f90 @@ -49,9 +49,6 @@ !FIRDialect: } !FIRDialect: omp.terminator !FIRDialect: } -!FIRDialect: omp.sections nowait { -!FIRDialect: omp.terminator -!FIRDialect: } !FIRDialect: return !FIRDialect: } @@ -101,12 +98,6 @@ !LLVMDialect: } !LLVMDialect: omp.terminator !LLVMDialect: } -!LLVMDialect: omp.sections nowait { -!LLVMDialect: omp.section { -!LLVMDialect: omp.terminator -!LLVMDialect: } -!LLVMDialect: omp.terminator -!LLVMDialect: } !LLVMDialect: llvm.return !LLVMDialect: } @@ -124,20 +115,11 @@ count = count * eta double_count = count - eta !$omp end sections - - !$omp sections - !$omp end sections nowait end program sample !FIRDialect: func @_QPfirstprivate(%[[ARG:.*]]: !fir.ref {fir.bindc_name = "alpha"}) { !FIRDialect: omp.sections { !FIRDialect: omp.section { -!FIRDialect: omp.terminator -!FIRDialect: } -!FIRDialect: omp.terminator -!FIRDialect: } -!FIRDialect: omp.sections { -!FIRDialect: omp.section { !FIRDialect: %[[PRIVATE_VAR:.*]] = fir.load %[[ARG]] : !fir.ref !FIRDialect: %[[CONSTANT:.*]] = arith.constant 5.000000e+00 : f32 !FIRDialect: %[[PRIVATE_VAR_2:.*]] = arith.mulf %[[PRIVATE_VAR]], %[[CONSTANT]] : f32 @@ -150,12 +132,6 @@ !FIRDialect: } !LLVMDialect: llvm.func @_QPfirstprivate(%[[ARG:.*]]: !llvm.ptr {fir.bindc_name = "alpha"}) { -!LLVMDialect: omp.sections { -!LLVMDialect: omp.section { -!LLVMDialect: omp.terminator -!LLVMDialect: } -!LLVMDialect: omp.terminator -!LLVMDialect: } !LLVMDialect: omp.sections { !LLVMDialect: omp.section { !LLVMDialect: {{.*}} = llvm.load %[[ARG]] : !llvm.ptr @@ -171,10 +147,37 @@ subroutine firstprivate(alpha) real :: alpha - !$omp sections firstprivate(alpha) - !$omp end sections !$omp sections alpha = alpha * 5 !$omp end sections end subroutine + +!FIRDialect-LABEL: func.func @_QPempty_sections +subroutine empty_sections() + !$omp sections + !$omp end sections + + !$omp sections + !$omp end sections nowait + + !$omp sections + !$omp section + !$omp end sections + + ! Make sure only the section with a statement gets emitted. + !FIRDialect-NEXT: omp.sections { + !FIRDialect-NEXT: omp.section { + !FIRDialect-NEXT: fir.call @_QPfoo() + !FIRDialect-NEXT: omp.terminator + !FIRDialect-NEXT: } + !FIRDialect-NEXT: omp.terminator + !FIRDialect-NEXT: } + !$omp sections + !$omp section + !$omp section + call foo() + !$omp end sections + + !FIRDialect-NEXT: return +end subroutine empty_sections