[flang-commits] [flang] [flang] Drop nuw on XArrayCoor for non-positive slice steps (PR #212639)

via flang-commits flang-commits at lists.llvm.org
Wed Jul 29 04:53:42 PDT 2026


================
@@ -3075,17 +3075,33 @@ struct XArrayCoorOpConversion
         if (normalSlice)
           step = integerCast(loc, rewriter, idxTy, operands[sliceOffset + 2]);
       }
+      // Wrap flags from the pre-cast step (keeps constants recognizable).
+      // Positive: nsw|nuw. Negative: nsw (result may be < 0). Zero/unknown:
+      // none.
+      mlir::LLVM::IntegerOverflowFlags indexFlags = addMulFlags;
+      if (normalSlice) {
+        mlir::Value stepOperand = operands[sliceOffset + 2];
+        if (std::optional<llvm::APInt> stepCst =
+                fir::getIntIfConstant(stepOperand)) {
+          if (stepCst->isZero())
+            indexFlags = mlir::LLVM::IntegerOverflowFlags::none;
+          else if (stepCst->isNegative())
+            indexFlags = nsw;
+        } else {
+          indexFlags = mlir::LLVM::IntegerOverflowFlags::none;
----------------
khaki3 wrote:

Dropping `nsw` was over-conservative. I've updated the PR to keep `nsw` in whichever case.

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


More information about the flang-commits mailing list