Skip to content

Commit 5b250a2

Browse files
committed
[Analysis][LoopVectorize] do not form reductions of pointers
This is a fix for https://llvm.org/PR49215 either before/after we make a verifier enhancement for vector reductions with D96904. I'm not sure what the current thinking is for pointer math/logic in IR. We allow icmp on pointer values. Therefore, we match min/max patterns, so without this patch, the vectorizer could form a vector reduction from that sequence. But the LangRef definitions for min/max and vector reduction intrinsics do not allow pointer types: https://llvm.org/docs/LangRef.html#llvm-smax-intrinsic https://llvm.org/docs/LangRef.html#llvm-vector-reduce-umax-intrinsic So we would crash/assert at some point - either in IR verification, in the cost model, or in codegen. If we do want to allow this kind of transform, we will need to update the LangRef and all of those parts of the compiler. Differential Revision: https://reviews.llvm.org/D97047
1 parent 91c472c commit 5b250a2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,14 @@ bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurKind Kind,
243243
if (RecurrenceType->isFloatingPointTy()) {
244244
if (!isFloatingPointRecurrenceKind(Kind))
245245
return false;
246-
} else {
246+
} else if (RecurrenceType->isIntegerTy()) {
247247
if (!isIntegerRecurrenceKind(Kind))
248248
return false;
249249
if (isArithmeticRecurrenceKind(Kind))
250250
Start = lookThroughAnd(Phi, RecurrenceType, VisitedInsts, CastInsts);
251+
} else {
252+
// Pointer min/max may exist, but it is not supported as a reduction op.
253+
return false;
251254
}
252255

253256
Worklist.push_back(Start);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -loop-vectorize -force-vector-width=4 -S | FileCheck %s
3+
4+
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5+
6+
; Reductions of pointer types are not supported.
7+
8+
define void @PR49215(i32* %p, i32* %q) {
9+
; CHECK-LABEL: @PR49215(
10+
; CHECK-NEXT: entry:
11+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
12+
; CHECK: for.body:
13+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]
14+
; CHECK-NEXT: [[G:%.*]] = phi i32* [ [[P:%.*]], [[ENTRY]] ], [ [[UMIN:%.*]], [[FOR_BODY]] ]
15+
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i32* [[Q:%.*]], [[G]]
16+
; CHECK-NEXT: [[UMIN]] = select i1 [[CMP2]], i32* [[Q]], i32* [[G]]
17+
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
18+
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[IV_NEXT]], undef
19+
; CHECK-NEXT: br i1 [[EXITCOND]], label [[LOOPEXIT:%.*]], label [[FOR_BODY]]
20+
; CHECK: loopexit:
21+
; CHECK-NEXT: [[UMIN_LCSSA:%.*]] = phi i32* [ [[UMIN]], [[FOR_BODY]] ]
22+
; CHECK-NEXT: [[PHI_CAST:%.*]] = ptrtoint i32* [[UMIN_LCSSA]] to i64
23+
; CHECK-NEXT: ret void
24+
;
25+
entry:
26+
br label %for.body
27+
28+
for.body:
29+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
30+
%g = phi i32* [ %p, %entry ], [ %umin, %for.body ]
31+
%cmp2 = icmp ult i32* %q, %g
32+
%umin = select i1 %cmp2, i32* %q, i32* %g
33+
%iv.next = add nuw nsw i64 %iv, 1
34+
%exitcond = icmp eq i64 %iv.next, undef
35+
br i1 %exitcond, label %loopexit, label %for.body
36+
37+
loopexit:
38+
%phi.cast = ptrtoint i32* %umin to i64
39+
ret void
40+
}

0 commit comments

Comments
 (0)