[flang-commits] [flang] [llvm] [mlir] [Flang][OpenMP] Add nsw flags to OMPIRBuilder loop IV arithmetic (PR #214165)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Thu Aug 6 03:12:34 PDT 2026


================
@@ -5807,9 +5807,18 @@ CanonicalLoopInfo *OpenMPIRBuilder::createLoopSkeleton(
   Builder.CreateBr(Latch);
 
   Builder.SetInsertPoint(Latch);
-  Value *Next = Builder.CreateAdd(IndVarPHI, ConstantInt::get(IndVarTy, 1),
-                                  "omp_" + Name + ".next", /*HasNUW=*/true,
-                                  /*HasNSW=*/Config.hasNoSignedWrap());
+  bool HasNSW = Config.hasNoSignedWrap();
+  if (HasNSW) {
+    if (auto *CI = dyn_cast<ConstantInt>(TripCount)) {
+      unsigned BitWidth = CI->getType()->getIntegerBitWidth();
+      APInt SignedMax = APInt::getSignedMaxValue(BitWidth);
+      if (CI->getValue().ugt(SignedMax))
+        HasNSW = false;
+    }
----------------
tblah wrote:

Really we should set `HasNSW = false` when we can't determine statically that it is safe (because the tripcount is not constant).

Please either set `HasNSW = false` or add a comment explaining exactly what the usecase is for not doing that right now.

https://github.com/llvm/llvm-project/pull/214165


More information about the flang-commits mailing list