Skip to content

[flang][OpenMP] fix predetermined privatization inside section #138159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,11 @@ struct OpWithBodyGenInfo {
return *this;
}

OpWithBodyGenInfo &setSkipDspStep2(bool value) {
skipDspStep2 = value;
return *this;
}

OpWithBodyGenInfo &setEntryBlockArgs(const EntryBlockArgs *value) {
blockArgs = value;
return *this;
Expand Down Expand Up @@ -1088,6 +1093,8 @@ struct OpWithBodyGenInfo {
const List<Clause> *clauses = nullptr;
/// [in] if provided, processes the construct's data-sharing attributes.
DataSharingProcessor *dsp = nullptr;
/// [in] if true, skip DataSharingProcessor::processStep2
bool skipDspStep2 = false;
/// [in] if provided, it is used to create the op's region entry block. It is
/// overriden when a \see genRegionEntryCB is provided. This is only valid for
/// operations implementing the \see mlir::omp::BlockArgOpenMPOpInterface.
Expand Down Expand Up @@ -1240,7 +1247,7 @@ static void createBodyOfOp(mlir::Operation &op, const OpWithBodyGenInfo &info,
// loop (this may not make sense in production code, but a user could
// write that and we should handle it).
firOpBuilder.setInsertionPoint(term);
if (privatize) {
if (privatize && !info.skipDspStep2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a special case in DataSharingProcessor::processStep2() to skip last-privatization when called for an omp.sections op. Wouldn't it make sense to return early for omp.section there, so the logic for handling the section(s) special case is spread across just two functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks that was a good idea.

// DataSharingProcessor::processStep2() may create operations before/after
// the one passed as argument. We need to treat loop wrappers and their
// nested loop as a unit, so we need to pass the bottom level wrapper (if
Expand Down Expand Up @@ -2162,7 +2169,10 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
OpWithBodyGenInfo(converter, symTable, semaCtx, loc, nestedEval,
llvm::omp::Directive::OMPD_section)
.setClauses(&sectionQueue.begin()->clauses)
.setEntryBlockArgs(&args),
.setEntryBlockArgs(&args)
.setDataSharingProcessor(&dsp)
// lastprivate is handled differently for SECTIONS, see below
.setSkipDspStep2(true),
sectionQueue, sectionQueue.begin());
}

Expand Down
34 changes: 34 additions & 0 deletions flang/test/Lower/OpenMP/sections-predetermined-private.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
! RUN: %flang_fc1 -fopenmp -emit-hlfir -o - %s | FileCheck %s

!$omp parallel sections
!$omp section
do i = 1, 2
end do
!$omp section
do i = 1, 2
end do
!$omp end parallel sections
end
! CHECK-LABEL: func.func @_QQmain() {
! CHECK: omp.parallel {
! CHECK: %[[VAL_3:.*]] = fir.alloca i32 {bindc_name = "i", pinned}
! CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_3]] {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
! CHECK: omp.sections {
! CHECK: omp.section {
! CHECK: %[[VAL_11:.*]]:2 = fir.do_loop %[[VAL_12:.*]] = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%{{.*}} = %{{.*}} -> (index, i32) {
! CHECK: }
! CHECK: fir.store %[[VAL_11]]#1 to %[[VAL_4]]#0 : !fir.ref<i32>
! CHECK: omp.terminator
! CHECK: }
! CHECK: omp.section {
! CHECK: %[[VAL_25:.*]]:2 = fir.do_loop %[[VAL_26:.*]] = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%{{.*}} = %{{.*}}) -> (index, i32) {
! CHECK: }
! CHECK: fir.store %[[VAL_25]]#1 to %[[VAL_4]]#0 : !fir.ref<i32>
! CHECK: omp.terminator
! CHECK: }
! CHECK: omp.terminator
! CHECK: }
! CHECK: omp.terminator
! CHECK: }
! CHECK: return
! CHECK: }
Loading