[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
Fri Aug 7 03:07:54 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:
I agree. It is collapse which allows the counter to go above representable values in the loop iteration variable type. Outside of collapse, NSW should be safe (that case from 0 to INT_MAX is clever but I agree we can ignore it).
For collapse, disallowing NSW for dynamic bounds is the safe option. I just wanted to check that didn't break the case you are trying to optimise.
https://github.com/llvm/llvm-project/pull/214165
More information about the flang-commits
mailing list