Skip to content

Commit e285602

Browse files
committed
[LV] Enforce addrec in current loop for uncountable exit load address check
Addresses post-commit review raised for #145663
1 parent 61f7f9b commit e285602

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,11 +1878,11 @@ bool LoopVectorizationLegality::canUncountableExitConditionLoadBeMoved(
18781878

18791879
// Make sure that the load address is not loop invariant; we want an
18801880
// address calculation that we can rotate to the next vector iteration.
1881-
const SCEV *PtrScev = PSE.getSE()->getSCEV(Ptr);
1882-
if (!isa<SCEVAddRecExpr>(PtrScev)) {
1881+
const auto *AR = dyn_cast<SCEVAddRecExpr>(PSE.getSE()->getSCEV(Ptr));
1882+
if (!AR || AR->getLoop() != TheLoop || !AR->isAffine()) {
18831883
reportVectorizationFailure(
18841884
"Uncountable exit condition depends on load with an address that is "
1885-
"not an add recurrence",
1885+
"not an add recurrence in the loop",
18861886
"EarlyExitLoadInvariantAddress", ORE, TheLoop);
18871887
return false;
18881888
}

llvm/test/Transforms/LoopVectorize/early_exit_store_legality.ll

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ exit:
353353

354354
define void @loop_contains_store_condition_load_is_chained(ptr dereferenceable(40) noalias %array, ptr align 8 dereferenceable(160) readonly %offsets, ptr align 2 dereferenceable(40) readonly %pred) {
355355
; CHECK-LABEL: LV: Checking a loop in 'loop_contains_store_condition_load_is_chained'
356-
; CHECK: LV: Not vectorizing: Uncountable exit condition depends on load with an address that is not an add recurrence.
356+
; CHECK: LV: Not vectorizing: Uncountable exit condition depends on load with an address that is not an add recurrence in the loop.
357357
entry:
358358
br label %for.body
359359

@@ -407,7 +407,7 @@ exit:
407407

408408
define void @loop_contains_store_condition_load_requires_gather(ptr dereferenceable(40) noalias %array, ptr align 2 dereferenceable(512) readonly %pred, ptr align 1 dereferenceable(20) readonly %offsets) {
409409
; CHECK-LABEL: LV: Checking a loop in 'loop_contains_store_condition_load_requires_gather'
410-
; CHECK: LV: Not vectorizing: Uncountable exit condition depends on load with an address that is not an add recurrence.
410+
; CHECK: LV: Not vectorizing: Uncountable exit condition depends on load with an address that is not an add recurrence in the loop.
411411
entry:
412412
br label %for.body
413413

@@ -544,7 +544,7 @@ exit:
544544

545545
define void @uncountable_exit_condition_address_is_invariant(ptr dereferenceable(40) noalias %array, ptr align 2 dereferenceable(2) readonly %pred) {
546546
; CHECK-LABEL: LV: Checking a loop in 'uncountable_exit_condition_address_is_invariant'
547-
; CHECK: LV: Not vectorizing: Uncountable exit condition depends on load with an address that is not an add recurrence.
547+
; CHECK: LV: Not vectorizing: Uncountable exit condition depends on load with an address that is not an add recurrence in the loop.
548548
entry:
549549
br label %for.body
550550

@@ -567,5 +567,40 @@ exit:
567567
ret void
568568
}
569569

570+
define void @uncountable_exit_condition_address_is_addrec_in_outer_loop(ptr dereferenceable(40) noalias %array, ptr align 2 dereferenceable(2) readonly %pred) {
571+
; CHECK-LABEL: LV: Checking a loop in 'uncountable_exit_condition_address_is_addrec_in_outer_loop'
572+
; CHECK: LV: Not vectorizing: Uncountable exit condition depends on load with an address that is not an add recurrence in the loop.
573+
entry:
574+
br label %outer.body
575+
576+
outer.body:
577+
%outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.inc ]
578+
%ee.addr = getelementptr inbounds nuw i16, ptr %pred, i64 %outer.iv
579+
br label %for.body
580+
581+
for.body:
582+
%iv = phi i64 [ 0, %outer.body ], [ %iv.next, %for.inc ]
583+
%st.addr = getelementptr inbounds nuw i16, ptr %array, i64 %iv
584+
%data = load i16, ptr %st.addr, align 2
585+
%inc = add nsw i16 %data, 1
586+
store i16 %inc, ptr %st.addr, align 2
587+
%ee.val = load i16, ptr %ee.addr, align 2
588+
%ee.cond = icmp sgt i16 %ee.val, 500
589+
br i1 %ee.cond, label %exit, label %for.inc
590+
591+
for.inc:
592+
%iv.next = add nuw nsw i64 %iv, 1
593+
%counted.cond = icmp eq i64 %iv.next, 20
594+
br i1 %counted.cond, label %outer.inc, label %for.body
595+
596+
outer.inc:
597+
%outer.iv.next = add nuw nsw i64 %outer.iv, 1
598+
%outer.cond = icmp eq i64 %outer.iv.next, 2
599+
br i1 %outer.cond, label %exit, label %outer.body
600+
601+
exit:
602+
ret void
603+
}
604+
570605
declare void @init_mem(ptr, i64);
571606
declare i64 @get_an_unknown_offset();

0 commit comments

Comments
 (0)