Skip to content
Open
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
19 changes: 19 additions & 0 deletions flang/lib/Lower/OpenMP/Atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "flang/Lower/SymbolMap.h"
#include "flang/Optimizer/Builder/FIRBuilder.h"
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Semantics/semantics.h"
#include "flang/Semantics/type.h"
Expand Down Expand Up @@ -459,6 +460,24 @@ void Fortran::lower::omp::lowerAtomic(
} else {
int action0 = analysis.op0.what & analysis.Action;
int action1 = analysis.op1.what & analysis.Action;

// Check for invalid atomic capture sequence.
// OpenMP allows: READ+UPDATE, UPDATE+READ, READ+WRITE, or UPDATE+WRITE
// in any order. Ensure we have one read-like and one write-like operation.
if (construct.IsCapture()) {
using Action = parser::OpenMPAtomicConstruct::Analysis;
const bool hasRead = (action0 == Action::Read || action1 == Action::Read);
const bool hasWriteOrUpdate =
(action0 == Action::Write || action0 == Action::Update ||
action1 == Action::Write || action1 == Action::Update);

if (!(hasRead && hasWriteOrUpdate)) {
mlir::emitError(loc, "OpenMP atomic capture requires one read and one "
"write/update operation");
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no test for this error.

Is this not already caught in Semantics? If not, why wasn't this code included in Semantics?

return;
}
}

mlir::Operation *captureOp = nullptr;
fir::FirOpBuilder::InsertPoint preAt = builder.saveInsertionPoint();
fir::FirOpBuilder::InsertPoint atomicAt, postAt;
Expand Down
27 changes: 27 additions & 0 deletions flang/test/Lower/OpenMP/atomic-capture-derived-array-diag.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
! Test that atomic capture with derived-type array elements fails appropriately
! RUN: not %flang_fc1 -emit-fir -fopenmp %s 2>&1 | FileCheck %s

! This test verifies that atomic capture with derived-type component array
! elements using different indices fails during MLIR verification. The issue
! is that different array elements represent different memory locations, which
! violates OpenMP's requirement that both statements in atomic capture must
! operate on the same variable. This will fail in the MLIR verifier until
! semantic analysis is fixed to detect this earlier.

program test_atomic_capture_invalid_sequence
type t1
integer :: i(1)
end type
type(t1) :: t2
integer :: j

t2%i = 0
j = 1

! CHECK: invalid sequence of operations in the capture region
!$omp atomic capture
t2%i(j*1) = t2%i(1) + 1
t2%i(1) = t2%i(j*1)
!$omp end atomic

end program test_atomic_capture_invalid_sequence
21 changes: 21 additions & 0 deletions flang/test/Lower/OpenMP/atomic-capture-same-element.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
! Test that atomic capture works correctly with same element (positive control)
! RUN: %flang_fc1 -emit-fir -fopenmp %s -o - | FileCheck %s

! This test verifies that atomic capture with the same array element in both
! statements works correctly and doesn't trigger the invalid sequence diagnostic.

! CHECK-LABEL: func @_QQmain
program test_atomic_capture_same_element
integer :: a(10)
integer :: v

a = 0

! This should work - same element a(1) in both statements
! CHECK: omp.atomic.capture
!$omp atomic capture
v = a(1)
a(1) = a(1) + 1
!$omp end atomic

end program test_atomic_capture_same_element