[clang] [openmp] [Clang][OpenMP] Mixed signed/unsigned trip counts fix (PR #226462)
Amit Tiwari via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 25 05:02:26 PDT 2026
https://github.com/loopacino created https://github.com/llvm/llvm-project/pull/226462
Fix: Build that 1 already in the loop-index type (same pattern as the existing 0).
Issue: [Link](https://github.com/llvm/llvm-project/issues/225753)
>From 71e52541e9a4567c6d92cba67245c6aac052cca5 Mon Sep 17 00:00:00 2001
From: amtiwari <amtiwari at amd.com>
Date: Fri, 25 Sep 2026 07:58:10 -0400
Subject: [PATCH] sign mismatch fix
---
clang/lib/Sema/SemaOpenMP.cpp | 15 ++++--------
clang/test/OpenMP/flatten_codegen.cpp | 14 +++++++++++
.../test/transform/flatten/mixed-sign.c | 24 +++++++++++++++++++
3 files changed, 43 insertions(+), 10 deletions(-)
create mode 100644 openmp/runtime/test/transform/flatten/mixed-sign.c
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 3bd68a9ace981..1f858a35090bb 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -17420,21 +17420,16 @@ SemaOpenMP::ActOnOpenMPFlattenDirective(ArrayRef<OMPClause *> Clauses,
auto MakeDivisorInIVTy = [&](unsigned I) -> Expr * {
Expr *N = MakeNumIterationsInIVTy(I);
Expr *NCmp = MakeNumIterationsInIVTy(I);
- auto MakeOne = [&]() -> ExprResult {
- return SemaRef.PerformImplicitConversion(
- SemaRef.ActOnIntegerConstant(CondLoc, 1).get(), IVTy,
- AssignmentAction::Converting, /*AllowExplicit=*/true);
+ auto MakeOne = [&]() -> Expr * {
+ return IntegerLiteral::Create(Context, llvm::APInt(IVWidth, 1), IVTy,
+ CondLoc);
};
- ExprResult OneCmp = MakeOne();
- ExprResult OneVal = MakeOne();
- if (!OneCmp.isUsable() || !OneVal.isUsable())
- return N;
ExprResult TooSmall =
- SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, NCmp, OneCmp.get());
+ SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, NCmp, MakeOne());
if (!TooSmall.isUsable())
return N;
return AssertSuccess(SemaRef.ActOnConditionalOp(
- CondLoc, CondLoc, TooSmall.get(), OneVal.get(), N));
+ CondLoc, CondLoc, TooSmall.get(), MakeOne(), N));
};
// \code{.cpp}
diff --git a/clang/test/OpenMP/flatten_codegen.cpp b/clang/test/OpenMP/flatten_codegen.cpp
index fe2e4350672f3..c470bd962ef47 100644
--- a/clang/test/OpenMP/flatten_codegen.cpp
+++ b/clang/test/OpenMP/flatten_codegen.cpp
@@ -6,6 +6,8 @@
#ifndef HEADER
#define HEADER
+#include <stddef.h>
+
extern "C" void body(int, int);
// CHECK-LABEL: define {{.*}}void @foo(
@@ -100,4 +102,16 @@ extern "C" void both_neg() {
body(i, j);
}
+// Mixed signed/unsigned trip counts: the flattened IV is unsigned, and the
+// signed inner count is converted into that type for max(1, N).
+// CHECK-LABEL: define {{.*}}void @mixed_sign(
+// CHECK: %.flatten.iv = alloca i64
+// CHECK: icmp ult i64
+extern "C" void mixed_sign(int n) {
+#pragma omp flatten depth(2)
+ for (size_t i = 0; i < (size_t)n; ++i)
+ for (int j = 0; j < n; ++j)
+ body((int)i, j);
+}
+
#endif
diff --git a/openmp/runtime/test/transform/flatten/mixed-sign.c b/openmp/runtime/test/transform/flatten/mixed-sign.c
new file mode 100644
index 0000000000000..c354e332a15dc
--- /dev/null
+++ b/openmp/runtime/test/transform/flatten/mixed-sign.c
@@ -0,0 +1,24 @@
+// RUN: %libomp-compile -fopenmp-version=61 && %libomp-run \
+// RUN: | FileCheck %s --match-full-lines
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(void) {
+ int n = 2;
+ printf("do\n");
+#pragma omp flatten depth(2)
+ for (size_t i = 0; i < (size_t)n; ++i)
+ for (int j = 0; j < n; ++j)
+ printf("i=%zu j=%d\n", i, j);
+ printf("done\n");
+ return EXIT_SUCCESS;
+}
+
+// CHECK: do
+// CHECK-NEXT: i=0 j=0
+// CHECK-NEXT: i=0 j=1
+// CHECK-NEXT: i=1 j=0
+// CHECK-NEXT: i=1 j=1
+// CHECK-NEXT: done
More information about the cfe-commits
mailing list