[flang-commits] [flang] [Flang] Apply nusw nuw flags on array_coor gep's (PR #184573)

Jack Styles via flang-commits flang-commits at lists.llvm.org
Wed Mar 4 02:21:47 PST 2026


================
@@ -2713,6 +2713,25 @@ struct XArrayCoorOpConversion
         baseIsBoxed ? getBoxTypePair(coor.getMemref().getType()) : TypePair{};
     mlir::LLVM::IntegerOverflowFlags nsw =
         mlir::LLVM::IntegerOverflowFlags::nsw;
+    mlir::LLVM::IntegerOverflowFlags nuw =
+        mlir::LLVM::IntegerOverflowFlags::nuw;
+    mlir::LLVM::IntegerOverflowFlags subFlags = nsw;
+    mlir::LLVM::IntegerOverflowFlags addMulFlags = nsw;
+    mlir::LLVM::GEPNoWrapFlags gepFlags = mlir::LLVM::GEPNoWrapFlags::none;
+
+    // In certain cases, where unsigned wrapping is known not to not occur, we
+    // can apply the nuw flag to Add/Mul operations, and `nusw nuw` flags to
+    // getelementptr's. By doing so, this enables better optimization through
+    // slp-vectorizer later in the LLVM pipeline.
+    const bool canUseNuw = !baseIsBoxed && !isShifted && !isSliced &&
+                           coor.getSubcomponent().empty() &&
+                           coor.getLenParams().empty() &&
+                           !coor.getShape().empty();
+    if (canUseNuw) {
----------------
Stylie777 wrote:

Applying NUW to subFlags was not suitable I found in testing. If the lb of the array was a negative value, it was likely that the unsigned value would wrap, causing Poison and undefined behaviour.

I will this information to the commit message.

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


More information about the flang-commits mailing list