Skip to content

[Flang][OpenMP] Push context when parsing DECLARE VARIANT #147075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2025

Conversation

Stylie777
Copy link
Contributor

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

Basic parsing and semantics support for Declare Variant was added in
PR 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 llvm#145222
@Stylie777 Stylie777 requested review from tblah and kiranchandramohan and removed request for tblah July 4, 2025 15:05
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:openmp flang:semantics flang:parser labels Jul 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 4, 2025

@llvm/pr-subscribers-flang-openmp
@llvm/pr-subscribers-flang-parser

@llvm/pr-subscribers-flang-semantics

Author: Jack Styles (Stylie777)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/147075.diff

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-directives.cpp (+6)
  • (modified) flang/test/Parser/OpenMP/declare-variant.f90 (+27)
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 8d741734601a7..0d11b4a21ca3d 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -495,6 +495,12 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
   bool Pre(const parser::OpenMPAllocatorsConstruct &);
   void Post(const parser::OpenMPAllocatorsConstruct &);
 
+  bool Pre(const parser::OmpDeclareVariantDirective &x) {
+    PushContext(x.source, llvm::omp::Directive::OMPD_declare_variant);
+    return true;
+  }
+  void Post (const parser::OmpDeclareVariantDirective &) { PopContext(); };
+
   void Post(const parser::OmpObjectList &x) {
     // The objects from OMP clauses should have already been resolved,
     // except common blocks (the ResolveNamesVisitor does not visit
diff --git a/flang/test/Parser/OpenMP/declare-variant.f90 b/flang/test/Parser/OpenMP/declare-variant.f90
index 1b97733ea9525..3366b143e62e6 100644
--- a/flang/test/Parser/OpenMP/declare-variant.f90
+++ b/flang/test/Parser/OpenMP/declare-variant.f90
@@ -102,3 +102,30 @@ subroutine vsub(v1, v2)
     type(c_ptr), value :: v1, v2
   end
 end subroutine
+
+subroutine f
+  real :: x, y
+  y = 2
+  !omp simd
+  call f2(x, y)
+  !omp end simd 
+contains
+  subroutine f1 (x, y)
+    real :: x, y
+  end
+
+  subroutine f2 (x, y)
+    real :: x, y
+    !$omp declare variant (f1) match (construct={simd(uniform(y))})
+  end
+end subroutine
+!CHECK: !$OMP DECLARE VARIANT (f1) MATCH(CONSTRUCT={SIMD(UNIFORM(y))})
+!PARSE-TREE: | | | | DeclarationConstruct -> SpecificationConstruct -> OpenMPDeclarativeConstruct -> OmpDeclareVariantDirective
+!PARSE-TREE-NEXT: | | | | | Verbatim
+!PARSE-TREE-NEXT: | | | | | Name = 'f1'
+!PARSE-TREE-NEXT: | | | | | OmpClauseList -> OmpClause -> Match -> OmpMatchClause -> OmpContextSelectorSpecification -> OmpTraitSetSelector
+!PARSE-TREE-NEXT: | | | | | | OmpTraitSetSelectorName -> Value = Construct
+!PARSE-TREE-NEXT: | | | | | | OmpTraitSelector
+!PARSE-TREE-NEXT: | | | | | | | OmpTraitSelectorName -> Value = Simd
+!PARSE-TREE-NEXT: | | | | | | | Properties
+!PARSE-TREE-NEXT: | | | | | | | | OmpTraitProperty -> OmpClause -> Uniform -> Name = 'y'

@Stylie777 Stylie777 requested a review from tblah July 4, 2025 15:05
Copy link

github-actions bot commented Jul 4, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the fix.

In future some name resolution will have to happen from !$omp declare variant (f1) to the subroutine f1.

@Stylie777 Stylie777 merged commit 8a221a5 into llvm:main Jul 7, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:openmp flang:parser flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang][OpenMP] omp declare variant match causes fatal internal error: CHECK(!dirContext_.empty()) failed
3 participants