Skip to content

Commit dfad983

Browse files
authored
[flang][OpenMP] Use OmpDirectiveSpecification in METADIRECTIVE (#159577)
1 parent 6264041 commit dfad983

File tree

11 files changed

+50
-48
lines changed

11 files changed

+50
-48
lines changed

flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "FlangOmpReportVisitor.h"
10+
#include "flang/Common/idioms.h"
1011
#include "flang/Parser/openmp-utils.h"
1112
#include "llvm/ADT/StringExtras.h"
1213
#include "llvm/Frontend/OpenMP/OMP.h"
@@ -58,9 +59,15 @@ SourcePosition OpenMPCounterVisitor::getLocation(const OmpWrapperType &w) {
5859
}
5960
SourcePosition OpenMPCounterVisitor::getLocation(
6061
const OpenMPDeclarativeConstruct &c) {
62+
const parser::AllCookedSources &allCooked{parsing->allCooked()};
6163
return std::visit(
62-
[&](const auto &o) -> SourcePosition {
63-
return parsing->allCooked().GetSourcePositionRange(o.source)->first;
64+
common::visitors{
65+
[&](const parser::OmpMetadirectiveDirective &o) -> SourcePosition {
66+
return allCooked.GetSourcePositionRange(o.v.source)->first;
67+
},
68+
[&](const auto &o) -> SourcePosition {
69+
return allCooked.GetSourcePositionRange(o.source)->first;
70+
},
6471
},
6572
c.u);
6673
}

flang/include/flang/Parser/openmp-utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ struct ConstructId {
4040

4141
MAKE_CONSTR_ID(OmpDeclareVariantDirective, D::OMPD_declare_variant);
4242
MAKE_CONSTR_ID(OmpErrorDirective, D::OMPD_error);
43-
MAKE_CONSTR_ID(OmpMetadirectiveDirective, D::OMPD_metadirective);
4443
MAKE_CONSTR_ID(OpenMPDeclarativeAllocate, D::OMPD_allocate);
4544
MAKE_CONSTR_ID(OpenMPDeclarativeAssumes, D::OMPD_assumes);
4645
MAKE_CONSTR_ID(OpenMPDeclareMapperConstruct, D::OMPD_declare_mapper);
@@ -101,7 +100,6 @@ struct DirectiveNameScope {
101100
return std::get<OmpBeginDirective>(x.t).DirName();
102101
} else if constexpr (std::is_same_v<T, OmpDeclareVariantDirective> ||
103102
std::is_same_v<T, OmpErrorDirective> ||
104-
std::is_same_v<T, OmpMetadirectiveDirective> ||
105103
std::is_same_v<T, OpenMPDeclarativeAllocate> ||
106104
std::is_same_v<T, OpenMPDeclarativeAssumes> ||
107105
std::is_same_v<T, OpenMPDeclareMapperConstruct> ||

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4834,9 +4834,8 @@ struct OmpBlockConstruct {
48344834
};
48354835

48364836
struct OmpMetadirectiveDirective {
4837-
TUPLE_CLASS_BOILERPLATE(OmpMetadirectiveDirective);
4838-
std::tuple<Verbatim, OmpClauseList> t;
4839-
CharBlock source;
4837+
WRAPPER_CLASS_BOILERPLATE(
4838+
OmpMetadirectiveDirective, OmpDirectiveSpecification);
48404839
};
48414840

48424841
// Ref: [5.1:89-90], [5.2:216]

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,11 +1008,11 @@ TYPE_PARSER(construct<OmpMatchClause>(
10081008
Parser<traits::OmpContextSelectorSpecification>{}))
10091009

10101010
TYPE_PARSER(construct<OmpOtherwiseClause>(
1011-
maybe(indirect(sourced(Parser<OmpDirectiveSpecification>{})))))
1011+
maybe(indirect(Parser<OmpDirectiveSpecification>{}))))
10121012

10131013
TYPE_PARSER(construct<OmpWhenClause>(
10141014
maybe(nonemptyList(Parser<OmpWhenClause::Modifier>{}) / ":"),
1015-
maybe(indirect(sourced(Parser<OmpDirectiveSpecification>{})))))
1015+
maybe(indirect(Parser<OmpDirectiveSpecification>{}))))
10161016

