Skip to content

Commit f7bb691

Browse files
committed
[RISCV] Implement isElementTypeLegalForScalableVector TTI hook
This brings us into alignment with AArch64, and in the process fixes a compiler crash bug in uniform store handling in the vectorizer. Before the recent invalid cost bailout work, this would have also avoided crashes on invalid costs in some cases. I honestly think the vectorizer should gracefully bailout on uniform stores it can't use a scatter for, but it doesn't, so lets take the path of least resistance here. It's also possible that there are other vectorizer bugs AArch64 isn't seeing because of this hook; we don't want to be finding them either. Differential Revision: https://reviews.llvm.org/D127514
1 parent 0fe88f9 commit f7bb691

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
105105
Optional<FastMathFlags> FMF,
106106
TTI::TargetCostKind CostKind);
107107

108+
bool isElementTypeLegalForScalableVector(Type *Ty) const {
109+
return TLI->isLegalElementTypeForRVV(Ty);
110+
}
111+
108112
bool isLegalMaskedLoadStore(Type *DataType, Align Alignment) {
109113
if (!ST->hasVInstructions())
110114
return false;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -loop-vectorize -mattr=+v -force-vector-width=4 -scalable-vectorization=on -S 2>&1 | FileCheck %s
3+
target triple = "riscv64-linux-gnu"
4+
5+
;
6+
define dso_local void @loop_i128(i128* nocapture %ptr, i64 %N) {
7+
; CHECK-LABEL: @loop_i128(
8+
; CHECK-NEXT: entry:
9+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
10+
; CHECK: for.body:
11+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]
12+
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i128, i128* [[PTR:%.*]], i64 [[IV]]
13+
; CHECK-NEXT: [[TMP0:%.*]] = load i128, i128* [[ARRAYIDX]], align 16
14+
; CHECK-NEXT: [[ADD:%.*]] = add nsw i128 [[TMP0]], 42
15+
; CHECK-NEXT: store i128 [[ADD]], i128* [[ARRAYIDX]], align 16
16+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
17+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N:%.*]]
18+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_END:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
19+
; CHECK: for.end:
20+
; CHECK-NEXT: ret void
21+
;
22+
entry:
23+
br label %for.body
24+
25+
for.body:
26+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
27+
%arrayidx = getelementptr inbounds i128, i128* %ptr, i64 %iv
28+
%0 = load i128, i128* %arrayidx, align 16
29+
%add = add nsw i128 %0, 42
30+
store i128 %add, i128* %arrayidx, align 16
31+
%iv.next = add i64 %iv, 1
32+
%exitcond.not = icmp eq i64 %iv.next, %N
33+
br i1 %exitcond.not, label %for.end, label %for.body, !llvm.loop !0
34+
35+
for.end:
36+
ret void
37+
}
38+
39+
define dso_local void @loop_f128(fp128* nocapture %ptr, i64 %N) {
40+
; CHECK-LABEL: @loop_f128(
41+
; CHECK-NEXT: entry:
42+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
43+
; CHECK: for.body:
44+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]
45+
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds fp128, fp128* [[PTR:%.*]], i64 [[IV]]
46+
; CHECK-NEXT: [[TMP0:%.*]] = load fp128, fp128* [[ARRAYIDX]], align 16
47+
; CHECK-NEXT: [[ADD:%.*]] = fsub fp128 [[TMP0]], 0xL00000000000000008000000000000000
48+
; CHECK-NEXT: store fp128 [[ADD]], fp128* [[ARRAYIDX]], align 16
49+
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
50+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N:%.*]]
51+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_END:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP0]]
52+
; CHECK: for.end:
53+
; CHECK-NEXT: ret void
54+
;
55+
entry:
56+
br label %for.body
57+
58+
for.body:
59+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
60+
%arrayidx = getelementptr inbounds fp128, fp128* %ptr, i64 %iv
61+
%0 = load fp128, fp128* %arrayidx, align 16
62+
%add = fsub fp128 %0, 0xL00000000000000008000000000000000
63+
store fp128 %add, fp128* %arrayidx, align 16
64+
%iv.next = add nuw nsw i64 %iv, 1
65+
%exitcond.not = icmp eq i64 %iv.next, %N
66+
br i1 %exitcond.not, label %for.end, label %for.body, !llvm.loop !0
67+
68+
for.end:
69+
ret void
70+
}
71+
72+
define dso_local void @loop_invariant_i128(i128* nocapture %ptr, i128 %val, i64 %N) {
73+
; CHECK-LABEL: @loop_invariant_i128(
74+
; CHECK-NEXT: entry:
75+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
76+
; CHECK: for.body:
77+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]
78+
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i128, i128* [[PTR:%.*]], i64 [[IV]]
79+
; CHECK-NEXT: store i128 [[VAL:%.*]], i128* [[ARRAYIDX]], align 16
80+
; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
81+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N:%.*]]
82+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_END:%.*]], label [[FOR_BODY]], !llvm.loop [[LOOP0]]
83+
; CHECK: for.end:
84+
; CHECK-NEXT: ret void
85+
;
86+
entry:
87+
br label %for.body
88+
89+
for.body:
90+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
91+
%arrayidx = getelementptr inbounds i128, i128* %ptr, i64 %iv
92+
store i128 %val, i128* %arrayidx, align 16
93+
%iv.next = add nuw nsw i64 %iv, 1
94+
%exitcond.not = icmp eq i64 %iv.next, %N
95+
br i1 %exitcond.not, label %for.end, label %for.body, !llvm.loop !0
96+
97+
for.end:
98+
ret void
99+
}
100+
101+
define void @uniform_store_i1(i1* noalias %dst, i64* noalias %start, i64 %N) {
102+
; CHECK-LABEL: @uniform_store_i1(
103+
; CHECK-NEXT: entry:
104+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
105+
; CHECK: for.body:
106+
; CHECK-NEXT: [[FIRST_SROA:%.*]] = phi i64* [ [[INCDEC_PTR:%.*]], [[FOR_BODY]] ], [ [[START:%.*]], [[ENTRY:%.*]] ]
107+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_NEXT:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY]] ]
108+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
109+
; CHECK-NEXT: [[TMP0:%.*]] = load i64, i64* [[FIRST_SROA]], align 4
110+
; CHECK-NEXT: [[INCDEC_PTR]] = getelementptr inbounds i64, i64* [[FIRST_SROA]], i64 1
111+
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i64* [[INCDEC_PTR]], [[START]]
112+
; CHECK-NEXT: store i1 [[CMP_NOT]], i1* [[DST:%.*]], align 1
113+
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[IV]], [[N:%.*]]
114+
; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END:%.*]], !llvm.loop [[LOOP0]]
115+
; CHECK: end:
116+
; CHECK-NEXT: ret void
117+
;
118+
entry:
119+
br label %for.body
120+
121+
for.body:
122+
%first.sroa = phi i64* [ %incdec.ptr, %for.body ], [ %start, %entry ]
123+
%iv = phi i64 [ %iv.next, %for.body ], [ 0, %entry ]
124+
%iv.next = add i64 %iv, 1
125+
%0 = load i64, i64* %first.sroa
126+
%incdec.ptr = getelementptr inbounds i64, i64* %first.sroa, i64 1
127+
%cmp.not = icmp eq i64* %incdec.ptr, %start
128+
store i1 %cmp.not, i1* %dst
129+
%cmp = icmp ult i64 %iv, %N
130+
br i1 %cmp, label %for.body, label %end, !llvm.loop !0
131+
132+
end:
133+
ret void
134+
}
135+
136+
define dso_local void @loop_fixed_width_i128(i128* nocapture %ptr, i64 %N) {
137+
; CHECK-LABEL: @loop_fixed_width_i128(
138+
; CHECK-NEXT: entry:
139+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
140+
; CHECK: for.body:
141+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[FOR_BODY]] ]
142+
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i128, i128* [[PTR:%.*]], i64 [[IV]]
143+
; CHECK-NEXT: [[TMP0:%.*]] = load i128, i128* [[ARRAYIDX]], align 16
144+
; CHECK-NEXT: [[ADD:%.*]] = add nsw i128 [[TMP0]], 42
145+
; CHECK-NEXT: store i128 [[ADD]], i128* [[ARRAYIDX]], align 16
146+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
147+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], [[N:%.*]]
148+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_END:%.*]], label [[FOR_BODY]]
149+
; CHECK: for.end:
150+
; CHECK-NEXT: ret void
151+
;
152+
entry:
153+
br label %for.body
154+
155+
for.body:
156+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
157+
%arrayidx = getelementptr inbounds i128, i128* %ptr, i64 %iv
158+
%0 = load i128, i128* %arrayidx, align 16
159+
%add = add nsw i128 %0, 42
160+
store i128 %add, i128* %arrayidx, align 16
161+
%iv.next = add i64 %iv, 1
162+
%exitcond.not = icmp eq i64 %iv.next, %N
163+
br i1 %exitcond.not, label %for.end, label %for.body
164+
165+
for.end:
166+
ret void
167+
}
168+
169+
!0 = distinct !{!0, !1}
170+
!1 = !{!"llvm.loop.vectorize.scalable.enable", i1 true}

0 commit comments

Comments
 (0)