[clang] [OpenMP] Fix crash on non-strict ICE unroll factor in C (PR #246275)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 01:12:06 PST 2026


https://github.com/sotw created https://github.com/llvm/llvm-project/pull/246275

Guard Expr::getIntegerConstantExpr(Context) before dereferencing in SemaOpenMP::ActOnOpenMPUnrollDirective, falling back to EvaluateAsInt for foldable integer expressions in C and llvm::omp::DefaultPartialUnrollFactor otherwise. Add a C regression test covering const variables and folded expressions.

Fixes #245442.

>From 88a469a103577e5e5b98b3efa92437e6bd20826c Mon Sep 17 00:00:00 2001
From: "PC. Tsai" <1614064+sotw at users.noreply.github.com>
Date: Thu, 26 Nov 2026 16:56:51 +0800
Subject: [PATCH] [OpenMP] Fix crash on non-strict ICE unroll factor in C

Guard Expr::getIntegerConstantExpr(Context) before dereferencing in
SemaOpenMP::ActOnOpenMPUnrollDirective, falling back to EvaluateAsInt for
foldable integer expressions in C and llvm::omp::DefaultPartialUnrollFactor
otherwise. Add a C regression test covering const variables and folded
expressions.

Fixes #245442.
---
 clang/lib/Sema/SemaOpenMP.cpp        | 11 ++++++++++-
 clang/test/OpenMP/unroll_partial_c.c | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/OpenMP/unroll_partial_c.c

diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 063d18843e45f5..5cebf662a914fb 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -16963,8 +16963,17 @@ StmtResult SemaOpenMP::ActOnOpenMPUnrollDirective(ArrayRef<OMPClause *> Clauses,
   SourceLocation FactorLoc;
   if (Expr *FactorVal = PartialClause->getFactor();
       FactorVal && !FactorVal->containsErrors()) {
-    Factor = FactorVal->getIntegerConstantExpr(Context)->getLimitedValue();
     FactorLoc = FactorVal->getExprLoc();
+    if (std::optional<llvm::APSInt> ICE =
+            FactorVal->getIntegerConstantExpr(Context)) {
+      Factor = ICE->getLimitedValue();
+    } else if (Expr::EvalResult EvalRes;
+               FactorVal->EvaluateAsInt(EvalRes, Context) &&
+               EvalRes.Val.isInt()) {
+      Factor = EvalRes.Val.getInt().getLimitedValue();
+    } else {
+      Factor = llvm::omp::DefaultPartialUnrollFactor;
+    }
   } else {
     // TODO: Use a better profitability model.
     Factor = llvm::omp::DefaultPartialUnrollFactor;
diff --git a/clang/test/OpenMP/unroll_partial_c.c b/clang/test/OpenMP/unroll_partial_c.c
new file mode 100644
index 00000000000000..44d78ec352223b
--- /dev/null
+++ b/clang/test/OpenMP/unroll_partial_c.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fopenmp -fsyntax-only -verify %s
+
+void test_partial_const_var(int n) {
+  const int factor = 4;
+#pragma omp unroll partial(factor)
+  for (int i = 0; i < n; ++i) {
+  }
+}
+
+void test_partial_folded_exprs(int n) {
+#pragma omp unroll partial((0, 4)) // expected-warning {{left operand of comma operator has no effect}}
+  for (int i = 0; i < n; ++i) {
+  }
+
+#pragma omp unroll partial((int)(2.0 * 2.0))
+  for (int i = 0; i < n; ++i) {
+  }
+}



More information about the cfe-commits mailing list