10171017
// OMP 5.2 12.6.1 grainsize([ prescriptiveness :] scalar-integer-expression)
10181018
TYPE_PARSER(construct<OmpGrainsizeClause>(
@@ -1304,9 +1304,9 @@ OmpDirectiveSpecification static makeFlushFromOldSyntax(Verbatim &&text,
13041304
std::move(clauses), std::move(flags)};
13051305
}
13061306

1307-
TYPE_PARSER(sourced(
1307+
TYPE_PARSER(
13081308
// Parse the old syntax: FLUSH [clauses] [(objects)]
1309-
construct<OmpDirectiveSpecification>(
1309+
sourced(construct<OmpDirectiveSpecification>(
13101310
// Force this old-syntax parser to fail for FLUSH followed by '('.
13111311
// Otherwise it could succeed on the new syntax but have one of
13121312
// lists absent in the parsed result.
@@ -1316,9 +1316,9 @@ TYPE_PARSER(sourced(
13161316
verbatim("FLUSH"_tok) / !lookAhead("("_tok),
13171317
maybe(Parser<OmpClauseList>{}),
13181318
maybe(parenthesized(Parser<OmpArgumentList>{})),
1319-
pure(OmpDirectiveSpecification::Flags::DeprecatedSyntax))) ||
1319+
pure(OmpDirectiveSpecification::Flags::DeprecatedSyntax)))) ||
13201320
// Parse the standard syntax: directive [(arguments)] [clauses]
1321-
construct<OmpDirectiveSpecification>( //
1321+
sourced(construct<OmpDirectiveSpecification>( //
13221322
sourced(OmpDirectiveNameParser{}),
13231323
maybe(parenthesized(Parser<OmpArgumentList>{})),
13241324
maybe(Parser<OmpClauseList>{}),
@@ -1376,8 +1376,10 @@ TYPE_PARSER(sourced(construct<OpenMPUtilityConstruct>(
13761376
sourced(construct<OpenMPUtilityConstruct>(
13771377
sourced(Parser<OmpNothingDirective>{}))))))
13781378

1379-
TYPE_PARSER(sourced(construct<OmpMetadirectiveDirective>(
1380-
verbatim("METADIRECTIVE"_tok), Parser<OmpClauseList>{})))
1379+
TYPE_PARSER(construct<OmpMetadirectiveDirective>(
1380+
predicated(Parser<OmpDirectiveName>{},
1381+
IsDirective(llvm::omp::Directive::OMPD_metadirective)) >=
1382+
Parser<OmpDirectiveSpecification>{}))
13811383

13821384
struct OmpBeginDirectiveParser {
13831385
using resultType = OmpDirectiveSpecification;

flang/lib/Parser/unparse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,8 +2672,8 @@ class UnparseVisitor {
26722672
void Unparse(const OmpFailClause &x) { Walk(x.v); }
26732673
void Unparse(const OmpMetadirectiveDirective &x) {
26742674
BeginOpenMP();
2675-
Word("!$OMP METADIRECTIVE ");
2676-
Walk(std::get<OmpClauseList>(x.t));
2675+
Word("!$OMP ");
2676+
Walk(x.v);
26772677
Put("\n");
26782678
EndOpenMP();
26792679
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ void OmpStructureChecker::CheckTraitSimd(
536536

537537
void OmpStructureChecker::Enter(const parser::OmpMetadirectiveDirective &x) {
538538
EnterDirectiveNest(MetadirectiveNest);
539-
PushContextAndClauseSets(x.source, llvm::omp::Directive::OMPD_metadirective);
539+
PushContextAndClauseSets(
540+
x.v.source, llvm::omp::Directive::OMPD_metadirective);
540541
}
541542

542543
void OmpStructureChecker::Leave(const parser::OmpMetadirectiveDirective &) {

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,6 @@ template <typename Checker> struct DirectiveSpellingVisitor {
627627
checker_(GetDirName(x.t).source, Directive::OMPD_allocators);
628628
return false;
629629
}
630-
bool Pre(const parser::OmpMetadirectiveDirective &x) {
631-
checker_(
632-
std::get<parser::Verbatim>(x.t).source, Directive::OMPD_metadirective);
633-
return false;
634-
}
635630
bool Pre(const parser::OpenMPDeclarativeAssumes &x) {
636631
checker_(std::get<parser::Verbatim>(x.t).source, Directive::OMPD_assumes);
637632
return false;

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
413413
}
414414

415415
bool Pre(const parser::OmpMetadirectiveDirective &x) {
416-
PushContext(x.source, llvm::omp::Directive::OMPD_metadirective);
416+
PushContext(x.v.source, llvm::omp::Directive::OMPD_metadirective);
417417
return true;
418418
}
419419
void Post(const parser::OmpMetadirectiveDirective &) { PopContext(); }

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ subroutine f00(x)
1111

1212
!UNPARSE: SUBROUTINE f00 (x)
1313
!UNPARSE: INTEGER x(10_4)
14-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: ALLOCATE(x))
14+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: ALLOCATE(x))
1515
!UNPARSE: END SUBROUTINE
1616

1717
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective
@@ -37,7 +37,7 @@ subroutine f01(x)
3737

3838
!UNPARSE: SUBROUTINE f01 (x)
3939
!UNPARSE: INTEGER x
40-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: CRITICAL(x))
40+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: CRITICAL(x))
4141
!UNPARSE: END SUBROUTINE
4242

4343
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective
@@ -61,8 +61,8 @@ subroutine f02
6161
end
6262

6363
!UNPARSE: SUBROUTINE f02
64-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE MAPPER(mymapper:INTEGER:&
65-
!UNPARSE: !$OMP&:v) MAP(TOFROM: v))
64+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE MAPPER(mymapper:INTEGER::&
65+
!UNPARSE: !$OMP&v) MAP(TOFROM: v))
6666
!UNPARSE: END SUBROUTINE
6767

6868
!PARSE-TREE: OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective
@@ -105,7 +105,7 @@ 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
108+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE REDUCTION(+:tt1,tt2: omp_out%x=omp_in%x+omp_out%x
109109
!UNPARSE: ))
110110
!UNPARSE: END SUBROUTINE
111111

@@ -149,7 +149,7 @@ subroutine f04
149149
end
150150

151151
!UNPARSE: SUBROUTINE f04
152-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE SIMD(f04))
152+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE SIMD(f04))
153153
!UNPARSE: END SUBROUTINE
154154

155155
!PARSE-TREE: OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective
@@ -174,7 +174,7 @@ subroutine f05
174174
end
175175

176176
!UNPARSE: SUBROUTINE f05
177-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE TARGET(f05))
177+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: DECLARE TARGET(f05))
178178
!UNPARSE: END SUBROUTINE
179179

180180
!PARSE-TREE: OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective
@@ -201,7 +201,7 @@ subroutine f06(x, y)
201201

202202
!UNPARSE: SUBROUTINE f06 (x, y)
203203
!UNPARSE: INTEGER x, y
204-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: FLUSH(x, y))
204+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: FLUSH(x, y))
205205
!UNPARSE: END SUBROUTINE
206206

