Skip to content

Commit 32d9a38

Browse files
committed
[LV] Keep Primary Induction alive when folding tail by masking
Fix PR47390. The primary induction should be considered alive when folding tail by masking, because it will be used by said masking; even when it may otherwise appear useless: feeding only its own 'bump', which is correctly considered dead, and as the 'bump' of another induction variable, which may wrongfully want to consider its bump = the primary induction, dead. Differential Revision: https://reviews.llvm.org/D92017
1 parent cb08558 commit 32d9a38

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7132,6 +7132,12 @@ void LoopVectorizationPlanner::collectTriviallyDeadInstructions(
71327132
for (auto &Induction : Legal->getInductionVars()) {
71337133
PHINode *Ind = Induction.first;
71347134
auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
7135+
7136+
// If the tail is to be folded by masking, the primary induction variable,
7137+
// if exists, isn't dead: it will be used for masking. Don't kill it.
7138+
if (CM.foldTailByMasking() && IndUpdate == Legal->getPrimaryInduction())
7139+
continue;
7140+
71357141
if (llvm::all_of(IndUpdate->users(), [&](User *U) -> bool {
71367142
return U == Ind || DeadInstructions.count(cast<Instruction>(U));
71377143
}))

llvm/test/Transforms/LoopVectorize/dead_instructions.ll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,32 @@ for.end:
4040
%tmp3 = phi i64 [ %tmp2, %for.body ]
4141
ret i64 %tmp3
4242
}
43+
44+
45+
; CHECK-LABEL: @pr47390
46+
;
47+
; This test ensures that the primary induction is not considered dead when
48+
; acting as the 'add' of another induction, and otherwise feeding only its own
49+
; 'add' (recognized earlier as 'dead'), when the tail of the loop is folded by
50+
; masking. Such masking uses the primary induction.
51+
;
52+
; CHECK: vector.body:
53+
;
54+
define void @pr47390(i32 *%a) {
55+
entry:
56+
br label %loop
57+
58+
exit:
59+
ret void
60+
61+
loop:
62+
%primary = phi i32 [ 0, %entry ], [ %primary_add, %loop ]
63+
%use_primary = phi i32 [ -1, %entry ], [ %primary, %loop ]
64+
%secondary = phi i32 [ 1, %entry ], [ %secondary_add, %loop ]
65+
%primary_add = add i32 %primary, 1
66+
%secondary_add = add i32 %secondary, 1
67+
%gep = getelementptr inbounds i32, i32* %a, i32 %secondary
68+
%load = load i32, i32* %gep, align 8
69+
%cmp = icmp eq i32 %secondary, 5
70+
br i1 %cmp, label %exit, label %loop
71+
}

0 commit comments

Comments
 (0)