llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LV] Vectorize loops that have phi uses outside a read-only loop

Open annamthomas opened this issue 8 years ago • 12 comments

Bugzilla Link 38410
Resolution FIXED
Resolved on Oct 05, 2018 12:12
Version trunk
OS All
CC @ayalz,@efriedma-quic,@aniragil,@hfinkel

Extended Description

Today we fail vectorization when we have a phi that's not one of the following:

  1. reduction PHI
  2. induction PHI
  3. first order recurrence

Basically, such phis have uses outside the loop and it's hard to identify which iteration out of the vectorization-factor number of iterations is the one we're interested in.

Loop characteristics which allow us to handle phis used outside the loop:

  1. read-only loop
  2. only exiting block is the latch block
  3. phis used outside the loop are generated in the latch block

Given these conditions (each of which is needed for legally doing the transform I'm describing!), we can teach the loop vectorizer to vectorize the read-only loop and extract the last element from the <PHI1, PHI2,..PHI VF> for use outside the loop. Note that we'll need to handle the case where the loop trip count is not exact multiple of VF, but I believe that's details we can chalk out: the post loop for handling extra iterations (tripCount mod VF) also needs the phi use to be handled.

There might be a problem if we were to "over-vectorize" the loop because it's a read-only loop, i.e. the element to be extracted from the phi-vector need not be the last one, but the last one where the loop termination condition was true. I think this is about generating the right set of vector instructions.

annamthomas avatar Aug 01 '18 23:08 annamthomas

The legal fixes allowed here are landed as: https://reviews.llvm.org/rL339703 https://reviews.llvm.org/rL340278

Closing this bug as done.

annamthomas avatar Oct 05 '18 19:10 annamthomas

The three types of header phi's listed represent the cross-iteration dependencies that LV knows how to handle.

The live-outs that LV handles are reductions (obviously) and inductions; the latter include 'penultimate' uses before the bump, or after it; see fixupIVUsers(). But live-outs aka 'lastprivate' are relevant for any value defined inside the loop, not only header phi's. More complicated indeed are live-outs whose definition inside the loop is conditional (which clearly exclude header phi's). One such example appears in https://reviews.llvm.org/D22341.

I've placed a change up for review that also teaches about other cross-iteration dependencies (when the header phi feeds are from if-convertable latch phis). For splitting up the work, I started with header phis that do not have outside uses currently: https://reviews.llvm.org/D50474

annamthomas avatar Aug 08 '18 21:08 annamthomas

One more useful thing to note is that the header phis that are not reduction/induction (hdrphi1 and hdrphi2) essentially feed from a splat in preheader and phi from latch (those phis can be if-converted - phiuseout1 and phiuseout2).

annamthomas avatar Aug 03 '18 18:08 annamthomas

Reduced IR for the case I'm interested.

There's 2 problems here which prevents vectorization:

  1. header phi that's not recognized as reduction. 2 header phis: hdrphi1 and hdrphi2. hdrphi1 is actually a reduction operation, where the reduction op is predicated on a select (see tmp41). Could we perhaps teach the vectorizer about this special case?
  1. phis in the latch that's used outside the loop. phiuseout1 and phiuseout2. phiuseout1 is a bool extended to short that is essentially hdrphi2 or 0.

Note that the

source_filename = "reducedir.ll"

declare i8 @​eggs()

declare i64 @​wombat()

