Skip to content

Commit 66fd420

Browse files
authored
[LV] Don't ignore invariant stores when costing (#158682)
Invariant stores of reductions are removed early in the VPlan construction, and there is no reason to ignore them while costing.
1 parent a02a5e7 commit 66fd420

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6381,19 +6381,8 @@ void LoopVectorizationCostModel::collectValuesToIgnore() {
63816381

63826382
LoopBlocksDFS DFS(TheLoop);
63836383
DFS.perform(LI);
6384-
MapVector<Value *, SmallVector<Value *>> DeadInvariantStoreOps;
63856384
for (BasicBlock *BB : reverse(make_range(DFS.beginRPO(), DFS.endRPO())))
63866385
for (Instruction &I : reverse(*BB)) {
6387-
// Find all stores to invariant variables. Since they are going to sink
6388-
// outside the loop we do not need calculate cost for them.
6389-
StoreInst *SI;
6390-
if ((SI = dyn_cast<StoreInst>(&I)) &&
6391-
Legal->isInvariantAddressOfReduction(SI->getPointerOperand())) {
6392-
ValuesToIgnore.insert(&I);
6393-
DeadInvariantStoreOps[SI->getPointerOperand()].push_back(
6394-
SI->getValueOperand());
6395-
}
6396-
63976386
if (VecValuesToIgnore.contains(&I) || ValuesToIgnore.contains(&I))
63986387
continue;
63996388

@@ -6440,9 +6429,6 @@ void LoopVectorizationCostModel::collectValuesToIgnore() {
64406429
append_range(DeadInterleavePointerOps, Op->operands());
64416430
}
64426431

6443-
for (const auto &[_, Ops] : DeadInvariantStoreOps)
6444-
llvm::append_range(DeadOps, drop_end(Ops));
6445-
64466432
// Mark ops that would be trivially dead and are only used by ignored
64476433
// instructions as free.
64486434
BasicBlock *Header = TheLoop->getHeader();

llvm/test/Transforms/LoopVectorize/X86/invariant-store-vectorization.ll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,30 @@ for.end: ; preds = %for.body
398398
define void @test_store_of_final_reduction_value(i64 %x, ptr %dst) {
399399
; CHECK-LABEL: define void @test_store_of_final_reduction_value(
400400
; CHECK-SAME: i64 [[X:%.*]], ptr [[DST:%.*]]) #[[ATTR0]] {
401-
; CHECK-NEXT: [[ENTRY:.*]]:
401+
; CHECK-NEXT: [[ENTRY:.*:]]
402+
; CHECK-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
403+
; CHECK: [[VECTOR_PH]]:
404+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i64> poison, i64 [[X]], i64 0
405+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i64> [[BROADCAST_SPLATINSERT]], <2 x i64> poison, <2 x i32> zeroinitializer
406+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
407+
; CHECK: [[VECTOR_BODY]]:
408+
; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <2 x i64> [ <i64 0, i64 1>, %[[VECTOR_PH]] ], [ [[TMP0:%.*]], %[[VECTOR_BODY]] ]
409+
; CHECK-NEXT: [[TMP0]] = mul <2 x i64> [[VEC_PHI]], [[BROADCAST_SPLAT]]
410+
; CHECK-NEXT: br i1 true, label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP32:![0-9]+]]
411+
; CHECK: [[MIDDLE_BLOCK]]:
412+
; CHECK-NEXT: [[TMP1:%.*]] = call i64 @llvm.vector.reduce.mul.v2i64(<2 x i64> [[TMP0]])
413+
; CHECK-NEXT: store i64 [[TMP1]], ptr [[DST]], align 8
414+
; CHECK-NEXT: br label %[[EXIT:.*]]
415+
; CHECK: [[SCALAR_PH]]:
402416
; CHECK-NEXT: br label %[[LOOP:.*]]
403417
; CHECK: [[LOOP]]:
404-
; CHECK-NEXT: [[IV4:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
405-
; CHECK-NEXT: [[RED:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[RED_NEXT:%.*]], %[[LOOP]] ]
418+
; CHECK-NEXT: [[IV4:%.*]] = phi i64 [ 0, %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
419+
; CHECK-NEXT: [[RED:%.*]] = phi i64 [ 0, %[[SCALAR_PH]] ], [ [[RED_NEXT:%.*]], %[[LOOP]] ]
406420
; CHECK-NEXT: [[RED_NEXT]] = mul i64 [[RED]], [[X]]
407421
; CHECK-NEXT: store i64 [[RED_NEXT]], ptr [[DST]], align 8
408422
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV4]], 1
409423
; CHECK-NEXT: [[EC:%.*]] = icmp eq i64 [[IV4]], 1
410-
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT:.*]], label %[[LOOP]]
424+
; CHECK-NEXT: br i1 [[EC]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP33:![0-9]+]]
411425
; CHECK: [[EXIT]]:
412426
; CHECK-NEXT: ret void
413427
;

0 commit comments

Comments
 (0)