Skip to content

Commit 6f37d36

Browse files
Mel-Chengithub-actions[bot]
authored andcommitted
Automerge: [VPlan] Fix nowrap flags for strided access pointers from SCEV (#209453)
This patch addresses two things. First, the offset calculation (canonical IV * stride) should not reuse the NSW flag of the add recurrence. The NSW property from SCEV for the original scalar recurrence does not necessarily hold for the reconstructed multiplication using the vector canonical IV. The NUW flag, however, can still be safely propagated. Second, because vputils::getGEPFlagsForPtr currently doesn't support recipes other than VPInstruction, and to avoid relying on LLVM IR function (like calling stripPointerCasts()), we change VPVectorPointerRecipe's GEP flags to use the add recurrence's flags to prevent propagating unprovable GEP flags like inbounds. (cherry picked from commit 1d41488)
2 parents cbd9179 + fc48c7e commit 6f37d36

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7787,16 +7787,16 @@ void VPlanTransforms::convertToStridedAccesses(VPlan &Plan,
77877787
auto *AddRecPtr = cast<SCEVAddRecExpr>(PtrSCEV);
77887788
auto *Offset = Builder.createOverflowingOp(
77897789
Instruction::Mul, {CanIV, StrideInBytes},
7790-
{AddRecPtr->hasNoUnsignedWrap(), AddRecPtr->hasNoSignedWrap()});
7791-
auto *BasePtr = Builder.createNoWrapPtrAdd(
7792-
StartVPV, Offset,
7793-
AddRecPtr->hasNoUnsignedWrap() ? GEPNoWrapFlags::noUnsignedWrap()
7794-
: GEPNoWrapFlags::none());
7790+
{AddRecPtr->hasNoUnsignedWrap(), /*HasNSW=*/false});
7791+
GEPNoWrapFlags NWFlags = AddRecPtr->hasNoUnsignedWrap()
7792+
? GEPNoWrapFlags::noUnsignedWrap()
7793+
: GEPNoWrapFlags::none();
7794+
VPValue *BasePtr = Builder.createNoWrapPtrAdd(StartVPV, Offset, NWFlags);
77957795

77967796
// Create a new vector pointer for strided access.
77977797
VPValue *NewPtr = Builder.createVectorPointer(
7798-
BasePtr, Type::getInt8Ty(Plan.getContext()), StrideInBytes,
7799-
Ptr->getGEPNoWrapFlags(), Ptr->getDebugLoc());
7798+
BasePtr, Type::getInt8Ty(Plan.getContext()), StrideInBytes, NWFlags,
7799+
Ptr->getDebugLoc());
78007800

78017801
VPValue *Mask = LoadR->getMask();
78027802
if (!Mask)

llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses-unroll.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --filter-out-after "^scalar.ph" --version 6
22
; RUN: opt < %s -S -p loop-vectorize -mtriple riscv64 -mattr=+v -force-vector-interleave=2 | FileCheck %s
33

