Skip to content

Commit 0de2cca

Browse files
[flang][OpenMP]Improve support for DECLARE REDUCTION (llvm#127088)
Part of the DECLARE REDUCTION was already supported by the parser, but the semantics to add the reduction identifier wasn't implemented. The semantics would not accept the name given by the reduction, so a few lines added to support that. Some tests were in place but not quite working, so fixed those up too. Adding new tests for unparsing and parse-tree, as well as checking the symbolic name being generated. Lowering of DECLARE REDUCTION is not supported in this patch, and a test that it hits the relevant TODO is in this patch (most of this was already existing, but not actually testing the TODO message).
1 parent 3c938d0 commit 0de2cca

File tree

10 files changed

+81
-26
lines changed

10 files changed

+81
-26
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4553,8 +4553,8 @@ WRAPPER_CLASS(OmpReductionInitializerClause, Expr);
45534553
struct OpenMPDeclareReductionConstruct {
45544554
TUPLE_CLASS_BOILERPLATE(OpenMPDeclareReductionConstruct);
45554555
CharBlock source;
4556-
std::tuple<Verbatim, OmpReductionIdentifier, std::list<DeclarationTypeSpec>,
4557-
OmpReductionCombiner, std::optional<OmpReductionInitializerClause>>
4556+
std::tuple<Verbatim, common::Indirection<OmpReductionSpecifier>,
4557+
std::optional<OmpReductionInitializerClause>>
45584558
t;
45594559
};
45604560

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ TYPE_PARSER(sourced( //
170170
TYPE_PARSER(construct<OmpLocatorList>(nonemptyList(Parser<OmpLocator>{})))
171171

172172
TYPE_PARSER( //
173-
construct<OmpTypeSpecifier>(Parser<TypeSpec>{}) ||
174-
construct<OmpTypeSpecifier>(Parser<DeclarationTypeSpec>{}))
173+
construct<OmpTypeSpecifier>(Parser<DeclarationTypeSpec>{}) ||
174+
construct<OmpTypeSpecifier>(Parser<TypeSpec>{}))
175175

176176
TYPE_PARSER(construct<OmpReductionSpecifier>( //
177177
Parser<OmpReductionIdentifier>{},
@@ -1148,9 +1148,7 @@ TYPE_PARSER(construct<OmpReductionInitializerClause>(
11481148
// 2.16 Declare Reduction Construct
11491149
TYPE_PARSER(sourced(construct<OpenMPDeclareReductionConstruct>(
11501150
verbatim("DECLARE REDUCTION"_tok),
1151-
"(" >> Parser<OmpReductionIdentifier>{} / ":",
1152-
nonemptyList(Parser<DeclarationTypeSpec>{}) / ":",
1153-
Parser<OmpReductionCombiner>{} / ")",
1151+
"(" >> indirect(Parser<OmpReductionSpecifier>{}) / ")",
11541152
maybe(Parser<OmpReductionInitializerClause>{}))))
11551153

11561154
// declare-target with list

flang/lib/Parser/unparse.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,11 +2690,10 @@ class UnparseVisitor {
26902690
BeginOpenMP();
26912691
Word("!$OMP DECLARE REDUCTION ");
26922692
Put("(");
2693-
Walk(std::get<OmpReductionIdentifier>(x.t)), Put(" : ");
2694-
Walk(std::get<std::list<DeclarationTypeSpec>>(x.t), ","), Put(" : ");
2695-
Walk(std::get<OmpReductionCombiner>(x.t));
2693+
Walk(std::get<common::Indirection<OmpReductionSpecifier>>(x.t));
26962694
Put(")");
26972695
Walk(std::get<std::optional<OmpReductionInitializerClause>>(x.t));
2696+
Put("\n");
26982697
EndOpenMP();
26992698
}
27002699

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,6 +3178,10 @@ bool OmpStructureChecker::CheckReductionOperator(
31783178
const SourceName &realName{name->symbol->GetUltimate().name()};
31793179
valid =
31803180
llvm::is_contained({"max", "min", "iand", "ior", "ieor"}, realName);
3181+
if (!valid) {
3182+
auto *misc{name->symbol->detailsIf<MiscDetails>()};
3183+
valid = misc && misc->kind() == MiscDetails::Kind::ConstructName;
3184+
}
31813185
}
31823186
if (!valid) {
31833187
context_.Say(source,

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
446446
bool Pre(const parser::OpenMPDeclareMapperConstruct &);
447447
void Post(const parser::OpenMPDeclareMapperConstruct &) { PopContext(); }
448448

449+
bool Pre(const parser::OpenMPDeclareReductionConstruct &);
450+
void Post(const parser::OpenMPDeclareReductionConstruct &) { PopContext(); }
451+
449452
bool Pre(const parser::OpenMPThreadprivate &);
450453
void Post(const parser::OpenMPThreadprivate &) { PopContext(); }
451454

@@ -1976,6 +1979,12 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPDeclareMapperConstruct &x) {
19761979
return true;
19771980
}
19781981

1982+
bool OmpAttributeVisitor::Pre(
1983+
const parser::OpenMPDeclareReductionConstruct &x) {
1984+
PushContext(x.source, llvm::omp::Directive::OMPD_declare_reduction);
1985+
return true;
1986+
}
1987+
19791988
bool OmpAttributeVisitor::Pre(const parser::OpenMPThreadprivate &x) {
19801989
PushContext(x.source, llvm::omp::Directive::OMPD_threadprivate);
19811990
const auto &list{std::get<parser::OmpObjectList>(x.t)};

flang/lib/Semantics/resolve-names.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,15 @@ class OmpVisitor : public virtual DeclarationVisitor {
14821482
return false;
14831483
}
14841484

1485+
bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
1486+
AddOmpSourceRange(x.source);
1487+
parser::OmpClauseList emptyList{std::list<parser::OmpClause>{}};
1488+
ProcessReductionSpecifier(
1489+
std::get<Indirection<parser::OmpReductionSpecifier>>(x.t).value(),
1490+
emptyList);
1491+
Walk(std::get<std::optional<parser::OmpReductionInitializerClause>>(x.t));
1492+
return false;
1493+
}
14851494
bool Pre(const parser::OmpMapClause &);
14861495

14871496
void Post(const parser::OmpBeginLoopDirective &) {
@@ -1732,11 +1741,19 @@ void OmpVisitor::ProcessMapperSpecifier(const parser::OmpMapperSpecifier &spec,
17321741
void OmpVisitor::ProcessReductionSpecifier(
17331742
const parser::OmpReductionSpecifier &spec,
17341743
const parser::OmpClauseList &clauses) {
1744+
BeginDeclTypeSpec();
1745+
const auto &id{std::get<parser::OmpReductionIdentifier>(spec.t)};
1746+
if (auto procDes{std::get_if<parser::ProcedureDesignator>(&id.u)}) {
1747+
if (auto *name{std::get_if<parser::Name>(&procDes->u)}) {
1748+
name->symbol =
1749+
&MakeSymbol(*name, MiscDetails{MiscDetails::Kind::ConstructName});
1750+
}
1751+
}
1752+
EndDeclTypeSpec();
17351753
// Creating a new scope in case the combiner expression (or clauses) use
17361754
// reerved identifiers, like "omp_in". This is a temporary solution until
17371755
// we deal with these in a more thorough way.
17381756
PushScope(Scope::Kind::OtherConstruct, nullptr);
1739-
Walk(std::get<parser::OmpReductionIdentifier>(spec.t));
17401757
Walk(std::get<parser::OmpTypeNameList>(spec.t));
17411758
Walk(std::get<std::optional<parser::OmpReductionCombiner>>(spec.t));
17421759
Walk(clauses);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
! This test checks lowering of OpenMP declare reduction Directive.
22

3-
// RUN: not flang -fc1 -emit-fir -fopenmp %s 2>&1 | FileCheck %s
3+
! RUN: not flang -fc1 -emit-fir -fopenmp %s 2>&1 | FileCheck %s
44

55
subroutine declare_red()
66
integer :: my_var
7-
// CHECK: not yet implemented: OpenMPDeclareReductionConstruct
7+
!CHECK: not yet implemented: OpenMPDeclareReductionConstruct
88
!$omp declare reduction (my_red : integer : omp_out = omp_in) initializer (omp_priv = 0)
99
my_var = 0
1010
end subroutine declare_red
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
! RUN: %flang_fc1 -fdebug-unparse -fopenmp %s | FileCheck --ignore-case %s
2+
! RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
!CHECK-LABEL: program main
4+
program main
5+
integer :: my_var
6+
!CHECK: !$OMP DECLARE REDUCTION (my_add_red:INTEGER: omp_out=omp_out+omp_in
7+
!CHECK-NEXT: ) INITIALIZER(OMP_PRIV = 0_4)
8+
9+
!$omp declare reduction (my_add_red : integer : omp_out = omp_out + omp_in) initializer (omp_priv=0)
10+
my_var = 0
11+
!$omp parallel reduction (my_add_red : my_var) num_threads(4)
12+
my_var = omp_get_thread_num() + 1
13+
!$omp end parallel
14+
print *, "sum of thread numbers is ", my_var
15+
end program main
16+
17+
!PARSE-TREE: OpenMPDeclareReductionConstruct
18+
!PARSE-TREE: OmpReductionIdentifier -> ProcedureDesignator -> Name = 'my_add_red'
19+
!PARSE-TREE: DeclarationTypeSpec -> IntrinsicTypeSpec -> IntegerTypeSpec
20+
!PARSE-TREE: OmpReductionCombiner -> AssignmentStmt = 'omp_out=omp_out+omp_in'
21+
!PARSE-TREE: OmpReductionInitializerClause -> Expr = '0_4'

flang/test/Semantics/OpenMP/declarative-directive01.f90

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
! Check OpenMP declarative directives
44

5-
!TODO: all internal errors
6-
! enable declare-reduction example after name resolution
7-
85
! 2.4 requires
96

107
subroutine requires_1(a)
@@ -88,15 +85,14 @@ end module m2
8885

8986
! 2.16 declare-reduction
9087

91-
! subroutine declare_red_1()
92-
! use omp_lib
93-
! integer :: my_var
94-
! !$omp declare reduction (my_add_red : integer : omp_out = omp_out + omp_in) initializer (omp_priv=0)
95-
! my_var = 0
96-
! !$omp parallel reduction (my_add_red : my_var) num_threads(4)
97-
! my_var = omp_get_thread_num() + 1
98-
! !$omp end parallel
99-
! print *, "sum of thread numbers is ", my_var
100-
! end subroutine declare_red_1
88+
subroutine declare_red_1()
89+
integer :: my_var
90+
!$omp declare reduction (my_add_red : integer : omp_out = omp_out + omp_in) initializer (omp_priv=0)
91+
my_var = 0
92+
!$omp parallel reduction (my_add_red : my_var) num_threads(4)
93+
my_var = 1
94+
!$omp end parallel
95+
print *, "sum of thread numbers is ", my_var
96+
end subroutine declare_red_1
10197

10298
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
! RUN: %flang_fc1 -fdebug-dump-symbols -fopenmp -fopenmp-version=50 %s | FileCheck %s
2+
3+
program main
4+
!CHECK-LABEL: MainProgram scope: main
5+
6+
!$omp declare reduction (my_add_red : integer : omp_out = omp_out + omp_in) initializer (omp_priv=0)
7+
8+
!CHECK: my_add_red: Misc ConstructName
9+
10+
end program main
11+

0 commit comments

Comments
 (0)