[llvm-branch-commits] [flang] [flang][OpenMP] Fix subtle bug in GetAffectedNestDepthWithReason (PR #190645)

Krzysztof Parzyszek via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Apr 6 11:53:45 PDT 2026


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

For constructs that allow COLLAPSE or ORDERED clauses, the function would return an empty value for the affected depth if none of these clauses were actually present. What should happen is that the return value should be 1 without a specific reason.

This bug was not detectable with any source program, since the empty value caused depth checks to be skipped. Detecting the problem would require a loop nest with a lower depth than needed that the bug would cause not to be diagnosed. Since the correct value was 1, such a loop would need to have a depth of 0 and such a nest cannot be constructed.

Issue: https://github.com/llvm/llvm-project/issues/185287

>From 22d5d5a866b82eff70457312c9b6cf65afb5226c Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Mon, 6 Apr 2026 11:27:49 -0500
Subject: [PATCH] [flang][OpenMP] Fix subtle bug in
 GetAffectedNestDepthWithReason

For constructs that allow COLLAPSE or ORDERED clauses, the function
would return an empty value for the affected depth if none of these
clauses were actually present. What should happen is that the return
value should be 1 without a specific reason.

This bug was not detectable with any source program, since the empty
value caused depth checks to be skipped. Detecting the problem would
require a loop nest with a lower depth than needed that the bug would
cause not to be diagnosed. Since the correct value was 1, such a loop
would need to have a depth of 0 and such a nest cannot be constructed.

Issue: https://github.com/llvm/llvm-project/issues/185287
---
 flang/lib/Semantics/openmp-utils.cpp | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/flang/lib/Semantics/openmp-utils.cpp b/flang/lib/Semantics/openmp-utils.cpp
index 33707f9d0d95b..1d9de49e6cd8e 100644
--- a/flang/lib/Semantics/openmp-utils.cpp
+++ b/flang/lib/Semantics/openmp-utils.cpp
@@ -850,17 +850,24 @@ std::pair<WithReason<int64_t>, bool> GetAffectedNestDepthWithReason(
       dir, llvm::omp::Clause::OMPC_ordered, version)};
 
   if (allowsCollapse || allowsOrdered) {
-    auto [count, reason]{GetArgumentValueWithReason(
+    auto [ccount, creason]{GetArgumentValueWithReason(
         spec, llvm::omp::Clause::OMPC_collapse, version)};
-    auto [vo, ro]{GetArgumentValueWithReason(
+    auto [ocount, oreason]{GetArgumentValueWithReason(
         spec, llvm::omp::Clause::OMPC_ordered, version)};
-    if (vo) {
-      if (!count || *count < *vo) {
-        count = vo;
-        reason = std::move(ro);
-      }
+    // Ignore invalid arguments.
+    if (ccount <= 0) {
+      ccount = std::nullopt;
+      creason = Reason();
+    }
+    if (ocount <= 0) {
+      ocount = std::nullopt;
+      oreason = Reason();
+    }
+    if (ccount < ocount) {
+      // `ocount` cannot be std::nullopt here (C++ std guarantee).
+      return {{ocount.value_or(1), std::move(oreason)}, true};
     }
-    return {{count, std::move(reason)}, true};
+    return {{ccount.value_or(1), std::move(creason)}, true};
   }
 
   if (IsLoopTransforming(dir)) {



More information about the llvm-branch-commits mailing list