define void @​baz(i64 %arg, i8 addrspace(1)* %arg1, i64 addrspace(1)* %arg2, i8 addrspace(1)* %arg3, i64 %arg4, i8 addrspace(1)* %boolarr, i32 addrspace(1)* %intarr3, i8 addrspace(1)* %shortarr, i8 addrspace(1)* > prehdr: %tmp = call i8 @​eggs() %tmp11 = call i64 @​wombat() br label %hdr

hdr: ; preds = %latch, %prehdr %hdrphi1 = phi i64 [ %phiuseout2, %latch ], [ %tmp11, %prehdr ] %hdrphi2 = phi i8 [ %phiuseout1, %latch ], [ %tmp, %prehdr ] %iv = phi i64 [ %iv.next, %latch ], [ 0, %prehdr ] %tmp16 = add nuw nsw i64 %iv, %arg4 %tmp17 = getelementptr inbounds i8, i8 addrspace(1)* %boolarr, i64 %tmp16 %tmp18 = load i8, i8 addrspace(1)* %tmp17, align 1 %tmp19 = icmp eq i8 %tmp18, 1 br i1 %tmp19, label %bb23, label %bb20

bb20: ; preds = %hdr %tmp21 = getelementptr inbounds i32, i32 addrspace(1)* %intarr3, i64 %tmp16 %tmp22 = load i32, i32 addrspace(1)* %tmp21, align 4 br label %bb23

bb23: ; preds = %bb20, %hdr %tmp24 = phi i32 [ %tmp22, %bb20 ], [ -1, %hdr ] %tmp25 = getelementptr inbounds i8, i8 addrspace(1)* %shortarr, i64 %tmp16 %tmp26 = load i8, i8 addrspace(1)* %tmp25, align 1 %tmp27 = icmp eq i8 %tmp26, 1 %tmp29 = icmp eq i8 %hdrphi2, 0 br i1 %tmp27, label %bb28, label %bb30

bb28: ; preds = %bb23 br label %bb46

bb30: ; preds = %bb23 %tmp31 = getelementptr inbounds i8, i8 addrspace(1)* %shortarr2, i64 %tmp16 %tmp32 = load i8, i8 addrspace(1)* %tmp31, align 1 %tmp33 = icmp eq i8 %tmp32, 1 %tmp34 = icmp eq i8 %hdrphi2, 0 %tmp35 = or i1 %tmp19, %tmp33 br i1 %tmp35, label %bb46, label %bb36

bb36: ; preds = %bb30 %tmp37 = getelementptr inbounds i32, i32 addrspace(1)* %intarr1, i64 %tmp16 %tmp38 = load i32, i32 addrspace(1)* %tmp37, align 4 %tmp39 = getelementptr inbounds i32, i32 addrspace(1)* %intarr2, i64 %tmp16 %tmp40 = load i32, i32 addrspace(1)* %tmp39, align 4 %tmp41 = select i1 %tmp34, i64 %hdrphi1, i64 0 %tmp42 = sub i32 %tmp24, %tmp38 %tmp43 = mul i32 %tmp40, %tmp42 %tmp44 = sext i32 %tmp43 to i64 %tmp45 = add i64 %tmp41, %tmp44 br label %latch

bb46: ; preds = %bb30, %bb28 %tmp48 = xor i1 %tmp29, true %tmp49 = zext i1 %tmp48 to i8 %tmp50 = select i1 %tmp29, i64 %hdrphi1, i64 -1 br label %latch

latch: ; preds = %bb46, %bb36 %phiuseout1 = phi i8 [ 0, %bb36 ], [ %tmp49, %bb46 ] %phiuseout2 = phi i64 [ %tmp45, %bb36 ], [ %tmp50, %bb46 ] %iv.next = add nuw nsw i64 %iv, 1 %tmp55 = icmp ult i64 %iv.next, %arg br i1 %tmp55, label %hdr, label %bb56

bb56: ; preds = %latch store i8 %phiuseout1, i8 addrspace(1)* %arg1, align 2 store i8 %phiuseout1, i8 addrspace(1)* %arg3, align 1 store i64 %phiuseout2, i64 addrspace(1)* %arg2, align 8 ret void }

annamthomas avatar Aug 03 '18 18:08 annamthomas

Reduced IR for the case I'm interested.

There's 2 problems here which prevents vectorization:

  1. header phi that's not recognized as reduction.
  2. phis in the latch that's used outside the loop.

source_filename = "reducedir.ll"

declare i8 @​eggs()

