[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
Thu Mar 5 03:31:08 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();
----------------
Stylie777 wrote:
So I have taken a look at this again, and on further investigation it's going to be safe to apply the `nusw nuw` flags regardless. The issues I saw were down to the sub operations having values that would wrap when the Array if shifted. Further testing on llvm-test-suite has shown its safe to apply the `nuw` flags to Sub when the array is not shifted, otherwise it is marked nsw to indicate there will be unsigned wrapping for negative bounds. The results value, index, will always be positive so that can be taken forward as guaranteeing no unsigned wrap.
I will prepare an update to this PR with these changes.
https://github.com/llvm/llvm-project/pull/184573
More information about the flang-commits
mailing list