[llvm] [LV] Add -fold-epilogue-tail option for tail-folded epilogue (PR #190697)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sat May 2 03:17:42 PDT 2026


================
@@ -0,0 +1,87 @@
+; REQUIRES: asserts
+; RUN: opt -S < %s -p loop-vectorize -debug-only=loop-vectorize --disable-output \
+; RUN: -tail-folding-policy=prefer-fold-epilogue-tail 2>&1 | FileCheck %s
+
+; RUN: opt -S < %s -p loop-vectorize -debug-only=loop-vectorize -enable-epilogue-vectorization=false \
+; RUN: --disable-output -tail-folding-policy=prefer-fold-epilogue-tail 2>&1 \
+; RUN: | FileCheck %s --check-prefix=CHECK-DISABLED-EPILOG
+
+define void @test_epilogue_tf(ptr %A, i64 %n) {
+; CHECK: LV: Checking a loop in 'test_epilogue_tf'
+; CHECK: LV: epilogue tail-folding is not supported yet
+; CHECK: The tail-folding policy prefer-fold-epilogue-tail is not supported yet, fall back to a normal epilogue.
+entry:
+  br label %for.body
+
+for.body:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
+  %arrayidx = getelementptr inbounds i8, ptr %A, i64 %iv
+  store i8 1, ptr %arrayidx, align 1
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond = icmp ne i64 %iv.next, %n
+  br i1 %exitcond, label %for.body, label %exit
+
+exit:
+  ret void
+}
+
+define void @epilogue_is_disabled(ptr %a, i64 %n) {
+; CHECK-DISABLED-EPILOG: LV: Checking a loop in 'epilogue_is_disabled'
+; CHECK-DISABLED-EPILOG: LV: Options conflict, epilogue vectorization is disallowed while epilogue tail-folding allowed!
+entry:
+  %cmp1 = icmp sgt i64 %n, 0
+  br i1 %cmp1, label %for.body, label %for.end
+for.body:
+  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
+  %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv
+  store i32 1, ptr %arrayidx, align 4
+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %exitcond = icmp ne i64 %indvars.iv.next, %n
+  br i1 %exitcond, label %for.body, label %for.end
+
+for.end:
+  ret void
+}
+
+define i16 @require_scalar_epilogue(ptr %dst, i64 %x) {
+; CHECK: LV: Checking a loop in 'require_scalar_epilogue'
+; CHECK: LV: Epilogue tail-folding can't be applied because scalar epilogue is required
+; CHECK-NEXT: LV: Fallback to a normal epilogue
+entry:
+  br label %loop.header
+loop.header:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]
+  %gep = getelementptr inbounds i32, ptr %dst, i64 %iv
+  store i64 0, ptr %gep
+  br i1 true, label %loop.then, label %exit.2
----------------
fhahn wrote:

Can we use a real condition to make this robust

https://github.com/llvm/llvm-project/pull/190697


More information about the llvm-commits mailing list