[flang-commits] [flang] [Flang] Apply nusw nuw flags on array_coor gep's (PR #184573)
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Wed Mar 4 03:53:05 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) {
----------------
tblah wrote:
Thanks it makes sense that unsigned arithmetic would easily wrap small negative numbers. But I wonder if using unsigned arithmetic would ever produce a sensible result in this case. Could you give an example of where this would happen?
Should we be checking for negative values in all lower bounds then?
https://github.com/llvm/llvm-project/pull/184573
More information about the flang-commits
mailing list