Skip to content

Commit 31941d6

Browse files
authored
[flang][OpenMP] Avoid extra newline when unparsing OmpReductionCombiner (#160200)
When the combiner contains an AssignmentStmt, the unparser for that will emit a newline after the assignment. Don't let it do that, unparse the assignment ourselves.
1 parent 0d08ffd commit 31941d6

File tree

8 files changed

+29
-29
lines changed

8 files changed

+29
-29
lines changed

flang/lib/Parser/unparse.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,7 @@ class UnparseVisitor {
21062106
Walk(std::get<OmpReductionIdentifier>(x.t));
21072107
Put(":");
21082108
Walk(std::get<OmpTypeNameList>(x.t));
2109-
Walk(":", std::get<std::optional<OmpReductionCombiner>>(x.t));
2109+
Walk(": ", std::get<std::optional<OmpReductionCombiner>>(x.t));
21102110
}
21112111
void Unparse(const llvm::omp::Directive &x) {
21122112
unsigned ompVersion{langOpts_.OpenMPVersion};
@@ -2500,7 +2500,16 @@ class UnparseVisitor {
25002500
// Don't let the visitor go to the normal AssignmentStmt Unparse function,
25012501
// it adds an extra newline that we don't want.
25022502
if (const auto *assignment{std::get_if<AssignmentStmt>(&x.u)}) {
2503-
Walk(assignment->t, "=");
2503+
Walk(assignment->t, " = ");
2504+
} else {
2505+
Walk(x.u);
2506+
}
2507+
}
2508+
void Unparse(const OmpReductionCombiner &x) {
2509+
// Don't let the visitor go to the normal AssignmentStmt Unparse function,
2510+
// it adds an extra newline that we don't want.
2511+
if (const auto *assignment{std::get_if<AssignmentStmt>(&x.u)}) {
2512+
Walk(assignment->t, " = ");
25042513
} else {
25052514
Walk(x.u);
25062515
}

flang/test/Parser/OpenMP/declare-reduction-multi.f90

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ program omp_examples
2626
type(tt) :: values(n), sum, prod, big, small
2727

2828
!$omp declare reduction(+:tt:omp_out%r = omp_out%r + omp_in%r) initializer(omp_priv%r = 0)
29-
!CHECK: !$OMP DECLARE REDUCTION(+:tt: omp_out%r=omp_out%r+omp_in%r
30-
!CHECK-NEXT: ) INITIALIZER(omp_priv%r=0_4)
29+
!CHECK: !$OMP DECLARE REDUCTION(+:tt: omp_out%r = omp_out%r+omp_in%r) INITIALIZER(omp_priv%r = 0_4)
3130

3231
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification
3332
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare reduction
@@ -39,8 +38,7 @@ program omp_examples
3938
!PARSE-TREE: | OmpClauseList -> OmpClause -> Initializer -> OmpInitializerClause -> AssignmentStmt = 'omp_priv%r=0._4'
4039

4140
!$omp declare reduction(*:tt:omp_out%r = omp_out%r * omp_in%r) initializer(omp_priv%r = 1)
42-
!CHECK-NEXT: !$OMP DECLARE REDUCTION(*:tt: omp_out%r=omp_out%r*omp_in%r
43-
!CHECK-NEXT: ) INITIALIZER(omp_priv%r=1_4)
41+
!CHECK-NEXT: !$OMP DECLARE REDUCTION(*:tt: omp_out%r = omp_out%r*omp_in%r) INITIALIZER(omp_priv%r = 1_4)
4442

4543
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification
4644
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare reduction
@@ -52,8 +50,7 @@ program omp_examples
5250
!PARSE-TREE: | OmpClauseList -> OmpClause -> Initializer -> OmpInitializerClause -> AssignmentStmt = 'omp_priv%r=1._4'
5351

5452
!$omp declare reduction(max:tt:omp_out = mymax(omp_out, omp_in)) initializer(omp_priv%r = 0)
55-
!CHECK-NEXT: !$OMP DECLARE REDUCTION(max:tt: omp_out=mymax(omp_out,omp_in)
56-
!CHECK-NEXT: ) INITIALIZER(omp_priv%r=0_4)
53+
!CHECK-NEXT: !$OMP DECLARE REDUCTION(max:tt: omp_out = mymax(omp_out,omp_in)) INITIALIZER(omp_priv%r = 0_4)
5754

5855
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification
5956
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare reduction
@@ -65,8 +62,7 @@ program omp_examples
6562
!PARSE-TREE: | OmpClauseList -> OmpClause -> Initializer -> OmpInitializerClause -> AssignmentStmt = 'omp_priv%r=0._4'
6663

6764
!$omp declare reduction(min:tt:omp_out%r = min(omp_out%r, omp_in%r)) initializer(omp_priv%r = 1)
68-
!CHECK-NEXT: !$OMP DECLARE REDUCTION(min:tt: omp_out%r=min(omp_out%r,omp_in%r)
69-
!CHECK-NEXT: ) INITIALIZER(omp_priv%r=1_4)
65+
!CHECK-NEXT: !$OMP DECLARE REDUCTION(min:tt: omp_out%r = min(omp_out%r,omp_in%r)) INITIALIZER(omp_priv%r = 1_4)
7066

7167
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification
7268
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare reduction

flang/test/Parser/OpenMP/declare-reduction-operator.f90

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ subroutine reduce_1 ( n, tts )
1616
type(tt) :: tts(n)
1717
type(tt2) :: tts2(n)
1818

19-
!CHECK: !$OMP DECLARE REDUCTION(+:tt: omp_out=tt(x=omp_out%x-omp_in%x,y=omp_out%y-omp_in%y)
20-
!CHECK: ) INITIALIZER(omp_priv=tt(x=0_4,y=0_4))
19+
!CHECK: !$OMP DECLARE REDUCTION(+:tt: omp_out = tt(x=omp_out%x-omp_in%x,y=omp_out%y-omp_in%y)) INITIALIZER(omp_priv = tt(x=0_4,y=0_4))
2120

2221
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification
2322
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare reduction
@@ -31,8 +30,7 @@ subroutine reduce_1 ( n, tts )
3130
!$omp declare reduction(+ : tt : omp_out = tt(omp_out%x - omp_in%x , omp_out%y - omp_in%y)) initializer(omp_priv = tt(0,0))
3231

3332

34-
!CHECK: !$OMP DECLARE REDUCTION(+:tt2: omp_out=tt2(x=omp_out%x-omp_in%x,y=omp_out%y-omp_in%y)
35-
!CHECK: ) INITIALIZER(omp_priv=tt2(x=0._8,y=0._8)
33+
!CHECK: !$OMP DECLARE REDUCTION(+:tt2: omp_out = tt2(x=omp_out%x-omp_in%x,y=omp_out%y-omp_in%y)) INITIALIZER(omp_priv = tt2(x=0._8,y=0._8)
3634

3735
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification
3836
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare reduction

flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ subroutine f00
88

99
!CHECK: !DEF: /f00 (Subroutine) Subprogram
1010
!CHECK: subroutine f00
11-
!CHECK: !$omp declare reduction(fred:integer,real:omp_out = omp_in+omp_out)
11+
!CHECK: !$omp declare reduction(fred:integer,real: omp_out = omp_in+omp_out)
1212
!CHECK: end subroutine
1313

flang/test/Parser/OpenMP/declare-reduction-unparse.f90

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ subroutine initme(x,n)
1919
end subroutine initme
2020
end interface
2121
!$omp declare reduction(red_add:integer(4):omp_out=omp_out+omp_in) initializer(initme(omp_priv,0))
22-
!CHECK: !$OMP DECLARE REDUCTION(red_add:INTEGER(KIND=4_4): omp_out=omp_out+omp_in
23-
!CHECK: ) INITIALIZER(initme(omp_priv, 0_4))
22+
!CHECK: !$OMP DECLARE REDUCTION(red_add:INTEGER(KIND=4_4): omp_out = omp_out+omp_in) INITIALIZER(initme(omp_priv, 0_4))
2423

2524
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification
2625
!PARSE-TREE: | OmpDirectiveName -> llvm::omp::Directive = declare reduction
@@ -60,8 +59,7 @@ end function func
6059
!CHECK-LABEL: program main
6160
program main
6261
integer :: my_var
63-
!CHECK: !$OMP DECLARE REDUCTION(my_add_red:INTEGER: omp_out=omp_out+omp_in
64-
!CHECK-NEXT: ) INITIALIZER(omp_priv=0_4)
62+
!CHECK: !$OMP DECLARE REDUCTION(my_add_red:INTEGER: omp_out = omp_out+omp_in) INITIALIZER(omp_priv = 0_4)
6563

6664
!$omp declare reduction (my_add_red : integer : omp_out = omp_out + omp_in) initializer (omp_priv=0)
6765
my_var = 0

flang/test/Parser/OpenMP/metadirective-dirspec.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ subroutine f03
105105
!UNPARSE: TYPE :: tt2
106106
!UNPARSE: REAL :: x
107107
!UNPARSE: END TYPE
108-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE REDUCTION(+:tt1,tt2: omp_out%x=omp_in%x+omp_out%x
109-
!UNPARSE: ))
108+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE REDUCTION(+:tt1,tt2: omp_out%x = omp_in%x+omp_out%x)&
109+
!UNPARSE: !$OMP&)
110110
!UNPARSE: END SUBROUTINE
111111

112112
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective

flang/test/Parser/OpenMP/openmp6-directive-spellings.f90

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ subroutine f02
7979
!UNPARSE: TYPE :: t
8080
!UNPARSE: INTEGER :: x
8181
!UNPARSE: END TYPE
82-
!UNPARSE: !$OMP DECLARE_REDUCTION(+:t: omp_out%x=omp_out%x+omp_in%x
83-
!UNPARSE: )
82+
!UNPARSE: !$OMP DECLARE_REDUCTION(+:t: omp_out%x = omp_out%x+omp_in%x)
8483
!UNPARSE: END SUBROUTINE
8584

8685
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OpenMPDeclareReductionConstruct -> OmpDirectiveSpecification

flang/test/Semantics/OpenMP/declare-reduction-modfile.f90

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
!type::t1
77
!integer(4)::val
88
!endtype
9-
!!$OMP DECLARE REDUCTION(*:t1:omp_out = omp_out*omp_in) INITIALIZER(omp_priv=t1&
10-
!!$OMP&(1))
9+
!!$OMP DECLARE REDUCTION(*:t1:omp_out=omp_out*omp_in)INITIALIZER(omp_priv=&
10+
!!$OMP&t1(1))
1111
!!$OMP METADIRECTIVE OTHERWISE(DECLARE REDUCTION(+:INTEGER))
12-
!!$OMP DECLARE REDUCTION(.fluffy.:t1:omp_out = omp_out.fluffy.omp_in) INITIALIZ&
13-
!!$OMP&ER(omp_priv=t1(0))
14-
!!$OMP DECLARE REDUCTION(.mul.:t1:omp_out = omp_out.mul.omp_in) INITIALIZER(omp&
15-
!!$OMP&_priv=t1(1))
12+
!!$OMP DECLARE REDUCTION(.fluffy.:t1:omp_out=omp_out.fluffy.omp_in)INITIALI&
13+
!!$OMP&ZER(omp_priv=t1(0))
14+
!!$OMP DECLARE REDUCTION(.mul.:t1:omp_out=omp_out.mul.omp_in)INITIALIZER(om&
15+
!!$OMP&p_priv=t1(1))
1616
!interface operator(.mul.)
1717
!procedure::mul
1818
!end interface

0 commit comments

Comments
 (0)