[llvm-branch-commits] [flang] [flang][OpenMP] Fold GetNumGeneratedNestsFrom into calculateLength, NFC (PR #185297)

Krzysztof Parzyszek via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Mar 8 08:42:15 PDT 2026


https://github.com/kparzysz created https://github.com/llvm/llvm-project/pull/185297

None

>From 0ac4d130136304468d51a26713956b7f31cb0ee0 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Mon, 2 Mar 2026 07:48:58 -0600
Subject: [PATCH] [flang][OpenMP] Fold GetNumGeneratedNestsFrom into
 calculateLength, NFC

---
 flang/lib/Semantics/openmp-utils.cpp | 53 +++++++++++++++++++++++++++-
 1 file changed, 52 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Semantics/openmp-utils.cpp b/flang/lib/Semantics/openmp-utils.cpp
index 1757dafdd3b3d..4a79a595eb7ac 100644
--- a/flang/lib/Semantics/openmp-utils.cpp
+++ b/flang/lib/Semantics/openmp-utils.cpp
@@ -820,7 +820,58 @@ std::optional<int64_t> LoopSequence::calculateLength() const {
   if (parser::Unwrap<parser::DoConstruct>(entry_->owner)) {
     return 1;
   }
-  return GetNumGeneratedNestsFrom(*entry_->owner, sumOfChildrenLengths());
+
+  auto &omp{DEREF(parser::Unwrap<parser::OpenMPLoopConstruct>(*entry_->owner))};
+  const parser::OmpDirectiveSpecification &beginSpec{omp.BeginDir()};
+  llvm::omp::Directive dir{beginSpec.DirId()};
+  if (!IsLoopTransforming(dir)) {
+    return 0;
+  }
+
+  // TODO: Handle split, apply.
+  if (IsFullUnroll(omp)) {
+    return std::nullopt;
+  }
+
+  auto nestedCount{sumOfChildrenLengths()};
+
+  if (dir == llvm::omp::Directive::OMPD_fuse) {
+    // If there are no loops nested inside of FUSE, then the construct is
+    // invalid. This case will be diagnosed when analyzing the body of the FUSE
+    // construct itself, not when checking a construct in which the FUSE is
+    // nested.
+    // Returning std::nullopt prevents error messages caused by the same
+    // problem from being emitted for every enclosing loop construct, for
+    // example:
+    //   !$omp do         ! error: this should contain a loop (superfluous)
+    //   !$omp fuse       ! error: this should contain a loop
+    //   !$omp end fuse
+    if (!nestedCount || *nestedCount == 0) {
+      return std::nullopt;
+    }
+    auto *clause{
+        parser::omp::FindClause(beginSpec, llvm::omp::Clause::OMPC_looprange)};
+    if (!clause) {
+      return 1;
+    }
+
+    auto *loopRange{parser::Unwrap<parser::OmpLooprangeClause>(*clause)};
+    std::optional<int64_t> count{GetIntValue(std::get<1>(loopRange->t))};
+    if (!count || *count <= 0) {
+      return std::nullopt;
+    }
+    if (*count <= *nestedCount) {
+      return 1 + *nestedCount - *count;
+    }
+    return std::nullopt;
+  }
+
+  if (dir == llvm::omp::Directive::OMPD_nothing) {
+    return nestedCount;
+  }
+
+  // For every other loop construct return 1.
+  return 1;
 }
 
 std::optional<int64_t> LoopSequence::sumOfChildrenLengths() const {



More information about the llvm-branch-commits mailing list