[llvm-branch-commits] [clang] [Clang][OpenMP] Fix upper-bound for number of threads (PR #218589)

Kevin Sala Penades via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Aug 24 23:05:41 PDT 2026


https://github.com/kevinsala created https://github.com/llvm/llvm-project/pull/218589

The condition to update upper-bound of number of threads was inverted. We now update the upper-bound with any constant value even if it we already found a non-constant value.

Test to be added.

>From 4a7453879437dece66b2f70ad30389e9e049da74 Mon Sep 17 00:00:00 2001
From: Kevin Sala <salapenades1 at llnl.gov>
Date: Tue, 14 Jul 2026 08:52:13 -0700
Subject: [PATCH] [Clang][OpenMP] Fix upper-bound for number of threads

---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 4b8b1de973407..4f5eb2d7160da 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -6712,8 +6712,8 @@ static void getNumThreads(CodeGenFunction &CGF, const CapturedStmt *CS,
       if (NTExpr->isIntegerConstantExpr(CGF.getContext()))
         if (auto Constant = NTExpr->getIntegerConstantExpr(CGF.getContext()))
           UpperBound =
-              UpperBound
-                  ? Constant->getZExtValue()
+              (UpperBound <= 0)
+                  ? static_cast<int32_t>(Constant->getZExtValue())
                   : std::min(UpperBound,
                              static_cast<int32_t>(Constant->getZExtValue()));
       // If we haven't found a upper bound, remember we saw a thread limiting
@@ -6757,12 +6757,13 @@ const Expr *CGOpenMPRuntime::getNumThreadsExprForTargetDirective(
   const Expr **NTPtr = UpperBoundOnly ? nullptr : &NT;
 
   auto CheckForConstExpr = [&](const Expr *E, const Expr **EPtr) {
-    if (E->isIntegerConstantExpr(CGF.getContext())) {
+    if (E->isIntegerConstantExpr(CGF.getContext()))
       if (auto Constant = E->getIntegerConstantExpr(CGF.getContext()))
-        UpperBound = UpperBound ? Constant->getZExtValue()
-                                : std::min(UpperBound,
-                                           int32_t(Constant->getZExtValue()));
-    }
+        UpperBound =
+            (UpperBound <= 0)
+                ? static_cast<int32_t>(Constant->getZExtValue())
+                : std::min(UpperBound,
+                           static_cast<int32_t>(Constant->getZExtValue()));
     // If we haven't found a upper bound, remember we saw a thread limiting
     // clause.
     if (UpperBound == -1)



More information about the llvm-branch-commits mailing list