Skip to content

Commit 870866f

Browse files
authored
[flang][OpenMP] Fix parsing of ASSUME directive (#155257)
The ASSUME directive is block-associated and whether the end-directive is optional or not depends on the form of the block. This is all taken care of automatically since the AST node for ASSUME inherits from OmpBlockConstruct.
1 parent 18f5e42 commit 870866f

File tree

8 files changed

+149
-94
lines changed

8 files changed

+149
-94
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ class ParseTreeDumper {
517517
NODE(OmpAppendArgsClause, OmpAppendOp)
518518
NODE(parser, OmpArgument)
519519
NODE(parser, OmpArgumentList)
520-
NODE(parser, OmpAssumeDirective)
521520
NODE(parser, OmpAtClause)
522521
NODE_ENUM(OmpAtClause, ActionTime)
523522
NODE(parser, OmpAtomicDefaultMemOrderClause)
@@ -570,7 +569,6 @@ class ParseTreeDumper {
570569
NODE(parser, OmpDoacrossClause)
571570
NODE(parser, OmpDynGroupprivateClause)
572571
NODE(OmpDynGroupprivateClause, Modifier)
573-
NODE(parser, OmpEndAssumeDirective)
574572
NODE(parser, OmpEndDirective)
575573
NODE(parser, OmpEndLoopDirective)
576574
NODE(parser, OmpEndSectionsDirective)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ struct ConstructId {
3838
static constexpr llvm::omp::Directive id{Id}; \
3939
}
4040

41-
MAKE_CONSTR_ID(OmpAssumeDirective, D::OMPD_assume);
4241
MAKE_CONSTR_ID(OmpDeclareVariantDirective, D::OMPD_declare_variant);
4342
MAKE_CONSTR_ID(OmpErrorDirective, D::OMPD_error);
4443
MAKE_CONSTR_ID(OmpMetadirectiveDirective, D::OMPD_metadirective);
@@ -103,8 +102,7 @@ struct DirectiveNameScope {
103102
} else if constexpr (TupleTrait<T>) {
104103
if constexpr (std::is_base_of_v<OmpBlockConstruct, T>) {
105104
return std::get<OmpBeginDirective>(x.t).DirName();
106-
} else if constexpr (std::is_same_v<T, OmpAssumeDirective> ||
107-
std::is_same_v<T, OmpDeclareVariantDirective> ||
105+
} else if constexpr (std::is_same_v<T, OmpDeclareVariantDirective> ||
108106
std::is_same_v<T, OmpErrorDirective> ||
109107
std::is_same_v<T, OmpMetadirectiveDirective> ||
110108
std::is_same_v<T, OpenMPDeclarativeAllocate> ||

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4835,28 +4835,14 @@ struct OpenMPDeclarativeAssumes {
48354835
CharBlock source;
48364836
};
48374837

4838-
struct OmpAssumeDirective {
4839-
TUPLE_CLASS_BOILERPLATE(OmpAssumeDirective);
4840-
std::tuple<Verbatim, OmpClauseList> t;
4841-
CharBlock source;
4842-
};
4843-
4844-
struct OmpEndAssumeDirective {
4845-
WRAPPER_CLASS_BOILERPLATE(OmpEndAssumeDirective, Verbatim);
4846-
CharBlock source;
4847-
};
4848-
4849-
// Ref: [5.2: 213-216]
4838+
// Ref: [5.1:86-89], [5.2:215], [6.0:369]
48504839
//
4851-
// assume-construct ->
4852-
// ASSUME absent-clause | contains-clause | holds_clause | no-openmp-clause
4853-
// no-openmp-routines-clause | no-parallelism-clause
4854-
// block
4840+
// assume-directive -> // since 5.1
4841+
// ASSUME assumption-clause...
4842+
// block
48554843
// [END ASSUME]
4856-
struct OpenMPAssumeConstruct {
4857-
TUPLE_CLASS_BOILERPLATE(OpenMPAssumeConstruct);
4858-
std::tuple<OmpAssumeDirective, Block, std::optional<OmpEndAssumeDirective>> t;
4859-
CharBlock source;
4844+
struct OpenMPAssumeConstruct : public OmpBlockConstruct {
4845+
INHERITED_TUPLE_CLASS_BOILERPLATE(OpenMPAssumeConstruct, OmpBlockConstruct);
48604846
};
48614847

48624848
// 2.7.2 SECTIONS

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,16 +1857,8 @@ TYPE_PARSER(
18571857
Parser<OmpMetadirectiveDirective>{})) /
18581858
endOmpLine))
18591859

1860-
// Assume Construct
1861-
TYPE_PARSER(sourced(construct<OmpAssumeDirective>(
1862-
verbatim("ASSUME"_tok), Parser<OmpClauseList>{})))
1863-
1864-
TYPE_PARSER(sourced(construct<OmpEndAssumeDirective>(
1865-
startOmpLine >> verbatim("END ASSUME"_tok))))
1866-
1867-
TYPE_PARSER(sourced(
1868-
construct<OpenMPAssumeConstruct>(Parser<OmpAssumeDirective>{} / endOmpLine,
1869-
block, maybe(Parser<OmpEndAssumeDirective>{} / endOmpLine))))
1860+
TYPE_PARSER(construct<OpenMPAssumeConstruct>(
1861+
sourced(OmpBlockConstructParser{llvm::omp::Directive::OMPD_assume})))
18701862

18711863
// Block Construct
18721864
#define MakeBlockConstruct(dir) \

flang/lib/Parser/unparse.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,20 +2580,11 @@ class UnparseVisitor {
25802580
Put("\n");
25812581
EndOpenMP();
25822582
}
2583-
void Unparse(const OpenMPAllocatorsConstruct &x) { //
2583+
void Unparse(const OpenMPAllocatorsConstruct &x) {
25842584
Unparse(static_cast<const OmpBlockConstruct &>(x));
25852585
}
2586-
void Unparse(const OmpAssumeDirective &x) {
2587-
BeginOpenMP();
2588-
Word("!$OMP ASSUME");
2589-
Walk(" ", std::get<OmpClauseList>(x.t).v);
2590-
Put("\n");
2591-
EndOpenMP();
2592-
}
2593-
void Unparse(const OmpEndAssumeDirective &x) {
2594-
BeginOpenMP();
2595-
Word("!$OMP END ASSUME\n");
2596-
EndOpenMP();
2586+
void Unparse(const OpenMPAssumeConstruct &x) {
2587+
Unparse(static_cast<const OmpBlockConstruct &>(x));
25972588
}
25982589
void Unparse(const OpenMPCriticalConstruct &x) {
25992590
Unparse(static_cast<const OmpBlockConstruct &>(x));

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,6 @@ template <typename Checker> struct DirectiveSpellingVisitor {
589589
checker_(GetDirName(x.t).source, Directive::OMPD_allocators);
590590
return false;
591591
}
592-
bool Pre(const parser::OmpAssumeDirective &x) {
593-
checker_(std::get<parser::Verbatim>(x.t).source, Directive::OMPD_assume);
594-
return false;
595-
}
596-
bool Pre(const parser::OmpEndAssumeDirective &x) {
597-
checker_(x.v.source, Directive::OMPD_assume);
598-
return false;
599-
}
600592
bool Pre(const parser::OmpMetadirectiveDirective &x) {
601593
checker_(
602594
std::get<parser::Verbatim>(x.t).source, Directive::OMPD_metadirective);

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
531531
bool Pre(const parser::OpenMPDeclarativeAllocate &);
532532
void Post(const parser::OpenMPDeclarativeAllocate &) { PopContext(); }
533533

534+
bool Pre(const parser::OpenMPAssumeConstruct &);
535+
void Post(const parser::OpenMPAssumeConstruct &) { PopContext(); }
536+
534537
bool Pre(const parser::OpenMPAtomicConstruct &);
535538
void Post(const parser::OpenMPAtomicConstruct &) { PopContext(); }
536539

@@ -2220,6 +2223,11 @@ bool OmpAttributeVisitor::Pre(const parser::OpenMPDeclarativeAllocate &x) {
22202223
return false;
22212224
}
22222225

2226+
bool OmpAttributeVisitor::Pre(const parser::OpenMPAssumeConstruct &x) {
2227+
PushContext(x.source, llvm::omp::Directive::OMPD_assume);
2228+
return true;
2229+
}
2230+
22232231
bool OmpAttributeVisitor::Pre(const parser::OpenMPAtomicConstruct &x) {
22242232
PushContext(x.source, llvm::omp::Directive::OMPD_atomic);
22252233
return true;
Lines changed: 129 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,149 @@
1-
! RUN: %flang_fc1 -fopenmp-version=51 -fopenmp -fdebug-unparse-no-sema %s 2>&1 | FileCheck %s
2-
! RUN: %flang_fc1 -fopenmp-version=51 -fopenmp -fdebug-dump-parse-tree-no-sema %s 2>&1 | FileCheck %s --check-prefix="PARSE-TREE"
1+
!RUN: %flang_fc1 -fopenmp-version=51 -fopenmp -fdebug-unparse-no-sema %s | FileCheck --check-prefix="UNPARSE" %s
2+
!RUN: %flang_fc1 -fopenmp-version=51 -fopenmp -fdebug-dump-parse-tree-no-sema %s | FileCheck --check-prefix="PARSE-TREE" %s
3+
34
subroutine sub1
45
integer :: r
5-
!CHECK: !$OMP ASSUME NO_OPENMP
6-
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
7-
!PARSE-TREE: Verbatim
8-
!PARSE-TREE: OmpClauseList -> OmpClause -> NoOpenmp
96
!$omp assume no_openmp
10-
!CHECK: !$OMP ASSUME NO_PARALLELISM
11-
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
12-
!PARSE-TREE: Verbatim
13-
!PARSE-TREE: OmpClauseList -> OmpClause -> NoParallelism
7+
!$omp end assume
8+
149
!$omp assume no_parallelism
15-
!CHECK: !$OMP ASSUME NO_OPENMP_ROUTINES
16-
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
17-
!PARSE-TREE: Verbatim
18-
!PARSE-TREE: OmpClauseList -> OmpClause -> NoOpenmpRoutines
10+
!$omp end assume
11+
1912
!$omp assume no_openmp_routines
20-
!CHECK: !$OMP ASSUME ABSENT(ALLOCATE), CONTAINS(WORKSHARE,TASK)
21-
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
22-
!PARSE-TREE: Verbatim
23-
!PARSE-TREE: OmpClauseList -> OmpClause -> Absent -> OmpAbsentClause -> llvm::omp::Directive = allocate
24-
!PARSE-TREE: OmpClause -> Contains -> OmpContainsClause -> llvm::omp::Directive = workshare
25-
!PARSE-TREE: llvm::omp::Directive = task
26-
!$omp assume absent(allocate), contains(workshare, task)
27-
!CHECK: !$OMP ASSUME HOLDS(1==1)
13+
!$omp end assume
14+
15+
!$omp assume absent(allocate), contains(workshare, task)
16+
block ! strictly-structured-block
17+
end block
18+
2819
!$omp assume holds(1.eq.1)
20+
block
21+
end block
2922
print *, r
3023
end subroutine sub1
3124

25+
!UNPARSE: SUBROUTINE sub1
26+
!UNPARSE: INTEGER r
27+
!UNPARSE: !$OMP ASSUME NO_OPENMP
28+
!UNPARSE: !$OMP END ASSUME
29+
!UNPARSE: !$OMP ASSUME NO_PARALLELISM
30+
!UNPARSE: !$OMP END ASSUME
31+
!UNPARSE: !$OMP ASSUME NO_OPENMP_ROUTINES
32+
!UNPARSE: !$OMP END ASSUME
33+
!UNPARSE: !$OMP ASSUME ABSENT(ALLOCATE) CONTAINS(WORKSHARE,TASK)
34+
!UNPARSE: BLOCK
35+
!UNPARSE: END BLOCK
36+
!UNPARSE: !$OMP ASSUME HOLDS(1==1)
37+
!UNPARSE: BLOCK
38+
!UNPARSE: END BLOCK
39+
!UNPARSE: PRINT *, r
40+
!UNPARSE: END SUBROUTINE sub1
41+
42+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
43+
!PARSE-TREE: | OmpBeginDirective
44+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
45+
!PARSE-TREE: | | OmpClauseList -> OmpClause -> NoOpenmp
46+
!PARSE-TREE: | | Flags = None
47+
!PARSE-TREE: | Block
48+
!PARSE-TREE: | OmpEndDirective
49+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
50+
!PARSE-TREE: | | OmpClauseList ->
51+
!PARSE-TREE: | | Flags = None
52+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
53+
!PARSE-TREE: | OmpBeginDirective
54+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
55+
!PARSE-TREE: | | OmpClauseList -> OmpClause -> NoParallelism
56+
!PARSE-TREE: | | Flags = None
57+
!PARSE-TREE: | Block
58+
!PARSE-TREE: | OmpEndDirective
59+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
60+
!PARSE-TREE: | | OmpClauseList ->
61+
!PARSE-TREE: | | Flags = None
62+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
63+
!PARSE-TREE: | OmpBeginDirective
64+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
65+
!PARSE-TREE: | | OmpClauseList -> OmpClause -> NoOpenmpRoutines
66+
!PARSE-TREE: | | Flags = None
67+
!PARSE-TREE: | Block
68+
!PARSE-TREE: | OmpEndDirective
69+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
70+
!PARSE-TREE: | | OmpClauseList ->
71+
!PARSE-TREE: | | Flags = None
72+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
73+
!PARSE-TREE: | OmpBeginDirective
74+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
75+
!PARSE-TREE: | | OmpClauseList -> OmpClause -> Absent -> OmpAbsentClause -> llvm::omp::Directive = allocate
76+
!PARSE-TREE: | | OmpClause -> Contains -> OmpContainsClause -> llvm::omp::Directive = workshare
77+
!PARSE-TREE: | | llvm::omp::Directive = task
78+
!PARSE-TREE: | | Flags = None
79+
!PARSE-TREE: | Block
80+
!PARSE-TREE: | | ExecutionPartConstruct -> ExecutableConstruct -> BlockConstruct
81+
!PARSE-TREE: | | | BlockStmt ->
82+
!PARSE-TREE: | | | BlockSpecificationPart -> SpecificationPart
83+
!PARSE-TREE: | | | | ImplicitPart ->
84+
!PARSE-TREE: | | | Block
85+
!PARSE-TREE: | | | EndBlockStmt ->
86+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
87+
!PARSE-TREE: | OmpBeginDirective
88+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
89+
!PARSE-TREE: | | OmpClauseList -> OmpClause -> Holds -> OmpHoldsClause -> Expr -> EQ
90+
!PARSE-TREE: | | | Expr -> LiteralConstant -> IntLiteralConstant = '1'
91+
!PARSE-TREE: | | | Expr -> LiteralConstant -> IntLiteralConstant = '1'
92+
!PARSE-TREE: | | Flags = None
93+
!PARSE-TREE: | Block
94+
!PARSE-TREE: | | ExecutionPartConstruct -> ExecutableConstruct -> BlockConstruct
95+
!PARSE-TREE: | | | BlockStmt ->
96+
!PARSE-TREE: | | | BlockSpecificationPart -> SpecificationPart
97+
!PARSE-TREE: | | | | ImplicitPart ->
98+
!PARSE-TREE: | | | Block
99+
!PARSE-TREE: | | | EndBlockStmt ->
100+
101+
32102
subroutine sub2
33103
integer :: r
34104
integer :: v
35-
!CHECK !$OMP ASSUME NO_OPENMP
36-
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
37-
!PARSE-TREE: OmpAssumeDirective
38-
!PARSE-TREE: Verbatim
39-
!PARSE-TREE: OmpClauseList -> OmpClause -> NoOpenmp
40-
!PARSE-TREE: Block
41-
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> AssignmentStmt
42-
!PARSE-TREE: Expr -> Add
43-
!PARSE-TREE: OmpEndAssumeDirective
44105
v = 87
45106
!$omp assume no_openmp
46107
r = r + 1
47-
!CHECK !$OMP END ASSUME
48108
!$omp end assume
49109
end subroutine sub2
50-
110+
111+
!UNPARSE: SUBROUTINE sub2
112+
!UNPARSE: INTEGER r
113+
!UNPARSE: INTEGER v
114+
!UNPARSE: v = 87
115+
!UNPARSE: !$OMP ASSUME NO_OPENMP
116+
!UNPARSE: r = r+1
117+
!UNPARSE: !$OMP END ASSUME
118+
!UNPARSE: END SUBROUTINE sub2
119+
120+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> AssignmentStmt
121+
!PARSE-TREE: | Variable -> Designator -> DataRef -> Name = 'v'
122+
!PARSE-TREE: | Expr -> LiteralConstant -> IntLiteralConstant = '87'
123+
!PARSE-TREE: ExecutionPartConstruct -> ExecutableConstruct -> OpenMPConstruct -> OpenMPAssumeConstruct
124+
!PARSE-TREE: | OmpBeginDirective
125+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
126+
!PARSE-TREE: | | OmpClauseList -> OmpClause -> NoOpenmp
127+
!PARSE-TREE: | | Flags = None
128+
!PARSE-TREE: | Block
129+
!PARSE-TREE: | | ExecutionPartConstruct -> ExecutableConstruct -> ActionStmt -> AssignmentStmt
130+
!PARSE-TREE: | | | Variable -> Designator -> DataRef -> Name = 'r'
131+
!PARSE-TREE: | | | Expr -> Add
132+
!PARSE-TREE: | | | | Expr -> Designator -> DataRef -> Name = 'r'
133+
!PARSE-TREE: | | | | Expr -> LiteralConstant -> IntLiteralConstant = '1'
134+
!PARSE-TREE: | OmpEndDirective
135+
!PARSE-TREE: | | OmpDirectiveName -> llvm::omp::Directive = assume
136+
!PARSE-TREE: | | OmpClauseList ->
137+
!PARSE-TREE: | | Flags = None
138+
51139
program p
52-
!CHECK !$OMP ASSUMES NO_OPENMP
53-
!PARSE-TREE: SpecificationPart
54-
!PARSE-TREE: OpenMPDeclarativeConstruct -> OpenMPDeclarativeAssumes
55-
!PARSE-TREE: Verbatim
56-
!PARSE-TREE: OmpClauseList -> OmpClause -> NoOpenmp
57140
!$omp assumes no_openmp
58141
end program p
59-
142+
143+
!UNPARSE: PROGRAM p
144+
!UNPARSE: !$OMP ASSUMES NO_OPENMP
145+
!UNPARSE: END PROGRAM p
146+
147+
!PARSE-TREE: OpenMPDeclarativeConstruct -> OpenMPDeclarativeAssumes
148+
!PARSE-TREE: | Verbatim
149+
!PARSE-TREE: | OmpClauseList -> OmpClause -> NoOpenmp

0 commit comments

Comments
 (0)