Skip to content

Commit e8ceadd

Browse files
committed
[LV][RISCV] Add a test case for a quality problem mixing vector index and data types
The problem here is target independent, but particularly painful on RISCV. If we chose to vectorize such that vscale x 2 x i32 is our widest type and fits in a register, a naive expansion of i64 comparisons results in comparisons and index types at <scalabe x 2 x i64>. This requires both an LMUL of 2, and a VSETVLI toggle in the loop. Note that we could have used <vscale x 2 x i32> for the compairons legally given the range of the trip count.
1 parent 65246d3 commit e8ceadd

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -loop-vectorize -scalable-vectorization=on -mtriple riscv64-linux-gnu -mattr=+v,+f -S 2>%t | FileCheck %s -check-prefix=VLENUNK
3+
4+
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
5+
target triple = "riscv64"
6+
7+
; FIXME: In this example, we pick a vector index with which is wider than
8+
; the data width. This is correct, but sub-optimal as it causes a vsetvli
9+
; toggle in the generated code for no reason. We could have used a i32
10+
; element type for the index here and matched the data width.
11+
define void @test(ptr noalias nocapture %a, ptr noalias nocapture %b, i32 %v) {
12+
; VLENUNK-LABEL: @test(
13+
; VLENUNK-NEXT: entry:
14+
; VLENUNK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
15+
; VLENUNK-NEXT: [[TMP1:%.*]] = mul i64 [[TMP0]], 4
16+
; VLENUNK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 1024, [[TMP1]]
17+
; VLENUNK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
18+
; VLENUNK: vector.ph:
19+
; VLENUNK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
20+
; VLENUNK-NEXT: [[TMP3:%.*]] = mul i64 [[TMP2]], 4
21+
; VLENUNK-NEXT: [[N_MOD_VF:%.*]] = urem i64 1024, [[TMP3]]
22+
; VLENUNK-NEXT: [[N_VEC:%.*]] = sub i64 1024, [[N_MOD_VF]]
23+
; VLENUNK-NEXT: [[TMP4:%.*]] = call <vscale x 2 x i64> @llvm.experimental.stepvector.nxv2i64()
24+
; VLENUNK-NEXT: [[TMP5:%.*]] = add <vscale x 2 x i64> [[TMP4]], zeroinitializer
25+
; VLENUNK-NEXT: [[TMP6:%.*]] = mul <vscale x 2 x i64> [[TMP5]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 1, i32 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
26+
; VLENUNK-NEXT: [[INDUCTION:%.*]] = add <vscale x 2 x i64> zeroinitializer, [[TMP6]]
27+
; VLENUNK-NEXT: [[TMP7:%.*]] = call i64 @llvm.vscale.i64()
28+
; VLENUNK-NEXT: [[TMP8:%.*]] = mul i64 [[TMP7]], 2
29+
; VLENUNK-NEXT: [[TMP9:%.*]] = mul i64 1, [[TMP8]]
30+
; VLENUNK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP9]], i32 0
31+
; VLENUNK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 2 x i64> [[DOTSPLATINSERT]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
32+
; VLENUNK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 2 x i32> poison, i32 [[V:%.*]], i32 0
33+
; VLENUNK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 2 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
34+
; VLENUNK-NEXT: [[BROADCAST_SPLATINSERT4:%.*]] = insertelement <vscale x 2 x i32> poison, i32 [[V]], i32 0
35+
; VLENUNK-NEXT: [[BROADCAST_SPLAT5:%.*]] = shufflevector <vscale x 2 x i32> [[BROADCAST_SPLATINSERT4]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
36+
; VLENUNK-NEXT: br label [[VECTOR_BODY:%.*]]
37+
; VLENUNK: vector.body:
38+
; VLENUNK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
39+
; VLENUNK-NEXT: [[VEC_IND:%.*]] = phi <vscale x 2 x i64> [ [[INDUCTION]], [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
40+
; VLENUNK-NEXT: [[STEP_ADD:%.*]] = add <vscale x 2 x i64> [[VEC_IND]], [[DOTSPLAT]]
41+
; VLENUNK-NEXT: [[TMP10:%.*]] = add i64 [[INDEX]], 0
42+
; VLENUNK-NEXT: [[TMP11:%.*]] = call i64 @llvm.vscale.i64()
43+
; VLENUNK-NEXT: [[TMP12:%.*]] = mul i64 [[TMP11]], 2
44+
; VLENUNK-NEXT: [[TMP13:%.*]] = add i64 [[TMP12]], 0
45+
; VLENUNK-NEXT: [[TMP14:%.*]] = mul i64 [[TMP13]], 1
46+
; VLENUNK-NEXT: [[TMP15:%.*]] = add i64 [[INDEX]], [[TMP14]]
47+
; VLENUNK-NEXT: [[TMP16:%.*]] = icmp ult <vscale x 2 x i64> [[VEC_IND]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 512, i32 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
48+
; VLENUNK-NEXT: [[TMP17:%.*]] = icmp ult <vscale x 2 x i64> [[STEP_ADD]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 512, i32 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
49+
; VLENUNK-NEXT: [[TMP18:%.*]] = getelementptr i32, ptr [[A:%.*]], i64 [[TMP10]]
50+
; VLENUNK-NEXT: [[TMP19:%.*]] = getelementptr i32, ptr [[A]], i64 [[TMP15]]
51+
; VLENUNK-NEXT: [[TMP20:%.*]] = getelementptr i32, ptr [[TMP18]], i32 0
52+
; VLENUNK-NEXT: [[WIDE_MASKED_LOAD:%.*]] = call <vscale x 2 x i32> @llvm.masked.load.nxv2i32.p0(ptr [[TMP20]], i32 4, <vscale x 2 x i1> [[TMP16]], <vscale x 2 x i32> poison)
53+
; VLENUNK-NEXT: [[TMP21:%.*]] = call i32 @llvm.vscale.i32()
54+
; VLENUNK-NEXT: [[TMP22:%.*]] = mul i32 [[TMP21]], 2
55+
; VLENUNK-NEXT: [[TMP23:%.*]] = getelementptr i32, ptr [[TMP18]], i32 [[TMP22]]
56+
; VLENUNK-NEXT: [[WIDE_MASKED_LOAD2:%.*]] = call <vscale x 2 x i32> @llvm.masked.load.nxv2i32.p0(ptr [[TMP23]], i32 4, <vscale x 2 x i1> [[TMP17]], <vscale x 2 x i32> poison)
57+
; VLENUNK-NEXT: [[TMP24:%.*]] = xor <vscale x 2 x i1> [[TMP16]], shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> poison, i1 true, i32 0), <vscale x 2 x i1> poison, <vscale x 2 x i32> zeroinitializer)
58+
; VLENUNK-NEXT: [[TMP25:%.*]] = xor <vscale x 2 x i1> [[TMP17]], shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> poison, i1 true, i32 0), <vscale x 2 x i1> poison, <vscale x 2 x i32> zeroinitializer)
59+
; VLENUNK-NEXT: [[PREDPHI:%.*]] = select <vscale x 2 x i1> [[TMP24]], <vscale x 2 x i32> zeroinitializer, <vscale x 2 x i32> [[WIDE_MASKED_LOAD]]
60+
; VLENUNK-NEXT: [[PREDPHI3:%.*]] = select <vscale x 2 x i1> [[TMP25]], <vscale x 2 x i32> zeroinitializer, <vscale x 2 x i32> [[WIDE_MASKED_LOAD2]]
61+
; VLENUNK-NEXT: [[TMP26:%.*]] = add <vscale x 2 x i32> [[PREDPHI]], [[BROADCAST_SPLAT]]
62+
; VLENUNK-NEXT: [[TMP27:%.*]] = add <vscale x 2 x i32> [[PREDPHI3]], [[BROADCAST_SPLAT5]]
63+
; VLENUNK-NEXT: [[TMP28:%.*]] = getelementptr inbounds i32, ptr [[B:%.*]], i64 [[TMP10]]
64+
; VLENUNK-NEXT: [[TMP29:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[TMP15]]
65+
; VLENUNK-NEXT: [[TMP30:%.*]] = getelementptr inbounds i32, ptr [[TMP28]], i32 0
66+
; VLENUNK-NEXT: store <vscale x 2 x i32> [[TMP26]], ptr [[TMP30]], align 4
67+
; VLENUNK-NEXT: [[TMP31:%.*]] = call i32 @llvm.vscale.i32()
68+
; VLENUNK-NEXT: [[TMP32:%.*]] = mul i32 [[TMP31]], 2
69+
; VLENUNK-NEXT: [[TMP33:%.*]] = getelementptr inbounds i32, ptr [[TMP28]], i32 [[TMP32]]
70+
; VLENUNK-NEXT: store <vscale x 2 x i32> [[TMP27]], ptr [[TMP33]], align 4
71+
; VLENUNK-NEXT: [[TMP34:%.*]] = call i64 @llvm.vscale.i64()
72+
; VLENUNK-NEXT: [[TMP35:%.*]] = mul i64 [[TMP34]], 4
73+
; VLENUNK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP35]]
74+
; VLENUNK-NEXT: [[VEC_IND_NEXT]] = add <vscale x 2 x i64> [[STEP_ADD]], [[DOTSPLAT]]
75+
; VLENUNK-NEXT: [[TMP36:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
76+
; VLENUNK-NEXT: br i1 [[TMP36]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
77+
; VLENUNK: middle.block:
78+
; VLENUNK-NEXT: [[CMP_N:%.*]] = icmp eq i64 1024, [[N_VEC]]
79+
; VLENUNK-NEXT: br i1 [[CMP_N]], label [[FOR_END:%.*]], label [[SCALAR_PH]]
80+
; VLENUNK: scalar.ph:
81+
; VLENUNK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
82+
; VLENUNK-NEXT: br label [[FOR_BODY:%.*]]
83+
; VLENUNK: for.body:
84+
; VLENUNK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], [[LATCH:%.*]] ]
85+
; VLENUNK-NEXT: [[ICMP:%.*]] = icmp ult i64 [[IV]], 512
86+
; VLENUNK-NEXT: br i1 [[ICMP]], label [[DO_LOAD:%.*]], label [[LATCH]]
87+
; VLENUNK: do_load:
88+
; VLENUNK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[IV]]
89+
; VLENUNK-NEXT: [[ELEM:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
90+
; VLENUNK-NEXT: br label [[LATCH]]
91+
; VLENUNK: latch:
92+
; VLENUNK-NEXT: [[PHI:%.*]] = phi i32 [ [[ELEM]], [[DO_LOAD]] ], [ 0, [[FOR_BODY]] ]
93+
; VLENUNK-NEXT: [[ADD:%.*]] = add i32 [[PHI]], [[V]]
94+
; VLENUNK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[IV]]
95+
; VLENUNK-NEXT: store i32 [[ADD]], ptr [[ARRAYIDX2]], align 4
96+
; VLENUNK-NEXT: [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
97+
; VLENUNK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i64 [[IV_NEXT]], 1024
98+
; VLENUNK-NEXT: br i1 [[EXITCOND_NOT]], label [[FOR_END]], label [[FOR_BODY]], !llvm.loop [[LOOP2:![0-9]+]]
99+
; VLENUNK: for.end:
100+
; VLENUNK-NEXT: ret void
101+
;
102+
entry:
103+
br label %for.body
104+
105+
for.body:
106+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %latch ]
107+
%icmp = icmp ult i64 %iv, 512
108+
br i1 %icmp, label %do_load, label %latch
109+
110+
do_load:
111+
%arrayidx = getelementptr inbounds i32, ptr %a, i64 %iv
112+
%elem = load i32, ptr %arrayidx
113+
br label %latch
114+
115+
latch:
116+
%phi = phi i32 [%elem, %do_load], [0, %for.body]
117+
%add = add i32 %phi, %v
118+
%arrayidx2 = getelementptr inbounds i32, ptr %b, i64 %iv
119+
store i32 %add, ptr %arrayidx2
120+
%iv.next = add nuw nsw i64 %iv, 1
121+
%exitcond.not = icmp eq i64 %iv.next, 1024
122+
br i1 %exitcond.not, label %for.end, label %for.body
123+
124+
for.end:
125+
ret void
126+
}
127+

0 commit comments

Comments
 (0)