Skip to content

[flang][OpenMP] Improve handling of REQUIRES ATOMIC_DEFAULT_MEM_ORDER #143917

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 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion flang/lib/Semantics/rewrite-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,22 @@ bool OmpRewriteMutator::Pre(parser::OpenMPAtomicConstruct &x) {

// Add a memory order clause to the atomic directive.
atomicDirectiveDefaultOrderFound_ = true;
llvm::omp::Clause kind{x.GetKind()};
switch (*defaultMemOrder) {
case common::OmpMemoryOrderType::Acq_Rel:
clauseList->v.emplace_back(parser::OmpClause{parser::OmpClause::AcqRel{}});
// FIXME: Implement 5.0 rules, pending clarification on later spec
// versions.
// [5.0:62:22-26]
if (kind == llvm::omp::Clause::OMPC_read) {
clauseList->v.emplace_back(
parser::OmpClause{parser::OmpClause::Acquire{}});
} else if (kind == llvm::omp::Clause::OMPC_update && x.IsCapture()) {
clauseList->v.emplace_back(
parser::OmpClause{parser::OmpClause::AcqRel{}});
} else {
clauseList->v.emplace_back(
parser::OmpClause{parser::OmpClause::Release{}});
}
break;
case common::OmpMemoryOrderType::Relaxed:
clauseList->v.emplace_back(parser::OmpClause{parser::OmpClause::Relaxed{}});
Expand Down
22 changes: 22 additions & 0 deletions flang/test/Lower/OpenMP/requires-atomic-default-mem-order.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
!RUN: %flang_fc1 -emit-hlfir %openmp_flags -fopenmp-version=50 %s -o - | FileCheck %s

module m
!$omp requires atomic_default_mem_order(acq_rel)

contains

!CHECK: %[[V:[0-9]+]]:2 = hlfir.declare {{.*}} {uniq_name = "_QMmFf00Ev"}
!CHECK: %[[X:[0-9]+]]:2 = hlfir.declare {{.*}} {uniq_name = "_QMmFf00Ex"}
!CHECK: omp.atomic.read %[[V]]#0 = %[[X]]#0 memory_order(acquire)
!CHECK: omp.atomic.write %[[X]]#0 = %{{[0-9]+}} memory_order(release)

subroutine f00(x, v)
integer :: x, v
!$omp atomic read
v = x

!$omp atomic write
x = v
end

end module
8 changes: 4 additions & 4 deletions flang/test/Semantics/OpenMP/requires-atomic02.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ program requires

! CHECK-LABEL: OpenMPAtomicConstruct
! CHECK: OmpClause -> Read
! CHECK: OmpClause -> AcqRel
! CHECK: OmpClause -> Acquire
!$omp atomic read
i = j

Expand All @@ -36,7 +36,7 @@ program requires

! CHECK-LABEL: OpenMPAtomicConstruct
! CHECK: OmpClause -> Write
! CHECK: OmpClause -> AcqRel
! CHECK: OmpClause -> Release
!$omp atomic write
i = j

Expand All @@ -60,7 +60,7 @@ program requires

! CHECK-LABEL: OpenMPAtomicConstruct
! CHECK: OmpClause -> Update
! CHECK: OmpClause -> AcqRel
! CHECK: OmpClause -> Release
!$omp atomic update
i = i + j

Expand All @@ -79,7 +79,7 @@ program requires
i = i + j

! CHECK-LABEL: OpenMPAtomicConstruct
! CHECK: OmpClause -> AcqRel
! CHECK: OmpClause -> Release
!$omp atomic
i = i + j

Expand Down
Loading