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

[LoopVectorizer] Pre-LTO LoopVectorization (as discussed in 10/21/25 Vectorization meeting)

Open bababuck opened this issue 1 year ago • 14 comments

@david-arm @ppenzin @hiraditya @mgudim Apologies if I missed anyone or tagged the wrong people. Opening the issue as a continuation of the discussion from the last vectorizer meeting as more information was requested.

We don't have good data on how often this occurs in other benchmarks, but if that is of interest I can try to gather that data. For x264, I only have instruction count data, but based on that I would guess this could give a 0.5-2% improvement for strided load targets. Since we aren't able to specialize all calls to the pixel_avg loop, not every instance would benefit from better improved vectorization.

The case brought up in the meeting is the following from x264; the loop bounds aren’t known at compile time, but they are exposed at link time for several of the function calls, so the loop bounds are constant after function specialization. Note: this involves a few local patches (we have begun the upstreaming process, see https://github.com/llvm/llvm-project/pull/163891) so is not yet reproduceable upstream.

void pixel_avg(uint8_t * restrict dst,  int i_dst_stride,
               uint8_t * restrict src1, int i_src1_stride,
               uint8_t * restrict src2, int i_src2_stride,
               int i_width, int i_height) {
  for(int y = 0; y < i_width; y++) {
    for (int x = 0; x < i_height; x++) {
      dst[x] = (src1[x] + src2[x] + 1) >> 1;
    }
    dst  += i_dst_stride;
    src1 += i_src1_stride;
    src2 += i_src2_stride;
  }
}

At compile time, the loops have non-constant bounds so aren’t unrolled, and the LoopVectorizer vectorizes them into the following scalable vectors:

87:                                               ; preds = %87, %81
  %88 = phi i64 [ %103, %87 ], [ 0, %81 ]
  %89 = phi i64 [ %104, %87 ], [ %49, %81 ]
  %90 = tail call i32 @llvm.experimental.get.vector.length.i64(i64 %89, i32 16, i1 true)
  %91 = getelementptr inbounds nuw i8, ptr %84, i64 %88
  %92 = tail call <vscale x 16 x i8> @llvm.vp.load.nxv16i8.p0(ptr align 1 %91, <vscale x 16 x i1> splat (i1 true), i32 %90), !alias.scope !69
  %93 = zext <vscale x 16 x i8> %92 to <vscale x 16 x i16>
  %94 = getelementptr inbounds nuw i8, ptr %85, i64 %88
  %95 = tail call <vscale x 16 x i8> @llvm.vp.load.nxv16i8.p0(ptr align 1 %94, <vscale x 16 x i1> splat (i1 true), i32 %90), !alias.scope !72
  %96 = zext <vscale x 16 x i8> %95 to <vscale x 16 x i16>
  %97 = add nuw nsw <vscale x 16 x i16> %93, splat (i16 1)
  %98 = add nuw nsw <vscale x 16 x i16> %97, %96
  %99 = lshr <vscale x 16 x i16> %98, splat (i16 1)
  %100 = trunc nuw <vscale x 16 x i16> %99 to <vscale x 16 x i8>
  %101 = getelementptr inbounds nuw i8, ptr %83, i64 %88
  tail call void @llvm.vp.store.nxv16i8.p0(<vscale x 16 x i8> %100, ptr align 1 %101, <vscale x 16 x i1> splat (i1 true), i32 %90), !alias.scope !74, !noalias !76
  %102 = zext i32 %90 to i64
  %103 = add nuw i64 %88, %102
  %104 = sub nuw i64 %89, %102
  %105 = icmp eq i64 %104, 0
  br i1 %105, label %178, label %87, !llvm.loop !77

During LTO, function specialization exposes constant values for i_width and i_height. But the inner loop is already vectorized (compile time) so it isn't unrolled. As a result, SLP is unable to optimize with strided loads/stores.

bababuck avatar Oct 23 '25 06:10 bababuck

That is a very good finding. Unit compilation is aware of LTO being set up and it could, in theory, change some of the settings expecting a follow up at link time.

ppenzin avatar Nov 17 '25 19:11 ppenzin

@bababuck what happens when we compile with lto but the pre-lto slp vectorizer is disabled?

mgudim avatar Nov 18 '25 12:11 mgudim

Since this involves downstream patches, it would be very helpful if you'd provide the full IR which reaches LTO. I want to explore the possibility of simplification after the initial vectorization resulting in reasonable revec opportunities.

preames avatar Nov 18 '25 17:11 preames

@bababuck what happens when we compile with lto but the pre-lto slp vectorizer is disabled?

If I disable the pre-LTO vectorizer (it is the LoopVectorizer pre-LTO since the bounds are unknown), then we avoid the above issue. In this case, the loop in the specialized version(s) is unrolled by LoopFullUnrollPass, but requires changes similar to #162324 to properly vectorize in the SLP vectorizer (I have these changes locally as well, waiting for #162324 since builds off of it).

bababuck avatar Nov 19 '25 08:11 bababuck

Since this involves downstream patches, it would be very helpful if you'd provide the full IR which reaches LTO. I want to explore the possibility of simplification after the initial vectorization resulting in reasonable revec opportunities.

@preames let me know if this was what you were looking for. This is IR of the specialized function during LTO. I included the entire function for get_ref (which inlines the in question pixel_avg) , and then a manually reduced version of just the loop in question.

Sorry for the file extensions, wouldn't let me upload a .ll file.

~~164762_reduced.ll.txt~~ -> 164762_reduced_updated.ll.txt 164762_specialized.ll.txt

bababuck avatar Nov 19 '25 21:11 bababuck

Looking at 164762_reduced.ll.txt, I note that there's a missing optimization involving the call to llvm.experimental.get.vector.length.i64. Since we know the maximum value of the induction variable is 16, I believe we know that the vector length returned will be equal to 16, and that this loop runs exactly once.

preames avatar Nov 21 '25 20:11 preames

After talking to @preames I think I agree that we probably need to collect a bit more data before considering to change pre-LTO vectorization settings. On the other hand, definitely looking forward to your function specialization changes landing.

ppenzin avatar Nov 22 '25 08:11 ppenzin

Looking at 164762_reduced.ll.txt, I note that there's a missing optimization involving the call to llvm.experimental.get.vector.length.i64. Since we know the maximum value of the induction variable is 16, I believe we know that the vector length returned will be equal to 16, and that this loop runs exactly once.

Seems like a good place to start then would be IndVarSimplify, but I'll double check that that's the right spot for this. Once that gets cleaned up I'd expect the LoopUnroll would handle removing the loop.

There's an outer loop that I dropped from 164762_reduced.ll.txt, doesn't affect the above conversation (removing the inner loop seems like a reasonable first step still). Apologies for the over-reduction and please refer to 164762_reduced_updated.ll.txt going forward.

bababuck avatar Nov 23 '25 04:11 bababuck

Looking at 164762_reduced.ll.txt, I note that there's a missing optimization involving the call to llvm.experimental.get.vector.length.i64. Since we know the maximum value of the induction variable is 16, I believe we know that the vector length returned will be equal to 16, and that this loop runs exactly once.

We recently added this optimisation to VPlan itself in #167647

Seems like a good place to start then would be IndVarSimplify, but I'll double check that that's the right spot for this. Once that gets cleaned up I'd expect the LoopUnroll would handle removing the loop.

I think you could potentially do this in InstSimplify if you use computeKnownBits to check that the AVL <= VF. If the incoming AVL phi value is coming from a non-wrapping subtract from the original AVL then it should be able to compute the maximum range?

  %80 = phi i64 [ %95, %loop ], [ 16, %entry ]
  %81 = tail call i32 @llvm.experimental.get.vector.length.i64(i64 %80, i32 16, i1 true)
  // ...
  %95 = sub nuw nsw i64 %80, %93

lukel97 avatar Nov 24 '25 03:11 lukel97

Running 164762_reduced_updated.ll.txt through a version of opt with @lukel97's recent changes, I do see significant improvements. We still have room to improve, both incrementally and radically.

On the incremental side, we currently end up with a loop running m2 operations when the constant VL is such that only m1 should be required. We don't currently have a "LMUL reduce" pass, but we've talked about this previously.

On the radical side, we blocks of 16 bytes being loaded with a runtime stride between them. This doesn't map to a strided load, but could be mapped to either an indexed load or a strided segment load. The later would require us to realize that the order of operations was independent, and perform the element wise operation independently on the even/odd halves.

preames avatar Dec 01 '25 19:12 preames

As a follow up to my previous, looking at 164762_specialized.ll.txt, it looks like we've got some form of loop unswitching problem. The outer loop ends up with a condition selecting between two inner loops with a loop invariant condition - looks like an aliasing check. It's interesting to ask why this didn't get unswitched into two separate loop nests. We may have some kind of pass ordering or costing problem here as running simple-loop-unswitch on the result does in fact unswitch. (Edit: The existing off by default-extra-vectorizer-passes looks relevant.)

An alternate question would be to ask if any of the callers can prove the no-aliasing fact, and whether there's a missed opportunity for specialization.

preames avatar Dec 01 '25 20:12 preames

The later would require us to realize that the order of operations was independent, and perform the element wise operation independently on the even/odd halves.

Is SCEV unable to prove this?

hiraditya avatar Dec 01 '25 20:12 hiraditya

On the radical side, we blocks of 16 bytes being loaded with a runtime stride between them. This doesn't map to a strided load, but could be mapped to either an indexed load or a strided segment load. The later would require us to realize that the order of operations was independent, and perform the element wise operation independently on the even/odd halves.

Might be easier to start with the 8 x 8 x i16 case which can be lowered in a single strided-segment-load. See 164762_reduced_8x8.ll.txt. The first road-block is re-vectorization of the existing load-store sequence.

Edits between 164762_reduced_8x8.ll.txt and 164762_reduced_updated.ll.txt:

  • Adjusted loop bounds for both loops to be 8 rather than 16
  • Annotated pixel_avg as external rather than internal so that ISCCP doesn't delete due to being unused

bababuck avatar Dec 01 '25 20:12 bababuck

On the incremental side, we currently end up with a loop running m2 operations when the constant VL is such that only m1 should be required. We don't currently have a "LMUL reduce" pass, but we've talked about this previously.

Was the discussion about this "LMUL reduce" pass documented anywhere, I'd be curious to read what was talked about.

bababuck avatar Dec 02 '25 00:12 bababuck