[flang-commits] [flang] [flang] Choose nsw/nuw/none for XArrayCoor slice steps (PR #212639)

via flang-commits flang-commits at lists.llvm.org
Wed Jul 29 00:55:02 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;
----------------
jeanPerier wrote:

What is the rational for dropping nsw for zero (and therefore unknown) stride?

Zero strides are illegal in Fortran ("The stride shall not be zero." in F2023 9.5.3.4.2), but even if they were not, I do not understand what could cause a sign overflow when executing the offset computations with a zero stride.

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


More information about the flang-commits mailing list