Skip to content

Commit 549e2e0

Browse files
Update based on review comments, consisting of:
* Accept only if statements, rather than an assignment statement. * Modify tests to match the if-statment required. * Add test to check for the TODO in lowering.
1 parent 6fa6600 commit 549e2e0

File tree

8 files changed

+79
-36
lines changed

8 files changed

+79
-36
lines changed

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ class ParseTreeDumper {
485485
NODE(OmpAtomicCapture, Stmt1)
486486
NODE(OmpAtomicCapture, Stmt2)
487487
NODE(parser, OmpAtomicCompare)
488+
NODE(parser, OmpAtomicCompareIfStmt)
488489
NODE(parser, OmpAtomicRead)
489490
NODE(parser, OmpAtomicUpdate)
490491
NODE(parser, OmpAtomicWrite)

flang/include/flang/Parser/parse-tree.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4126,12 +4126,17 @@ struct OmpAtomicCapture {
41264126
t;
41274127
};
41284128

4129-
// ATOMCI COMPARE (OpenMP 5.1, Spec: 15.8.4)
4129+
struct OmpAtomicCompareIfStmt {
4130+
UNION_CLASS_BOILERPLATE(OmpAtomicCompareIfStmt);
4131+
std::variant<common::Indirection<IfStmt>, common::Indirection<IfConstruct>> u;
4132+
};
4133+
4134+
// ATOMIC COMPARE (OpenMP 5.1, OPenMP 5.2 spec: 15.8.4)
41304135
struct OmpAtomicCompare {
41314136
TUPLE_CLASS_BOILERPLATE(OmpAtomicCompare);
41324137
CharBlock source;
41334138
std::tuple<OmpAtomicClauseList, Verbatim, OmpAtomicClauseList,
4134-
Statement<AssignmentStmt>, std::optional<OmpEndAtomic>>
4139+
OmpAtomicCompareIfStmt, std::optional<OmpEndAtomic>>
41354140
t;
41364141
};
41374142

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// See OpenMP-4.5-grammar.txt for documentation.
1111

1212
#include "basic-parsers.h"
13+
#include "debug-parser.h"
1314
#include "expr-parsers.h"
1415
#include "misc-parsers.h"
1516
#include "stmt-parser.h"
@@ -912,10 +913,15 @@ TYPE_PARSER("ATOMIC" >>
912913
Parser<OmpAtomicClauseList>{} / endOmpLine, statement(assignmentStmt),
913914
statement(assignmentStmt), Parser<OmpEndAtomic>{} / endOmpLine)))
914915

916+
TYPE_PARSER(construct<OmpAtomicCompareIfStmt>(indirect(Parser<IfStmt>{})) ||
917+
construct<OmpAtomicCompareIfStmt>(indirect(Parser<IfConstruct>{})))
918+
919+
// OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] COMPARE [MEMORY-ORDER-CLAUSE-LIST]
915920
TYPE_PARSER("ATOMIC" >>
916921
sourced(construct<OmpAtomicCompare>(
917922
Parser<OmpAtomicClauseList>{} / maybe(","_tok), verbatim("COMPARE"_tok),
918-
Parser<OmpAtomicClauseList>{} / endOmpLine, statement(assignmentStmt),
923+
Parser<OmpAtomicClauseList>{} / endOmpLine,
924+
Parser<OmpAtomicCompareIfStmt>{},
919925
maybe(Parser<OmpEndAtomic>{} / endOmpLine))))
920926

921927
// OMP ATOMIC [MEMORY-ORDER-CLAUSE-LIST] UPDATE [MEMORY-ORDER-CLAUSE-LIST]

