Skip to content

Conversation

kparzysz
Copy link
Contributor

No description provided.

@kparzysz kparzysz requested review from Stylie777 and tblah September 10, 2025 14:46
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:openmp flang:semantics flang:parser labels Sep 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-flang-openmp

Author: Krzysztof Parzyszek (kparzysz)

Changes

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

2 Files Affected:

  • (modified) flang/lib/Semantics/unparse-with-symbols.cpp (+11-1)
  • (added) flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90 (+13)
diff --git a/flang/lib/Semantics/unparse-with-symbols.cpp b/flang/lib/Semantics/unparse-with-symbols.cpp
index b199481131065..ec5b3ffadf30e 100644
--- a/flang/lib/Semantics/unparse-with-symbols.cpp
+++ b/flang/lib/Semantics/unparse-with-symbols.cpp
@@ -37,6 +37,8 @@ class SymbolDumpVisitor {
   template <typename T> void Post(const parser::Statement<T> &) {
     currStmt_ = std::nullopt;
   }
+  void Post(const parser::Name &name);
+
   bool Pre(const parser::AccClause &clause) {
     currStmt_ = clause.source;
     return true;
@@ -57,7 +59,6 @@ class SymbolDumpVisitor {
     return true;
   }
   void Post(const parser::OpenMPThreadprivate &) { currStmt_ = std::nullopt; }
-  void Post(const parser::Name &name);
 
   bool Pre(const parser::OpenMPDeclareMapperConstruct &x) {
     currStmt_ = x.source;
@@ -67,6 +68,14 @@ class SymbolDumpVisitor {
     currStmt_ = std::nullopt;
   }
 
+  bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
+    currStmt_ = x.source;
+    return true;
+  }
+  void Post(const parser::OpenMPDeclareReductionConstruct &) {
+    currStmt_ = std::nullopt;
+  }
+
   bool Pre(const parser::OpenMPDeclareTargetConstruct &x) {
     currStmt_ = x.source;
     return true;
@@ -120,6 +129,7 @@ void SymbolDumpVisitor::Indent(llvm::raw_ostream &out, int indent) const {
 void SymbolDumpVisitor::Post(const parser::Name &name) {
   if (const auto *symbol{name.symbol}) {
     if (!symbol->has<MiscDetails>()) {
+      CHECK(currStmt_.has_value());
       symbols_.emplace(currStmt_.value().begin(), symbol);
     }
   }
diff --git a/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90 b/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90
new file mode 100644
index 0000000000000..fbcd5b62821a3
--- /dev/null
+++ b/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90
@@ -0,0 +1,13 @@
+!RUN: %flang_fc1 -fdebug-unparse-with-symbols -fopenmp %s | FileCheck %s
+
+! This used to crash.
+
+subroutine f00
+  !$omp declare reduction(fred : integer, real : omp_out = omp_in + omp_out)
+end
+
+!CHECK: !DEF: /f00 (Subroutine) Subprogram
+!CHECK: subroutine f00
+!CHECK: !$omp declare reduction (fred:integer,real:omp_out = omp_in+omp_out)
+!CHECK: end subroutine
+

@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-flang-parser

Author: Krzysztof Parzyszek (kparzysz)

Changes

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

2 Files Affected:

  • (modified) flang/lib/Semantics/unparse-with-symbols.cpp (+11-1)
  • (added) flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90 (+13)
diff --git a/flang/lib/Semantics/unparse-with-symbols.cpp b/flang/lib/Semantics/unparse-with-symbols.cpp
index b199481131065..ec5b3ffadf30e 100644
--- a/flang/lib/Semantics/unparse-with-symbols.cpp
+++ b/flang/lib/Semantics/unparse-with-symbols.cpp
@@ -37,6 +37,8 @@ class SymbolDumpVisitor {
   template <typename T> void Post(const parser::Statement<T> &) {
     currStmt_ = std::nullopt;
   }
+  void Post(const parser::Name &name);
+
   bool Pre(const parser::AccClause &clause) {
     currStmt_ = clause.source;
     return true;
@@ -57,7 +59,6 @@ class SymbolDumpVisitor {
     return true;
   }
   void Post(const parser::OpenMPThreadprivate &) { currStmt_ = std::nullopt; }
-  void Post(const parser::Name &name);
 
   bool Pre(const parser::OpenMPDeclareMapperConstruct &x) {
     currStmt_ = x.source;
@@ -67,6 +68,14 @@ class SymbolDumpVisitor {
     currStmt_ = std::nullopt;
   }
 
+  bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
+    currStmt_ = x.source;
+    return true;
+  }
+  void Post(const parser::OpenMPDeclareReductionConstruct &) {
+    currStmt_ = std::nullopt;
+  }
+
   bool Pre(const parser::OpenMPDeclareTargetConstruct &x) {
     currStmt_ = x.source;
     return true;
@@ -120,6 +129,7 @@ void SymbolDumpVisitor::Indent(llvm::raw_ostream &out, int indent) const {
 void SymbolDumpVisitor::Post(const parser::Name &name) {
   if (const auto *symbol{name.symbol}) {
     if (!symbol->has<MiscDetails>()) {
+      CHECK(currStmt_.has_value());
       symbols_.emplace(currStmt_.value().begin(), symbol);
     }
   }
diff --git a/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90 b/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90
new file mode 100644
index 0000000000000..fbcd5b62821a3
--- /dev/null
+++ b/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90
@@ -0,0 +1,13 @@
+!RUN: %flang_fc1 -fdebug-unparse-with-symbols -fopenmp %s | FileCheck %s
+
+! This used to crash.
+
+subroutine f00
+  !$omp declare reduction(fred : integer, real : omp_out = omp_in + omp_out)
+end
+
+!CHECK: !DEF: /f00 (Subroutine) Subprogram
+!CHECK: subroutine f00
+!CHECK: !$omp declare reduction (fred:integer,real:omp_out = omp_in+omp_out)
+!CHECK: end subroutine
+

@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-flang-semantics

Author: Krzysztof Parzyszek (kparzysz)

Changes

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

2 Files Affected:

  • (modified) flang/lib/Semantics/unparse-with-symbols.cpp (+11-1)
  • (added) flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90 (+13)
diff --git a/flang/lib/Semantics/unparse-with-symbols.cpp b/flang/lib/Semantics/unparse-with-symbols.cpp
index b199481131065..ec5b3ffadf30e 100644
--- a/flang/lib/Semantics/unparse-with-symbols.cpp
+++ b/flang/lib/Semantics/unparse-with-symbols.cpp
@@ -37,6 +37,8 @@ class SymbolDumpVisitor {
   template <typename T> void Post(const parser::Statement<T> &) {
     currStmt_ = std::nullopt;
   }
+  void Post(const parser::Name &name);
+
   bool Pre(const parser::AccClause &clause) {
     currStmt_ = clause.source;
     return true;
@@ -57,7 +59,6 @@ class SymbolDumpVisitor {
     return true;
   }
   void Post(const parser::OpenMPThreadprivate &) { currStmt_ = std::nullopt; }
-  void Post(const parser::Name &name);
 
   bool Pre(const parser::OpenMPDeclareMapperConstruct &x) {
     currStmt_ = x.source;
@@ -67,6 +68,14 @@ class SymbolDumpVisitor {
     currStmt_ = std::nullopt;
   }
 
+  bool Pre(const parser::OpenMPDeclareReductionConstruct &x) {
+    currStmt_ = x.source;
+    return true;
+  }
+  void Post(const parser::OpenMPDeclareReductionConstruct &) {
+    currStmt_ = std::nullopt;
+  }
+
   bool Pre(const parser::OpenMPDeclareTargetConstruct &x) {
     currStmt_ = x.source;
     return true;
@@ -120,6 +129,7 @@ void SymbolDumpVisitor::Indent(llvm::raw_ostream &out, int indent) const {
 void SymbolDumpVisitor::Post(const parser::Name &name) {
   if (const auto *symbol{name.symbol}) {
     if (!symbol->has<MiscDetails>()) {
+      CHECK(currStmt_.has_value());
       symbols_.emplace(currStmt_.value().begin(), symbol);
     }
   }
diff --git a/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90 b/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90
new file mode 100644
index 0000000000000..fbcd5b62821a3
--- /dev/null
+++ b/flang/test/Parser/OpenMP/declare-reduction-unparse-with-symbols.f90
@@ -0,0 +1,13 @@
+!RUN: %flang_fc1 -fdebug-unparse-with-symbols -fopenmp %s | FileCheck %s
+
+! This used to crash.
+
+subroutine f00
+  !$omp declare reduction(fred : integer, real : omp_out = omp_in + omp_out)
+end
+
+!CHECK: !DEF: /f00 (Subroutine) Subprogram
+!CHECK: subroutine f00
+!CHECK: !$omp declare reduction (fred:integer,real:omp_out = omp_in+omp_out)
+!CHECK: end subroutine
+

Copy link
Contributor

@Stylie777 Stylie777 left a comment

Choose a reason for hiding this comment

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

LGTM

@kparzysz kparzysz merged commit fc9fe0f into llvm:main Sep 10, 2025
14 checks passed
@kparzysz kparzysz deleted the users/kparzysz/unparse-declare-reduction branch September 10, 2025 15:07
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.

3 participants