[llvm] [SLP][NFC]Add a test with the spills for loop invariants, NFC (PR #215092)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 06:01:00 PDT 2026


https://github.com/alexey-bataev created https://github.com/llvm/llvm-project/pull/215092

None

>From 3789001e6df521aeb757ac5dcf89099103a05d57 Mon Sep 17 00:00:00 2001
From: Alexey Bataev <a.bataev at outlook.com>
Date: Sun, 9 Aug 2026 06:00:46 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7
---
 .../RISCV/spillcost-loop-invariant-gather.ll  | 79 +++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 llvm/test/Transforms/SLPVectorizer/RISCV/spillcost-loop-invariant-gather.ll

diff --git a/llvm/test/Transforms/SLPVectorizer/RISCV/spillcost-loop-invariant-gather.ll b/llvm/test/Transforms/SLPVectorizer/RISCV/spillcost-loop-invariant-gather.ll
new file mode 100644
index 0000000000000..a67a3d22d8cd9
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/RISCV/spillcost-loop-invariant-gather.ll
@@ -0,0 +1,79 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S < %s -passes=slp-vectorizer -mtriple=riscv64 -mattr=+v | FileCheck %s
+
+declare void @g()
+
+; A loop-invariant buildvector (%b,%c) feeding the vectorized fmul is hoisted
+; to the preheader by optimizeGatherSequence, so its vector value is live
+; across the call in the loop body and must be spilled/reloaded around it.
+; Charging that keep-live cost makes this marginal tree unprofitable, so it
+; must stay scalar.
+define void @invariant_gather_over_call(ptr %out, ptr %in, double %a, double %b, double %c, double %d, i64 %n) {
+; CHECK-LABEL: define void @invariant_gather_over_call(
+; CHECK-SAME: ptr [[OUT:%.*]], ptr [[IN:%.*]], double [[A:%.*]], double [[B:%.*]], double [[C:%.*]], double [[D:%.*]], i64 [[N:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = insertelement <2 x double> poison, double [[B]], i64 0
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <2 x double> [[TMP0]], double [[C]], i64 1
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[INC:%.*]], %[[LOOP]] ]
+; CHECK-NEXT:    [[P0:%.*]] = getelementptr inbounds double, ptr [[IN]], i64 [[I]]
+; CHECK-NEXT:    [[V0:%.*]] = load double, ptr [[P0]], align 8
+; CHECK-NEXT:    [[I1:%.*]] = add nuw nsw i64 [[I]], 3
+; CHECK-NEXT:    [[P1:%.*]] = getelementptr inbounds double, ptr [[IN]], i64 [[I1]]
+; CHECK-NEXT:    [[I3:%.*]] = add nuw nsw i64 [[I]], 100
+; CHECK-NEXT:    [[P3:%.*]] = getelementptr inbounds double, ptr [[IN]], i64 [[I3]]
+; CHECK-NEXT:    [[V3:%.*]] = load double, ptr [[P3]], align 8
+; CHECK-NEXT:    [[R0:%.*]] = fmul double [[V0]], [[A]]
+; CHECK-NEXT:    [[R3:%.*]] = fmul double [[V3]], [[D]]
+; CHECK-NEXT:    [[O0:%.*]] = getelementptr inbounds double, ptr [[OUT]], i64 [[I]]
+; CHECK-NEXT:    [[O1:%.*]] = getelementptr inbounds double, ptr [[O0]], i64 1
+; CHECK-NEXT:    [[O3:%.*]] = getelementptr inbounds double, ptr [[O0]], i64 3
+; CHECK-NEXT:    [[TMP2:%.*]] = call <2 x double> @llvm.experimental.vp.strided.load.v2f64.p0.i64(ptr align 8 [[P1]], i64 32, <2 x i1> splat (i1 true), i32 2)
+; CHECK-NEXT:    [[TMP3:%.*]] = fmul <2 x double> [[TMP2]], [[TMP1]]
+; CHECK-NEXT:    store double [[R0]], ptr [[O0]], align 8
+; CHECK-NEXT:    store <2 x double> [[TMP3]], ptr [[O1]], align 8
+; CHECK-NEXT:    store double [[R3]], ptr [[O3]], align 8
+; CHECK-NEXT:    call void @g()
+; CHECK-NEXT:    [[INC]] = add nuw nsw i64 [[I]], 4
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i64 [[INC]], [[N]]
+; CHECK-NEXT:    br i1 [[CMP]], label %[[LOOP]], label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %i = phi i64 [ 0, %entry ], [ %inc, %loop ]
+  %p0 = getelementptr inbounds double, ptr %in, i64 %i
+  %v0 = load double, ptr %p0, align 8
+  %i1 = add nuw nsw i64 %i, 3
+  %p1 = getelementptr inbounds double, ptr %in, i64 %i1
+  %v1 = load double, ptr %p1, align 8
+  %i2 = add nuw nsw i64 %i, 7
+  %p2 = getelementptr inbounds double, ptr %in, i64 %i2
+  %v2 = load double, ptr %p2, align 8
+  %i3 = add nuw nsw i64 %i, 100
+  %p3 = getelementptr inbounds double, ptr %in, i64 %i3
+  %v3 = load double, ptr %p3, align 8
+  %r0 = fmul double %v0, %a
+  %r1 = fmul double %v1, %b
+  %r2 = fmul double %v2, %c
+  %r3 = fmul double %v3, %d
+  %o0 = getelementptr inbounds double, ptr %out, i64 %i
+  %o1 = getelementptr inbounds double, ptr %o0, i64 1
+  %o2 = getelementptr inbounds double, ptr %o0, i64 2
+  %o3 = getelementptr inbounds double, ptr %o0, i64 3
+  store double %r0, ptr %o0, align 8
+  store double %r1, ptr %o1, align 8
+  store double %r2, ptr %o2, align 8
+  store double %r3, ptr %o3, align 8
+  call void @g()
+  %inc = add nuw nsw i64 %i, 4
+  %cmp = icmp ult i64 %inc, %n
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}



More information about the llvm-commits mailing list