declare i64 @​wombat()

define void @​baz(i64 %arg, i8 addrspace(1)* %arg1, i64 addrspace(1)* %arg2, i8 addrspace(1)* %arg3, i64 %arg4, i8 addrspace(1)* %boolarr, i32 addrspace(1)* %intarr3, i8 addrspace(1)* %shortarr, i8 addrspace(1)* > prehdr: %tmp = call i8 @​eggs() %tmp11 = call i64 @​wombat() br label %hdr

hdr: ; preds = %latch, %prehdr %hdrphi1 = phi i64 [ %phiuseout2, %latch ], [ %tmp11, %prehdr ] %hdrphi2 = phi i8 [ %phiuseout1, %latch ], [ %tmp, %prehdr ] %iv = phi i64 [ %iv.next, %latch ], [ 0, %prehdr ] %tmp16 = add nuw nsw i64 %iv, %arg4 %tmp17 = getelementptr inbounds i8, i8 addrspace(1)* %boolarr, i64 %tmp16 %tmp18 = load i8, i8 addrspace(1)* %tmp17, align 1 %tmp19 = icmp eq i8 %tmp18, 1 br i1 %tmp19, label %bb23, label %bb20

bb20: ; preds = %hdr %tmp21 = getelementptr inbounds i32, i32 addrspace(1)* %intarr3, i64 %tmp16 %tmp22 = load i32, i32 addrspace(1)* %tmp21, align 4 br label %bb23

bb23: ; preds = %bb20, %hdr %tmp24 = phi i32 [ %tmp22, %bb20 ], [ -1, %hdr ] %tmp25 = getelementptr inbounds i8, i8 addrspace(1)* %shortarr, i64 %tmp16 %tmp26 = load i8, i8 addrspace(1)* %tmp25, align 1 %tmp27 = icmp eq i8 %tmp26, 1 %tmp29 = icmp eq i8 %hdrphi2, 0 br i1 %tmp27, label %bb28, label %bb30

bb28: ; preds = %bb23 br label %bb46

bb30: ; preds = %bb23 %tmp31 = getelementptr inbounds i8, i8 addrspace(1)* %shortarr2, i64 %tmp16 %tmp32 = load i8, i8 addrspace(1)* %tmp31, align 1 %tmp33 = icmp eq i8 %tmp32, 1 %tmp34 = icmp eq i8 %hdrphi2, 0 %tmp35 = or i1 %tmp19, %tmp33 br i1 %tmp35, label %bb46, label %bb36

bb36: ; preds = %bb30 %tmp37 = getelementptr inbounds i32, i32 addrspace(1)* %intarr1, i64 %tmp16 %tmp38 = load i32, i32 addrspace(1)* %tmp37, align 4 %tmp39 = getelementptr inbounds i32, i32 addrspace(1)* %intarr2, i64 %tmp16 %tmp40 = load i32, i32 addrspace(1)* %tmp39, align 4 %tmp41 = select i1 %tmp34, i64 %hdrphi1, i64 0 %tmp42 = sub i32 %tmp24, %tmp38 %tmp43 = mul i32 %tmp40, %tmp42 %tmp44 = sext i32 %tmp43 to i64 %tmp45 = add i64 %tmp41, %tmp44 br label %latch

bb46: ; preds = %bb30, %bb28 %tmp48 = xor i1 %tmp29, true %tmp49 = zext i1 %tmp48 to i8 %tmp50 = select i1 %tmp29, i64 %hdrphi1, i64 -1 br label %latch

latch: ; preds = %bb46, %bb36 %phiuseout1 = phi i8 [ 0, %bb36 ], [ %tmp49, %bb46 ] %phiuseout2 = phi i64 [ %tmp45, %bb36 ], [ %tmp50, %bb46 ] %iv.next = add nuw nsw i64 %iv, 1 %tmp55 = icmp ult i64 %iv.next, %arg br i1 %tmp55, label %hdr, label %bb56

bb56: ; preds = %latch store i8 %phiuseout1, i8 addrspace(1)* %arg1, align 2 store i8 %phiuseout1, i8 addrspace(1)* %arg3, align 1 store i64 %phiuseout2, i64 addrspace(1)* %arg2, align 8 ret void }