207207
!PARSE-TREE: DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective
@@ -228,7 +228,7 @@ subroutine f07
228228

229229
!UNPARSE: SUBROUTINE f07
230230
!UNPARSE: INTEGER t
231-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: THREADPRIVATE(t))
231+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(.true._4)}: THREADPRIVATE(t))
232232
!UNPARSE: END SUBROUTINE
233233

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

flang/test/Parser/OpenMP/metadirective.f90

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ subroutine f00
88

99
!UNPARSE: SUBROUTINE f00
1010
!UNPARSE: CONTINUE
11-
!UNPARSE: !$OMP METADIRECTIVE WHEN(CONSTRUCT={TARGET, PARALLEL}: NOTHING)
11+
!UNPARSE: !$OMP METADIRECTIVE WHEN(CONSTRUCT={TARGET, PARALLEL}: NOTHING)
1212
!UNPARSE: END SUBROUTINE
1313

1414
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OmpMetadirectiveDirective
@@ -30,7 +30,7 @@ subroutine f01
3030

3131
!UNPARSE: SUBROUTINE f01
3232
!UNPARSE: CONTINUE
33-
!UNPARSE: !$OMP METADIRECTIVE WHEN(TARGET_DEVICE={KIND(host), DEVICE_NUM(1_4)}: NOTHING)
33+
!UNPARSE: !$OMP METADIRECTIVE WHEN(TARGET_DEVICE={KIND(host), DEVICE_NUM(1_4)}: NOTHING)
3434
!UNPARSE: END SUBROUTINE
3535

