Skip to content

Commit 6cf4303

Browse files
committed
[VPlan] Use SCEV to prove non-aliasing for stores at different offsets.
Extend the logic add in #168771 to also allow sinking stores past stores in the same noalias set by checking if we can prove no-alias via the distance between accesses, checked via SCEV.
1 parent 1cbed4f commit 6cf4303

File tree

2 files changed

+73
-34
lines changed

2 files changed

+73
-34
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,54 @@ bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(
139139
return true;
140140
}
141141

142+
// Return true if \p A and \p B are known to not alias for all VFs in the plan,
143+
// checked via the distance between the accesses
144+
static bool isNoAliasViaDistance(VPReplicateRecipe *A, VPReplicateRecipe *B,
145+
ScalarEvolution &SE, const Loop &L,
146+
VPTypeAnalysis &TypeInfo) {
147+
if (A->getOpcode() != Instruction::Store ||
148+
B->getOpcode() != Instruction::Store)
149+
return false;
150+
151+
VPValue *AddrA = A->getOperand(1);
152+
const SCEV *SCEVA = vputils::getSCEVExprForVPValue(AddrA, SE, &L);
153+
VPValue *AddrB = B->getOperand(1);
154+
const SCEV *SCEVB = vputils::getSCEVExprForVPValue(AddrB, SE, &L);
155+
if (isa<SCEVCouldNotCompute>(SCEVA) || isa<SCEVCouldNotCompute>(SCEVB))
156+
return false;
157+
158+
const SCEV *Distance = SE.getMinusSCEV(SCEVA, SCEVB);
159+
auto *ConstDist = dyn_cast<SCEVConstant>(Distance);
160+
if (!ConstDist)
161+
return false;
162+
163+
const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
164+
Type *TyA = TypeInfo.inferScalarType(A->getOperand(0));
165+
TypeSize SizeA = DL.getTypeStoreSize(TyA);
166+
Type *TyB = TypeInfo.inferScalarType(B->getOperand(0));
167+
TypeSize SizeB = DL.getTypeStoreSize(TyB);
168+
169+
// Use the maximum store size to ensure no overlap from either direction.
170+
uint64_t MaxStoreSize =
171+
std::max(SizeA.getFixedValue(), SizeB.getFixedValue());
172+
const APInt &DistValue = ConstDist->getAPInt();
173+
auto VFs = B->getParent()->getPlan()->vectorFactors();
174+
ElementCount MaxVF = *max_element(VFs, ElementCount::isKnownLT);
175+
return DistValue.abs().uge(
176+
MaxVF.multiplyCoefficientBy(MaxStoreSize).getFixedValue());
177+
}
178+
142179
// Check if a memory operation doesn't alias with memory operations in blocks
143180
// between FirstBB and LastBB using scoped noalias metadata.
144181
// For load hoisting, we only check writes in one direction.
145182
// For store sinking, we check both reads and writes bidirectionally.
146183
static bool canHoistOrSinkWithNoAliasCheck(
147184
const MemoryLocation &MemLoc, VPBasicBlock *FirstBB, VPBasicBlock *LastBB,
148185
bool CheckReads,
149-
const SmallPtrSetImpl<VPRecipeBase *> *ExcludeRecipes = nullptr) {
186+
const SmallPtrSetImpl<VPRecipeBase *> *ExcludeRecipes = nullptr,
187+
ScalarEvolution *SE = nullptr, const Loop *L = nullptr,
188+
VPTypeAnalysis *TypeInfo = nullptr,
189+
ArrayRef<VPReplicateRecipe *> MemOpsInGroup = {}) {
150190
if (!MemLoc.AATags.Scope)
151191
return false;
152192

@@ -165,6 +205,13 @@ static bool canHoistOrSinkWithNoAliasCheck(
165205
if (!R.mayWriteToMemory() && !(CheckReads && R.mayReadFromMemory()))
166206
continue;
167207

208+
// For stores, check if we can use SCEV to prove no-alias.
209+
if (auto *Store = dyn_cast<VPReplicateRecipe>(&R)) {
210+
if (SE && L && TypeInfo && !MemOpsInGroup.empty() &&
211+
isNoAliasViaDistance(Store, MemOpsInGroup[0], *SE, *L, *TypeInfo))
212+
continue;
213+
}
214+
168215
auto Loc = vputils::getMemoryLocation(R);
169216
if (!Loc)
170217
// Conservatively assume aliasing for memory operations without
@@ -4301,7 +4348,9 @@ void VPlanTransforms::hoistPredicatedLoads(VPlan &Plan, ScalarEvolution &SE,
43014348
}
43024349

43034350
static bool
4304-
canSinkStoreWithNoAliasCheck(ArrayRef<VPReplicateRecipe *> StoresToSink) {
4351+
canSinkStoreWithNoAliasCheck(ArrayRef<VPReplicateRecipe *> StoresToSink,
4352+
ScalarEvolution *SE, const Loop *L,
4353+
VPTypeAnalysis &TypeInfo) {
43054354
auto StoreLoc = vputils::getMemoryLocation(*StoresToSink.front());
43064355
if (!StoreLoc || !StoreLoc->AATags.Scope)
43074356
return false;
@@ -4314,7 +4363,8 @@ canSinkStoreWithNoAliasCheck(ArrayRef<VPReplicateRecipe *> StoresToSink) {
43144363
VPBasicBlock *FirstBB = StoresToSink.front()->getParent();
43154364
VPBasicBlock *LastBB = StoresToSink.back()->getParent();
43164365
return canHoistOrSinkWithNoAliasCheck(*StoreLoc, FirstBB, LastBB,
4317-
/*CheckReads=*/true, &StoresToSinkSet);
4366+
/*CheckReads=*/true, &StoresToSinkSet,
4367+
SE, L, &TypeInfo, StoresToSink);
43184368
}
43194369

43204370
void VPlanTransforms::sinkPredicatedStores(VPlan &Plan, ScalarEvolution &SE,
@@ -4325,13 +4375,14 @@ void VPlanTransforms::sinkPredicatedStores(VPlan &Plan, ScalarEvolution &SE,
43254375
return;
43264376

43274377
VPDominatorTree VPDT(Plan);
4378+
VPTypeAnalysis TypeInfo(Plan);
43284379

43294380
for (auto &Group : Groups) {
43304381
sort(Group, [&VPDT](VPReplicateRecipe *A, VPReplicateRecipe *B) {
43314382
return VPDT.properlyDominates(A, B);
43324383
});
43334384

4334-
if (!canSinkStoreWithNoAliasCheck(Group))
4385+
if (!canSinkStoreWithNoAliasCheck(Group, &SE, L, TypeInfo))
43354386
continue;
43364387

43374388
// Use the last (most dominated) store's location for the unconditional

llvm/test/Transforms/LoopVectorize/hoist-predicated-loads-with-predicated-stores.ll

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ define void @sink_multiple_store_groups_noalias_via_scev(ptr %dst, ptr %src) {
764764
; CHECK: [[VECTOR_PH]]:
765765
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
766766
; CHECK: [[VECTOR_BODY]]:
767-
; CHECK-NEXT: [[INDEX1:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_STORE_CONTINUE7:.*]] ]
767+
; CHECK-NEXT: [[INDEX1:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_STORE_CONTINUE3:.*]] ]
768768
; CHECK-NEXT: [[INDEX:%.*]] = mul i64 [[INDEX1]], 16
769769
; CHECK-NEXT: [[IV:%.*]] = add i64 [[INDEX]], 0
770770
; CHECK-NEXT: [[TMP17:%.*]] = add i64 [[INDEX]], 16
@@ -781,42 +781,30 @@ define void @sink_multiple_store_groups_noalias_via_scev(ptr %dst, ptr %src) {
781781
; CHECK-NEXT: [[TMP14:%.*]] = load double, ptr [[TMP22]], align 8, !alias.scope [[META78]]
782782
; CHECK-NEXT: [[TMP15:%.*]] = insertelement <2 x double> poison, double [[TMP13]], i32 0
783783
; CHECK-NEXT: [[WIDE_LOAD:%.*]] = insertelement <2 x double> [[TMP15]], double [[TMP14]], i32 1
784-
; CHECK-NEXT: [[TMP16:%.*]] = xor <2 x i1> [[TMP10]], splat (i1 true)
785784
; CHECK-NEXT: [[TMP34:%.*]] = fadd <2 x double> [[WIDE_LOAD]], splat (double 8.000000e+00)
786-
; CHECK-NEXT: [[TMP24:%.*]] = extractelement <2 x i1> [[TMP16]], i32 0
787-
; CHECK-NEXT: br i1 [[TMP24]], label %[[PRED_STORE_IF:.*]], label %[[PRED_STORE_CONTINUE:.*]]
788-
; CHECK: [[PRED_STORE_IF]]:
789785
; CHECK-NEXT: [[TMP18:%.*]] = getelementptr double, ptr [[DST]], i64 [[IV]]
790-
; CHECK-NEXT: [[TMP19:%.*]] = extractelement <2 x double> [[TMP34]], i32 0
791-
; CHECK-NEXT: store double [[TMP19]], ptr [[TMP18]], align 8, !alias.scope [[META81:![0-9]+]], !noalias [[META78]]
786+
; CHECK-NEXT: [[TMP21:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP17]]
787+
; CHECK-NEXT: [[TMP31:%.*]] = insertelement <2 x ptr> poison, ptr [[TMP18]], i32 0
788+
; CHECK-NEXT: [[TMP19:%.*]] = insertelement <2 x ptr> [[TMP31]], ptr [[TMP21]], i32 1
789+
; CHECK-NEXT: [[TMP20:%.*]] = select <2 x i1> [[TMP10]], <2 x double> [[WIDE_LOAD]], <2 x double> [[TMP34]]
790+
; CHECK-NEXT: [[TMP32:%.*]] = extractelement <2 x double> [[TMP20]], i32 0
791+
; CHECK-NEXT: store double [[TMP32]], ptr [[TMP18]], align 8, !alias.scope [[META81:![0-9]+]], !noalias [[META78]]
792+
; CHECK-NEXT: [[TMP33:%.*]] = extractelement <2 x double> [[TMP20]], i32 1
793+
; CHECK-NEXT: store double [[TMP33]], ptr [[TMP21]], align 8, !alias.scope [[META81]], !noalias [[META78]]
794+
; CHECK-NEXT: [[TMP23:%.*]] = extractelement <2 x i1> [[TMP10]], i32 0
795+
; CHECK-NEXT: br i1 [[TMP23]], label %[[PRED_STORE_IF:.*]], label %[[PRED_STORE_CONTINUE:.*]]
796+
; CHECK: [[PRED_STORE_IF]]:
797+
; CHECK-NEXT: [[TMP24:%.*]] = getelementptr i8, ptr [[TMP18]], i64 16
798+
; CHECK-NEXT: store double 1.000000e+01, ptr [[TMP24]], align 8, !alias.scope [[META81]], !noalias [[META78]]
792799
; CHECK-NEXT: br label %[[PRED_STORE_CONTINUE]]
793800
; CHECK: [[PRED_STORE_CONTINUE]]:
794-
; CHECK-NEXT: [[TMP20:%.*]] = extractelement <2 x i1> [[TMP16]], i32 1
795-
; CHECK-NEXT: br i1 [[TMP20]], label %[[PRED_STORE_IF2:.*]], label %[[PRED_STORE_CONTINUE3:.*]]
801+
; CHECK-NEXT: [[TMP25:%.*]] = extractelement <2 x i1> [[TMP10]], i32 1
802+
; CHECK-NEXT: br i1 [[TMP25]], label %[[PRED_STORE_IF2:.*]], label %[[PRED_STORE_CONTINUE3]]
796803
; CHECK: [[PRED_STORE_IF2]]:
797-
; CHECK-NEXT: [[TMP21:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP17]]
798-
; CHECK-NEXT: [[TMP33:%.*]] = extractelement <2 x double> [[TMP34]], i32 1
799-
; CHECK-NEXT: store double [[TMP33]], ptr [[TMP21]], align 8, !alias.scope [[META81]], !noalias [[META78]]
804+
; CHECK-NEXT: [[TMP35:%.*]] = getelementptr i8, ptr [[TMP21]], i64 16
805+
; CHECK-NEXT: store double 1.000000e+01, ptr [[TMP35]], align 8, !alias.scope [[META81]], !noalias [[META78]]
800806
; CHECK-NEXT: br label %[[PRED_STORE_CONTINUE3]]
801807
; CHECK: [[PRED_STORE_CONTINUE3]]:
802-
; CHECK-NEXT: [[TMP23:%.*]] = extractelement <2 x i1> [[TMP10]], i32 0
803-
; CHECK-NEXT: br i1 [[TMP23]], label %[[PRED_STORE_IF4:.*]], label %[[PRED_STORE_CONTINUE5:.*]]
804-
; CHECK: [[PRED_STORE_IF4]]:
805-
; CHECK-NEXT: [[TMP31:%.*]] = getelementptr double, ptr [[DST]], i64 [[IV]]
806-
; CHECK-NEXT: store double [[TMP13]], ptr [[TMP31]], align 8, !alias.scope [[META81]], !noalias [[META78]]
807-
; CHECK-NEXT: [[TMP37:%.*]] = getelementptr i8, ptr [[TMP31]], i64 16
808-
; CHECK-NEXT: store double 1.000000e+01, ptr [[TMP37]], align 8, !alias.scope [[META81]], !noalias [[META78]]
809-
; CHECK-NEXT: br label %[[PRED_STORE_CONTINUE5]]
810-
; CHECK: [[PRED_STORE_CONTINUE5]]:
811-
; CHECK-NEXT: [[TMP25:%.*]] = extractelement <2 x i1> [[TMP10]], i32 1
812-
; CHECK-NEXT: br i1 [[TMP25]], label %[[PRED_STORE_IF6:.*]], label %[[PRED_STORE_CONTINUE7]]
813-
; CHECK: [[PRED_STORE_IF6]]:
814-
; CHECK-NEXT: [[TMP32:%.*]] = getelementptr double, ptr [[DST]], i64 [[TMP17]]
815-
; CHECK-NEXT: store double [[TMP14]], ptr [[TMP32]], align 8, !alias.scope [[META81]], !noalias [[META78]]
816-
; CHECK-NEXT: [[TMP47:%.*]] = getelementptr i8, ptr [[TMP32]], i64 16
817-
; CHECK-NEXT: store double 1.000000e+01, ptr [[TMP47]], align 8, !alias.scope [[META81]], !noalias [[META78]]
818-
; CHECK-NEXT: br label %[[PRED_STORE_CONTINUE7]]
819-
; CHECK: [[PRED_STORE_CONTINUE7]]:
820808
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX1]], 2
821809
; CHECK-NEXT: [[TMP52:%.*]] = icmp eq i64 [[INDEX_NEXT]], 100
822810
; CHECK-NEXT: br i1 [[TMP52]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP83:![0-9]+]]

0 commit comments

Comments
 (0)