flang/lib/Parser/unparse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2528,7 +2528,7 @@ class UnparseVisitor {
25282528
Walk(std::get<2>(x.t));
25292529
Put("\n");
25302530
EndOpenMP();
2531-
Walk(std::get<Statement<AssignmentStmt>>(x.t));
2531+
Walk(std::get<OmpAtomicCompareIfStmt>(x.t));
25322532
}
25332533
void Unparse(const OmpAtomicRead &x) {
25342534
BeginOpenMP();

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,9 +2411,8 @@ void OmpStructureChecker::CheckAtomicUpdateStmt(
24112411
void OmpStructureChecker::CheckAtomicCompareConstruct(
24122412
const parser::OmpAtomicCompare &atomicCompareConstruct) {
24132413

2414-
CheckAtomicWriteStmt(std::get<parser::Statement<parser::AssignmentStmt>>(
2415-
atomicCompareConstruct.t)
2416-
.statement);
2414+
// TODO: Check that the if-stmt is `if (var == expr) var = new`
2415+
// [with or without then/end-do]
24172416

24182417
unsigned version{context_.langOptions().OpenMPVersion};
24192418
if (version < 51) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -fopenmp-version=51 -o - %s 2>&1 | FileCheck %s
2+
3+
! CHECK: not yet implemented: OpenMP atomic compare
4+
program p
5+
integer :: x
6+
logical :: r
7+
!$omp atomic compare
8+
if (x .eq. 0) then
9+
x = 2
10+
end if
11+
end program p

flang/test/Parser/OpenMP/atomic-unparse.f90

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
program main
44
implicit none
55
integer :: i, j = 10
6-
logical :: k
6+
integer :: k
77
!READ
88
!$omp atomic read
99
i = j
@@ -124,27 +124,46 @@ program main
124124

125125
!COMPARE
126126
!$omp atomic compare
127-
r = i .eq. j
127+
if (k == i) k = j
128128
!$omp atomic seq_cst compare
129-
r = i .eq. j
129+
if (k == j) then
130+
k = i
131+
end if
130132
!$omp atomic compare seq_cst
131-
r = i .eq. j
133+
if (k .eq. j) then
134+
k = i
135+
end if
132136
!$omp atomic release compare
133-
r = i .eq. j
137+
if (i .eq. j) k = i
134138
!$omp atomic compare release
135-
r = i .eq. j
139+
if (i .eq. j) then
140+
i = k
141+
end if
136142
!$omp atomic acq_rel compare
137-
r = i .eq. j
143+
if (k .eq. j) then
144+
j = i
145+
end if
138146
!$omp atomic compare acq_rel
139-
r = i .eq. j
147+
if (i .eq. j) then
148+
i = k
149+
end if
140150
!$omp atomic acquire compare
141-
r = i .eq. j
151+
if (i .eq. j + 1) then
152+
i = j
153+
end if
154+
142155
!$omp atomic compare acquire
143-
r = i .eq. j
156+
if (i .eq. j) then
157+
i = k
158+
end if
144159
!$omp atomic relaxed compare
145-
r = i .eq. j
160+
if (i .eq. j) then
161+
i = k
162+
end if
146163
!$omp atomic compare relaxed
147-
r = i .eq. j
164+
if (i .eq. k) then
165+
i = j
166+
end if
148167

149168
!ATOMIC
150169
!$omp atomic

flang/test/Semantics/OpenMP/atomic-compare.f90

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,71 +6,73 @@
66
! higher openmp version than the others, so need a separate file.
77

88

9-
real a, b
10-
logical r
9+
real a, b, c
1110
a = 1.0
1211
b = 2.0
12+
c = 3.0
1313
!$omp parallel num_threads(4)
1414
! First a few things that should compile without error.
1515
!$omp atomic seq_cst, compare
16-
r = b .ne. a
16+
if (b .eq. a) then
17+
b = c
18+
end if
1719

1820
!$omp atomic seq_cst compare
19-
r = a .ge. b
21+
if (a .eq. b) a = c
2022
!$omp end atomic
2123

2224
!$omp atomic compare acquire hint(OMP_LOCK_HINT_CONTENDED)
23-
r = a .lt. b
25+
if (b .eq. a) b = c
2426

2527
!$omp atomic release hint(OMP_LOCK_HINT_UNCONTENDED) compare
26-
r = a .gt. b
28+
if (b .eq. a) b = c
2729

2830
!$omp atomic compare seq_cst
29-
r = b .ne. a
31+
if (b .eq. c) b = a
3032

3133
!$omp atomic hint(1) acq_rel compare
32-
r = b .eq. a
34+
if (b .eq. a) b = c
3335
!$omp end atomic
3436

3537
! Check for error conidtions:
3638
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
3739
!ERROR: At most one SEQ_CST clause can appear on the COMPARE directive
3840
!$omp atomic seq_cst seq_cst compare
39-
r = a .le. b
41+
if (b .eq. c) b = a
4042
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
4143
!ERROR: At most one SEQ_CST clause can appear on the COMPARE directive
4244
!$omp atomic compare seq_cst seq_cst
43-
r = b .gt. a
45+
if (b .eq. c) b = a
4446
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
4547
!ERROR: At most one SEQ_CST clause can appear on the COMPARE directive
4648
!$omp atomic seq_cst compare seq_cst
47-
r = b .ge. b
49+
if (b .eq. c) b = a
4850

4951
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
5052
!ERROR: At most one ACQUIRE clause can appear on the COMPARE directive
5153
!$omp atomic acquire acquire compare
52-
r = a .le. b
54+
if (b .eq. c) b = a
5355
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
5456
!ERROR: At most one ACQUIRE clause can appear on the COMPARE directive
5557
!$omp atomic compare acquire acquire
56-
r = b .gt. a
58+
if (b .eq. c) b = a
5759
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
5860
!ERROR: At most one ACQUIRE clause can appear on the COMPARE directive
5961
!$omp atomic acquire compare acquire
60-
r = b .ge. b
62+
if (b .eq. c) b = a
6163

6264
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
6365
!ERROR: At most one RELAXED clause can appear on the COMPARE directive
6466
!$omp atomic relaxed relaxed compare
65-
r = a .le. b
67+
if (b .eq. c) b = a
6668
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
6769
!ERROR: At most one RELAXED clause can appear on the COMPARE directive
6870
!$omp atomic compare relaxed relaxed
69-
r = b .gt. a
71+
if (b .eq. c) b = a
7072
!ERROR: More than one memory order clause not allowed on OpenMP Atomic construct
7173
!ERROR: At most one RELAXED clause can appear on the COMPARE directive
7274
!$omp atomic relaxed compare relaxed
73-
r = b .ge. b
75+
if (b .eq. c) b = a
7476

7577
!$omp end parallel
7678
end

0 commit comments

Comments
 (0)