Skip to content

Commit 8b45614

Browse files
authored
LAA: add missed swap when inverting src, sink (#122254)
When inverting source and sink on a negative induction step, the types of the source and sink should also be swapped. This fixes a bug in the code that follows, that computes properties based on these types. With 234cc40 ([LAA] Limit no-overlap check to at least one loop-invariant accesses.), that code is guarded by a loop-invariant condition: however, the commit did not add any new tests exercising the guarded code, and hence the bugfix in this patch requires additional tests to exercise that guarded codepath.
1 parent 21e58ee commit 8b45614

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,7 @@ MemoryDepChecker::getDependenceDistanceStrideAndSize(
19211921
if (StrideAPtr && *StrideAPtr < 0) {
19221922
std::swap(Src, Sink);
19231923
std::swap(AInst, BInst);
1924+
std::swap(ATy, BTy);
19241925
std::swap(StrideAPtr, StrideBPtr);
19251926
}
19261927

llvm/test/Analysis/LoopAccessAnalysis/depend_diff_types.ll

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,79 @@ loop:
194194
exit:
195195
ret void
196196
}
197+
198+
; In the following test, the sink is loop-invariant.
199+
200+
define void @type_size_equivalence_sink_loopinv(ptr nocapture %vec, i64 %n) {
201+
; CHECK-LABEL: 'type_size_equivalence_sink_loopinv'
202+
; CHECK-NEXT: loop:
203+
; CHECK-NEXT: Memory dependences are safe
204+
; CHECK-NEXT: Dependences:
205+
; CHECK-NEXT: Run-time memory checks:
206+
; CHECK-NEXT: Grouped accesses:
207+
; CHECK-EMPTY:
208+
; CHECK-NEXT: Non vectorizable stores to invariant address were not found in loop.
209+
; CHECK-NEXT: SCEV assumptions:
210+
; CHECK-EMPTY:
211+
; CHECK-NEXT: Expressions re-written:
212+
;
213+
entry:
214+
%gep.n = getelementptr inbounds i64, ptr %vec, i64 %n
215+
br label %loop
216+
217+
loop:
218+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
219+
220+
%gep.iv = getelementptr i64, ptr %vec, i64 %iv
221+
%ld.i64 = load i64, ptr %gep.iv, align 8
222+
223+
%ld.i64.i32 = trunc i64 %ld.i64 to i32
224+
store i32 %ld.i64.i32, ptr %gep.n, align 8
225+
226+
%iv.next = add nuw nsw i64 %iv, 1
227+
%cond = icmp eq i64 %iv.next, %n
228+
br i1 %cond, label %exit, label %loop
229+
230+
exit:
231+
ret void
232+
}
233+
234+
; Variant of the above, with a negative induction step and a gep exposing
235+
; type-mismtach.
236+
237+
define void @type_size_equivalence_sink_loopinv_negind(ptr nocapture %vec, i64 %n) {
238+
; CHECK-LABEL: 'type_size_equivalence_sink_loopinv_negind'
239+
; CHECK-NEXT: loop:
240+
; CHECK-NEXT: Memory dependences are safe
241+
; CHECK-NEXT: Dependences:
242+
; CHECK-NEXT: Run-time memory checks:
243+
; CHECK-NEXT: Grouped accesses:
244+
; CHECK-EMPTY:
245+
; CHECK-NEXT: Non vectorizable stores to invariant address were not found in loop.
246+
; CHECK-NEXT: SCEV assumptions:
247+
; CHECK-EMPTY:
248+
; CHECK-NEXT: Expressions re-written:
249+
;
250+
entry:
251+
%minus.n = sub nsw i64 0, %n
252+
%gep.minus.n = getelementptr inbounds i64, ptr %vec, i64 %minus.n
253+
br label %loop
254+
255+
loop:
256+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
257+
258+
%minus.iv = sub nsw i64 0, %iv
259+
%gep.minus.iv = getelementptr i64, ptr %vec, i64 %minus.iv
260+
%gep.minus.iv.4 = getelementptr i8, ptr %gep.minus.iv, i64 -4
261+
%ld.i64 = load i64, ptr %gep.minus.iv.4, align 8
262+
263+
%ld.i64.i32 = trunc i64 %ld.i64 to i32
264+
store i32 %ld.i64.i32, ptr %gep.minus.n, align 8
265+
266+
%iv.next = add nuw nsw i64 %iv, 1
267+
%cond = icmp eq i64 %iv.next, %n
268+
br i1 %cond, label %exit, label %loop
269+
270+
exit:
271+
ret void
272+
}

0 commit comments

Comments
 (0)