Skip to content

Commit 8a221a5

Browse files
authored
[Flang][OpenMP] Push context when parsing DECLARE VARIANT (#147075)
Basic parsing and semantics support for Declare Variant was added in #130578. However, this did not include variant of `Pre` and `Post` within `OmpAttributeVisitor`. This meant that when a function in the class tried to get the context using `GetContext`, Flang would crash as the context was empty. To ensure this is possible, such as when resolving names as part of the `uniform` clause in the `simd` directive, the context is now pushed within `OmpAttributeVisitor` when parsing a `DECLARE VARIANT` directive. Fixes #145222
1 parent 5271f9f commit 8a221a5

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

flang/lib/Semantics/resolve-directives.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
495495
bool Pre(const parser::OpenMPAllocatorsConstruct &);
496496
void Post(const parser::OpenMPAllocatorsConstruct &);
497497

498+
bool Pre(const parser::OmpDeclareVariantDirective &x) {
499+
PushContext(x.source, llvm::omp::Directive::OMPD_declare_variant);
500+
return true;
501+
}
502+
void Post(const parser::OmpDeclareVariantDirective &) { PopContext(); };
503+
498504
void Post(const parser::OmpObjectList &x) {
499505
// The objects from OMP clauses should have already been resolved,
500506
// except common blocks (the ResolveNamesVisitor does not visit

flang/test/Parser/OpenMP/declare-variant.f90

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,30 @@ subroutine vsub(v1, v2)
102102
type(c_ptr), value :: v1, v2
103103
end
104104
end subroutine
105+
106+
subroutine f
107+
real :: x, y
108+
y = 2
109+
!omp simd
110+
call f2(x, y)
111+
!omp end simd
112+
contains
113+
subroutine f1 (x, y)
114+
real :: x, y
115+
end
116+
117+
subroutine f2 (x, y)
118+
real :: x, y
119+
!$omp declare variant (f1) match (construct={simd(uniform(y))})
120+
end
121+
end subroutine
122+
!CHECK: !$OMP DECLARE VARIANT (f1) MATCH(CONSTRUCT={SIMD(UNIFORM(y))})
123+
!PARSE-TREE: | | | | DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OmpDeclareVariantDirective
124+
!PARSE-TREE-NEXT: | | | | | Verbatim
125+
!PARSE-TREE-NEXT: | | | | | Name = 'f1'
126+
!PARSE-TREE-NEXT: | | | | | OmpClauseList -> OmpClause -> Match -> OmpMatchClause -> OmpContextSelectorSpecification -> OmpTraitSetSelector
127+
!PARSE-TREE-NEXT: | | | | | | OmpTraitSetSelectorName -> Value = Construct
128+
!PARSE-TREE-NEXT: | | | | | | OmpTraitSelector
129+
!PARSE-TREE-NEXT: | | | | | | | OmpTraitSelectorName -> Value = Simd
130+
!PARSE-TREE-NEXT: | | | | | | | Properties
131+
!PARSE-TREE-NEXT: | | | | | | | | OmpTraitProperty -> OmpClause -> Uniform -> Name = 'y'

0 commit comments

Comments
 (0)