annamthomas avatar Aug 03 '18 18:08 annamthomas

Note that over-vectorizing also needs to satisfy the legality constraint of not generating a faulting load, so we'll need something like "run normal vectorized loop with N - N modVF iterations", followed by "one loop containing masked loads" with VF iterations. I've to verify if the masked load will guarantee we will not execute the load if it will fault. This will get rid of the need for the scalar remainder loop.

I think there's also cost model changes that we don't run a small runtime trip count scalar loop using the above vectorization pattern.

I will get the loop I'm interested in into a more readable form, but preserving the basic semantics within the loop. Maybe we can reduce the phi to a reduction type recognized by loop vectorizer, so that it's automatically handled.

annamthomas avatar Aug 03 '18 00:08 annamthomas

Can you give an example of the sort of loop you're talking about?

The example I have is slightly more complicated - if we're able to vectorize it, we will generated predicated loads within the vectorized loop.

A simple example is this one from LoopVectorizer under test directory: no_outside_user.ll: ; We used to vectorize this loop. But it has a value that is used outside of the ; and is not a recognized reduction variable "tmp17".

; CHECK-LABEL: @​main( ; CHECK-NOT: <2 x i32>

define i32 @​main() { bb: %b.promoted = load i32, i32* @​b, align 4 br label %.lr.ph.i

.lr.ph.i: %tmp8 = phi i32 [ %tmp18, %bb16 ], [ %b.promoted, %bb ] %tmp2 = icmp sgt i32 %tmp8, 10 br i1 %tmp2, label %bb16, label %bb10

bb10: br label %bb16

bb16: %tmp17 = phi i32 [ 0, %bb10 ], [ 1, %.lr.ph.i ] %tmp18 = add nsw i32 %tmp8, 1 %tmp19 = icmp slt i32 %tmp18, 4 br i1 %tmp19, label %.lr.ph.i, label %f1.exit.loopexit

f1.exit.loopexit: %.lcssa = phi i32 [ %tmp17, %bb16 ] ret i32 %.lcssa }

annamthomas avatar Aug 03 '18 00:08 annamthomas

The three types of header phi's listed represent the cross-iteration dependencies that LV knows how to handle.

The live-outs that LV handles are reductions (obviously) and inductions; the latter include 'penultimate' uses before the bump, or after it; see fixupIVUsers(). But live-outs aka 'lastprivate' are relevant for any value defined inside the loop, not only header phi's. More complicated indeed are live-outs whose definition inside the loop is conditional (which clearly exclude header phi's). One such example appears in https://reviews.llvm.org/D22341.

ayalz avatar Aug 02 '18 23:08 ayalz

Can you give an example of the sort of loop you're talking about?

efriedma-quic avatar Aug 02 '18 23:08 efriedma-quic

Ayal, Gil, Hal What do folks think? Also, currently we never over-vectorize the loop. We just run upto N - (VF mod N) in the vectorized loop.

annamthomas avatar Aug 02 '18 21:08 annamthomas

I'd like to mention one key fact: this only works if the loop is either "over vectorized" or has exact number of iterations = VF.

Since, it's a read-only loop, over-vectorizing is not a correctness issue here. In the worst case, it's a profitability issue where we will run atmost (VF -1) iterations as part of the last vectorized iteration.

The reason we need this is that when we have to run a scalar "remainder" loop, we have to identify the "resume" value for each operation that feeds into the phi. That will be hard to implement.

Ayal, Gil, Hal What do folks think?

annamthomas avatar Aug 02 '18 21:08 annamthomas

assigned to @annamthomas

annamthomas avatar Aug 01 '18 23:08 annamthomas