3636
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OmpMetadirectiveDirective
@@ -57,7 +57,7 @@ subroutine f02
5757

5858
!UNPARSE: SUBROUTINE f02
5959
!UNPARSE: CONTINUE
60-
!UNPARSE: !$OMP METADIRECTIVE WHEN(TARGET_DEVICE={KIND(any), DEVICE_NUM(7_4)}: NOTHING)
60+
!UNPARSE: !$OMP METADIRECTIVE WHEN(TARGET_DEVICE={KIND(any), DEVICE_NUM(7_4)}: NOTHING)
6161
!UNPARSE: END SUBROUTINE
6262

6363
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OmpMetadirectiveDirective
@@ -85,8 +85,8 @@ subroutine f03
8585

8686
!UNPARSE: SUBROUTINE f03
8787
!UNPARSE: CONTINUE
88-
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={ATOMIC_DEFAULT_MEM_ORDER(ACQ_REL)}: &
89-
!UNPARSE: !$OMP&NOTHING)
88+
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={ATOMIC_DEFAULT_MEM_ORDER(ACQ_REL)}: N&
89+
!UNPARSE: !$OMP&OTHING)
9090
!UNPARSE: END SUBROUTINE
9191

9292
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OmpMetadirectiveDirective
@@ -109,8 +109,8 @@ subroutine f04
109109

110110
!UNPARSE: SUBROUTINE f04
111111
!UNPARSE: CONTINUE
112-
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={extension_trait(haha(1_4), foo(baz,bar(1_4&
113-
!UNPARSE: !$OMP&)))}: NOTHING)
112+
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={extension_trait(haha(1_4), foo(baz,bar(1_4)&
113+
!UNPARSE: !$OMP&))}: NOTHING)
114114
!UNPARSE: END SUBROUTINE
115115

116116
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OmpMetadirectiveDirective
@@ -149,8 +149,8 @@ subroutine f05(x)
149149
!UNPARSE: SUBROUTINE f05 (x)
150150
!UNPARSE: INTEGER x
151151
!UNPARSE: CONTINUE
152-
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(SCORE(100_4): .true._4)}: PARALLEL DO REDUCTION(+&
153-
!UNPARSE: !$OMP&: x)) OTHERWISE(NOTHING)
152+
!UNPARSE: !$OMP METADIRECTIVE WHEN(USER={CONDITION(SCORE(100_4): .true._4)}: PARALLEL DO REDUCTION(+:&
153+
!UNPARSE: !$OMP& x)) OTHERWISE(NOTHING)
154154
!UNPARSE: DO i=1_4,10_4
155155
!UNPARSE: END DO
156156
!UNPARSE: END SUBROUTINE
@@ -186,8 +186,8 @@ subroutine f06
186186

187187
!UNPARSE: SUBROUTINE f06
188188
!UNPARSE: CONTINUE
189-
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={VENDOR(amd)}, USER={CONDITION(.true._4)}: NO&
190-
!UNPARSE: !$OMP&THING)
189+
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={VENDOR(amd)}, USER={CONDITION(.true._4)}: NOT&
190+
!UNPARSE: !$OMP&HING)
191191
!UNPARSE: END SUBROUTINE
192192

193193
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPStandaloneConstruct -> OmpMetadirectiveDirective
@@ -219,8 +219,8 @@ subroutine f07
219219
end
220220

221221
!UNPARSE: SUBROUTINE f07
222-
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={VENDOR(amd)}: DECLARE SIMD) WHEN(USE&
223-
!UNPARSE: !$OMP&R={CONDITION(.true._4)}: DECLARE TARGET) OTHERWISE(NOTHING)
222+
!UNPARSE: !$OMP METADIRECTIVE WHEN(IMPLEMENTATION={VENDOR(amd)}: DECLARE SIMD) WHEN(USER&
223+
!UNPARSE: !$OMP&={CONDITION(.true._4)}: DECLARE TARGET) OTHERWISE(NOTHING)
224224
!UNPARSE: END SUBROUTINE
225225

226226
!PARSE-TREE: OpenMPDeclarativeConstruct -> OmpMetadirectiveDirective

0 commit comments

Comments
 (0)