4-
; FIXME: the second part of the strided load's gep shouldn't have nuw.
5-
64
define void @negative_stride_nuw(ptr noalias %src, ptr noalias %dst) {
75
; CHECK-LABEL: define void @negative_stride_nuw(
86
; CHECK-SAME: ptr noalias [[SRC:%.*]], ptr noalias [[DST:%.*]]) #[[ATTR0:[0-9]+]] {
@@ -26,7 +24,7 @@ define void @negative_stride_nuw(ptr noalias %src, ptr noalias %dst) {
2624
; CHECK-NEXT: [[TMP8:%.*]] = mul i64 [[INDEX]], -8
2725
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr i8, ptr [[TMP7]], i64 [[TMP8]]
2826
; CHECK-NEXT: [[TMP10:%.*]] = mul i64 [[TMP2]], -8
29-
; CHECK-NEXT: [[TMP11:%.*]] = getelementptr nuw i8, ptr [[TMP9]], i64 [[TMP10]]
27+
; CHECK-NEXT: [[TMP11:%.*]] = getelementptr i8, ptr [[TMP9]], i64 [[TMP10]]
3028
; CHECK-NEXT: [[TMP12:%.*]] = call <vscale x 4 x i32> @llvm.experimental.vp.strided.load.nxv4i32.p0.i64(ptr align 4 [[TMP9]], i64 -8, <vscale x 4 x i1> splat (i1 true), i32 [[TMP6]])
3129
; CHECK-NEXT: [[TMP13:%.*]] = call <vscale x 4 x i32> @llvm.experimental.vp.strided.load.nxv4i32.p0.i64(ptr align 4 [[TMP11]], i64 -8, <vscale x 4 x i1> splat (i1 true), i32 [[TMP6]])
3230
; CHECK-NEXT: [[TMP14:%.*]] = getelementptr inbounds i32, ptr [[DST]], i64 [[INDEX]]

llvm/test/Transforms/LoopVectorize/RISCV/strided-accesses.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,13 +1262,13 @@ define void @double_stride_ptr_iv(ptr %p, ptr %p2, i64 %stride) {
12621262
; STRIDED-UF2-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
12631263
; STRIDED-UF2: [[SCALAR_PH]]:
12641264
; STRIDED-UF2-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ], [ 0, %[[VECTOR_MEMCHECK]] ]
1265-
; STRIDED-UF2-NEXT: [[BC_RESUME_VAL16:%.*]] = phi ptr [ [[TMP11]], %[[MIDDLE_BLOCK]] ], [ [[P]], %[[ENTRY]] ], [ [[P]], %[[VECTOR_MEMCHECK]] ]
1266-
; STRIDED-UF2-NEXT: [[BC_RESUME_VAL17:%.*]] = phi ptr [ [[TMP12]], %[[MIDDLE_BLOCK]] ], [ [[P2]], %[[ENTRY]] ], [ [[P2]], %[[VECTOR_MEMCHECK]] ]
1265+
; STRIDED-UF2-NEXT: [[BC_RESUME_VAL15:%.*]] = phi ptr [ [[TMP11]], %[[MIDDLE_BLOCK]] ], [ [[P]], %[[ENTRY]] ], [ [[P]], %[[VECTOR_MEMCHECK]] ]
1266+
; STRIDED-UF2-NEXT: [[BC_RESUME_VAL16:%.*]] = phi ptr [ [[TMP12]], %[[MIDDLE_BLOCK]] ], [ [[P2]], %[[ENTRY]] ], [ [[P2]], %[[VECTOR_MEMCHECK]] ]
12671267
; STRIDED-UF2-NEXT: br label %[[LOOP:.*]]
12681268
; STRIDED-UF2: [[LOOP]]:
12691269
; STRIDED-UF2-NEXT: [[I:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[NEXTI:%.*]], %[[LOOP]] ]
1270-
; STRIDED-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL16]], %[[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], %[[LOOP]] ]
1271-
; STRIDED-UF2-NEXT: [[PTR2:%.*]] = phi ptr [ [[BC_RESUME_VAL17]], %[[SCALAR_PH]] ], [ [[PTR2_NEXT:%.*]], %[[LOOP]] ]
1270+
; STRIDED-UF2-NEXT: [[PTR:%.*]] = phi ptr [ [[BC_RESUME_VAL15]], %[[SCALAR_PH]] ], [ [[PTR_NEXT:%.*]], %[[LOOP]] ]
1271+
; STRIDED-UF2-NEXT: [[PTR2:%.*]] = phi ptr [ [[BC_RESUME_VAL16]], %[[SCALAR_PH]] ], [ [[PTR2_NEXT:%.*]], %[[LOOP]] ]
12721272
; STRIDED-UF2-NEXT: [[X0:%.*]] = load i32, ptr [[PTR]], align 4
12731273
; STRIDED-UF2-NEXT: [[Y0:%.*]] = add i32 [[X0]], 1
12741274
; STRIDED-UF2-NEXT: store i32 [[Y0]], ptr [[PTR2]], align 4
@@ -1356,7 +1356,7 @@ define void @constant_stride_reinterpret(ptr noalias %in, ptr noalias %out) {
13561356
; CHECK-UF2-NEXT: [[TMP5:%.*]] = shl nuw i64 [[INDEX]], 2
13571357
; CHECK-UF2-NEXT: [[TMP6:%.*]] = getelementptr nuw i8, ptr [[IN]], i64 [[TMP5]]
13581358
; CHECK-UF2-NEXT: [[TMP7:%.*]] = mul i64 [[TMP3]], 4
1359-
; CHECK-UF2-NEXT: [[TMP13:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP6]], i64 [[TMP7]]
1359+
; CHECK-UF2-NEXT: [[TMP13:%.*]] = getelementptr nuw i8, ptr [[TMP6]], i64 [[TMP7]]
13601360
; CHECK-UF2-NEXT: [[WIDE_MASKED_GATHER:%.*]] = call <vscale x 2 x i64> @llvm.experimental.vp.strided.load.nxv2i64.p0.i64(ptr align 8 [[TMP6]], i64 4, <vscale x 2 x i1> splat (i1 true), i32 [[TMP12]])
13611361
; CHECK-UF2-NEXT: [[WIDE_MASKED_GATHER1:%.*]] = call <vscale x 2 x i64> @llvm.experimental.vp.strided.load.nxv2i64.p0.i64(ptr align 8 [[TMP13]], i64 4, <vscale x 2 x i1> splat (i1 true), i32 [[TMP12]])
13621362
; CHECK-UF2-NEXT: [[TMP8:%.*]] = getelementptr inbounds nuw i64, ptr [[OUT]], i64 [[INDEX]]

0 commit comments

Comments
 (0)